MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / run_pthreads

Function run_pthreads

src/pipeline/worker_pool.c:54–92  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

52}
53
54static void run_pthreads(int count, cbm_parallel_fn fn, void *ctx, int nworkers) {
55 _Atomic int next_idx = 0;
56
57 pthread_worker_arg_t wa = {
58 .fn = fn,
59 .ctx = ctx,
60 .next_idx = &next_idx,
61 .count = count,
62 };
63
64 cbm_thread_t *threads = (cbm_thread_t *)malloc((size_t)nworkers * sizeof(cbm_thread_t));
65 if (!threads) {
66 run_serial(count, fn, ctx);
67 return;
68 }
69
70 for (int i = 0; i < nworkers; i++) {
71 if (cbm_thread_create(&threads[i], CBM_WORKER_STACK_SIZE, pthread_worker, &wa) != 0) {
72 /* Failed to create thread — let remaining work run in main thread */
73 nworkers = i;
74 break;
75 }
76 }
77
78 /* Main thread also participates */
79 while (WP_TRUE) {
80 int idx = atomic_fetch_add_explicit(&next_idx, WP_STEP, memory_order_relaxed);
81 if (idx >= count) {
82 break;
83 }
84 fn(idx, ctx);
85 }
86
87 for (int i = 0; i < nworkers; i++) {
88 cbm_thread_join(&threads[i]);
89 }
90
91 free(threads);
92}
93
94/* ── Public API ──────────────────────────────────────────────────── */
95

Callers 1

cbm_parallel_forFunction · 0.85

Calls 3

run_serialFunction · 0.85
cbm_thread_createFunction · 0.85
cbm_thread_joinFunction · 0.85

Tested by

no test coverage detected