53 #if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(__SYMBIAN32__)
54 #define _WIN32_WINNT 0x0400
89 sbthread_internal_main(LPVOID arg)
108 th->th = CreateThread(NULL, 0, sbthread_internal_main, th, 0, &th->tid);
109 if (th->th == NULL) {
125 rv = WaitForSingleObject(th->th, INFINITE);
126 if (rv == WAIT_FAILED) {
127 E_ERROR(
"Failed to join thread: WAIT_FAILED\n");
130 GetExitCodeThread(th->th, &exit);
137 cond_timed_wait(HANDLE cond,
int sec,
int nsec)
141 rv = WaitForSingleObject(cond, INFINITE);
146 ms = sec * 1000 + nsec / (1000*1000);
147 rv = WaitForSingleObject(cond, ms);
159 evt->evt = CreateEventW(NULL, FALSE, FALSE, NULL);
160 if (evt->evt == NULL) {
170 CloseHandle(evt->evt);
177 return SetEvent(evt->evt) ? 0 : -1;
185 rv = cond_timed_wait(evt->evt, sec, nsec);
195 InitializeCriticalSection(&mtx->mtx);
202 return TryEnterCriticalSection(&mtx->mtx) ? 0 : -1;
208 EnterCriticalSection(&mtx->mtx);
215 LeaveCriticalSection(&mtx->mtx);
222 DeleteCriticalSection(&mtx->mtx);
233 msgq->evt = CreateEventW(NULL, FALSE, FALSE, NULL);
234 if (msgq->evt == NULL) {
238 InitializeCriticalSection(&msgq->mtx);
247 CloseHandle(msgq->evt);
256 char const *cdata = (
char const *)data;
260 if (len +
sizeof(len) > q->depth)
263 if (q->nbytes + len +
sizeof(len) > q->depth)
264 WaitForSingleObject(q->evt, INFINITE);
268 EnterCriticalSection(&q->mtx);
269 in = (q->out + q->nbytes) % q->depth;
271 if (in +
sizeof(len) > q->depth) {
273 size_t len1 = q->depth - in;
274 memcpy(q->data + in, &len, len1);
275 memcpy(q->data, ((
char *)&len) + len1,
sizeof(len) - len1);
276 q->nbytes +=
sizeof(len);
277 in =
sizeof(len) - len1;
280 memcpy(q->data + in, &len,
sizeof(len));
281 q->nbytes +=
sizeof(len);
286 if (in + len > q->depth) {
288 size_t len1 = q->depth - in;
289 memcpy(q->data + in, cdata, len1);
295 memcpy(q->data + in, cdata, len);
301 LeaveCriticalSection(&q->mtx);
313 if (q->nbytes == 0) {
314 if (cond_timed_wait(q->evt, sec, nsec) == WAIT_FAILED)
319 EnterCriticalSection(&q->mtx);
321 if (q->out +
sizeof(q->msglen) > q->depth) {
323 size_t len1 = q->depth - q->out;
324 memcpy(&q->msglen, q->data + q->out, len1);
325 memcpy(((
char *)&q->msglen) + len1, q->data,
326 sizeof(q->msglen) - len1);
327 q->out =
sizeof(q->msglen) - len1;
330 memcpy(&q->msglen, q->data + q->out,
sizeof(q->msglen));
331 q->out +=
sizeof(q->msglen);
333 q->nbytes -=
sizeof(q->msglen);
337 if (q->out + q->msglen > q->depth) {
339 size_t len1 = q->depth - q->out;
340 memcpy(outptr, q->data + q->out, len1);
346 memcpy(outptr, q->data + q->out, len);
353 LeaveCriticalSection(&q->mtx);
355 *out_len = q->msglen;
361 #include <sys/time.h>
396 sbthread_internal_main(
void *arg)
401 rv = (*th->func)(th);
402 return (
void *)(long)rv;
416 if ((rv = pthread_create(&th->th, NULL, &sbthread_internal_main, th)) != 0) {
417 E_ERROR(
"Failed to create thread: %d\n", rv);
431 if (th->th == (pthread_t)-1)
434 rv = pthread_join(th->th, &exit);
436 E_ERROR(
"Failed to join thread: %d\n", rv);
439 th->th = (pthread_t)-1;
440 return (
int)(long)exit;
450 if (pthread_cond_init(&msgq->cond, NULL) != 0) {
454 if (pthread_mutex_init(&msgq->mtx, NULL) != 0) {
455 pthread_cond_destroy(&msgq->cond);
467 pthread_mutex_destroy(&msgq->mtx);
468 pthread_cond_destroy(&msgq->cond);
480 if (len +
sizeof(len) > q->depth)
484 pthread_mutex_lock(&q->mtx);
485 if (q->nbytes + len +
sizeof(len) > q->depth) {
487 if (pthread_cond_wait(&q->cond, &q->mtx) != 0) {
489 pthread_mutex_unlock(&q->mtx);
494 in = (q->out + q->nbytes) % q->depth;
497 if (in +
sizeof(len) > q->depth) {
499 size_t len1 = q->depth - in;
500 memcpy(q->data + in, &len, len1);
501 memcpy(q->data, ((
char *)&len) + len1,
sizeof(len) - len1);
502 q->nbytes +=
sizeof(len);
503 in =
sizeof(len) - len1;
506 memcpy(q->data + in, &len,
sizeof(len));
507 q->nbytes +=
sizeof(len);
512 if (in + len > q->depth) {
514 size_t len1 = q->depth - in;
515 memcpy(q->data + in, data, len1);
517 data = (
char const *)data + len1;
521 memcpy(q->data + in, data, len);
525 pthread_cond_signal(&q->cond);
527 pthread_mutex_unlock(&q->mtx);
532 cond_timed_wait(pthread_cond_t *cond, pthread_mutex_t *mtx,
int sec,
int nsec)
536 rv = pthread_cond_wait(cond, mtx);
542 gettimeofday(&now, NULL);
543 end.tv_sec = now.tv_sec + sec;
544 end.tv_nsec = now.tv_usec * 1000 + nsec;
545 if (end.tv_nsec > (1000*1000*1000)) {
546 sec += end.tv_nsec / (1000*1000*1000);
547 end.tv_nsec = end.tv_nsec % (1000*1000*1000);
549 rv = pthread_cond_timedwait(cond, mtx, &end);
561 pthread_mutex_lock(&q->mtx);
562 if (q->nbytes == 0) {
564 if (cond_timed_wait(&q->cond, &q->mtx, sec, nsec) != 0) {
566 pthread_mutex_unlock(&q->mtx);
572 if (q->out +
sizeof(q->msglen) > q->depth) {
574 size_t len1 = q->depth - q->out;
575 memcpy(&q->msglen, q->data + q->out, len1);
576 memcpy(((
char *)&q->msglen) + len1, q->data,
577 sizeof(q->msglen) - len1);
578 q->out =
sizeof(q->msglen) - len1;
581 memcpy(&q->msglen, q->data + q->out,
sizeof(q->msglen));
582 q->out +=
sizeof(q->msglen);
584 q->nbytes -=
sizeof(q->msglen);
588 if (q->out + q->msglen > q->depth) {
590 size_t len1 = q->depth - q->out;
591 memcpy(outptr, q->data + q->out, len1);
597 memcpy(outptr, q->data + q->out, len);
602 pthread_cond_signal(&q->cond);
604 pthread_mutex_unlock(&q->mtx);
606 *out_len = q->msglen;
617 if ((rv = pthread_mutex_init(&evt->mtx, NULL)) != 0) {
618 E_ERROR(
"Failed to initialize mutex: %d\n", rv);
622 if ((rv = pthread_cond_init(&evt->cond, NULL)) != 0) {
624 pthread_mutex_destroy(&evt->mtx);
634 pthread_mutex_destroy(&evt->mtx);
635 pthread_cond_destroy(&evt->cond);
644 pthread_mutex_lock(&evt->mtx);
645 evt->signalled = TRUE;
646 rv = pthread_cond_signal(&evt->cond);
647 pthread_mutex_unlock(&evt->mtx);
657 pthread_mutex_lock(&evt->mtx);
660 rv = cond_timed_wait(&evt->cond, &evt->mtx, sec, nsec);
663 evt->signalled = FALSE;
665 pthread_mutex_unlock(&evt->mtx);
676 if (pthread_mutex_init(&mtx->mtx, NULL) != 0) {
686 return pthread_mutex_trylock(&mtx->mtx);
692 return pthread_mutex_lock(&mtx->mtx);
698 return pthread_mutex_unlock(&mtx->mtx);
704 pthread_mutex_destroy(&mtx->mtx);