Advertisement
danielhilst

arrayfifo.c

Oct 20th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5.  
  6. #define EVQ_MAX 10
  7. #define EVQ_SIZ (EVQ_MAX - 1)
  8.  
  9. struct evqueue {
  10.         int ev[EVQ_MAX];
  11.         int start;
  12.         int end;
  13.         pthread_mutex_t lock;
  14.         pthread_cond_t cond;
  15.         pthread_t task;
  16. };
  17.  
  18. #define evq_diff(q) (((q)->end + 1) % EVQ_MAX)
  19. #define evq_full(q) ((q)->start == evq_diff(q))
  20. #define evq_empty(q) ((q)->start == (q)->end)
  21.  
  22. void evq_signal(struct evqueue *q)
  23. {
  24.         pthread_cond_signal(&q->cond);
  25. }
  26.  
  27. void evq_init(struct evqueue *q, void *(*handler)(void *))
  28. {
  29.         assert(q);
  30.         assert(handler);
  31.  
  32.         q->start = q->end = 0;
  33.         pthread_mutex_init(&q->lock, NULL);
  34.         pthread_cond_init(&q->cond, NULL);
  35.         pthread_create(&q->task, NULL, handler, q);
  36. }
  37.  
  38. int evq_put(struct evqueue *q, int ev)
  39. {
  40.         int rc = 0;
  41.  
  42.         pthread_mutex_lock(&q->lock);
  43.         if (evq_full(q)) {
  44.                 rc = -1;
  45.         } else {
  46.                 q->ev[q->end] = ev;
  47.                 q->end = (q->end + 1) % EVQ_MAX;
  48.         }
  49.         pthread_mutex_unlock(&q->lock);
  50.         return rc;
  51. }
  52.  
  53. int evq_get(struct evqueue *q)
  54. {
  55.         int ev;
  56.  
  57.         pthread_mutex_lock(&q->lock);
  58.         if (evq_empty(q)) {
  59.                 ev = -1;
  60.         } else {
  61.                 ev = q->ev[q->start];
  62.                 q->start = (q->start + 1) % EVQ_MAX;
  63.         }
  64.         pthread_mutex_unlock(&q->lock);
  65.         return ev;
  66. }
  67.  
  68.  
  69. void *ev_handler(void *evq)
  70. {
  71.         struct evqueue *evqp = evq;
  72.  
  73.         printf("Starting event handler\n");
  74.         for (;;) {
  75.                 pthread_mutex_lock(&evqp->lock);
  76.                 while (evq_empty(evqp))
  77.                         pthread_cond_wait(&evqp->cond, &evqp->lock);
  78.                 pthread_mutex_unlock(&evqp->lock);
  79.  
  80.                 printf("Handling event %d\n", evq_get(evqp));
  81.         }
  82. }
  83.  
  84. static struct evqueue evq;
  85.  
  86. int main(void)
  87. {
  88.         int i;
  89.  
  90.         evq_init(&evq, ev_handler);
  91.        
  92.         for (i = 0; i < 2000000 ; i++) {
  93.                 if (evq_put(&evq, i)) {
  94.                         puts("Full");
  95.                         usleep(20 * 1000);
  96.                 }
  97.                 evq_signal(&evq);
  98.         }
  99.  
  100.         return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement