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