X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=c%2Fpthread_extra%2Fpthread_msgqueue.c;h=ff1a37f865e65afdab274ebc76f061b2748d848e;hb=e7d1ce5c0f321dc0e5894527c1d1401369e46a1b;hp=9531d7884b8e2e13cc7cdf8ef6ef7b74fc0eb145;hpb=774d1b147f61214f12ad9057f8be382932fa49fa;p=mirrors%2FPrograms.git diff --git a/c/pthread_extra/pthread_msgqueue.c b/c/pthread_extra/pthread_msgqueue.c index 9531d78..ff1a37f 100644 --- a/c/pthread_extra/pthread_msgqueue.c +++ b/c/pthread_extra/pthread_msgqueue.c @@ -1,8 +1,5 @@ -/* - * CFLAGS=-lpthread make pthread_msgqueue - */ - #include +#include #include #include #include @@ -10,18 +7,6 @@ #include #include -typedef struct pthread_mq_t { - pthread_mutex_t lock; - pthread_cond_t cond_readable; - pthread_cond_t cond_writable; - void * data; - size_t msg_size; - size_t msg_count; - size_t msg_count_max; - size_t head_idx; - char * name; -} pthread_mq_t; - bool pthread_mq_readable(pthread_mq_t *mq) { return (mq->msg_count > 0); } bool pthread_mq_writable(pthread_mq_t *mq) { return (mq->msg_count < mq->msg_count_max); } @@ -29,6 +14,7 @@ bool pthread_mq_init(pthread_mq_t *mq, size_t msg_size, size_t msg_count_max) { pthread_mutex_init(&mq->lock, NULL); pthread_cond_init(&mq->cond_readable, NULL); pthread_cond_init(&mq->cond_writable, NULL); + //pthread_mutexattr_setclock(&mq->lock, CLOCK_MONOTONIC); mq->data = malloc(msg_size*msg_count_max); mq->msg_size = msg_size; mq->msg_count_max = msg_count_max; @@ -49,6 +35,10 @@ void pthread_mq_cond(pthread_mq_t *mq) { if(pthread_mq_writable(mq)) pthread_cond_broadcast(&mq->cond_writable); } +size_t pthread_mq_waiting(pthread_mq_t *mq) { + return mq->msg_count; +} + bool pthread_mq_reset(pthread_mq_t *mq) { if(pthread_mutex_lock(&mq->lock)) return false; mq->msg_count = 0; @@ -58,20 +48,31 @@ bool pthread_mq_reset(pthread_mq_t *mq) { return true; } -bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, bool block, const struct timespec *restrict abs_timeout) { +bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const struct timespec *restrict abs_timeout) { + //printf("S-Timed: %p\n", abs_timeout); + int ret; + //Lock queue if(pthread_mutex_timedlock(&mq->lock, abs_timeout)) return false; //Wait for queue to be in writable condition while(!pthread_mq_writable(mq)) { - if(pthread_cond_timedwait(&mq->cond_writable, &mq->lock, abs_timeout)) { + //printf("S+Timed: %p\n", abs_timeout); + if(abs_timeout == NULL) { + ret = pthread_cond_wait(&mq->cond_writable, &mq->lock); + } else { + //printf("STimed: %p\n", abs_timeout); + //assert(abs_timeout != NULL); + ret = pthread_cond_timedwait(&mq->cond_writable, &mq->lock, abs_timeout); + } + if(ret) { pthread_mutex_unlock(&mq->lock); return false; } } //Write data to queue - size_t idx = ( ( mq->msg_count_max + mq->head_idx + (to_front?-1:mq->msg_count) ) % mq->msg_count_max ); + size_t idx = ( ( mq->head_idx + (to_front?mq->msg_count_max-1:mq->msg_count) ) % mq->msg_count_max ); void *ptr = mq->data + (idx * mq->msg_size); mq->msg_count++; if(to_front) mq->head_idx = (mq->msg_count_max + mq->head_idx - 1) % mq->msg_count_max; @@ -83,6 +84,39 @@ bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, bool return true; } -int main() { +bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const struct timespec *restrict abs_timeout) { + int ret; + + //Lock queue + if(pthread_mutex_timedlock(&mq->lock, abs_timeout)) return false; + + //Wait for queue to be in readable condition + while(!pthread_mq_readable(mq)) { + if(abs_timeout == NULL) { + ret = pthread_cond_wait(&mq->cond_readable, &mq->lock); + } else { + //printf("RTimed: %p\n", abs_timeout); + //assert(abs_timeout != NULL); + ret = pthread_cond_timedwait(&mq->cond_readable, &mq->lock, abs_timeout); + } + if(ret) { + pthread_mutex_unlock(&mq->lock); + return false; + } + } + + //Read data from queue + void *ptr = mq->data + (mq->head_idx * mq->msg_size); + memcpy(data, ptr, mq->msg_size); + //Delete data from queue if not peeking + if(!peek) { + mq->msg_count--; + mq->head_idx = (mq->head_idx+1) % mq->msg_count_max; + } + + //Signal conditions and unlock + pthread_mq_cond(mq); + pthread_mutex_unlock(&mq->lock); + return true; }