Skip to content

Commit

Permalink
Move H5TS_rw_lock_t implementation into its own source file.
Browse files Browse the repository at this point in the history
Now that the H5TS_rw_lock_t code is fully using only H5TS package objects
and operations, and no pthread-specific code, it can be moved into its own
source file.  It still doesn't actually work with Windows threads, but the
H5TS__rw_lock_init() routine will throw an error for that case.

Signed-off-by: Quincey Koziol <[email protected]>
  • Loading branch information
qkoziol committed Mar 20, 2024
1 parent 1b48b4c commit 730e7c4
Show file tree
Hide file tree
Showing 4 changed files with 725 additions and 735 deletions.
43 changes: 14 additions & 29 deletions src/H5TSpkg.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ typedef struct H5TS_api_info_t {
unsigned attempt_lock_count;
} H5TS_api_info_t;

#ifdef H5_HAVE_WIN_THREADS

#else

/* Enable statistics when H5TS debugging is enabled */
#ifdef H5TS_DEBUG
#define H5TS_ENABLE_REC_RW_LOCK_STATS 1
Expand All @@ -90,15 +86,11 @@ typedef struct H5TS_api_info_t {
*
* Structure H5TS_rw_lock_stats_t
*
* Catchall structure for statistics on the recursive p-threads based
* recursive R/W lock (see declaration of H5TS_rw_lock_t below).
*
* Since the mutex must be held when reading a consistent set of statistics
* from the recursibe R/W lock, it simplifies matters to bundle them into
* a single structure. This structure exists for that purpose.
* Statistics for the recursive R/W lock.
*
* If you modify this structure, be sure to make equivalent changes to
* the reset_stats code in H5TS__rw_lock_reset_stats().
* Since a mutex must be held to read a consistent set of statistics from a
* recursive R/W lock, it simplifies matters to bundle them into a single
* structure.
*
* Individual fields are:
*
Expand Down Expand Up @@ -173,11 +165,7 @@ typedef struct H5TS_rw_lock_stats_t {
*
* Structure H5TS_rw_lock_t
*
* A readers / writer (R/W) lock is a lock that allows either an arbitrary
* number of readers, or a single writer into a critical region. A recursive
* lock is one that allows a thread that already holds a lock (read or
* write) to successfully request the lock again, only dropping the lock
* when the number of unlock calls equals the number of lock calls.
* A recursive readers / writer (R/W) lock.
*
* This structure holds the fields needed to implement a recursive R/W lock
* that allows recursive write locks, and for the associated statistics
Expand All @@ -186,10 +174,6 @@ typedef struct H5TS_rw_lock_stats_t {
* Note that we can't use the pthreads or Win32 R/W locks: they permit
* recursive read locks, but disallow recursive write locks.
*
* This recursive R/W lock implementation is an extension of the R/W lock
* implementation given in "UNIX network programming" Volume 2, Chapter 8
* by w. Richard Stevens, 2nd edition.
*
* Individual fields are:
*
* mutex: Mutex used to maintain mutual exclusion on the fields of
Expand All @@ -209,21 +193,24 @@ typedef struct H5TS_rw_lock_stats_t {
*
* readers_cv: Condition variable used for waiting readers.
*
* active_reader_threads: The # of threads holding a read lock.
* reader_thread_count: The # of threads holding a read lock.
*
* rec_read_lock_count_key: Instance of thread-local key used to maintain
* a thread-specific recursive lock count for each thread
* holding a read lock.
*
* is_key_registered: Flag to track if the rec_read_lock_count_key has been
* registered yet for a lock.
*
* stats: Instance of H5TS_rw_lock_stats_t used to track
* statistics on the recursive R/W lock.
*
******************************************************************************/

typedef enum {
UNUSED = 0, /* Lock is currently unused */
WRITE, /* Lock is a recursive write lock */
READ /* Lock is a recursive read lock */
H5TS_RW_LOCK_UNUSED = 0, /* Lock is currently unused */
H5TS_RW_LOCK_WRITE, /* Lock is a recursive write lock */
H5TS_RW_LOCK_READ /* Lock is a recursive read lock */
} H5TS_rw_lock_type_t;

typedef struct H5TS_rw_lock_t {
Expand All @@ -238,19 +225,17 @@ typedef struct H5TS_rw_lock_t {
int32_t waiting_writers_count;

/* Reader fields */
bool is_key_registered;
H5TS_cond_t readers_cv;
int32_t active_reader_threads;
int32_t reader_thread_count;
H5TS_key_t rec_read_lock_count_key;
bool is_key_registered;

#if H5TS_ENABLE_REC_RW_LOCK_STATS
/* Stats */
H5TS_rw_lock_stats_t stats;
#endif
} H5TS_rw_lock_t;

#endif /* H5_HAVE_WIN_THREADS */

/*****************************/
/* Package Private Variables */
/*****************************/
Expand Down
17 changes: 8 additions & 9 deletions src/H5TSprivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,16 @@ typedef pthread_once_t H5TS_once_t;
typedef void (*H5TS_once_init_func_t)(void);
#endif

/*****************************/
/* Library-private Variables */
/*****************************/
/*****************************/
/* Library-private Variables */
/*****************************/

/***************************************/
/* Library-private Function Prototypes */
/***************************************/
/***************************************/
/* Library-private Function Prototypes */
/***************************************/

/* Library/thread init/term operations */
H5_DLL void
H5TS_term_package(void);
/* Library/thread init/term operations */
H5_DLL void H5TS_term_package(void);

/* API locking */
H5_DLL herr_t H5TS_api_lock(void);
Expand Down
Loading

0 comments on commit 730e7c4

Please sign in to comment.