/* This function is used internally by malloc. */ int _pthread_mutex_init_calloc_cb(pthread_mutex_t *mutex, void *(calloc_cb)(size_t, size_t)) { static const struct pthread_mutex_attr attr = { .m_type = PTHREAD_MUTEX_NORMAL, .m_protocol = PTHREAD_PRIO_NONE, .m_ceiling = 0, .m_flags = 0 }; static const struct pthread_mutex_attr *pattr = &attr; return (thr_mutex_init(mutex, (pthread_mutexattr_t *)&pattr, calloc_cb)); } void _thr_mutex_reinit(pthread_mutex_t *mutex) { _lock_reinit(&(*mutex)->m_lock, LCK_ADAPTIVE, _thr_lock_wait, _thr_lock_wakeup); TAILQ_INIT(&(*mutex)->m_queue); (*mutex)->m_owner = NULL; (*mutex)->m_count = 0; (*mutex)->m_refcount = 0; (*mutex)->m_prio = 0; (*mutex)->m_saved_prio = 0; }
int main(void) { int my_count = 5; thr_mutex_init(&mutex); if (thr_event_init(&event) < 0) { report_syserr("thr_event_init"); return -1; } thr_start(&worker, work_func, NULL); while (my_count) { thr_mutex_lock(&mutex); counter++; my_count--; printf("producer: counter++\n"); thr_mutex_unlock(&mutex); thr_event_raise(&event); clock_wait(10); } thr_join(worker); test_timedwait(); thr_event_destroy(&event); thr_mutex_destroy(&mutex); return 0; }
int __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutex_attr) { return (thr_mutex_init(mutex, mutex_attr, calloc)); }
void afile_init(struct afile *a, struct ioq *q, handle_t h) { ioq_fd_init(&a->fd, q, h); a->flags = 0; memset(&a->read, 0, sizeof(a->read)); memset(&a->write, 0, sizeof(a->write)); thr_mutex_init(&a->lock); }
int ioq_init(struct ioq *q, unsigned int bg_threads) { struct epoll_event evt; syserr_t err; if (runq_init(&q->run, bg_threads) < 0) { err = syserr_last(); goto fail_runq; } if (!bg_threads) q->run.wakeup = wakeup_runq; waitq_init(&q->wait, &q->run); q->wait.wakeup = wakeup_waitq; thr_mutex_init(&q->lock); slist_init(&q->mod_list); if (pipe(q->intr) < 0) { err = syserr_last(); goto fail_pipe; } fcntl(q->intr[0], F_SETFL, fcntl(q->intr[0], F_GETFL) | O_NONBLOCK); q->intr_state = 0; q->epoll_fd = epoll_create(64); if (q->epoll_fd < 0) { err = syserr_last(); goto fail_epoll; } memset(&evt, 0, sizeof(evt)); evt.events = EPOLLIN; if (epoll_ctl(q->epoll_fd, EPOLL_CTL_ADD, q->intr[0], &evt) < 0) { err = syserr_last(); goto fail_ctl; } return 0; fail_ctl: close(q->epoll_fd); fail_epoll: close(q->intr[0]); close(q->intr[1]); fail_pipe: thr_mutex_destroy(&q->lock); waitq_destroy(&q->wait); runq_destroy(&q->run); fail_runq: syserr_set(err); return -1; }