1/*****************************************************************************
2
3Copyright (c) 1995, 2019, Oracle and/or its affiliates. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is also distributed with certain software (including but not
10limited to OpenSSL) that is licensed under separate terms, as designated in a
11particular file or component or in included license documentation. The authors
12of MySQL hereby grant you an additional permission to link the program and
13your derivative works with the separately licensed software that they have
14included with MySQL.
15
16This program is distributed in the hope that it will be useful, but WITHOUT
17ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24
25*****************************************************************************/
26
27/** @file include/sync0arr.ic
28 The wait array for synchronization primitives
29
30 Inline code
31
32 Created 9/5/1995 Heikki Tuuri
33 *******************************************************/
34
35extern ulint sync_array_size;
36extern sync_array_t **sync_wait_array;
37
38#include "ut0counter.h"
39
40/** Get an instance of the sync wait array.
41 @return an instance of the sync wait array. */
42
43UNIV_INLINE
44sync_array_t *sync_array_get() {
45  if (sync_array_size <= 1) {
46    return (sync_wait_array[0]);
47  }
48
49  return (
50      sync_wait_array[default_indexer_t<>::get_rnd_index() % sync_array_size]);
51}
52
53/** Get an instance of the sync wait array and reserve a wait array cell
54 in the instance for waiting for an object. The event of the cell is
55 reset to nonsignalled state.
56 If reserving cell of the instance fails, try to get another new
57 instance until we can reserve an empty cell of it.
58 @return the sync array reserved, never NULL. */
59UNIV_INLINE
60sync_array_t *sync_array_get_and_reserve_cell(
61    void *object,       /*!< in: pointer to the object to wait for */
62    ulint type,         /*!< in: lock request type */
63    const char *file,   /*!< in: file where requested */
64    ulint line,         /*!< in: line where requested */
65    sync_cell_t **cell) /*!< out: the cell reserved, never NULL */
66{
67  sync_array_t *sync_arr = nullptr;
68
69  *cell = nullptr;
70  for (ulint i = 0; i < sync_array_size && *cell == nullptr; ++i) {
71    /* Although the sync_array is get in a random way currently,
72    we still try at most sync_array_size times, in case any
73    of the sync_array we get is full */
74    sync_arr = sync_array_get();
75    *cell = sync_array_reserve_cell(sync_arr, object, type, file, line);
76  }
77
78  /* This won't be true every time, for the loop above may execute
79  more than srv_sync_array_size times to reserve a cell.
80  But an assertion here makes the code more solid. */
81  ut_a(*cell != nullptr);
82
83  return (sync_arr);
84}
85