aboutsummaryrefslogtreecommitdiff
path: root/build/autotests/tests/threadedqueue.c
diff options
context:
space:
mode:
authorIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2015-09-01 14:32:27 +0000
committerIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2015-09-01 14:32:27 +0000
commit9f0009faeaa0960f57fcacea91b45997258016e0 (patch)
treef434613d74c0bebef7abfdbdb67e0bd55f3fa220 /build/autotests/tests/threadedqueue.c
parente6f799e41d7910e1388c68630c49bf3141cb8d9c (diff)
synch more with git
svn path=/trunk/externals/iem/iemnet/; revision=17546
Diffstat (limited to 'build/autotests/tests/threadedqueue.c')
-rw-r--r--build/autotests/tests/threadedqueue.c77
1 files changed, 77 insertions, 0 deletions
diff --git a/build/autotests/tests/threadedqueue.c b/build/autotests/tests/threadedqueue.c
new file mode 100644
index 0000000..b68f11d
--- /dev/null
+++ b/build/autotests/tests/threadedqueue.c
@@ -0,0 +1,77 @@
+#include <common.h>
+
+#include <pthread.h>
+
+#define NUMCHUNKS 1000
+
+
+typedef union {
+ unsigned char cp;
+ int count;
+} data_t;
+static int producer(t_iemnet_queue*q, unsigned int count, unsigned int msec) {
+ unsigned int i;
+ data_t data;
+ for(i=0; i<count; i++) {
+ t_iemnet_chunk*chunk=0;
+ data.count=i;
+
+ chunk=iemnet__chunk_create_data(sizeof(data), &data.cp);
+ queue_push(q, chunk);
+ usleep(1000*msec);
+ }
+ return 0;
+}
+
+static int consumer(t_iemnet_queue*q) {
+ t_iemnet_chunk*chunk=NULL;
+ while(1) {
+ data_t*data=NULL;
+ chunk=queue_pop_block(q);
+ if(!chunk)
+ break;
+ if(sizeof(data_t)!=chunk->size) {
+ error("size mismatch %d!=%d", sizeof(data_t), chunk->size);
+ fail();
+ }
+ data=chunk->data;
+ //printf("%d ", data->count);
+ iemnet__chunk_destroy(chunk);
+ }
+ printf("\n");
+ return 0;
+}
+static void* consumer_thread(void*qq) {
+ t_iemnet_queue*q=(t_iemnet_queue*)qq;
+ consumer(q);
+ return NULL;
+}
+
+void threadedqueue_setup(void) {
+ pthread_t thread;
+ pthread_attr_t threadattr;
+
+ t_iemnet_queue*q=queue_create();
+
+ /* prepare child thread */
+ if(pthread_attr_init(&threadattr) < 0) {
+ error("warning: could not prepare child thread");
+ fail();
+ }
+ if(pthread_attr_setdetachstate(&threadattr, PTHREAD_CREATE_DETACHED) < 0) {
+ error("warning: could not prepare child thread...");
+ fail();
+ }
+
+ if(pthread_create(&thread, &threadattr, consumer_thread, q) < 0) {
+ error("warning: could not create child thread");
+ fail();
+ }
+
+
+ producer(q, 1000, 1);
+
+
+ queue_destroy(q);
+ pass();
+}