<commit_before>/******************************************************
The wait array used in synchronization primitives

(c) 1995 <NAME>

Created 9/5/1995 <NAME>
*******************************************************/

#include "sync0arr.h"
#ifdef UNIV_NONINL
#include "sync0arr.ic"
#endif

#include "sync0sync.h"
#include "sync0rw.h"
#include "os0sync.h"
#include "os0file.h"
#include "srv0srv.h"

/*
			WAIT ARRAY
			==========

The wait array consists of cells each of which has an
an operating system event object created for it. The threads
waiting for a mutex, for example, can reserve a cell
in the array and suspend themselves to wait for the event
to become signaled. When using the wait array, remember to make
sure that some thread holding the synchronization object
will eventually know that there is a waiter in the array and
signal the object, to prevent infinite wait.
Why we chose to implement a wait array? First, to make
mutexes fast, we had to code our own implementation of them,
which only in usually uncommon cases resorts to using
slow operating system primitives. Then we had the choice of
assigning a unique OS event for each mutex, which would
be simpler, or using a global wait array. In some operating systems,
the global wait array solution is more efficient and flexible,
because we can do with a very small number of OS events,
say 200. In NT 3.51, allocating events seems to be a quadratic
algorithm, because 10 000 events are created fast, but
100 000 events takes a couple of minutes to create.
*/

/* A cell where an individual thread may wait suspended
until a resource is released. The suspending is implemented
using an operating system event semaphore. */
struct sync_cell_struct {
        void*           wait_object;    /* pointer to the object the
                                        thread is waiting for; if NULL
                                        the cell is free for use */
	mutex_t*	old_wait_mutex;	/* the latest wait mutex in cell */
	rw_lock_t*	old_wait_rw_lock;/* the latest wait rw-lock in cell */
        ulint		request_type;	/* lock type requested on the
        				object */
	char*		file;		/* in debug version file where
					requested */
	ulint		line;		/* in debug version line where
					requested */
	os_thread_id_t	thread;		/* thread id of this waiting
					thread */
	ibool		waiting;	/* TRUE if the thread has already
					called sync_array_event_wait
					on this cell but not yet
					sync_array_free_cell (which
					actually resets wait_object and thus
					whole cell) */
	ibool		event_set;	/* TRUE if the event is set */
        os_event_t 	event;   	/* operating system event
                                        semaphore handle */
	time_t		reservation_time;/* time when the thread reserved
					the wait cell */
};

/* NOTE: It is allowed for a thread to wait
for an event allocated for the array without owning the
protecting mutex (depending on the case: OS or database mutex), but
all changes (set or reset) to the state of the event must be made
while owning the mutex. */
struct sync_array_struct {
	ulint		n_reserved;	/* number of currently reserved<commit_msg>sync0arr.c:   Correct the comment on the 'waiting' field in sync_cell_struct<commit_after>/******************************************************
The wait array used in synchronization primitives

(c) 1995 <NAME>

Created 9/5/1995 <NAME>
*******************************************************/

#include "sync0arr.h"
#ifdef UNIV_NONINL
#include "sync0arr.ic"
#endif

#include "sync0sync.h"
#include "sync0rw.h"
#include "os0sync.h"
#include "os0file.h"
#include "srv0srv.h"

/*
			WAIT ARRAY
			==========

The wait array consists of cells each of which has an
an operating system event object created for it. The threads
waiting for a mutex, for example, can reserve a cell
in the array and suspend themselves to wait for the event
to become signaled. When using the wait array, remember to make
sure that some thread holding the synchronization object
will eventually know that there is a waiter in the array and
signal the object, to prevent infinite wait.
Why we chose to implement a wait array? First, to make
mutexes fast, we had to code our own implementation of them,
which only in usually uncommon cases resorts to using
slow operating system primitives. Then we had the choice of
assigning a unique OS event for each mutex, which would
be simpler, or using a global wait array. In some operating systems,
the global wait array solution is more efficient and flexible,
because we can do with a very small number of OS events,
say 200. In NT 3.51, allocating events seems to be a quadratic
algorithm, because 10 000 events are created fast, but
100 000 events takes a couple of minutes to create.
*/

/* A cell where an individual thread may wait suspended
until a resource is released. The suspending is implemented
using an operating system event semaphore. */
struct sync_cell_struct {
        void*           wait_object;    /* pointer to the object the
                                        thread is waiting for; if NULL
                                        the cell is free for use */
	mutex_t*	old_wait_mutex;	/* the latest wait mutex in cell */
	rw_lock_t*	old_wait_rw_lock;/* the latest wait rw-lock in cell */
        ulint		request_type;	/* lock type requested on the
        				object */
	char*		file;		/* in debug version file where
					requested */
	ulint		line;		/* in debug version line where
					requested */
	os_thread_id_t	thread;		/* thread id of this waiting
					thread */
	ibool		waiting;	/* TRUE if the thread has already
					called sync_array_event_wait
					on this cell */
	ibool		event_set;	/* TRUE if the event is set */
        os_event_t 	event;   	/* operating system event
                                        semaphore handle */
	time_t		reservation_time;/* time when the thread reserved
					the wait cell */
};

/* NOTE: It is allowed for a thread to wait
for an event allocated for the array without owning the
protecting mutex (depending on the case: OS or database mutex), but
all changes (set or reset) to the state of the event must be made
while owning the mutex. */
struct sync_array_struct {
	ulint		n_reserved;	/* number of currently reserved
{'fraction_non_alphanumeric': 0.08915162166480269, 'fraction_numerical': 0.011024125259626137, 'mean_word_length': 3.1456953642384105, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5414917', 'n_tokens_mistral': 1807, 'n_tokens_neox': 1651, 'n_words': 852}