1/*****************************************************************************
2
3Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
4Copyright (c) 2015, 2018, 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.h
22The wait array used in synchronization primitives
23
24Created 9/5/1995 Heikki Tuuri
25*******************************************************/
26
27#ifndef sync0arr_h
28#define sync0arr_h
29
30#include "univ.i"
31#include "os0thread.h"
32
33/** Synchronization wait array cell */
34struct sync_cell_t;
35
36/** Synchronization wait array */
37struct sync_array_t;
38
39/******************************************************************//**
40Get an instance of the sync wait array and reserve a wait array cell
41in the instance for waiting for an object. The event of the cell is
42reset to nonsignalled state.
43If reserving cell of the instance fails, try to get another new
44instance until we can reserve an empty cell of it.
45@return the sync array found, never NULL. */
46UNIV_INLINE
47sync_array_t*
48sync_array_get_and_reserve_cell(
49 void* object, /*!< in: pointer to the object to wait for */
50 ulint type, /*!< in: lock request type */
51 const char* file, /*!< in: file where requested */
52 unsigned line, /*!< in: line where requested */
53 sync_cell_t** cell); /*!< out: the cell reserved, never NULL */
54/******************************************************************//**
55Reserves a wait array cell for waiting for an object.
56The event of the cell is reset to nonsignalled state. */
57sync_cell_t*
58sync_array_reserve_cell(
59 sync_array_t* arr, /*!< in: wait array */
60 void* object, /*!< in: pointer to the object to wait for */
61 ulint type, /*!< in: lock request type */
62 const char* file, /*!< in: file where requested */
63 unsigned line); /*!< in: line where requested */
64
65/******************************************************************//**
66This function should be called when a thread starts to wait on
67a wait array cell. In the debug version this function checks
68if the wait for a semaphore will result in a deadlock, in which
69case prints info and asserts. */
70void
71sync_array_wait_event(
72 sync_array_t* arr, /*!< in: wait array */
73 sync_cell_t*& cell); /*!< in: the reserved cell */
74
75/******************************************************************//**
76Frees the cell. NOTE! sync_array_wait_event frees the cell
77automatically! */
78void
79sync_array_free_cell(
80 sync_array_t* arr, /*!< in: wait array */
81 sync_cell_t*& cell); /*!< in: the reserved cell */
82
83/**********************************************************************//**
84Note that one of the wait objects was signalled. */
85void
86sync_array_object_signalled();
87
88/**********************************************************************//**
89Prints warnings of long semaphore waits to stderr.
90@return TRUE if fatal semaphore wait threshold was exceeded */
91ibool
92sync_array_print_long_waits(
93 os_thread_id_t* waiter, /*!< out: longest waiting thread */
94 const void** sema); /*!< out: longest-waited-for semaphore */
95
96/**********************************************************************//**
97Prints info of the wait array. */
98void
99sync_array_print(
100 FILE* file); /*!< in: file where to print */
101
102/** Create the primary system wait arrays */
103void sync_array_init();
104
105/** Destroy the sync array wait sub-system. */
106void sync_array_close();
107
108/**********************************************************************//**
109Get an instance of the sync wait array. */
110UNIV_INLINE
111sync_array_t*
112sync_array_get();
113/**********************************************************************//**
114Prints info of the wait array without using any mutexes/semaphores. */
115UNIV_INTERN
116void
117sync_array_print_innodb(void);
118
119/*****************************************************************//**
120Gets the nth cell in array.
121@return cell */
122UNIV_INTERN
123sync_cell_t*
124sync_array_get_nth_cell(
125/*====================*/
126 sync_array_t* arr, /*!< in: sync array */
127 ulint n); /*!< in: index */
128
129#include "sync0arr.ic"
130
131#endif /* sync0arr_h */
132