aboutsummaryrefslogtreecommitdiff
path: root/build/autotests/tests/serialqueue.c
blob: 238be41cedb82829ed1f07bdd80be93953793106 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#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;
    //    post("producing %d", 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;

    /* in a non-threaded context, we must not use queue_pop_block() 
     * as we have no way to unblock the queue,
     * resulting in a deadlock
     */
    chunk=queue_pop_noblock(q);
    if(!chunk)
      break;
    if(sizeof(data_t)!=chunk->size) {
      error("size mismatch %d!=%d", sizeof(data_t), chunk->size);
      fail();
    }
    data=chunk->data;
    //    post("consumed %d", data->count);
    iemnet__chunk_destroy(chunk);
  }
  printf("\n");
  return 0;
}

void serialqueue_setup(void) {
  t_iemnet_queue*q=queue_create();
  producer(q, 1000, 1);
  consumer(q);

  queue_destroy(q);
  pass();
}