| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2006, 2026, Oracle and/or its affiliates. |
| 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, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
#include "ut0wqueue.h" |
| 29 |
|
| 30 |
#include <stddef.h> |
| 31 |
#include <sys/types.h> |
| 32 |
|
| 33 |
#include "mem0mem.h" |
| 34 |
#include "ut0list.h" |
| 35 |
|
| 36 |
/** @file ut/ut0wqueue.cc |
| 37 |
A work queue |
| 38 |
|
| 39 |
Created 4/26/2006 Osku Salerma |
| 40 |
************************************************************************/ |
| 41 |
|
| 42 |
/* Work queue. */ |
| 43 |
struct ib_wqueue_t { |
| 44 |
ib_mutex_t mutex; /*!< mutex protecting everything */ |
| 45 |
ib_list_t *items; /*!< work item list */ |
| 46 |
uint64_t count; /*!< total number of work items */ |
| 47 |
os_event_t event; /*!< event we use to signal additions to list */ |
| 48 |
}; |
| 49 |
|
| 50 |
/** Create a new work queue. |
| 51 |
@return work queue */ |
| 52 |
ib_wqueue_t *ib_wqueue_create(void) { |
| 53 |
ib_wqueue_t *wq = static_cast<ib_wqueue_t *>( |
| 54 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*wq))); |
| 55 |
|
| 56 |
/* Function ib_wqueue_create() has not been used anywhere, |
| 57 |
not necessary to instrument this mutex */ |
| 58 |
|
| 59 |
mutex_create(LATCH_ID_WORK_QUEUE, &wq->mutex); |
| 60 |
|
| 61 |
wq->items = ib_list_create(); |
| 62 |
wq->event = os_event_create(); |
| 63 |
wq->count = 0; |
| 64 |
|
| 65 |
return (wq); |
| 66 |
} |
| 67 |
|
| 68 |
/** Free a work queue. */ |
| 69 |
void ib_wqueue_free(ib_wqueue_t *wq) /*!< in: work queue */ |
| 70 |
{ |
| 71 |
mutex_free(&wq->mutex); |
| 72 |
ib_list_free(wq->items); |
| 73 |
os_event_destroy(wq->event); |
| 74 |
|
| 75 |
ut::free(wq); |
| 76 |
} |
| 77 |
|
| 78 |
/** Add a work item to the queue. |
| 79 |
@param[in] wq Work queue |
| 80 |
@param[in] item Work item |
| 81 |
@param[in] heap Memory heap to use for allocating the list node */ |
| 82 |
void ib_wqueue_add(ib_wqueue_t *wq, void *item, mem_heap_t *heap) { |
| 83 |
mutex_enter(&wq->mutex); |
| 84 |
|
| 85 |
ib_list_add_last(wq->items, item, heap); |
| 86 |
wq->count++; |
| 87 |
os_event_set(wq->event); |
| 88 |
|
| 89 |
mutex_exit(&wq->mutex); |
| 90 |
} |
| 91 |
|
| 92 |
uint64_t ib_wqueue_get_count(ib_wqueue_t *wq) { |
| 93 |
uint64_t count; |
| 94 |
mutex_enter(&wq->mutex); |
| 95 |
count = wq->count; |
| 96 |
mutex_exit(&wq->mutex); |
| 97 |
return count; |
| 98 |
} |
| 99 |
|
| 100 |
/******************************************************************** |
| 101 |
Wait for a work item to appear in the queue for specified time. */ |
| 102 |
void *ib_wqueue_timedwait( |
| 103 |
/* out: work item or NULL on timeout*/ |
| 104 |
ib_wqueue_t *wq, /* in: work queue */ |
| 105 |
std::chrono::microseconds wait) /* in: wait time */ |
| 106 |
{ |
| 107 |
ib_list_node_t *node = nullptr; |
| 108 |
|
| 109 |
for (;;) { |
| 110 |
ulint error; |
| 111 |
int64_t sig_count; |
| 112 |
|
| 113 |
mutex_enter(&wq->mutex); |
| 114 |
|
| 115 |
node = ib_list_get_first(wq->items); |
| 116 |
|
| 117 |
if (node) { |
| 118 |
ib_list_remove(wq->items, node); |
| 119 |
wq->count--; |
| 120 |
|
| 121 |
mutex_exit(&wq->mutex); |
| 122 |
break; |
| 123 |
} |
| 124 |
|
| 125 |
sig_count = os_event_reset(wq->event); |
| 126 |
|
| 127 |
mutex_exit(&wq->mutex); |
| 128 |
|
| 129 |
error = os_event_wait_time_low(wq->event, wait, sig_count); |
| 130 |
|
| 131 |
if (error == OS_SYNC_TIME_EXCEEDED) { |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return (node ? node->data : nullptr); |
| 137 |
} |
| 138 |
|
| 139 |
/******************************************************************** |
| 140 |
Check if queue is empty. */ |
| 141 |
bool ib_wqueue_is_empty( |
| 142 |
/* out: true if queue empty |
| 143 |
else false */ |
| 144 |
const ib_wqueue_t *wq) /* in: work queue */ |
| 145 |
{ |
| 146 |
return (ib_list_is_empty(wq->items)); |
| 147 |
} |