File: pcqueue.c

package info (click to toggle)
libpulp 0.3.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,976 kB
  • sloc: ansic: 11,792; python: 1,216; sh: 881; makefile: 871; cpp: 582; asm: 387
file content (48 lines) | stat: -rw-r--r-- 933 bytes parent folder | download | duplicates (2)
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
/* Include the C file so we have access to the implementation of the pcqueue
   without having to implement it into a library.  */

#include "../tools/pcqueue.c"

/* How many enqueues/dequeue.  */
#define NUM_TEST 1000000

static void *
producer_thread(void *a)
{
  long i;
  producer_consumer_t *pcqueue = a;

  for (i = 0; i < NUM_TEST; i++) {
    if (producer_consumer_enqueue(pcqueue, (void *)i) != 0) {
      abort();
    }
  }

  return NULL;
}

int
main()
{
  pthread_t thread;
  long i;

  producer_consumer_t *pcqueue = producer_consumer_new(8);
  if (pcqueue == NULL) {
    printf("Error allocating queue\n");
    return 1;
  }

  pthread_create(&thread, NULL, producer_thread, pcqueue);

  for (i = 0; i < NUM_TEST; i++) {
    void *elem = producer_consumer_dequeue(pcqueue);
    if ((long)elem != i)
      abort();
  }

  pthread_join(thread, NULL);
  producer_consumer_delete(pcqueue);
  printf("Pass\n");
  return 0;
}