diff options
author | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2018-01-31 11:35:41 +0100 |
---|---|---|
committer | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2018-01-31 11:35:41 +0100 |
commit | cd5e95fea660148140136cfb5e88b72eca754e5d (patch) | |
tree | 1a0f0f88dd4bdcb43b269685d41856ef14bcd17e /rb | |
parent | 37cc2ad02f5e7e75f5eec5b3fd23500b89879c58 (diff) | |
download | librb-cd5e95fea660148140136cfb5e88b72eca754e5d.tar.gz librb-cd5e95fea660148140136cfb5e88b72eca754e5d.tar.bz2 librb-cd5e95fea660148140136cfb5e88b72eca754e5d.zip |
rb_new creates nonthreading object by default
To create threaded object O_MULTITHREAD flag should be passed
to rb_new.
Diffstat (limited to 'rb')
-rw-r--r-- | rb/rb.c | 55 | ||||
-rw-r--r-- | rb/tests.c | 29 |
2 files changed, 38 insertions, 46 deletions
@@ -424,7 +424,6 @@ static long rb_sends #if ENABLE_THREADS -#include <stdio.h> long rb_sendt ( @@ -608,6 +607,14 @@ struct rb *rb_new struct rb *rb; /* pointer to newly created buffer */ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ +#if ENABLE_THREADS == 0 + /* + * multithreaded operations are not allowed when library is compiled + * without threads + */ + + VALID(ENOSYS, flags & O_MULTIHREAD); +#endif if (rb_is_power_of_two(count) == 0) { @@ -637,16 +644,21 @@ struct rb *rb_new #if ENABLE_THREADS == 0 return rb; #else - if (flags & O_NONTHREAD) - { - VALIDGO(EINVAL, error, flags & O_NONBLOCK) - return rb; - } - /* * Multithreaded environment */ + if (flags & ~O_MULTITHREAD) + { + /* + * when working in non multi-threaded mode, force O_NONBLOCK flag, + * and return, as we don't need to init pthread elements. + */ + + flags |= O_NONBLOCK; + return rb; + } + rb->stopped_all = -1; rb->force_exit = 0; @@ -674,10 +686,10 @@ error: /* ========================================================================== Reads maximum of count elements from rb and stores them into buffer. - If rb is O_NONTHREAD or O_NONBLOCK, function will never block, and - cannot guarantee writing count elements into buffer. If there is not - enough data in ring buffer, function will read whole buffer and - return with elements read. + If rb is working in single thread mode or O_NONBLOCK flag is set, + function will never block, and cannot guarantee writing count elements + into buffer. If there is not enough data in ring buffer, function will + read whole buffer and return with elements read. If rb is threaded and blocking, function will block (sleep) caller thread until all count elements were copied into buffer. Function @@ -714,7 +726,7 @@ long rb_recv VALID(EINVAL, rb->buffer); #if ENABLE_THREADS - if (rb->flags & O_NONTHREAD) + if ((rb->flags & O_MULTITHREAD) == 0) { return rb_recvs(rb, buffer, count, flags); } @@ -752,13 +764,14 @@ long rb_recv /* ========================================================================== Writes maximum count data from buffer into rb. - If rb is O_NONTHREAD or O_NONBLOCK, function will never block, but also - cannot guarantee that count elements will be copied from buffer. If - there is not enough space in rb, function will store as many elements as - it can, and return with number of elements stored into rb. + If rb is working in single thread mode or O_NONBLOCK flag is set, + function will never block, but also cannot guarantee that count elements + will be copied from buffer. If there is not enough space in rb, function + will store as many elements as it can, and return with number of + elements stored into rb. - If rb is multithreaded, and blocking function will block (sleep) caller - until count elements have been stored into rb. + If rb is multithreaded, and in blocking mode function will block (sleep) + caller until count elements have been stored into rb. Function os equivalent to call rb_send with flags == 0 ========================================================================== */ @@ -793,7 +806,7 @@ long rb_send VALID(EINVAL, rb->buffer); #if ENABLE_THREADS - if (rb->flags & O_NONTHREAD) + if ((rb->flags & O_MULTITHREAD) == 0) { return rb_sends(rb, buffer, count, flags); } @@ -869,7 +882,7 @@ int rb_destroy VALID(EINVAL, rb->buffer); #if ENABLE_THREADS - if (rb->flags & O_NONTHREAD) + if (rb->flags & ~O_MULTITHREAD) { free(rb->buffer); free(rb); @@ -921,7 +934,7 @@ int rb_stop VALID(EINVAL, rb); - VALID(EINVAL, (rb->flags & O_NONTHREAD) != O_NONTHREAD); + VALID(EINVAL, rb->flags & O_MULTITHREAD); rb->stopped_all = 0; if ((e = pthread_create(&rb->stop_thread, NULL, rb_stop_thread, rb)) != 0) @@ -141,7 +141,7 @@ static void multi_producers_consumers(void) cons = malloc(t_num_consumers * sizeof(*cons)); prod = malloc(t_num_producers * sizeof(*prod)); - rb = rb_new(8, sizeof(unsigned int), 0); + rb = rb_new(8, sizeof(unsigned int), O_MULTITHREAD); pthread_mutex_init(&multi_mutex, NULL); pthread_mutex_init(&multi_mutex_count, NULL); @@ -230,7 +230,7 @@ static void multi_thread(void) recv_buf[i] = 0; } - rb = rb_new(t_rblen, t_objsize, 0); + rb = rb_new(t_rblen, t_objsize, O_MULTITHREAD); proddata.data = send_buf; proddata.len = t_writelen; @@ -265,16 +265,6 @@ static void multi_thread(void) } -static void nonthread_without_nonblock(void) -{ - struct rb *rb; - - rb = rb_new(4, 1, O_NONTHREAD); - mt_fail(errno == EINVAL); - mt_fail(rb == NULL); - rb_destroy(rb); -} - #endif static void invalid_read_write(void) @@ -293,7 +283,7 @@ static void invalid_read_write(void) static void invalid_stop(void) { struct rb *rb; - rb = rb_new(4, 1, O_NONBLOCK | O_NONTHREAD); + rb = rb_new(4, 1, 0); mt_ferr(rb_stop(rb), EINVAL); rb_destroy(rb); } @@ -344,7 +334,6 @@ static void single_thread(void) struct rb *rb; static unsigned long c; - int flags; size_t i; int rc; @@ -356,12 +345,7 @@ static void single_thread(void) recv_buf[i] = 0; } - flags = 0; -#if ENABLE_THREADS - flags = O_NONBLOCK | O_NONTHREAD; -#endif - - rb = rb_new(t_rblen, t_objsize, flags); + rb = rb_new(t_rblen, t_objsize, 0); written = 0; read = 0; @@ -403,7 +387,6 @@ static void single_thread(void) rb_destroy(rb); } - static void bad_count_value(void) { struct rb *rb; @@ -462,9 +445,5 @@ int main(void) mt_run(bad_count_value); mt_run(invalid_read_write); -#if ENABLE_THREADS - mt_run(nonthread_without_nonblock); -#endif - mt_return(); } |