summaryrefslogtreecommitdiff
path: root/libddekit/ddekit/lock.h
blob: dd398b385e6a38500b44372381e5f046f1a71050 (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
#ifndef _ddekit_lock_h
#define _ddekit_lock_h

struct ddekit_lock;

/** Initialize a DDEKit lock. 
 *
 * \ingroup DDEKit_synchronization
 */
void _ddekit_lock_init    (struct ddekit_lock **mtx);

/** Uninitialize a DDEKit lock.
 *
 * \ingroup DDEKit_synchronization
 */
void _ddekit_lock_deinit  (struct ddekit_lock **mtx);

/** Acquire a lock.
 *
 * \ingroup DDEKit_synchronization
 */
void _ddekit_lock_lock    (struct ddekit_lock **mtx);

/** Acquire a lock, non-blocking.
 *
 * \ingroup DDEKit_synchronization
 */
int  _ddekit_lock_try_lock(struct ddekit_lock **mtx);

/** Unlock function.
 *
 * \ingroup DDEKit_synchronization
 */
void _ddekit_lock_unlock  (struct ddekit_lock **mtx);

/** Get lock owner.
 *
 * \ingroup DDEKit_synchronization
 */
int _ddekit_lock_owner(struct ddekit_lock **mtx);

// definition of ddekit_lock_t
typedef struct ddekit_lock *ddekit_lock_t;

// common prototypes
static void ddekit_lock_init_locked(ddekit_lock_t *mtx);
static void ddekit_lock_init_unlocked(ddekit_lock_t *mtx);
#define ddekit_lock_init	ddekit_lock_init_unlocked
static void ddekit_lock_deinit  (ddekit_lock_t *mtx);
static void ddekit_lock_lock    (ddekit_lock_t *mtx);
static int  ddekit_lock_try_lock(ddekit_lock_t *mtx); // returns 0 on success, != 0 if it would block
static void ddekit_lock_unlock  (ddekit_lock_t *mtx);

// inline implementation or inline call to non-inline implementation
#include "ddekit/inline.h"

static INLINE void ddekit_lock_init_unlocked(ddekit_lock_t *mtx) {
	_ddekit_lock_init(mtx);
}

static INLINE void ddekit_lock_init_locked(ddekit_lock_t *mtx) {
	_ddekit_lock_init(mtx);
	_ddekit_lock_lock(mtx);
}

static INLINE void ddekit_lock_deinit(ddekit_lock_t *mtx) {
	_ddekit_lock_deinit(mtx);
}
static INLINE void ddekit_lock_lock(ddekit_lock_t *mtx) {
	_ddekit_lock_lock(mtx);
}
static INLINE int  ddekit_lock_try_lock(ddekit_lock_t *mtx) {
	return _ddekit_lock_try_lock(mtx);
}
static INLINE void ddekit_lock_unlock(ddekit_lock_t *mtx) {
	_ddekit_lock_unlock(mtx);
}

static INLINE int ddekit_lock_owner(ddekit_lock_t *mtx) {
	return _ddekit_lock_owner(mtx);
}

#endif