X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=c%2Fpthread_extra%2Fpthread_msgqueue.c;h=44ad399ecb9ef195b78cae64994af93cd4c685db;hb=HEAD;hp=6e509c775eff38300699a3d65a321d4a1e8937ec;hpb=63658368a85bba1239a9049b6bbd4ea4d87469b2;p=mirrors%2FPrograms.git diff --git a/c/pthread_extra/pthread_msgqueue.c b/c/pthread_extra/pthread_msgqueue.c index 6e509c7..44ad399 100644 --- a/c/pthread_extra/pthread_msgqueue.c +++ b/c/pthread_extra/pthread_msgqueue.c @@ -1,30 +1,11 @@ -/* - * CFLAGS=-lpthread make pthread_msgqueue - */ - #include +#include #include #include #include #include #include #include -#include - -#define PTHREAD_X_NONWAIT (&(struct timespec){ .tv_sec = 0, .tv_nsec = 0 }) -#define PTHREAD_X_FOREVER NULL - -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); } @@ -58,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; @@ -67,7 +52,8 @@ bool pthread_mq_reset(pthread_mq_t *mq) { return true; } -bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const struct timespec *restrict abs_timeout) { +bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout) { + //printf("S-Timed: %p\n", abs_timeout); int ret; //Lock queue @@ -75,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) { @@ -86,8 +75,12 @@ bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const } } + //Handle overwrite + assert(!(flags & PTHREAD_XMQ_OVERW) && "FIXME: Overwrite not implemented yet!"); + //Write data to queue - size_t idx = ( ( mq->msg_count_max + mq->head_idx + (to_front?-1:mq->msg_count) ) % mq->msg_count_max ); + bool to_front = (flags & PTHREAD_XMQ_FRONT); + 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; @@ -99,7 +92,7 @@ bool pthread_mq_send_generic(pthread_mq_t *mq, void * data, bool to_front, const return true; } -bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const struct timespec *restrict abs_timeout) { +bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, pthread_mq_flags_t flags, const struct timespec *restrict abs_timeout) { int ret; //Lock queue @@ -110,6 +103,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) { @@ -123,6 +118,7 @@ bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const memcpy(data, ptr, mq->msg_size); //Delete data from queue if not peeking + bool peek = (flags & PTHREAD_XMQ_PEEK); if(!peek) { mq->msg_count--; mq->head_idx = (mq->head_idx+1) % mq->msg_count_max; @@ -133,48 +129,3 @@ bool pthread_mq_receive_generic(pthread_mq_t *mq, void * data, bool peek, const pthread_mutex_unlock(&mq->lock); return true; } - - - - - - - - - - -pthread_mq_t myq; - -void *thread_recv(void *args) { - char str[128]; - while(1) { - pthread_mq_receive_generic(&myq, &str, false, NULL); - printf("RECVD: %.6s\t\t(waiting %d)\n", str, pthread_mq_waiting(&myq)); - sleep(1); - } -} - -int main() { - char tmp[128]; - - pthread_mq_init(&myq, 6, 5); - - pthread_t t; - pthread_create(&t, NULL, thread_recv, NULL); - - pthread_mq_send_generic(&myq, "AHOJ1", false, NULL); - pthread_mq_send_generic(&myq, "AHOJ2", false, NULL); - pthread_mq_send_generic(&myq, "AHOJ3", true, NULL); - pthread_mq_send_generic(&myq, "AHOJ4", true, NULL); - pthread_mq_send_generic(&myq, "AHOJ5", false, NULL); - pthread_mq_send_generic(&myq, "AHOJ6", true, NULL); - - while(1) { - pthread_mq_send_generic(&myq, "B", false, NULL); - pthread_mq_send_generic(&myq, "A", true, NULL); - pthread_mq_send_generic(&myq, " A", false, NULL); - pthread_mq_send_generic(&myq, " B", false, NULL); - } - - pthread_join(t, NULL); -}