1 /*****************************************************************************
2 
3 Copyright (c) 2006, 2014, Oracle and/or its affiliates. All Rights Reserved.
4 Copyright (c) 2017, 2021, MariaDB Corporation.
5 
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free Software
8 Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along with
15 this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
17 
18 *****************************************************************************/
19 
20 /*******************************************************************//**
21 @file include/ut0wqueue.h
22 A work queue
23 
24 Created 4/26/2006 Osku Salerma
25 ************************************************************************/
26 
27 /*******************************************************************//**
28 A Work queue. Threads can add work items to the queue and other threads can
29 wait for work items to be available and take them off the queue for
30 processing.
31 ************************************************************************/
32 
33 #ifndef IB_WORK_QUEUE_H
34 #define IB_WORK_QUEUE_H
35 
36 #include "ut0list.h"
37 #include "mem0mem.h"
38 
39 // Forward declaration
40 struct ib_list_t;
41 
42 /** Work queue */
43 struct ib_wqueue_t
44 {
45 	/** Mutex protecting everything */
46 	ib_mutex_t	mutex;
47 	/** Work item list */
48 	ib_list_t*	items;
49 	/** ib_list_len(*items) */
50 	size_t		length;
51 	/** event we use to signal additions to list;
52 	os_event_set() and os_event_reset() are protected by the mutex */
53 	os_event_t	event;
VBaseInfoVBaseInfo54 };
55 
56 /****************************************************************//**
hasVtorDispVBaseInfo57 Create a new work queue.
58 @return work queue */
59 ib_wqueue_t*
60 ib_wqueue_create();
61 /*===============*/
62 
63 /****************************************************************//**
64 Free a work queue. */
65 void
66 ib_wqueue_free(
67 /*===========*/
68 	ib_wqueue_t*	wq);		/*!< in: work queue */
69 
70 /** Add a work item to the queue.
71 @param[in,out]	wq		work queue
72 @param[in]	item		work item
73 @param[in,out]	heap		memory heap to use for allocating list node
74 @param[in]	wq_locked	work queue mutex locked */
75 void
76 ib_wqueue_add(ib_wqueue_t* wq, void* item, mem_heap_t* heap,
77 	      bool wq_locked = false);
78 
79 /** Check if queue is empty.
80 @param wq wait queue
81 @return whether the queue is empty */
82 bool ib_wqueue_is_empty(ib_wqueue_t* wq);
83 
84 /****************************************************************//**
85 Wait for a work item to appear in the queue.
86 @return work item */
87 void*
88 ib_wqueue_wait(
89 /*===========*/
90 	ib_wqueue_t*	wq);		/*!< in: work queue */
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 	ulint		wait_in_usecs); /* in: wait time in micro seconds */
100 
101 /********************************************************************
102 Return first item on work queue or NULL if queue is empty
103 @return work item or NULL */
104 void*
105 ib_wqueue_nowait(
106 /*=============*/
107 	ib_wqueue_t*	wq);		/*<! in: work queue */
108 
109 #endif /* IB_WORK_QUEUE_H */
110