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