A new template for Lab 5 (Consumer Producer Assignment) based on the old one.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.0 KiB

  1. #ifndef QUEUE_H
  2. #define QUEUE_H
  3. #define QUEUESIZE 5
  4. #ifdef UNIX
  5. #include <pthread.h>
  6. #include <unistd.h>
  7. /*
  8. * TODO for students: Define your lock type here
  9. */
  10. #endif
  11. #ifdef WINDOWS
  12. #include <windows.h>
  13. /*
  14. * TODO for students: Define your lock type here
  15. */
  16. #endif
  17. typedef struct
  18. {
  19. int buff[QUEUESIZE];
  20. int index;
  21. int len;
  22. /*
  23. * TODO for students: Implement your lock here
  24. */
  25. } queue;
  26. queue *queueInit();
  27. void queueDestroy(queue *q);
  28. void queueLock(queue *q);
  29. void queueUnlock(queue *q);
  30. static inline int queueLen(queue *q) {return q->len;}
  31. static inline int queueIsFull(queue *q) {return (q->len == QUEUESIZE);}
  32. static inline int queueIsEmpty(queue *q) {return (q->len == 0);}
  33. // Add item to the queue
  34. static inline void enqueue(queue *q, int in)
  35. {
  36. q->buff[(q->index + q->len++) % QUEUESIZE] = in;
  37. }
  38. // Remove item from queue
  39. static inline int dequeue(queue *q)
  40. {
  41. int n = q->buff[q->index];
  42. if ((q->index = (q->index + 1) % QUEUESIZE) == -1)
  43. q->index = QUEUESIZE - 1;
  44. --q->len;
  45. return n;
  46. }
  47. #endif