1/*****************************************************************************
2
3Copyright (c) 1995, 2015, Oracle and/or its affiliates. All Rights Reserved.
4Copyright (c) 2017, MariaDB Corporation.
5
6This program is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free Software
8Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful, but WITHOUT
11ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program; if not, write to the Free Software Foundation, Inc.,
1651 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17
18*****************************************************************************/
19
20/**************************************************//**
21@file include/sync0arr.ic
22The wait array for synchronization primitives
23
24Inline code
25
26Created 9/5/1995 Heikki Tuuri
27*******************************************************/
28
29extern ulint sync_array_size;
30extern sync_array_t** sync_wait_array;
31
32#include "ut0counter.h"
33
34/**********************************************************************//**
35Get an instance of the sync wait array.
36@return an instance of the sync wait array. */
37
38UNIV_INLINE
39sync_array_t*
40sync_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/******************************************************************//**
52Get an instance of the sync wait array and reserve a wait array cell
53in the instance for waiting for an object. The event of the cell is
54reset to nonsignalled state.
55If reserving cell of the instance fails, try to get another new
56instance until we can reserve an empty cell of it.
57@return the sync array reserved, never NULL. */
58UNIV_INLINE
59sync_array_t*
60sync_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