Skip to content

Commit 0ced16e

Browse files
authored
[Proxying][NFC] Encapsulate task queue notification logic (#18653)
Rather than exposing the details of how task queues are notified to proxying.c, move the logic into a new `em_task_queue_notify` function declared in em_task_queue.h and implemented in em_task_queue.c.
1 parent 382a273 commit 0ced16e

File tree

4 files changed

+31
-23
lines changed

4 files changed

+31
-23
lines changed

src/library_pthread.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,6 @@ var LibraryPThread = {
11101110
}
11111111
worker.postMessage({'cmd' : 'processProxyingQueue', 'queue': queue});
11121112
}
1113-
return 1;
11141113
}
11151114
};
11161115

system/lib/pthread/em_task_queue.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* found in the LICENSE file.
66
*/
77

8+
#include <emscripten/threading.h>
9+
#include <stdatomic.h>
810
#include <stdlib.h>
911
#include <string.h>
1012

@@ -102,3 +104,27 @@ task em_task_queue_dequeue(em_task_queue* queue) {
102104
queue->head = (queue->head + 1) % queue->capacity;
103105
return t;
104106
}
107+
108+
// Send a postMessage notification containing the em_task_queue pointer to the
109+
// target thread so it will execute the queue when it returns to the event loop.
110+
// Also pass in the current thread and main thread ids to minimize calls back
111+
// into Wasm.
112+
void _emscripten_notify_task_queue(pthread_t target_thread,
113+
pthread_t curr_thread,
114+
pthread_t main_thread,
115+
em_task_queue* queue);
116+
117+
void em_task_queue_notify(em_task_queue* queue) {
118+
// If there is no pending notification for this queue, create one. If an old
119+
// notification is currently being processed, it may or may not execute this
120+
// work. In case it does not, the new notification will ensure the work is
121+
// still executed.
122+
notification_state previous =
123+
atomic_exchange(&queue->notification, NOTIFICATION_PENDING);
124+
if (previous != NOTIFICATION_PENDING) {
125+
_emscripten_notify_task_queue(queue->thread,
126+
pthread_self(),
127+
emscripten_main_browser_thread_id(),
128+
queue);
129+
}
130+
}

system/lib/pthread/em_task_queue.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@ typedef struct em_task_queue {
4141
int tail;
4242
} em_task_queue;
4343

44-
// Send a postMessage notification containing the em_task_queue pointer to the
45-
// target thread so it will execute the queue when it returns to the event loop.
46-
// Also pass in the current thread and main thread ids to minimize calls back
47-
// into Wasm.
48-
extern int _emscripten_notify_task_queue(pthread_t target_thread,
49-
pthread_t curr_thread,
50-
pthread_t main_thread,
51-
em_task_queue* queue);
52-
5344
em_task_queue* em_task_queue_create(pthread_t thread);
5445

5546
void em_task_queue_destroy(em_task_queue* queue);
@@ -72,3 +63,7 @@ int em_task_queue_enqueue(em_task_queue* queue, task t);
7263

7364
// Not thread safe. Assumes the queue is not empty.
7465
task em_task_queue_dequeue(em_task_queue* queue);
66+
67+
// Schedule the queue to be executed next time its owning thread returns to its
68+
// event loop.
69+
void em_task_queue_notify(em_task_queue* queue);

system/lib/pthread/proxying.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <emscripten/proxying.h>
1010
#include <emscripten/threading.h>
1111
#include <pthread.h>
12-
#include <stdatomic.h>
1312
#include <stdlib.h>
1413
#include <string.h>
1514

@@ -268,18 +267,7 @@ int emscripten_proxy_async(em_proxying_queue* q,
268267
return 0;
269268
}
270269

271-
// If there is no pending notification for this queue, create one. If an old
272-
// notification is currently being processed, it may or may not execute this
273-
// work. In case it does not, the new notification will ensure the work is
274-
// still executed.
275-
notification_state previous =
276-
atomic_exchange(&tasks->notification, NOTIFICATION_PENDING);
277-
if (previous != NOTIFICATION_PENDING) {
278-
_emscripten_notify_task_queue(target_thread,
279-
pthread_self(),
280-
emscripten_main_browser_thread_id(),
281-
tasks);
282-
}
270+
em_task_queue_notify(tasks);
283271
return 1;
284272
}
285273

0 commit comments

Comments
 (0)