int _Thrd_create(_Thrd_t *thr, _Thrd_start_t func, void *d) { /* create thread */ int res; binder b; int started = 0; _Cnd_t cond; _Mtx_t mtx; _Cnd_init(&cond); _Mtx_init(&mtx, _Mtx_plain); b.func = func; b.data = d; b.cond = &cond; b.mtx = &mtx; b.started = &started; _Mtx_lock(mtx); if ((res = _Thrd_start(thr, run, &b)) == _Thrd_success) { /* wait for handshake */ while (!started) _Cnd_wait(cond, mtx); } _Mtx_unlock(mtx); _Cnd_destroy(cond); _Mtx_destroy(mtx); return (res); }
static unsigned int _STDCALL run(void *d) { /* call thread function */ unsigned res; binder b = *(binder *)d; _Mtx_lock(*b.mtx); *b.started = 1; _Cnd_signal(*b.cond); _Mtx_unlock(*b.mtx); res = (b.func)(b.data); _Cnd_do_broadcast_at_thread_exit(); return (res); }
_Mtx_guard() { _Mtx_lock(&ext_once_mtx); }