Packet tunneling over UDP, multiple channels
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.
 
 
 
 

72 lines
1.4 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "queue.h"
  4. void Queue_addItem(Queue *list,QueueItem *item) {
  5. if ( pthread_mutex_lock( &list->mutex ) ) {
  6. perror( "FATAL" );
  7. exit( 1 );
  8. }
  9. item->next = 0; // just in case
  10. if ( list->last ) {
  11. list->last->next = item;
  12. } else {
  13. list->head = item;
  14. }
  15. list->last = item;
  16. if ( sem_post( &list->count ) ) {
  17. perror( "FATAL" );
  18. exit( 1 );
  19. }
  20. if ( pthread_mutex_unlock( &list->mutex ) ) {
  21. perror( "FATAL" );
  22. exit( 1 );
  23. }
  24. }
  25. QueueItem *Queue_getItem(Queue *list) {
  26. QueueItem *item;
  27. if ( sem_wait( &list->count ) ) {
  28. perror( "FATAL" );
  29. exit( 1 );
  30. }
  31. if ( pthread_mutex_lock( &list->mutex ) ) {
  32. perror( "FATAL" );
  33. exit( 1 );
  34. }
  35. item = list->head;
  36. list->head = item->next;
  37. if ( list->head == 0 ) {
  38. list->last = 0;
  39. }
  40. if ( pthread_mutex_unlock( &list->mutex ) ) {
  41. perror( "FATAL" );
  42. exit( 1 );
  43. }
  44. return item;
  45. }
  46. void Queue_initialize(Queue *list,int n,size_t size) {
  47. if ( pthread_mutex_lock( &list->mutex ) ) {
  48. perror( "FATAL" );
  49. exit( 1 );
  50. }
  51. if ( list->head == 0 ) {
  52. int i = 0;
  53. for ( ; i < n; i++ ) {
  54. QueueItem *x = (QueueItem *) calloc( 1, size );
  55. if ( list->head ) {
  56. list->last->next = x;
  57. } else {
  58. list->head = x;
  59. }
  60. list->last = x;
  61. }
  62. sem_init( &list->count, 0, n );
  63. }
  64. if ( pthread_mutex_unlock( &list->mutex ) ) {
  65. perror( "FATAL" );
  66. exit( 1 );
  67. }
  68. }