| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved. |
| 4 | Copyright (c) 2015, 2018, 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, Suite 500, 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 | #include "os0thread.h" |
| 32 | |
| 33 | /** Synchronization wait array cell */ |
| 34 | struct sync_cell_t; |
| 35 | |
| 36 | /** Synchronization wait array */ |
| 37 | struct sync_array_t; |
| 38 | |
| 39 | /******************************************************************//** |
| 40 | Get an instance of the sync wait array and reserve a wait array cell |
| 41 | in the instance for waiting for an object. The event of the cell is |
| 42 | reset to nonsignalled state. |
| 43 | If reserving cell of the instance fails, try to get another new |
| 44 | instance until we can reserve an empty cell of it. |
| 45 | @return the sync array found, never NULL. */ |
| 46 | UNIV_INLINE |
| 47 | sync_array_t* |
| 48 | sync_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 | /******************************************************************//** |
| 55 | Reserves a wait array cell for waiting for an object. |
| 56 | The event of the cell is reset to nonsignalled state. */ |
| 57 | sync_cell_t* |
| 58 | sync_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 | /******************************************************************//** |
| 66 | This function should be called when a thread starts to wait on |
| 67 | a wait array cell. In the debug version this function checks |
| 68 | if the wait for a semaphore will result in a deadlock, in which |
| 69 | case prints info and asserts. */ |
| 70 | void |
| 71 | sync_array_wait_event( |
| 72 | sync_array_t* arr, /*!< in: wait array */ |
| 73 | sync_cell_t*& cell); /*!< in: the reserved cell */ |
| 74 | |
| 75 | /******************************************************************//** |
| 76 | Frees the cell. NOTE! sync_array_wait_event frees the cell |
| 77 | automatically! */ |
| 78 | void |
| 79 | sync_array_free_cell( |
| 80 | sync_array_t* arr, /*!< in: wait array */ |
| 81 | sync_cell_t*& cell); /*!< in: the reserved cell */ |
| 82 | |
| 83 | /**********************************************************************//** |
| 84 | Note that one of the wait objects was signalled. */ |
| 85 | void |
| 86 | sync_array_object_signalled(); |
| 87 | |
| 88 | /**********************************************************************//** |
| 89 | Prints warnings of long semaphore waits to stderr. |
| 90 | @return TRUE if fatal semaphore wait threshold was exceeded */ |
| 91 | ibool |
| 92 | sync_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 | /**********************************************************************//** |
| 97 | Prints info of the wait array. */ |
| 98 | void |
| 99 | sync_array_print( |
| 100 | FILE* file); /*!< in: file where to print */ |
| 101 | |
| 102 | /** Create the primary system wait arrays */ |
| 103 | void sync_array_init(); |
| 104 | |
| 105 | /** Destroy the sync array wait sub-system. */ |
| 106 | void sync_array_close(); |
| 107 | |
| 108 | /**********************************************************************//** |
| 109 | Get an instance of the sync wait array. */ |
| 110 | UNIV_INLINE |
| 111 | sync_array_t* |
| 112 | sync_array_get(); |
| 113 | /**********************************************************************//** |
| 114 | Prints info of the wait array without using any mutexes/semaphores. */ |
| 115 | UNIV_INTERN |
| 116 | void |
| 117 | sync_array_print_innodb(void); |
| 118 | |
| 119 | /*****************************************************************//** |
| 120 | Gets the nth cell in array. |
| 121 | @return cell */ |
| 122 | UNIV_INTERN |
| 123 | sync_cell_t* |
| 124 | sync_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 | |