1 | /***************************************************************************** |
2 | |
3 | Copyright (c) 1995, 2015, Oracle and/or its affiliates. All Rights Reserved. |
4 | Copyright (c) 2017, 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, Suite 500, Boston, MA 02110-1335 USA |
17 | |
18 | *****************************************************************************/ |
19 | |
20 | /**************************************************//** |
21 | @file include/sync0arr.ic |
22 | The wait array for synchronization primitives |
23 | |
24 | Inline code |
25 | |
26 | Created 9/5/1995 Heikki Tuuri |
27 | *******************************************************/ |
28 | |
29 | extern ulint sync_array_size; |
30 | extern sync_array_t** sync_wait_array; |
31 | |
32 | #include "ut0counter.h" |
33 | |
34 | /**********************************************************************//** |
35 | Get an instance of the sync wait array. |
36 | @return an instance of the sync wait array. */ |
37 | |
38 | UNIV_INLINE |
39 | sync_array_t* |
40 | sync_array_get() |
41 | /*============*/ |
42 | { |
43 | if (sync_array_size <= 1) { |
44 | return(sync_wait_array[0]); |
45 | } |
46 | |
47 | return(sync_wait_array[default_indexer_t<>::get_rnd_index() |
48 | % sync_array_size]); |
49 | } |
50 | |
51 | /******************************************************************//** |
52 | Get an instance of the sync wait array and reserve a wait array cell |
53 | in the instance for waiting for an object. The event of the cell is |
54 | reset to nonsignalled state. |
55 | If reserving cell of the instance fails, try to get another new |
56 | instance until we can reserve an empty cell of it. |
57 | @return the sync array reserved, never NULL. */ |
58 | UNIV_INLINE |
59 | sync_array_t* |
60 | sync_array_get_and_reserve_cell( |
61 | /*============================*/ |
62 | void* object, /*!< in: pointer to the object to wait for */ |
63 | ulint type, /*!< in: lock request type */ |
64 | const char* file, /*!< in: file where requested */ |
65 | unsigned line, /*!< in: line where requested */ |
66 | sync_cell_t** cell) /*!< out: the cell reserved, never NULL */ |
67 | { |
68 | sync_array_t* sync_arr = NULL; |
69 | |
70 | *cell = NULL; |
71 | for (ulint i = 0; i < sync_array_size && *cell == NULL; ++i) { |
72 | /* Although the sync_array is get in a random way currently, |
73 | we still try at most sync_array_size times, in case any |
74 | of the sync_array we get is full */ |
75 | sync_arr = sync_array_get(); |
76 | *cell = sync_array_reserve_cell(sync_arr, object, type, |
77 | file, line); |
78 | } |
79 | |
80 | /* This won't be true every time, for the loop above may execute |
81 | more than srv_sync_array_size times to reserve a cell. |
82 | But an assertion here makes the code more solid. */ |
83 | ut_a(*cell != NULL); |
84 | |
85 | return(sync_arr); |
86 | } |
87 | |