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.

175 lines
4.0 KiB

  1. /*****************************************************************************
  2. Copyright (c) 2006, 2011, Oracle and/or its affiliates. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
  12. *****************************************************************************/
  13. #include "ut0wqueue.h"
  14. /*******************************************************************//**
  15. @file ut/ut0wqueue.cc
  16. A work queue
  17. Created 4/26/2006 Osku Salerma
  18. ************************************************************************/
  19. /****************************************************************//**
  20. Create a new work queue.
  21. @return work queue */
  22. UNIV_INTERN
  23. ib_wqueue_t*
  24. ib_wqueue_create(void)
  25. /*===================*/
  26. {
  27. ib_wqueue_t* wq = static_cast<ib_wqueue_t*>(mem_alloc(sizeof(*wq)));
  28. /* Function ib_wqueue_create() has not been used anywhere,
  29. not necessary to instrument this mutex */
  30. mutex_create(PFS_NOT_INSTRUMENTED, &wq->mutex, SYNC_WORK_QUEUE);
  31. wq->items = ib_list_create();
  32. wq->event = os_event_create();
  33. return(wq);
  34. }
  35. /****************************************************************//**
  36. Free a work queue. */
  37. UNIV_INTERN
  38. void
  39. ib_wqueue_free(
  40. /*===========*/
  41. ib_wqueue_t* wq) /*!< in: work queue */
  42. {
  43. mutex_free(&wq->mutex);
  44. ib_list_free(wq->items);
  45. os_event_free(wq->event);
  46. mem_free(wq);
  47. }
  48. /****************************************************************//**
  49. Add a work item to the queue. */
  50. UNIV_INTERN
  51. void
  52. ib_wqueue_add(
  53. /*==========*/
  54. ib_wqueue_t* wq, /*!< in: work queue */
  55. void* item, /*!< in: work item */
  56. mem_heap_t* heap) /*!< in: memory heap to use for allocating the
  57. list node */
  58. {
  59. mutex_enter(&wq->mutex);
  60. ib_list_add_last(wq->items, item, heap);
  61. os_event_set(wq->event);
  62. mutex_exit(&wq->mutex);
  63. }
  64. /****************************************************************//**
  65. Wait for a work item to appear in the queue.
  66. @return work item */
  67. UNIV_INTERN
  68. void*
  69. ib_wqueue_wait(
  70. /*===========*/
  71. ib_wqueue_t* wq) /*!< in: work queue */
  72. {
  73. ib_list_node_t* node;
  74. for (;;) {
  75. os_event_wait(wq->event);
  76. mutex_enter(&wq->mutex);
  77. node = ib_list_get_first(wq->items);
  78. if (node) {
  79. ib_list_remove(wq->items, node);
  80. if (!ib_list_get_first(wq->items)) {
  81. /* We must reset the event when the list
  82. gets emptied. */
  83. os_event_reset(wq->event);
  84. }
  85. break;
  86. }
  87. mutex_exit(&wq->mutex);
  88. }
  89. mutex_exit(&wq->mutex);
  90. return(node->data);
  91. }
  92. /********************************************************************
  93. Wait for a work item to appear in the queue for specified time. */
  94. void*
  95. ib_wqueue_timedwait(
  96. /*================*/
  97. /* out: work item or NULL on timeout*/
  98. ib_wqueue_t* wq, /* in: work queue */
  99. ib_time_t wait_in_usecs) /* in: wait time in micro seconds */
  100. {
  101. ib_list_node_t* node = NULL;
  102. for (;;) {
  103. ulint error;
  104. ib_int64_t sig_count;
  105. mutex_enter(&wq->mutex);
  106. node = ib_list_get_first(wq->items);
  107. if (node) {
  108. ib_list_remove(wq->items, node);
  109. mutex_exit(&wq->mutex);
  110. break;
  111. }
  112. sig_count = os_event_reset(wq->event);
  113. mutex_exit(&wq->mutex);
  114. error = os_event_wait_time_low(wq->event,
  115. (ulint) wait_in_usecs,
  116. sig_count);
  117. if (error == OS_SYNC_TIME_EXCEEDED) {
  118. break;
  119. }
  120. }
  121. return(node ? node->data : NULL);
  122. }
  123. /********************************************************************
  124. Check if queue is empty. */
  125. ibool
  126. ib_wqueue_is_empty(
  127. /*===============*/
  128. /* out: TRUE if queue empty
  129. else FALSE */
  130. const ib_wqueue_t* wq) /* in: work queue */
  131. {
  132. return(ib_list_is_empty(wq->items));
  133. }