MySQL  8.0.19
Source Code Documentation
sync0arr.cc File Reference
#include "sync0arr.h"
#include <sys/types.h>
#include <time.h>
#include "lock0lock.h"
#include "os0event.h"
#include "os0file.h"
#include "srv0srv.h"
#include "sync0debug.h"
#include "sync0rw.h"
#include "sync0sync.h"

Classes

union  sync_object_t
 The latch types that use the sync array. More...
 
struct  sync_cell_t
 A cell where an individual thread may wait suspended until a resource is released. More...
 
struct  sync_array_t
 Synchronization array. More...
 

Macros

#define sync_array_exit(a)   mutex_exit(&(a)->mutex)
 
#define sync_array_enter(a)   mutex_enter(&(a)->mutex)
 
#define SYNC_ARRAY_TIMEOUT   240
 

Typedefs

typedef SyncArrayMutex::MutexType WaitMutex
 
typedef BlockSyncArrayMutex::MutexType BlockWaitMutex
 

Functions

static bool sync_array_detect_deadlock (sync_array_t *arr, sync_cell_t *start, sync_cell_t *cell, ulint depth)
 This function is called only in the debug version. More...
 
static void sync_array_validate (sync_array_t *arr)
 Validates the integrity of the wait array. More...
 
static sync_cell_tsync_array_get_nth_cell (sync_array_t *arr, ulint n)
 Gets the nth cell in array. More...
 
static void sync_array_free (sync_array_t *arr)
 Frees the resources in a wait array. More...
 
static os_event_t sync_cell_get_event (sync_cell_t *cell)
 Returns the event that the thread owning the cell waits for. More...
 
sync_cell_tsync_array_reserve_cell (sync_array_t *arr, void *object, ulint type, const char *file, ulint line)
 Reserves a wait array cell for waiting for an object. More...
 
void sync_array_free_cell (sync_array_t *arr, sync_cell_t *&cell)
 Frees the cell. More...
 
void sync_array_wait_event (sync_array_t *arr, sync_cell_t *&cell)
 This function should be called when a thread starts to wait on a wait array cell. More...
 
static void sync_array_cell_print (FILE *file, sync_cell_t *cell)
 Reports info of a wait array cell. More...
 
static sync_cell_tsync_array_find_thread (sync_array_t *arr, os_thread_id_t thread)
 Looks for a cell with the given thread id. More...
 
static ibool sync_array_deadlock_step (sync_array_t *arr, sync_cell_t *start, os_thread_id_t thread, ulint pass, ulint depth)
 Recursion step for deadlock detection. More...
 
static void sync_array_report_error (rw_lock_t *lock, rw_lock_debug_t *debug, sync_cell_t *cell)
 Report an error to stderr. More...
 
static bool sync_arr_cell_can_wake_up (sync_cell_t *cell)
 Determines if we can wake up the thread waiting for a sempahore. More...
 
void sync_array_object_signalled ()
 Increments the signalled count. More...
 
static void sync_array_wake_threads_if_sema_free_low (sync_array_t *arr)
 If the wakeup algorithm does not work perfectly at semaphore relases, this function will do the waking (see the comment in mutex_exit). More...
 
void sync_arr_wake_threads_if_sema_free (void)
 If the wakeup algorithm does not work perfectly at semaphore relases, this function will do the waking (see the comment in mutex_exit). More...
 
static bool sync_array_print_long_waits_low (sync_array_t *arr, os_thread_id_t *waiter, const void **sema, ibool *noticed)
 Prints warnings of long semaphore waits to stderr. More...
 
ibool sync_array_print_long_waits (os_thread_id_t *waiter, const void **sema)
 Prints warnings of long semaphore waits to stderr. More...
 
static void sync_array_print_info_low (FILE *file, sync_array_t *arr)
 Prints info of the wait array. More...
 
static void sync_array_print_info (FILE *file, sync_array_t *arr)
 Prints info of the wait array. More...
 
void sync_array_init (ulint n_threads)
 Create the primary system wait array(s), they are protected by an OS mutex. More...
 
void sync_array_close (void)
 Close sync array wait sub-system. More...
 
