PIOMan

Documentation

piom_lock.h
Go to the documentation of this file.
1/*
2 * Pioman: a Generic I/O Manager
3 * Copyright (C) 2001-2024 (see AUTHORS file)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 */
15
16#ifndef PIOM_LOCK_H
17#define PIOM_LOCK_H
18
19#include <assert.h>
20
21#ifndef PIOM_CONFIG_H
22# error "Cannot include this file directly. Please include <pioman.h>."
23#endif /* PIOM_CONFIG_H */
24
32#if defined(PIOMAN_SIMGRID)
33# include "piom_lock_simgrid.h"
34#elif defined(PIOMAN_MARCEL)
35# include "piom_lock_marcel.h"
36#elif defined(PIOMAN_PTHREAD)
37# include "piom_lock_pthread.h"
38#elif defined(PIOMAN_ABT)
39# include "piom_lock_abt.h"
40#elif defined(PIOMAN_NOTHREAD)
41# include "piom_lock_nothread.h"
42#else
43# error "no lock defined"
44#endif
45
46/* ** cond ************************************************* */
47
49typedef uint32_t piom_cond_value_t;
50
51#if defined(PIOMAN_MULTITHREAD)
52
55struct piom_waitsem_s
56{
57 piom_sem_t sem;
59};
60typedef struct
61{
62 volatile piom_cond_value_t value;
63 struct piom_waitsem_s*p_waitsem;
64 piom_spinlock_t lock;
66#else /* PIOMAN_MULTITHREAD */
68#endif /* PIOMAN_MULTITHREAD */
69
70/* ** mask ************************************************* */
71
72#ifdef PIOMAN_MULTITHREAD
73
75static inline void piom_cond_add(piom_cond_t*cond, piom_cond_value_t mask)
76{
77 __sync_fetch_and_or(&cond->value, mask); /* cond->value |= mask; */
78}
80static inline void piom_cond_signal(piom_cond_t*cond, piom_cond_value_t mask)
81{
82 /* WARNING- order matters here!
83 * 1. check whether somebody is waiting on the cond with spinlock.
84 * (to ensure waiter atomically checks for status & sets waitsem)
85 * 2. add bitmask to cond value. Do not ever access cond after
86 * (not even a spin unlock). Waiter may be busy-waiting and may
87 * free the cond immediately after the bit is set.
88 * 3. signal on the semaphore. The semaphore must not have been freed
89 * since a waiter that sets waitsem _must_ wait on a sem_P.
90 */
91 piom_spin_lock(&cond->lock);
92 struct piom_waitsem_s*p_waitsem = cond->p_waitsem;
93 const piom_cond_value_t waitmask = p_waitsem ? (p_waitsem->mask & mask) : 0;
94 __sync_fetch_and_or(&cond->value, mask); /* cond->value |= mask; */
95 piom_spin_unlock(&cond->lock);
96 if(waitmask)
97 {
98 piom_sem_V(&p_waitsem->sem);
99 }
100}
103{
104 return cond->value & mask;
105}
110{
111 piom_spin_lock(&cond->lock);
112 piom_cond_value_t val = cond->value & mask;
113 piom_spin_unlock(&cond->lock);
114 return val;
115}
116
118static inline void piom_cond_init(piom_cond_t*cond, piom_cond_value_t initial)
119{
120 cond->value = initial;
121 cond->p_waitsem = NULL;
122 piom_spin_init(&cond->lock);
123}
124
126static inline void piom_cond_destroy(piom_cond_t*cond)
127{
128 piom_spin_destroy(&cond->lock);
129}
130
132static inline void piom_cond_mask(piom_cond_t*cond, piom_cond_value_t mask)
133{
134 __sync_fetch_and_and(&cond->value, ~mask); /* cond->value &= ~mask; */
135}
136
138extern void piom_cond_wait(piom_cond_t*cond, piom_cond_value_t mask);
139
141extern void piom_cond_wait_all(void**pp_conds, int n, uintptr_t offset, piom_cond_value_t mask);
142
143#else /* PIOMAN_MULTITHREAD */
144
145static inline void piom_cond_wait(piom_cond_t*cond, piom_cond_value_t mask)
146{
147 while(!(*cond & mask))
149}
150static inline void piom_cond_wait_all(void**pp_conds, int n, uintptr_t offset, piom_cond_value_t mask)
151{
152 int i;
153 for(i = 0; i < n; i++)
154 {
155 if(pp_conds[i] != NULL)
156 {
157 piom_cond_t*p_cond = (piom_cond_t*)(pp_conds[i] + offset);
158 piom_cond_wait(p_cond, mask);
159 }
160 }
161}
162static inline void piom_cond_add(piom_cond_t*cond, piom_cond_value_t mask)
163{
164 *cond |= mask;
165}
166static inline void piom_cond_signal(piom_cond_t*cond, piom_cond_value_t mask)
167{
168 *cond |= mask;
169}
171{
172 return *cond & mask;
173}
175{
176 return *cond & mask;
177}
178static inline void piom_cond_init(piom_cond_t*cond, piom_cond_value_t initial)
179{
180 *cond = initial;
181}
182static inline void piom_cond_destroy(piom_cond_t*cond)
183{
184}
185static inline void piom_cond_mask(piom_cond_t*cond, piom_cond_value_t mask)
186{
187 *cond &= ~mask;
188}
189
190#endif /* PIOMAN_MULTITHREAD */
191
192
195#endif /* PIOM_LOCK_H */
static piom_cond_value_t piom_cond_test(const piom_cond_t *cond, piom_cond_value_t mask)
Definition: piom_lock.h:170
static void piom_cond_add(piom_cond_t *cond, piom_cond_value_t mask)
Definition: piom_lock.h:162
static void piom_cond_wait_all(void **pp_conds, int n, uintptr_t offset, piom_cond_value_t mask)
Definition: piom_lock.h:150
static void piom_cond_init(piom_cond_t *cond, piom_cond_value_t initial)
Definition: piom_lock.h:178
piom_cond_value_t piom_cond_t
Definition: piom_lock.h:67
static void piom_cond_signal(piom_cond_t *cond, piom_cond_value_t mask)
Definition: piom_lock.h:166
static void piom_cond_mask(piom_cond_t *cond, piom_cond_value_t mask)
Definition: piom_lock.h:185
uint32_t piom_cond_value_t
value (bitmask) of cond
Definition: piom_lock.h:49
static void piom_cond_wait(piom_cond_t *cond, piom_cond_value_t mask)
Definition: piom_lock.h:145
static piom_cond_value_t piom_cond_test_locked(piom_cond_t *cond, piom_cond_value_t mask)
Definition: piom_lock.h:174
static void piom_cond_destroy(piom_cond_t *cond)
Definition: piom_lock.h:182
void piom_ltask_schedule(int point)
Schedule tasks from local or all queues (depending on 'point')
#define PIOM_POLL_POINT_BUSY
poll in a busy wait
Definition: pioman.h:86
#define piom_spin_unlock(lock)
Definition: piom_lock_abt.h:48
#define piom_spin_lock(lock)
Definition: piom_lock_abt.h:47
#define piom_spin_init(lock)
Definition: piom_lock_abt.h:46
static void piom_sem_V(piom_sem_t *sem)
Definition: piom_lock_abt.h:71
ABT_mutex piom_spinlock_t
Definition: piom_lock_abt.h:45
#define piom_spin_destroy(lock)