Drizzled Public API Documentation

ut0wqueue.cc
1 /*****************************************************************************
2 
3 Copyright (C) 2006, 2009, Innobase Oy. 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., 51 Franklin
15 St, Fifth Floor, Boston, MA 02110-1301 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(ib_wqueue_t)));
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(NULL);
44 
45  return(wq);
46 }
47 
48 /****************************************************************/
50 UNIV_INTERN
51 void
53 /*===========*/
54  ib_wqueue_t* wq)
55 {
57 
58  mutex_free(&wq->mutex);
59  ib_list_free(wq->items);
60  os_event_free(wq->event);
61 
62  mem_free(wq);
63 }
64 
65 /****************************************************************/
67 UNIV_INTERN
68 void
70 /*==========*/
71  ib_wqueue_t* wq,
72  void* item,
73  mem_heap_t* heap)
75 {
76  mutex_enter(&wq->mutex);
77 
78  ib_list_add_last(wq->items, item, heap);
79  os_event_set(wq->event);
80 
81  mutex_exit(&wq->mutex);
82 }
83 
84 /****************************************************************/
87 UNIV_INTERN
88 void*
90 /*===========*/
91  ib_wqueue_t* wq)
92 {
93  ib_list_node_t* node;
94 
95  for (;;) {
96  os_event_wait(wq->event);
97 
98  mutex_enter(&wq->mutex);
99 
100  node = ib_list_get_first(wq->items);
101 
102  if (node) {
103  ib_list_remove(wq->items, node);
104 
105  if (!ib_list_get_first(wq->items)) {
106  /* We must reset the event when the list
107  gets emptied. */
108  os_event_reset(wq->event);
109  }
110 
111  break;
112  }
113 
114  mutex_exit(&wq->mutex);
115  }
116 
117  mutex_exit(&wq->mutex);
118 
119  return(node->data);
120 }
UNIV_INTERN ib_wqueue_t * ib_wqueue_create(void)
Definition: ut0wqueue.cc:33
UNIV_INTERN void ib_wqueue_free(ib_wqueue_t *wq)
Definition: ut0wqueue.cc:52
UNIV_INTERN ib_list_node_t * ib_list_add_last(ib_list_t *list, void *data, mem_heap_t *heap)
Definition: ut0list.cc:103
ib_list_t * items
Definition: ut0wqueue.h:82
#define mem_free(PTR)
Definition: mem0mem.h:249
UNIV_INTERN void * ib_wqueue_wait(ib_wqueue_t *wq)
Definition: ut0wqueue.cc:89
UNIV_INTERN os_event_t os_event_create(const char *name)
Definition: os0sync.cc:365
UNIV_INLINE ib_list_node_t * ib_list_get_first(ib_list_t *list)
os_event_t event
Definition: ut0wqueue.h:83
UNIV_INTERN void os_event_set(os_event_t event)
Definition: os0sync.cc:434
UNIV_INTERN ib_int64_t os_event_reset(os_event_t event)
Definition: os0sync.cc:472
#define ut_a(EXPR)
Definition: ut0dbg.h:105
UNIV_INTERN void ib_list_remove(ib_list_t *list, ib_list_node_t *node)
Definition: ut0list.cc:170
UNIV_INTERN ib_list_t * ib_list_create(void)
Definition: ut0list.cc:36
UNIV_INTERN void os_event_free(os_event_t event)
Definition: os0sync.cc:535
UNIV_INTERN void ib_wqueue_add(ib_wqueue_t *wq, void *item, mem_heap_t *heap)
Definition: ut0wqueue.cc:69
UNIV_INTERN void ib_list_free(ib_list_t *list)
Definition: ut0list.cc:71
mutex_t mutex
Definition: ut0wqueue.h:81