Skip to content

Critical section for thread safety#1

Merged
hasumikin merged 3 commits into
masterfrom
critical-section
Mar 30, 2026
Merged

Critical section for thread safety#1
hasumikin merged 3 commits into
masterfrom
critical-section

Conversation

@hasumikin

@hasumikin hasumikin commented Mar 30, 2026

Copy link
Copy Markdown
Member

est_set_critical_section() to register callbacks for wrapping critical section provided by the platform.

Example for pico-sdk:

#include <pico/critical_section.h>

static critical_section_t heap_critsec;

static void heap_enter_critical(void)
{
  critical_section_enter_blocking(&heap_critsec);
}

static void heap_exit_critical(void)
{
  critical_section_exit(&heap_critsec);
}

int main(void)
{
  ESTALLOC *est = est_init(mem, bytes);
  critical_section_init(&heap_critsec); // from pico-sdk
  est_set_critical_section(est, heap_enter_critical, heap_exit_critical);
}

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds optional platform-provided critical section callbacks to make the allocator’s core operations thread-safe on embedded targets (e.g., by disabling interrupts or acquiring a lock) without hard-coding a specific RTOS/SDK dependency.

Changes:

  • Extend ESTALLOC with enter_critical / exit_critical callback pointers and expose est_set_critical_section() to register them.
  • Wrap allocator operations (malloc/permalloc/free/realloc and statistics collection) with enter/exit critical section hooks.
  • Add a few additional internal assertions to detect heap corruption cases earlier.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
estalloc.h Adds critical section callbacks to ESTALLOC and declares est_set_critical_section() API.
estalloc.c Implements callback registration and applies critical section wrapping to allocator operations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread estalloc.c Outdated
Comment thread estalloc.c
Comment thread estalloc.h
`est_set_critical_section()` to register callbacks for wrapping critical section provided by the platform.

Example for pico-sdk:

```c
#include <pico/critical_section.h>

static critical_section_t heap_critsec;

static void heap_enter_critical(void)
{
  critical_section_enter_blocking(&heap_critsec);
}

static void heap_exit_critical(void)
{
  critical_section_exit(&heap_critsec);
}

int main(void)
{
  ESTALLOC *est = est_init(mem, bytes);
  critical_section_init(&heap_critsec); // from pico-sdk
  est_set_critical_section(est, heap_enter_critical, heap_exit_critical);
}
```
hasumikin and others added 2 commits March 30, 2026 21:11
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@hasumikin hasumikin merged commit 971b793 into master Mar 30, 2026
@hasumikin hasumikin deleted the critical-section branch March 30, 2026 12:17
hasumikin added a commit to picoruby/picoruby that referenced this pull request Mar 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants