static int unbuffered_chan_init(chan_t* chan) { if (pthread_mutex_init(&chan->w_mu, NULL) != 0) { return -1; } if (pthread_mutex_init(&chan->r_mu, NULL) != 0) { pthread_mutex_destroy(&chan->w_mu); return -1; } if (pthread_mutex_init(&chan->m_mu, NULL) != 0) { pthread_mutex_destroy(&chan->w_mu); pthread_mutex_destroy(&chan->r_mu); return -1; } if (pthread_cond_init(&chan->m_cond, NULL) != 0) { pthread_mutex_destroy(&chan->m_mu); pthread_mutex_destroy(&chan->w_mu); pthread_mutex_destroy(&chan->r_mu); return -1; } blocking_pipe_t* pipe = blocking_pipe_init(); if (!pipe) { pthread_mutex_destroy(&chan->m_mu); pthread_mutex_destroy(&chan->w_mu); pthread_mutex_destroy(&chan->r_mu); pthread_cond_destroy(&chan->m_cond); return -1; } chan->readers = 0; chan->closed = 0; chan->pipe = pipe; return 0; }
static int unbuffered_chan_init(chan_t* chan) { pthread_mutex_t* w_mu = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t)); if (!w_mu) { errno = ENOMEM; return -1; } if (pthread_mutex_init(w_mu, NULL) != 0) { free(w_mu); } pthread_mutex_t* r_mu = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t)); if (!r_mu) { errno = ENOMEM; pthread_mutex_destroy(w_mu); return -1; } if (pthread_mutex_init(r_mu, NULL) != 0) { free(r_mu); pthread_mutex_destroy(w_mu); return -1; } pthread_mutex_t* mu = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t)); if (!mu) { errno = ENOMEM; pthread_mutex_destroy(w_mu); pthread_mutex_destroy(r_mu); return -1; } if (pthread_mutex_init(mu, NULL) != 0) { free(mu); pthread_mutex_destroy(w_mu); pthread_mutex_destroy(r_mu); return -1; } pthread_cond_t* cond = (pthread_cond_t*) malloc(sizeof(pthread_cond_t)); if (!cond) { errno = ENOMEM; pthread_mutex_destroy(mu); pthread_mutex_destroy(w_mu); pthread_mutex_destroy(r_mu); return -1; } if (pthread_cond_init(cond, NULL) != 0) { free(cond); pthread_mutex_destroy(mu); pthread_mutex_destroy(w_mu); pthread_mutex_destroy(r_mu); return -1; } blocking_pipe_t* pipe = blocking_pipe_init(); if (pipe == NULL) { free(cond); pthread_mutex_destroy(mu); pthread_mutex_destroy(w_mu); pthread_mutex_destroy(r_mu); return -1; } chan->m_mu = mu; chan->m_cond = cond; chan->w_mu = w_mu; chan->r_mu = r_mu; chan->readers = 0; chan->closed = 0; chan->pipe = pipe; return 0; }