async_queue.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2008-2010 Nokia Corporation
  3. *
  4. * Author: Felipe Contreras <felipe.contreras@nokia.com>
  5. *
  6. * This file may be used under the terms of the GNU Lesser General Public
  7. * License version 2.1, a copy of which is found in LICENSE included in the
  8. * packaging of this file.
  9. */
  10. #include <glib.h>
  11. #include "async_queue.h"
  12. AsyncQueue *
  13. async_queue_new(void)
  14. {
  15. AsyncQueue *queue;
  16. queue = g_slice_new0(AsyncQueue);
  17. queue->condition = g_cond_new();
  18. queue->mutex = g_mutex_new();
  19. queue->enabled = TRUE;
  20. return queue;
  21. }
  22. void
  23. async_queue_free(AsyncQueue *queue)
  24. {
  25. g_cond_free(queue->condition);
  26. g_mutex_free(queue->mutex);
  27. g_list_free(queue->head);
  28. g_slice_free(AsyncQueue, queue);
  29. }
  30. void
  31. async_queue_push(AsyncQueue *queue,
  32. gpointer data)
  33. {
  34. g_mutex_lock(queue->mutex);
  35. queue->head = g_list_prepend(queue->head, data);
  36. if (!queue->tail)
  37. queue->tail = queue->head;
  38. queue->length++;
  39. g_cond_signal(queue->condition);
  40. g_mutex_unlock(queue->mutex);
  41. }
  42. gpointer
  43. async_queue_pop(AsyncQueue *queue)
  44. {
  45. gpointer data = NULL;
  46. g_mutex_lock(queue->mutex);
  47. if (!queue->enabled)
  48. goto leave;
  49. if (!queue->tail)
  50. g_cond_wait(queue->condition, queue->mutex);
  51. if (queue->tail) {
  52. GList *node = queue->tail;
  53. data = node->data;
  54. queue->tail = node->prev;
  55. if (queue->tail)
  56. queue->tail->next = NULL;
  57. else
  58. queue->head = NULL;
  59. queue->length--;
  60. g_list_free_1(node);
  61. }
  62. leave:
  63. g_mutex_unlock(queue->mutex);
  64. return data;
  65. }
  66. gpointer
  67. async_queue_pop_forced(AsyncQueue *queue)
  68. {
  69. gpointer data = NULL;
  70. g_mutex_lock(queue->mutex);
  71. if (queue->tail) {
  72. GList *node = queue->tail;
  73. data = node->data;
  74. queue->tail = node->prev;
  75. if (queue->tail)
  76. queue->tail->next = NULL;
  77. else
  78. queue->head = NULL;
  79. queue->length--;
  80. g_list_free_1(node);
  81. }
  82. g_mutex_unlock(queue->mutex);
  83. return data;
  84. }
  85. void
  86. async_queue_disable(AsyncQueue *queue)
  87. {
  88. g_mutex_lock(queue->mutex);
  89. queue->enabled = FALSE;
  90. g_cond_broadcast(queue->condition);
  91. g_mutex_unlock(queue->mutex);
  92. }
  93. void
  94. async_queue_enable(AsyncQueue *queue)
  95. {
  96. g_mutex_lock(queue->mutex);
  97. queue->enabled = TRUE;
  98. g_mutex_unlock(queue->mutex);
  99. }
  100. void
  101. async_queue_flush(AsyncQueue *queue)
  102. {
  103. g_mutex_lock(queue->mutex);
  104. g_list_free(queue->head);
  105. queue->head = queue->tail = NULL;
  106. queue->length = 0;
  107. g_mutex_unlock(queue->mutex);
  108. }