Skip to content

Commit

Permalink
Add a 'destroy' op for atomic values.
Browse files Browse the repository at this point in the history
Not needed for C11 atomics, but we need a chance to destroy the mutex.

Signed-off-by: Quincey Koziol <[email protected]>
  • Loading branch information
qkoziol committed Mar 27, 2024
1 parent cef9bae commit 0496d35
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/H5FDsubfiling/H5FDioc_threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ finalize_ioc_threads(void *_sf_context)

/* Wait for IOC main thread to exit */
H5TS_thread_join(ioc_data->ioc_main_thread, NULL);

/* Destroy atomic vars */
H5TS_atomic_destroy_int(&ioc_data->sf_ioc_ready);
H5TS_atomic_destroy_int(&ioc_data->sf_shutdown_flag);
H5TS_atomic_destroy_int(&ioc_data->sf_io_ops_pending);
H5TS_atomic_destroy_int(&ioc_data->sf_work_pending);
}

if (ioc_data->io_queue.num_failed > 0)
Expand Down
23 changes: 23 additions & 0 deletions src/H5TSatomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,29 @@ H5TS_atomic_fetch_sub_int(H5TS_atomic_int_t *obj, int arg)
FUNC_LEAVE_NOAPI_NAMECHECK_ONLY(ret_value)
} /* end H5TS_atomic_fetch_sub_int() */

/*--------------------------------------------------------------------------
* Function: H5TS_atomic_destroy_int
*
* Purpose: Destroys / releases resources for an atomic variable
*
* Note: No equivalent in the C11 atomics, but needed here, to destroy
* the mutex used to protect the atomic value.
*
* Return: None
*
*--------------------------------------------------------------------------
*/
void
H5TS_atomic_destroy_int(H5TS_atomic_int_t *obj)
{
FUNC_ENTER_NOAPI_NAMECHECK_ONLY

/* Destroy mutex that protects the "atomic" value */
(void) H5TS_mutex_destroy(&obj->mutex);

FUNC_LEAVE_NOAPI_VOID_NAMECHECK_ONLY
} /* end H5TS_atomic_destroy_int() */

#endif /* H5_HAVE_THREADS_H */

#endif /* H5_HAVE_THREADSAFE */
4 changes: 3 additions & 1 deletion src/H5TSprivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
#define H5TS_atomic_store_int(obj, desired) atomic_store((obj), (desired))
#define H5TS_atomic_fetch_add_int(obj, arg) atomic_fetch_add((obj), (arg))
#define H5TS_atomic_fetch_sub_int(obj, arg) atomic_fetch_sub((obj), (arg))
#define H5TS_atomic_destroy_int(obj) /* void */
#endif /* H5_HAVE_STDATOMIC_H */

/****************************/
Expand Down Expand Up @@ -124,7 +125,7 @@ typedef void (*H5TS_once_init_func_t)(void);
#ifdef H5_HAVE_STDATOMIC_H
typedef atomic_int H5TS_atomic_int_t;
#else
typedef struct {
typedef struct {
H5TS_mutex_t mutex;
int value;
} H5TS_atomic_int_t;
Expand Down Expand Up @@ -191,6 +192,7 @@ H5_DLL int H5TS_atomic_load_int(H5TS_atomic_int_t *obj);
H5_DLL void H5TS_atomic_store_int(H5TS_atomic_int_t *obj, int desired);
H5_DLL int H5TS_atomic_fetch_add_int(H5TS_atomic_int_t *obj, int arg);
H5_DLL int H5TS_atomic_fetch_sub_int(H5TS_atomic_int_t *obj, int arg);
H5_DLL void H5TS_atomic_destroy_int(H5TS_atomic_int_t *obj);
#endif /* H5_HAVE_STDATOMIC_H */

#else /* H5_HAVE_THREADSAFE */
Expand Down
3 changes: 3 additions & 0 deletions test/ttsafe_atomic.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ tts_atomics(void)

VERIFY(H5TS_atomic_load_int(&counter_g), 5 + (1000 * 1000), "2,000,000 incr + 1,000,000 decr");

/* Destroy the atomic counter */
H5TS_atomic_destroy_int(&counter_g);

} /* end tts_atomics() */

#endif /*H5_HAVE_THREADSAFE*/

0 comments on commit 0496d35

Please sign in to comment.