P99
p99_notifier.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-2014 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_NOTIFIER_H
22 #define P99_NOTIFIER_H
23 
24 #include "p99_futex.h"
25 
75 #ifdef P00_DOXYGEN
76 struct p99_notifier { };
77 #else
78 typedef p99_futex p99_notifier;
79 #endif
80 
87 p99_notifier* p99_notifier_init(p99_notifier* p00_n, unsigned p00_v) {
88  return p99_futex_init(p00_n, p00_v);
89 }
90 
91 #ifndef DOXYGEN
92 #define p99_notifier_init(...) P99_CALL_DEFARG(p99_notifier_init, 2, __VA_ARGS__)
93 #define p99_notifier_init_defarg_1() 0U
94 #endif
95 
96 
103  p99_futex_destroy(p00_n);
104 }
105 
116 P00_FUTEX_INLINE(p99_notifier_block)
117 void p99_notifier_block(p99_notifier volatile* p00_n, unsigned p00_v) {
118  P99_FUTEX_COMPARE_EXCHANGE(p00_n, p00_act,
119  /* wait for p00_v */
120  (p00_act == p00_v),
121  /* never update */
122  p00_act,
123  /* never wakeup others */
124  0u, 0u);
125 }
126 
127 #ifndef DOXYGEN
128 #define p99_notifier_block(...) P99_CALL_DEFARG(p99_notifier_block, 2, __VA_ARGS__)
129 #define p99_notifier_block_defarg_1() 1U
130 #endif
131 
135 # define P99_NOTIFIER_INITIALIZER P99_FUTEX_INITIALIZER(0u)
136 
142 unsigned p99_notifier_load(p99_notifier volatile* p00_n) {
143  return p99_futex_load(p00_n);
144 }
145 
153 P99_DEFARG_DOCU(p99_notifier_set)
154 P99_SETJMP_INLINE(p99_notifier_set)
155 void p99_notifier_set(p99_notifier volatile* p00_n, unsigned p00_v) {
156  if (p00_v)
158  // name of the local variable
159  p00_l,
160  // never wait
161  true,
162  // the new value
163  p00_v,
164  // no enforced wake up
165  0u,
166  // wake up all waiters only if there was a change
167  ((p00_l != p00_v) ? P99_FUTEX_MAX_WAITERS : 0u));
168  else
170  // name of the local variable
171  p00_l,
172  // never wait
173  true,
174  // the new value
175  p00_v,
176  // never wake up anybody
177  0u, 0u);
178 }
179 
180 #ifndef DOXYGEN
181 #define p99_notifier_set(...) P99_CALL_DEFARG(p99_notifier_set, 2, __VA_ARGS__)
182 #define p99_notifier_set_defarg_1() 1U
183 #endif
184 
194 void p99_notifier_unset(p99_notifier volatile* p00_n) {
195  p99_futex_exchange(p00_n, 0u, 1u, 0u, 0u, 0u);
196 }
197 
203 #endif
void
void void
Definition: p99_bitset.h:84
p99_futex.h
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_notifier::p99_notifier_block
void p99_notifier_block(p99_notifier volatile *p00_n, unsigned p00_v)
Block until the notification flag has been set to the indicated value.
Definition: p99_notifier.h:117
p99_notifier::p99_notifier_init
p99_notifier * p99_notifier_init(p99_notifier *p00_n, unsigned p00_v)
Initialize a notifier to value p00_v.
Definition: p99_notifier.h:87
P99_SETJMP_INLINE
#define P99_SETJMP_INLINE(NAME)
Definition: p99_compiler.h:628
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_notifier
Set a flag and notify other threads that an event has occurred.
Definition: p99_notifier.h:76
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::p99_futex_load
unsigned p99_futex_load(p99_futex volatile *p00_fut)
Obtain the value of futex p00_fut atomically.
p99_notifier::p99_notifier_destroy
void p99_notifier_destroy(p99_notifier *p00_n)
destroy a notifier
Definition: p99_notifier.h:102
p99_notifier::p99_notifier_load
unsigned p99_notifier_load(p99_notifier volatile *p00_n)
Return the value of the notifier. Non blocking.
Definition: p99_notifier.h:142
p99_futex::P99_FUTEX_MAX_WAITERS
#define P99_FUTEX_MAX_WAITERS
the maximum number of waiters that an p99_futex may have
Definition: p99_futex.h:178
p99_futex::p99_futex_init
p99_futex * p99_futex_init(p99_futex *p00_c, unsigned p00_ini)
Initialize an p99_futex object.