1 /*****************************************************************************
2 
3 Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation.  The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file include/sync0arr.h
29 The wait array used in synchronization primitives
30 
31 Created 9/5/1995 Heikki Tuuri
32 *******************************************************/
33 
34 #ifndef sync0arr_h
35 #define sync0arr_h
36 
37 #include "univ.i"
38 #include "ut0lst.h"
39 #include "ut0mem.h"
40 #include "os0thread.h"
41 
42 /** Synchronization wait array cell */
43 struct sync_cell_t;
44 /** Synchronization wait array */
45 struct sync_array_t;
46 
47 /******************************************************************//**
48 Get an instance of the sync wait array and reserve a wait array cell
49 in the instance for waiting for an object. The event of the cell is
50 reset to nonsignalled state.
51 If reserving cell of the instance fails, try to get another new
52 instance until we can reserve an empty cell of it.
53 @return the instance found, never NULL. */
54 UNIV_INLINE
55 sync_array_t*
56 sync_array_get_and_reserve_cell(
57 /*============================*/
58 	void*		object,	/*!< in: pointer to the object to wait for */
59 	ulint		type,	/*!< in: lock request type */
60 	const char*	file,	/*!< in: file where requested */
61 	ulint		line,	/*!< in: line where requested */
62 	ulint*		index);	/*!< out: index of the reserved cell */
63 /******************************************************************//**
64 Reserves a wait array cell for waiting for an object.
65 The event of the cell is reset to nonsignalled state.
66 @return true if free cell is found, otherwise false */
67 UNIV_INTERN
68 bool
69 sync_array_reserve_cell(
70 /*====================*/
71 	sync_array_t*	arr,	/*!< in: wait array */
72 	void*		object, /*!< in: pointer to the object to wait for */
73 	ulint		type,	/*!< in: lock request type */
74 	const char*	file,	/*!< in: file where requested */
75 	ulint		line,	/*!< in: line where requested */
76 	ulint*		index); /*!< out: index of the reserved cell */
77 /******************************************************************//**
78 This function should be called when a thread starts to wait on
79 a wait array cell. In the debug version this function checks
80 if the wait for a semaphore will result in a deadlock, in which
81 case prints info and asserts. */
82 UNIV_INTERN
83 void
84 sync_array_wait_event(
85 /*==================*/
86 	sync_array_t*	arr,	/*!< in: wait array */
87 	ulint		index);	 /*!< in: index of the reserved cell */
88 /******************************************************************//**
89 Frees the cell. NOTE! sync_array_wait_event frees the cell
90 automatically! */
91 UNIV_INTERN
92 void
93 sync_array_free_cell(
94 /*=================*/
95 	sync_array_t*	arr,	/*!< in: wait array */
96 	ulint		index);	/*!< in: index of the cell in array */
97 /**********************************************************************//**
98 Note that one of the wait objects was signalled. */
99 UNIV_INTERN
100 void
101 sync_array_object_signalled(void);
102 /*=============================*/
103 
104 /**********************************************************************//**
105 If the wakeup algorithm does not work perfectly at semaphore relases,
106 this function will do the waking (see the comment in mutex_exit). This
107 function should be called about every 1 second in the server. */
108 UNIV_INTERN
109 void
110 sync_arr_wake_threads_if_sema_free(void);
111 /*====================================*/
112 /**********************************************************************//**
113 Prints warnings of long semaphore waits to stderr.
114 @return	TRUE if fatal semaphore wait threshold was exceeded */
115 UNIV_INTERN
116 ibool
117 sync_array_print_long_waits(
118 /*========================*/
119 	os_thread_id_t*	waiter,	/*!< out: longest waiting thread */
120 	const void**	sema)	/*!< out: longest-waited-for semaphore */
121 	MY_ATTRIBUTE((nonnull));
122 /********************************************************************//**
123 Validates the integrity of the wait array. Checks
124 that the number of reserved cells equals the count variable. */
125 UNIV_INTERN
126 void
127 sync_array_validate(
128 /*================*/
129 	sync_array_t*	arr);	/*!< in: sync wait array */
130 /**********************************************************************//**
131 Prints info of the wait array. */
132 UNIV_INTERN
133 void
134 sync_array_print(
135 /*=============*/
136 	FILE*		file);	/*!< in: file where to print */
137 
138 /**********************************************************************//**
139 Create the primary system wait array(s), they are protected by an OS mutex */
140 UNIV_INTERN
141 void
142 sync_array_init(
143 /*============*/
144 	ulint		n_threads);	/*!< in: Number of slots to create */
145 /**********************************************************************//**
146 Close sync array wait sub-system. */
147 UNIV_INTERN
148 void
149 sync_array_close(void);
150 /*==================*/
151 
152 /**********************************************************************//**
153 Get an instance of the sync wait array. */
154 UNIV_INTERN
155 sync_array_t*
156 sync_array_get(void);
157 /*================*/
158 
159 #ifndef UNIV_NONINL
160 #include "sync0arr.ic"
161 #endif
162 
163 #endif
164