ae2f_docs
Lock.h
Go to the documentation of this file.
1/**
2 * @file Lock.h
3 * @todo Give a way for err-handling
4 */
5
6#ifndef ae2f_Lock_h
7#define ae2f_Lock_h
8
9#include "./Platform.h"
10
11
13#include <windows.h>
14#pragma comment(lib, "synchronization") /** synchronization link */
15
16/**
17 * @brief
18 * Address element type.
19 */
20#define ae2f_addrel_t LONG
21
22/**
23 * @def __ae2f_win_wait
24 * @param uaddr {ae2f_addrel_t*}
25 * @param v {int}
26 * @brief
27 * Wait `uaddr` if its value equals to `v`.
28 */
29#define __ae2f_win_wait(uaddr, v)
30 { int vv = v; WaitOnAddress(((volatile LONG*)(uaddr)), &vv, sizeof(int), INFINITE); }
31
32/**
33 * @def __ae2f_win_wakesingle
34 * @param uaddr {ae2f_addrel_t*}
35 */
36#define __ae2f_win_wakesingle(uaddr)
37 WakeByAddressSingle((PVOID)(uaddr))
38
39#define __ae2f_WakeSingle __ae2f_win_wakesingle
40#define __ae2f_Wait __ae2f_win_wait
41
42#endif
43
45#include <linux/futex.h>
46#include <sys/syscall.h>
47#include <unistd.h>
48
49/**
50 * @brief
51 * Address element type.
52 */
53#define ae2f_addrel_t int
54
55/**
56 * @def __ae2f_futex
57 * @param uaddr {int*}
58 * @param op {int}
59 * @param val {int}
60 * @param timeout {const struct timespec*}
61 * @param uaddr2 {int*}
62 * @param val3 {int}
63 * @returns {int}
64 */
65#define __ae2f_futex(uaddr, op, val, timeout, uaddr2, val3)
66 syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3)
67
68
69/**
70 * @def __ae2f_futex_wait
71 * @param uaddr {ae2f_addrel_t*}
72 * @param v {int}
73 * @brief
74 * Wait `uaddr` if its value equals to `v`.
75 */
76#define __ae2f_futex_wait(uaddr, v)
77 __ae2f_futex(uaddr, FUTEX_WAIT, v, NULL, NULL, 0)
78
79/**
80 * @def __ae2f_futex_wake
81 * @param uaddr {ae2f_addrel_t*}
82 * @param n {int} waking thread count.
83 * @brief
84 * Wake `n` counts of threads waiting as `uaddr`.
85 */
86#define __ae2f_futex_wake(uaddr, n)
87 __ae2f_futex(uaddr, FUTEX_WAKE, n, NULL, NULL, 0)
88
89
90/**
91 * @def __ae2f_futex_wakesigle
92 * @param uaddr {ae2f_addrel_t*}
93 */
94#define __ae2f_futex_wakesigle(uaddr)
95 __ae2f_futex_wake(uaddr, 1)
96
97/**/
98
99#define __ae2f_Wait __ae2f_futex_wait
100#define __ae2f_WakeSingle __ae2f_futex_wakesigle
101
102#endif
103
105#include <pthread.h>
106#include <errno.h>
107
108/**
109 * @brief Address element type.
110 */
111#define ae2f_addrel_t int32_t
112
113/**
114 * @brief Static mutex and condition variable for synchronization.
115 */
116static pthread_mutex_t __ae2f_osx_mutex = PTHREAD_MUTEX_INITIALIZER;
117static pthread_cond_t __ae2f_osx_cond = PTHREAD_COND_INITIALIZER;
118
119/**
120 * @brief Wait on uaddr if its value equals v.
121 * @param uaddr Pointer to address to wait on.
122 * @param v Value to compare against.
123 * @return 0 on success, -1 on error with errno set.
124 */
125#define __ae2f_osx_wait(uaddr, v)
126 ({ int __ret = 0;
127 if (pthread_mutex_lock(&__ae2f_osx_mutex) != 0) {
128 errno = EAGAIN;
129 __ret = -1;
130 } else {
131 while (*(uaddr) == (v) && __ret == 0) {
132 if (pthread_cond_wait(&__ae2f_osx_cond, &__ae2f_osx_mutex) != 0) {
133 errno = EAGAIN;
134 __ret = -1;
135 }
136 }
137 if (pthread_mutex_unlock(&__ae2f_osx_mutex) != 0) {
138 errno = EAGAIN;
139 __ret = -1;
140 }
141 }
142 __ret; })
143
144/**
145 * @brief Wake one thread waiting on uaddr.
146 * @param uaddr Pointer to address (unused, wakes condition variable).
147 * @return 0 on success, -1 on error with errno set.
148 */
149#define __ae2f_osx_wakesingle(uaddr)
150 ({ int __ret = 0;
151 if (pthread_mutex_lock(&__ae2f_osx_mutex) != 0) {
152 errno = EAGAIN;
153 __ret = -1;
154 } else {
155 if (pthread_cond_signal(&__ae2f_osx_cond) != 0) {
156 errno = EAGAIN;
157 __ret = -1;
158 }
159 if (pthread_mutex_unlock(&__ae2f_osx_mutex) != 0) {
160 errno = EAGAIN;
161 __ret = -1;
162 }
163 }
164 __ret; })
165
166#define __ae2f_Wait __ae2f_osx_wait
167#define __ae2f_WakeSingle __ae2f_osx_wakesingle
168#endif
169
170#endif
#define __ae2f_Wait
Definition Lock.h:166
#define __ae2f_WakeSingle
Definition Lock.h:167
#define ae2f_addrel_t
Address element type.
Definition Lock.h:111
#define ae2f_IS_APPLE
Definition Platform.h:19
#define ae2f_IS_WIN
Definition Platform.h:7
#define ae2f_IS_LINUX
Definition Platform.h:13
#define ae2f_IDK
Definition Platform.h:26
ae2f_addrel_t el
Definition WaitWake.c:6
void * P(void *)
Definition WaitWake.c:8