void sync_array_print (FILE *file)
 Print info about the sync array(s). More...
 

Variables

ulong srv_sync_array_size = 1
 User configured sync array size. More...
 
ulint sync_array_size
 Locally stored copy of srv_sync_array_size. More...
 
sync_array_t ** sync_wait_array
 The global array of wait cells for implementation of the database's own mutexes and read-write locks. More...
 
static ulint sg_count
 count of how many times an object has been signalled More...
 

Detailed Description

The wait array used in synchronization primitives

Created 9/5/1995 Heikki Tuuri

Macro Definition Documentation

◆ sync_array_enter

#define sync_array_enter (   a)    mutex_enter(&(a)->mutex)

◆ sync_array_exit

#define sync_array_exit (   a)    mutex_exit(&(a)->mutex)

◆ SYNC_ARRAY_TIMEOUT

#define SYNC_ARRAY_TIMEOUT   240

Typedef Documentation

◆ BlockWaitMutex

typedef BlockSyncArrayMutex::MutexType BlockWaitMutex

◆ WaitMutex

typedef SyncArrayMutex::MutexType WaitMutex

Function Documentation

◆ sync_arr_cell_can_wake_up()

static bool sync_arr_cell_can_wake_up ( sync_cell_t cell)
static

Determines if we can wake up the thread waiting for a sempahore.

Parameters
cellin: cell to search

◆ sync_arr_wake_threads_if_sema_free()

void sync_arr_wake_threads_if_sema_free ( void  )

If the wakeup algorithm does not work perfectly at semaphore relases, this function will do the waking (see the comment in mutex_exit).

This function should be called about every 1 second in the server.

Note that there's a race condition between this thread and mutex_exit changing the lock_word and calling signal_object, so sometimes this finds threads to wake up even when nothing has gone wrong.

◆ sync_array_cell_print()

static void sync_array_cell_print ( FILE *  file,
sync_cell_t cell 
)
static

Reports info of a wait array cell.

Parameters
filein: file where to print
cellin: sync cell

◆ sync_array_close()

void sync_array_close ( void  )

Close sync array wait sub-system.

◆ sync_array_deadlock_step()

static ibool sync_array_deadlock_step ( sync_array_t arr,
sync_cell_t start,
os_thread_id_t  thread,
ulint  pass,
ulint  depth 
)
static

Recursion step for deadlock detection.

Returns
true if deadlock detected
Parameters
arrin: wait array; NOTE! the caller must own the mutex to array
startin: cell where recursive search started
threadin: thread to look at
passin: pass value
depthin: recursion depth

◆ sync_array_detect_deadlock()

static bool sync_array_detect_deadlock ( sync_array_t arr,
sync_cell_t start,
sync_cell_t cell,
ulint  depth 
)
static

This function is called only in the debug version.

Detects a deadlock of one or more threads because of waits of semaphores.

Returns
true if deadlock detected in: recursion depth

Detects a deadlock of one or more threads because of waits of semaphores.

Returns
true if deadlock detected
Parameters
arrin: wait array; NOTE! the caller must own the mutex to array
startin: cell where recursive search started
cellin: cell to search
depthin: recursion depth

◆ sync_array_find_thread()

static sync_cell_t* sync_array_find_thread ( sync_array_t arr,
os_thread_id_t  thread 
)
static

Looks for a cell with the given thread id.

Returns
pointer to cell or NULL if not found
Parameters
arrin: wait array
threadin: thread id

◆ sync_array_free()

static void sync_array_free ( sync_array_t arr)
static

Frees the resources in a wait array.

Parameters
arrin, own: sync wait array

◆ sync_array_free_cell()

void sync_array_free_cell ( sync_array_t arr,
sync_cell_t *&  cell 
)

Frees the cell.

NOTE! sync_array_wait_event frees the cell automatically!

Parameters
arrin: wait array
cellin/out: the cell in the array

◆ sync_array_get_nth_cell()

static sync_cell_t* sync_array_get_nth_cell ( sync_array_t arr,
ulint  n 
)
static

Gets the nth cell in array.

Returns
cell
Parameters
arrin: sync array
nin: index

