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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/*
* Copyright (c) 2013-2014 Intel Corporation. All rights reserved.
* Copyright (c) 2016 Cisco Systems, Inc. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _OFI_LOCK_H_
#define _OFI_LOCK_H_
#include "config.h"
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ofi_osd.h>
#ifdef __cplusplus
extern "C" {
#endif
int ofi_wait_cond(pthread_cond_t *cond, pthread_mutex_t *mut, int timeout_ms);
#if PT_LOCK_SPIN == 1
#define ofi_spin_t_ pthread_spinlock_t
#define ofi_spin_init_(lock) pthread_spin_init(lock, PTHREAD_PROCESS_PRIVATE)
#define ofi_spin_destroy_(lock) pthread_spin_destroy(lock)
#define ofi_spin_lock_(lock) pthread_spin_lock(lock)
#define ofi_spin_trylock_(lock) pthread_spin_trylock(lock)
#define ofi_spin_unlock_(lock) pthread_spin_unlock(lock)
#else
#define ofi_spin_t_ pthread_mutex_t
#define ofi_spin_init_(lock) pthread_mutex_init(lock, NULL)
#define ofi_spin_destroy_(lock) pthread_mutex_destroy(lock)
#define ofi_spin_lock_(lock) pthread_mutex_lock(lock)
#define ofi_spin_trylock_(lock) pthread_mutex_trylock(lock)
#define ofi_spin_unlock_(lock) pthread_mutex_unlock(lock)
#endif /* PT_LOCK_SPIN */
#if ENABLE_DEBUG
typedef struct {
ofi_spin_t_ impl;
int is_initialized;
int in_use;
} ofi_spin_t;
static inline int ofi_spin_init(ofi_spin_t *lock)
{
int ret;
ret = ofi_spin_init_(&lock->impl);
lock->is_initialized = !ret;
lock->in_use = 0;
return ret;
}
static inline void ofi_spin_destroy(ofi_spin_t *lock)
{
int ret;
assert(lock->is_initialized);
lock->is_initialized = 0;
ret = ofi_spin_destroy_(&lock->impl);
assert(!ret);
}
static inline void ofi_spin_lock(ofi_spin_t *lock)
{
int ret;
assert(lock->is_initialized);
ret = ofi_spin_lock_(&lock->impl);
assert(!ret);
lock->in_use++;
}
static inline int ofi_spin_trylock(ofi_spin_t *lock)
{
int ret;
assert(lock->is_initialized);
ret = ofi_spin_trylock_(&lock->impl);
if (!ret)
lock->in_use++;
return ret;
}
static inline void ofi_spin_unlock(ofi_spin_t *lock)
{
int ret;
assert(lock->in_use);
assert(lock->is_initialized);
lock->in_use--;
ret = ofi_spin_unlock_(&lock->impl);
assert(!ret);
}
static inline int ofi_spin_held(ofi_spin_t *lock)
{
return lock->in_use;
}
static inline void ofi_spin_lock_noop(ofi_spin_t *lock)
{
/* These non-op routines must be used only by single-threaded code*/
assert(!lock->in_use);
lock->in_use = 1;
}
static inline void ofi_spin_unlock_noop(ofi_spin_t *lock)
{
assert(lock->in_use);
lock->in_use = 0;
}
#else /* !ENABLE_DEBUG */
# define ofi_spin_t ofi_spin_t_
# define ofi_spin_init(lock) ofi_spin_init_(lock)
# define ofi_spin_destroy(lock) ofi_spin_destroy_(lock)
# define ofi_spin_lock(lock) ofi_spin_lock_(lock)
# define ofi_spin_trylock(lock) ofi_spin_trylock_(lock)
# define ofi_spin_unlock(lock) ofi_spin_unlock_(lock)
# define ofi_spin_held(lock) true
static inline void ofi_spin_lock_noop(ofi_spin_t *lock)
{
(void) lock;
}
static inline void ofi_spin_unlock_noop(ofi_spin_t *lock)
{
(void) lock;
}
#endif
typedef void(*ofi_spin_lock_t)(ofi_spin_t *lock);
typedef void(*ofi_spin_unlock_t)(ofi_spin_t *lock);
static inline void ofi_spin_lock_op(ofi_spin_t *lock)
{
ofi_spin_lock(lock);
}
static inline void ofi_spin_unlock_op(ofi_spin_t *lock)
{
ofi_spin_unlock(lock);
}
static inline int ofi_spin_held_op(ofi_spin_t *lock)
{
return ofi_spin_held(lock);
}
#define ofi_mutex_t_ pthread_mutex_t
#define ofi_mutex_init_(lock) pthread_mutex_init(lock, NULL)
#define ofi_mutex_destroy_(lock) pthread_mutex_destroy(lock)
#define ofi_mutex_lock_(lock) pthread_mutex_lock(lock)
#define ofi_mutex_trylock_(lock) pthread_mutex_trylock(lock)
#define ofi_mutex_unlock_(lock) pthread_mutex_unlock(lock)
#if ENABLE_DEBUG
typedef struct {
ofi_mutex_t_ impl;
int is_initialized;
int in_use;
} ofi_mutex_t;
static inline int ofi_mutex_init(ofi_mutex_t *lock)
{
int ret;
ret = ofi_mutex_init_(&lock->impl);
lock->is_initialized = !ret;
lock->in_use = 0;
return ret;
}
static inline void ofi_mutex_destroy(ofi_mutex_t *lock)
{
int ret;
assert(lock->is_initialized);
lock->is_initialized = 0;
ret = ofi_mutex_destroy_(&lock->impl);
assert(!ret);
}
static inline void ofi_mutex_lock(ofi_mutex_t *lock)
{
int ret;
assert(lock->is_initialized);
ret = ofi_mutex_lock_(&lock->impl);
assert(!ret);
lock->in_use++;
}
static inline int ofi_mutex_trylock(ofi_mutex_t *lock)
{
int ret;
assert(lock->is_initialized);
ret = ofi_mutex_trylock_(&lock->impl);
if (!ret)
lock->in_use++;
return ret;
}
static inline void ofi_mutex_unlock(ofi_mutex_t *lock)
{
int ret;
assert(lock->in_use);
assert(lock->is_initialized);
lock->in_use--;
ret = ofi_mutex_unlock_(&lock->impl);
assert(!ret);
}
static inline int ofi_mutex_held(ofi_mutex_t *lock)
{
return lock->in_use;
}
static inline void ofi_mutex_lock_noop(ofi_mutex_t *lock)
{
/* These non-op routines must be used only by single-threaded code*/
assert(!lock->in_use);
lock->in_use = 1;
}
static inline void ofi_mutex_unlock_noop(ofi_mutex_t *lock)
{
assert(lock->in_use);
lock->in_use = 0;
}
static inline int
ofi_pthread_wait_cond(pthread_cond_t *cond, ofi_mutex_t *lock, int timeout_ms)
{
assert(lock->is_initialized);
return ofi_wait_cond(cond, &lock->impl, timeout_ms);
}
#else /* !ENABLE_DEBUG */
# define ofi_mutex_t ofi_mutex_t_
# define ofi_mutex_init(lock) ofi_mutex_init_(lock)
# define ofi_mutex_destroy(lock) ofi_mutex_destroy_(lock)
# define ofi_mutex_lock(lock) ofi_mutex_lock_(lock)
# define ofi_mutex_trylock(lock) ofi_mutex_trylock_(lock)
# define ofi_mutex_unlock(lock) ofi_mutex_unlock_(lock)
# define ofi_mutex_held(lock) true
static inline void ofi_mutex_lock_noop(ofi_mutex_t *lock)
{
(void) lock;
}
static inline void ofi_mutex_unlock_noop(ofi_mutex_t *lock)
{
(void) lock;
}
#define ofi_pthread_wait_cond(cond, mut, timeout_ms) \
ofi_wait_cond(cond, mut, timeout_ms)
#endif
typedef void(*ofi_mutex_lock_t)(ofi_mutex_t *lock);
typedef void(*ofi_mutex_unlock_t)(ofi_mutex_t *lock);
static inline void ofi_mutex_lock_op(ofi_mutex_t *lock)
{
ofi_mutex_lock(lock);
}
static inline void ofi_mutex_unlock_op(ofi_mutex_t *lock)
{
ofi_mutex_unlock(lock);
}
static inline int ofi_mutex_held_op(ofi_mutex_t *lock)
{
return ofi_mutex_held(lock);
}
static inline void ofi_nolock_lock_op(void *nolock)
{
(void) nolock;
}
static inline void ofi_nolock_unlock_op(void *nolock)
{
(void) nolock;
}
/* No way to verify, so return true to pass all asserts.
* User should provide their own checks higher-up.
*/
static inline int ofi_nolock_held_op(void *nolock)
{
return 1;
}
/*
* Generic lock abstraction
* Caller selects lock implementation at runtime
*/
enum ofi_lock_type {
OFI_LOCK_MUTEX, /* default */
OFI_LOCK_SPINLOCK,
OFI_LOCK_NOOP,
OFI_LOCK_NONE,
};
typedef int (*ofi_genlock_lockheld_t)(void *baselock);
typedef void (*ofi_genlock_lockop_t)(void *baselock);
struct ofi_genlock {
enum ofi_lock_type lock_type;
union {
ofi_mutex_t mutex;
ofi_spin_t spinlock;
void *nolock;
} base;
ofi_genlock_lockheld_t held;
ofi_genlock_lockop_t lock;
ofi_genlock_lockop_t unlock;
};
int ofi_genlock_init(struct ofi_genlock *lock,
enum ofi_lock_type lock_type);
void ofi_genlock_destroy(struct ofi_genlock *lock);
static inline int ofi_genlock_held(struct ofi_genlock *lock)
{
return lock->held(&lock->base);
}
static inline void ofi_genlock_lock(struct ofi_genlock *lock)
{
lock->lock(&lock->base);
}
static inline void ofi_genlock_unlock(struct ofi_genlock *lock)
{
lock->unlock(&lock->base);
}
#ifdef __cplusplus
}
#endif
#endif /* _OFI_LOCK_H_ */
|