-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsync.h
81 lines (68 loc) · 2.06 KB
/
sync.h
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
/*!
* @file libxutils/src/sys/sync.h
*
* This source is part of "libxutils" project
* 2015-2020 Sun Dro ([email protected])
*
* @brief Implementation of the various cross-platform
* sync functionality with the POSIX mutex and SynchAPI
* critical section, rwlocks, atomic built-ins, and etc.
*/
#ifndef __XUTILS_XSYNC_H__
#define __XUTILS_XSYNC_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "xstd.h"
typedef struct XSyncMutex {
#ifdef _WIN32
CRITICAL_SECTION mutex;
#else
pthread_mutex_t mutex;
#endif
xbool_t bEnabled;
} xsync_mutex_t;
typedef struct XSyncRW {
#ifdef _WIN32
xbool_t bExclusive;
SRWLOCK rwLock;
#else
pthread_rwlock_t rwLock;
#endif
xbool_t bEnabled;
} xsync_rw_t;
typedef struct XSyncBar {
xatomic_t nBar;
xatomic_t nAck;
} xsync_bar_t;
#ifdef _WIN32
#define XSYNC_ATOMIC_ADD(dst,val) InterlockedExchangeAdd(dst, val)
#define XSYNC_ATOMIC_SUB(dst,val) InterlockedExchangeSubtract(dst, val)
#define XSYNC_ATOMIC_SET(dst,val) InterlockedExchange(dst, val)
#define XSYNC_ATOMIC_GET(dst) InterlockedExchangeAdd(dst, 0)
#else
#define XSYNC_ATOMIC_ADD(dst,val) __sync_add_and_fetch(dst, val)
#define XSYNC_ATOMIC_SUB(dst,val) __sync_sub_and_fetch(dst, val)
#define XSYNC_ATOMIC_SET(dst,val) __sync_lock_test_and_set(dst, val)
#define XSYNC_ATOMIC_GET(dst) __sync_add_and_fetch(dst, 0)
#endif
void xusleep(uint32_t nUsecs);
void XSync_Init(xsync_mutex_t *pSync);
void XSync_Destroy(xsync_mutex_t *pSync);
void XSync_Lock(xsync_mutex_t *pSync);
void XSync_Unlock(xsync_mutex_t *pSync);
void XRWSync_Init(xsync_rw_t *pSync);
void XRWSync_ReadLock(xsync_rw_t *pSync);
void XRWSync_WriteLock(xsync_rw_t *pSync);
void XRWSync_Unlock(xsync_rw_t *pSync);
void XRWSync_Destroy(xsync_rw_t *pSync);
void XSyncBar_Bar(xsync_bar_t *pBar);
void XSyncBar_Ack(xsync_bar_t *pBar);
void XSyncBar_Reset(xsync_bar_t *pBar);
uint8_t XSyncBar_CheckBar(xsync_bar_t *pBar);
uint8_t XSyncBar_CheckAck(xsync_bar_t *pBar);
uint32_t XSyncBar_WaitAck(xsync_bar_t *pBar, uint32_t nSleepUsec);
#ifdef __cplusplus
}
#endif
#endif /* __XUTILS_XSYNC_H__ */