Skip to content

[pthread] Use intrusive list for thread mailbox - #28517

Open
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:intrusive_pthread_mailbox
Open

sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:intrusive_pthread_mailbox

Conversation

@sbc100

@sbc100 sbc100 commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Following #28449, each em_task_queue only ever carries one pending notification in its target thread's mailbox at a time (coalesced via NOTIFICATION_PENDING) and already stays alive on the zombie list until notification transitions back to NOTIFICATION_NONE.

Replace em_task_queue mailbox in struct pthread with a lightweight em_thread_mailbox intrusive FIFO linked via
em_task_queue::mailbox_next:

  • Eliminates the remaining malloc/free on thread creation and shutdown, saving 1,536 bytes (wasm32) or 3,072 bytes (wasm64) of heap allocation per thread (plus allocator overhead) and making _emscripten_thread_mailbox_init infallible.
  • Shrinks struct pthread from 176 to 152 bytes on wasm32 (312 to 272 on wasm64) and drops the em_task_queue_init/deinit split and indirect notification wrapper functions.

@sbc100
sbc100 requested a review from tlively as a code owner December 2, 2026 00:46
@sbc100

sbc100 commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator Author

Assuming the mailbox is only going to contain 0-3 elements in the linked list I guess we could just drop the tail pointer and have O(n) insertion?

@sbc100
sbc100 force-pushed the intrusive_pthread_mailbox branch from b58a409 to eebdc3c Compare December 2, 2026 01:05
Following emscripten-core#28449, each `em_task_queue` only ever carries one pending
notification in its target thread's mailbox at a time (coalesced via
`NOTIFICATION_PENDING`) and already stays alive on the zombie list until
`notification` transitions back to `NOTIFICATION_NONE`.

Replace `em_task_queue mailbox` in `struct pthread` with a lightweight
`em_thread_mailbox` intrusive FIFO linked via
`em_task_queue::mailbox_next`:
- Eliminates the remaining `malloc`/`free` on thread creation and
  shutdown, saving 1,536 bytes (wasm32) or 3,072 bytes (wasm64) of heap
  allocation per thread (plus allocator overhead) and making
  `_emscripten_thread_mailbox_init` infallible.
- Shrinks `struct pthread` from 176 to 152 bytes on wasm32 (312 to 272
  on wasm64) and drops the `em_task_queue_init`/`deinit` split and
  indirect notification wrapper functions.
@sbc100
sbc100 force-pushed the intrusive_pthread_mailbox branch from eebdc3c to ae96983 Compare December 2, 2026 16:39

@tlively tlively left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@tlively

tlively commented Dec 2, 2026

Copy link
Copy Markdown
Member

I'd keep the tail. It seems worth 4 bytes to have constant-time insertion, especially because the alternative is not just O(n), but also requires O(n) pointer chasing.

@tlively tlively left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Gemini nits:

Now that thread_mailbox.c no longer calls em_task_queue_enqueue, em_task_queue_enqueue, em_task_queue_dequeue, em_task_queue_is_empty, and em_task_queue_is_full are all internal to em_task_queue.c
and could be made static.

Comment on lines -211 to -212
// Any subsequent messages to this queue (for example if a pthread struct is
// reused for a future thread, potentially on a different worker) will require

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's keep this parenthetical.

Comment on lines +83 to +84
pthread_mutex_destroy(&thread->mailbox.mutex);
thread->mailbox.notification = NOTIFICATION_NONE;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Resetting this notification is now unnecessary, but harmless.

This branch has not been deployed

No deployments
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