int s_eventq_init (s_window_t *window) { window->eventq = (s_eventq_t *) s_calloc(1, sizeof(s_eventq_t)); window->eventq->queue = (s_list_t *) s_calloc(1, sizeof(s_list_t)); if (s_thread_cond_init(&window->eventq->cond)) { goto err0; } if (s_thread_mutex_init(&window->eventq->mut)) { goto err1; } return 0; err1: s_thread_cond_destroy(window->eventq->cond); err0: s_free(window->eventq->queue); s_free(window->eventq); return 1; }
int s_eventq_uninit (s_window_t *window) { s_event_t *e; s_thread_mutex_destroy(window->eventq->mut); s_thread_cond_destroy(window->eventq->cond); while (!s_list_eol(window->eventq->queue, 0)) { e = (s_event_t *) s_list_get(window->eventq->queue, 0); s_list_remove(window->eventq->queue, 0); s_event_uninit(e); } s_free(window->eventq->queue); s_free(window->eventq); return 0; }
s_thread_t * s_thread_create (void * (*f) (void *), void *farg) { s_thread_t *tid; s_thread_arg_t *arg; if ((s_thread_api == NULL) || (s_thread_api->thread_create == NULL)) { return NULL; } tid = (s_thread_t *) s_malloc(sizeof(s_thread_t)); arg = (s_thread_arg_t *) s_malloc(sizeof(s_thread_arg_t)); arg->r = &s_thread_run; arg->f = f; arg->arg = farg; s_thread_cond_init(&arg->cond); s_thread_mutex_init(&arg->mut); s_thread_mutex_lock(arg->mut); arg->flag = 0; s_thread_cond_signal(arg->cond); s_thread_mutex_unlock(arg->mut); s_thread_api->thread_create(tid, arg); s_thread_mutex_lock(arg->mut); while (arg->flag != 1) { if (s_thread_cond_wait(arg->cond, arg->mut)) { debugf(DSYS, "s_thread_cond_wait failed"); return NULL; } } s_thread_mutex_unlock(arg->mut); s_thread_cond_destroy(arg->cond); s_thread_mutex_destroy(arg->mut); s_free(arg); arg = NULL; return tid; }