◆ sync_array_init()

void sync_array_init ( ulint  n_threads)

Create the primary system wait array(s), they are protected by an OS mutex.

in: Number of slots to create

Parameters
n_threadsin: Number of slots to create in all arrays

◆ sync_array_object_signalled()

void sync_array_object_signalled ( )

Increments the signalled count.

Note that one of the wait objects was signalled.

◆ sync_array_print()

void sync_array_print ( FILE *  file)

Print info about the sync array(s).

Prints info of the wait array.

Parameters
filein/out: Print to this stream

◆ sync_array_print_info()

static void sync_array_print_info ( FILE *  file,
sync_array_t arr 
)
static

Prints info of the wait array.

Parameters
filein: file where to print
arrin: wait array

◆ sync_array_print_info_low()

static void sync_array_print_info_low ( FILE *  file,
sync_array_t arr 
)
static

Prints info of the wait array.

Parameters
filein: file where to print
arrin: wait array

◆ sync_array_print_long_waits()

ibool sync_array_print_long_waits ( os_thread_id_t waiter,
const void **  sema 
)

Prints warnings of long semaphore waits to stderr.

Returns
true if fatal semaphore wait threshold was exceeded
Parameters
waiterout: longest waiting thread
semaout: longest-waited-for semaphore

◆ sync_array_print_long_waits_low()

static bool sync_array_print_long_waits_low ( sync_array_t arr,
os_thread_id_t waiter,
const void **  sema,
ibool *  noticed 
)
static

Prints warnings of long semaphore waits to stderr.

Returns
true if fatal semaphore wait threshold was exceeded
Parameters
arrin: sync array instance
waiterout: longest waiting thread
semaout: longest-waited-for semaphore
noticedout: TRUE if long wait noticed

◆ sync_array_report_error()

static void sync_array_report_error ( rw_lock_t lock,
rw_lock_debug_t debug,
sync_cell_t cell 
)
static

Report an error to stderr.

Parameters
lockrw-lock instance
debugrw-lock debug information
cellthread context

◆ sync_array_reserve_cell()

sync_cell_t* sync_array_reserve_cell ( sync_array_t arr,
void *  object,
ulint  type,
const char *  file,
ulint  line 
)

Reserves a wait array cell for waiting for an object.

The event of the cell is reset to nonsignalled state.

Returns
sync cell to wait on
Parameters
arrin: wait array
objectin: pointer to the object to wait for
typein: lock request type
filein: file where requested
linein: line where requested

◆ sync_array_validate()

static void sync_array_validate ( sync_array_t arr)
static

Validates the integrity of the wait array.

Checks that the number of reserved cells equals the count variable.

Parameters
arrin: sync wait array

◆ sync_array_wait_event()

void sync_array_wait_event ( sync_array_t arr,
sync_cell_t *&  cell 
)

This function should be called when a thread starts to wait on a wait array cell.

In the debug version this function checks if the wait for a semaphore will result in a deadlock, in which case prints info and asserts.

Parameters
arrin: wait array
cellin: index of the reserved cell

◆ sync_array_wake_threads_if_sema_free_low()

static void sync_array_wake_threads_if_sema_free_low ( sync_array_t arr)
static

If the wakeup algorithm does not work perfectly at semaphore relases, this function will do the waking (see the comment in mutex_exit).

This function should be called about every 1 second in the server.

Note that there's a race condition between this thread and mutex_exit changing the lock_word and calling signal_object, so sometimes this finds threads to wake up even when nothing has gone wrong.

◆ sync_cell_get_event()

static os_event_t sync_cell_get_event ( sync_cell_t cell)
static

Returns the event that the thread owning the cell waits for.

Parameters
cellin: non-empty sync array cell

Variable Documentation

◆ sg_count

ulint sg_count
static

count of how many times an object has been signalled

◆ srv_sync_array_size

ulong srv_sync_array_size = 1

User configured sync array size.

◆ sync_array_size

ulint sync_array_size

Locally stored copy of srv_sync_array_size.

◆ sync_wait_array

sync_array_t** sync_wait_array

The global array of wait cells for implementation of the database's own mutexes and read-write locks.