X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=c%2Fpthread_extra%2Fpthread_msgqueue.c;h=b8562965eef620cce7a21c634bab85129071c4c9;hb=77c7dc7ffcaa64879e90178df5585f22790f46be;hp=dbdb65ffc7166d3138a684a310cb72a6e7a6845e;hpb=3aac261937c3a94461f4dd8fa64c17d5e158c692;p=mirrors%2FPrograms.git diff --git a/c/pthread_extra/pthread_msgqueue.c b/c/pthread_extra/pthread_msgqueue.c index dbdb65f..b856296 100644 --- a/c/pthread_extra/pthread_msgqueue.c +++ b/c/pthread_extra/pthread_msgqueue.c @@ -39,6 +39,10 @@ size_t pthread_mq_waiting(pthread_mq_t *mq) { return mq->msg_count; } +size_t pthread_mq_vacant(pthread_mq_t *mq) { + return (mq->msg_count_max - mq->msg_count); +} + bool pthread_mq_reset(pthread_mq_t *mq) { if(pthread_mutex_lock(&mq->lock)) return false; mq->msg_count = 0; @@ -49,6 +53,7 @@ bool pthread_mq_reset(pthread_mq_t *mq) { } 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 @@ -56,9 +61,12 @@ bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const //Wait for queue to be in writable condition while(!pthread_mq_writable(mq)) { + //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) { @@ -68,7 +76,7 @@ bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const } //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; @@ -91,6 +99,8 @@ bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const 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) {