-
Notifications
You must be signed in to change notification settings - Fork 18
/
wtz.h
62 lines (53 loc) · 1.37 KB
/
wtz.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
/* SPDX-License-Identifier: BSD-3-Clause */
/*
* wtz.h - Wait Til Zero wait object
*
* This is opposite of a semaphore. It arms the object with a count
* and only the last arrival releases the waiter. Usually used
* for a barrier, where main thread needs to wait for all workers
* to finish a stage.
*
* TODO: where is the MPI true barrier where also all the workers
* then wait for the barrier to finish? Probably we can do that with
* the semaphore or another one, I need to calculate that
*
* Copyright (c) 2018 NetApp, Inc. All rights reserved.
*
* See module.c for LICENSE details.
*
* Authors:
* Boaz Harrosh <[email protected]>
*/
#ifndef __WTZ_H__
#define __WTZ_H__
#include <semaphore.h>
struct wait_til_zero {
int acnt;
sem_t sem;
};
static void wtz_init(struct wait_til_zero *wtz)
{
__atomic_store_n(&wtz->acnt, 0, __ATOMIC_RELAXED);
sem_init(&wtz->sem, 0, 0);
}
static int wtz_arm(struct wait_til_zero *wtz, int c)
{
int prev = __atomic_fetch_add(&wtz->acnt, c,
__ATOMIC_RELAXED);
return prev;
}
/* Release one at a time sorry ;-) */
static int wtz_release(struct wait_til_zero *wtz)
{
int prev = __atomic_fetch_sub(&wtz->acnt, 1,
__ATOMIC_RELAXED);
if (prev == 1)
sem_post(&wtz->sem);
return prev - 1;
}
/* Wait all arms are released */
static void wtz_wait(struct wait_til_zero *wtz)
{
sem_wait(&wtz->sem);
}
#endif /* define __WTZ_H__ */