summaryrefslogtreecommitdiffstats
path: root/src/H5FDsubfiling/mercury/src/util/mercury_thread_mutex.h
blob: 73ceafb8850594c963218dd2222a49a80ca57ead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
 * Copyright (c) 2013-2022 UChicago Argonne, LLC and The HDF Group.
 * Copyright (c) 2022-2023 Intel Corporation.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef MERCURY_THREAD_MUTEX_H
#define MERCURY_THREAD_MUTEX_H

#include "mercury_util_config.h"

#include "mercury_thread_annotation.h"

#ifdef _WIN32
#define _WINSOCKAPI_
#include <windows.h>
/* clang-format off */
#    define HG_THREAD_MUTEX_INITIALIZER {NULL}
/* clang-format on */
typedef CRITICAL_SECTION hg_thread_mutex_t;
#else
#include <pthread.h>
#define HG_THREAD_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
typedef pthread_mutex_t HG_LOCK_CAPABILITY("mutex") hg_thread_mutex_t;
#endif

#ifdef __cplusplus
extern "C" {
#endif

/**
 * Initialize the mutex.
 *
 * \param mutex [IN/OUT]        pointer to mutex object
 *
 * \return Non-negative on success or negative on failure
 */
HG_UTIL_PUBLIC int hg_thread_mutex_init(hg_thread_mutex_t *mutex);

/**
 * Initialize the mutex, asking for "fast" mutex.
 *
 * \param mutex [IN/OUT]        pointer to mutex object
 *
 * \return Non-negative on success or negative on failure
 */
HG_UTIL_PUBLIC int hg_thread_mutex_init_fast(hg_thread_mutex_t *mutex);

/**
 * Destroy the mutex.
 *
 * \param mutex [IN/OUT]        pointer to mutex object
 *
 * \return Non-negative on success or negative on failure
 */
HG_UTIL_PUBLIC int hg_thread_mutex_destroy(hg_thread_mutex_t *mutex);

/**
 * Lock the mutex.
 *
 * \param mutex [IN/OUT]        pointer to mutex object
 */
static HG_UTIL_INLINE void hg_thread_mutex_lock(hg_thread_mutex_t *mutex) HG_LOCK_ACQUIRE(*mutex);

/**
 * Try locking the mutex.
 *
 * \param mutex [IN/OUT]        pointer to mutex object
 *
 * \return Non-negative on success or negative on failure
 */
static HG_UTIL_INLINE int hg_thread_mutex_try_lock(hg_thread_mutex_t *mutex)
    HG_LOCK_TRY_ACQUIRE(HG_UTIL_SUCCESS, *mutex);

/**
 * Unlock the mutex.
 *
 * \param mutex [IN/OUT]        pointer to mutex object
 */
static HG_UTIL_INLINE void hg_thread_mutex_unlock(hg_thread_mutex_t *mutex) HG_LOCK_RELEASE(*mutex);

/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE void
hg_thread_mutex_lock(hg_thread_mutex_t *mutex) HG_LOCK_NO_THREAD_SAFETY_ANALYSIS
{
#ifdef _WIN32
    EnterCriticalSection(mutex);
#else
    (void)pthread_mutex_lock(mutex);
#endif
}

/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE int
hg_thread_mutex_try_lock(hg_thread_mutex_t *mutex) HG_LOCK_NO_THREAD_SAFETY_ANALYSIS
{
#ifdef _WIN32
    if (!TryEnterCriticalSection(mutex))
        return HG_UTIL_FAIL;
#else
    if (pthread_mutex_trylock(mutex))
        return HG_UTIL_FAIL;
#endif

    return HG_UTIL_SUCCESS;
}

/*---------------------------------------------------------------------------*/
static HG_UTIL_INLINE void
hg_thread_mutex_unlock(hg_thread_mutex_t *mutex) HG_LOCK_NO_THREAD_SAFETY_ANALYSIS
{
#ifdef _WIN32
    LeaveCriticalSection(mutex);
#else
    (void)pthread_mutex_unlock(mutex);
#endif
}

#ifdef __cplusplus
}
#endif

#endif /* MERCURY_THREAD_MUTEX_H */