MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ut0wqueue.cc
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 2006, 2011, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation; version 2 of the License.
8 
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License along with
14 this program; if not, write to the Free Software Foundation, Inc.,
15 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16 
17 *****************************************************************************/
18 
19 #include "ut0wqueue.h"
20 
21 /*******************************************************************/
28 /****************************************************************/
31 UNIV_INTERN
34 /*===================*/
35 {
36  ib_wqueue_t* wq = static_cast<ib_wqueue_t*>(mem_alloc(sizeof(*wq)));
37 
38  /* Function ib_wqueue_create() has not been used anywhere,
39  not necessary to instrument this mutex */
40  mutex_create(PFS_NOT_INSTRUMENTED, &wq->mutex, SYNC_WORK_QUEUE);
41 
42  wq->items = ib_list_create();
43  wq->event = os_event_create();
44 
45  return(wq);
46 }
47 
48 /****************************************************************/
50 UNIV_INTERN
51 void
53 /*===========*/
54  ib_wqueue_t* wq)
55 {
56  mutex_free(&wq->mutex);
57  ib_list_free(wq->items);
58  os_event_free(wq->event);
59 
60  mem_free(wq);
61 }
62 
63 /****************************************************************/
65 UNIV_INTERN
66 void
68 /*==========*/
69  ib_wqueue_t* wq,
70  void* item,
71  mem_heap_t* heap)
73 {
74  mutex_enter(&wq->mutex);
75 
76  ib_list_add_last(wq->items, item, heap);
77  os_event_set(wq->event);
78 
79  mutex_exit(&wq->mutex);
80 }
81 
82 /****************************************************************/
85 UNIV_INTERN
86 void*
88 /*===========*/
89  ib_wqueue_t* wq)
90 {
91  ib_list_node_t* node;
92 
93  for (;;) {
94  os_event_wait(wq->event);
95 
96  mutex_enter(&wq->mutex);
97 
98  node = ib_list_get_first(wq->items);
99 
100  if (node) {
101  ib_list_remove(wq->items, node);
102 
103  if (!ib_list_get_first(wq->items)) {
104  /* We must reset the event when the list
105  gets emptied. */
106  os_event_reset(wq->event);
107  }
108 
109  break;
110  }
111 
112  mutex_exit(&wq->mutex);
113  }
114 
115  mutex_exit(&wq->mutex);
116 
117  return(node->data);
118 }
119 
120 
121 /********************************************************************
122 Wait for a work item to appear in the queue for specified time. */
123 
124 void*
125 ib_wqueue_timedwait(
126 /*================*/
127  /* out: work item or NULL on timeout*/
128  ib_wqueue_t* wq, /* in: work queue */
129  ib_time_t wait_in_usecs) /* in: wait time in micro seconds */
130 {
131  ib_list_node_t* node = NULL;
132 
133  for (;;) {
134  ulint error;
135  ib_int64_t sig_count;
136 
137  mutex_enter(&wq->mutex);
138 
139  node = ib_list_get_first(wq->items);
140 
141  if (node) {
142  ib_list_remove(wq->items, node);
143 
144  mutex_exit(&wq->mutex);
145  break;
146  }
147 
148  sig_count = os_event_reset(wq->event);
149 
150  mutex_exit(&wq->mutex);
151 
152  error = os_event_wait_time_low(wq->event,
153  (ulint) wait_in_usecs,
154  sig_count);
155 
156  if (error == OS_SYNC_TIME_EXCEEDED) {
157  break;
158  }
159  }
160 
161  return(node ? node->data : NULL);
162 }
163 
164 /********************************************************************
165 Check if queue is empty. */
166 
167 ibool
168 ib_wqueue_is_empty(
169 /*===============*/
170  /* out: TRUE if queue empty
171  else FALSE */
172  const ib_wqueue_t* wq) /* in: work queue */
173 {
174  return(ib_list_is_empty(wq->items));
175 }