P99
p99_futex.h
Go to the documentation of this file.
1 /* This may look like nonsense, but it really is -*- mode: C; coding: utf-8 -*- */
2 /* */
3 /* Except for parts copied from previous work and as explicitly stated below, */
4 /* the author and copyright holder for this work is */
5 /* (C) copyright 2012 Jens Gustedt, INRIA, France */
6 /* */
7 /* This file is free software; it is part of the P99 project. */
8 /* */
9 /* Licensed under the Apache License, Version 2.0 (the "License"); */
10 /* you may not use this file except in compliance with the License. */
11 /* You may obtain a copy of the License at */
12 /* */
13 /* http://www.apache.org/licenses/LICENSE-2.0 */
14 /* */
15 /* Unless required by applicable law or agreed to in writing, software */
16 /* distributed under the License is distributed on an "AS IS" BASIS, */
17 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
18 /* See the License for the specific language governing permissions and */
19 /* limitations under the License. */
20 /* */
21 #ifndef P99_FUTEX_H
22 #define P99_FUTEX_H
23 
24 #include "p99_type.h"
25 #include "p99_threads.h"
26 #include "p99_posix_default.h"
27 
29 
30 #if defined(DOXYGEN)
31 #define P00_FUTEX_INLINE(NAME) p99_inline
32 
33 #elif (defined(__linux__) && !defined(NO_FUTEX)) || defined(DOXYGEN)
34 typedef _Atomic(unsigned) p99_futex;
35 #define P00_FUTEX_INLINE(NAME) p99_inline
36 
37 #else
38 typedef p99_futex_c11 p99_futex;
39 /* The C11 implementation needs setjmp in its internals so the
40  functions must be implemented as weak symbols. */
41 #define P00_FUTEX_INLINE(NAME) P99_WEAK(NAME)
42 #endif
43 
82 #ifdef P00_DOXYGEN
83 
163 struct p99_futex { };
164 #endif
165 
166 
167 
178 # define P99_FUTEX_MAX_WAITERS (INT_MAX+0U)
179 
180 
185 p99_inline p99_futex* p99_futex_init(p99_futex* p00_c, unsigned p00_ini);
186 
192 
220 P00_FUTEX_INLINE(p99_futex_add) unsigned p99_futex_add(p99_futex volatile* p00_fut, unsigned p00_hmuch,
221  unsigned p00_cstart, unsigned p00_clen,
222  unsigned p00_wmin, unsigned p00_wmax);
223 
224 #ifndef DOXYGEN
225 #define p99_futex_add(...) P99_CALL_DEFARG(p99_futex_add, 6, __VA_ARGS__)
226 #define p99_futex_add_defarg_3() 1u
227 #define p99_futex_add_defarg_4() 0u
228 #define p99_futex_add_defarg_5() P99_FUTEX_MAX_WAITERS
229 #endif
230 
231 
236 P00_FUTEX_INLINE(p99_futex_load) unsigned p99_futex_load(p99_futex volatile* p00_fut);
237 
261 P00_FUTEX_INLINE(p99_futex_exchange) unsigned p99_futex_exchange(p99_futex volatile* p00_fut, unsigned p00_desired,
262  unsigned p00_cstart, unsigned p00_clen,
263  unsigned p00_wmin, unsigned p00_wmax);
264 
265 #ifndef DOXYGEN
266 #define p99_futex_exchange(...) P99_CALL_DEFARG(p99_futex_exchange, 6, __VA_ARGS__)
267 #define p99_futex_exchange_defarg_3() 1u
268 #define p99_futex_exchange_defarg_4() 0u
269 #define p99_futex_exchange_defarg_5() P99_FUTEX_MAX_WAITERS
270 #endif
271 
282 P00_FUTEX_INLINE(p99_futex_wakeup)
283 void p99_futex_wakeup(p99_futex volatile* p00_fut,
284  unsigned p00_wmin, unsigned p00_wmax);
285 
286 
297 P00_FUTEX_INLINE(p99_futex_wait)
298 void p99_futex_wait(p99_futex volatile* p00_fut);
299 
300 
301 
302 #ifdef DOXYGEN
303 
562 P00_DOCUMENT_IDENTIFIER_ARGUMENT(P99_FUTEX_COMPARE_EXCHANGE, 1)
563 #define P99_FUTEX_COMPARE_EXCHANGE(FUTEX, ACT, EXPECTED, DESIRED, WAKEMIN, WAKEMAX)
564 #endif
565 
570 #if (defined(__linux__) && !defined(NO_FUTEX)) || defined(DOXYGEN)
571 #include "p99_futex_linux.h"
572 #else
573 #include "p99_futex_c11.h"
574 #endif
575 
576 
577 #endif
p99_futex::p99_futex_destroy
void p99_futex_destroy(p99_futex *p00_c)
Destroy an p99_futex object.
p99_futex
A counter similar to a conditional variable that allows atomic increment and decrement and to wait fo...
Definition: p99_futex.h:163
p99_type.h
p99_futex::p99_futex_exchange
unsigned p99_futex_exchange(p99_futex volatile *p00_fut, unsigned p00_desired, unsigned p00_cstart, unsigned p00_clen, unsigned p00_wmin, unsigned p00_wmax)
Unconditionally and atomically set the futex p00_fut to value p00_desired.
p99_inline
#define p99_inline
Try to force a function always to be inlined.
Definition: p99_compiler.h:496
p99_futex::P99_FUTEX_COMPARE_EXCHANGE
#define P99_FUTEX_COMPARE_EXCHANGE(FUTEX, ACT, EXPECTED, DESIRED, WAKEMIN, WAKEMAX)
a catch all macro to operate on p99_futex
Definition: p99_futex.h:563
p99_futex_c11.h
P99_DEFARG_DOCU
#define P99_DEFARG_DOCU(NAME)
Provide a documentation section to a function defined with P99_CALL_DEFARG.
Definition: p99_defarg.h:318
p99_futex_c11
The structure that is used in the fallback implementation of p99_futex on non-linux systems.
Definition: p99_futex_c11.h:28
P99_DECLARE_STRUCT
#define P99_DECLARE_STRUCT(NAME)
forward declaration of a struct NAME
Definition: p99_type.h:59
p99_futex::p99_futex_load
unsigned p99_futex_load(p99_futex volatile *p00_fut)
Obtain the value of futex p00_fut atomically.
p99_posix_default.h
p99_futex::p99_futex_wakeup
void p99_futex_wakeup(p99_futex volatile *p00_fut, unsigned p00_wmin, unsigned p00_wmax)
Wake up threads that are waiting for a futex.
p99_futex::p99_futex_wait
void p99_futex_wait(p99_futex volatile *p00_fut)
Unconditionally wait for futex p00_fut.
p99_futex::p99_futex_add
unsigned p99_futex_add(p99_futex volatile *p00_fut, unsigned p00_hmuch, unsigned p00_cstart, unsigned p00_clen, unsigned p00_wmin, unsigned p00_wmax)
increment the counter of p00_fut atomically by p00_hmuch.
p99_futex::p99_futex_init
p99_futex * p99_futex_init(p99_futex *p00_c, unsigned p00_ini)
Initialize an p99_futex object.
p99_threads.h
p99_futex_linux.h