#ifndef _FI_LOCK_H_
#define _FI_LOCK_H_
#include "config.h"
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
#include <fi_osd.h>
#ifdef __cplusplus
extern "C" {
#endif
int fi_wait_cond(pthread_cond_t *cond, pthread_mutex_t *mut, int timeout_ms);
#if PT_LOCK_SPIN == 1
#define fastlock_t_ pthread_spinlock_t
#define fastlock_init_(lock) pthread_spin_init(lock, PTHREAD_PROCESS_PRIVATE)
#define fastlock_destroy_(lock) pthread_spin_destroy(lock)
#define fastlock_acquire_(lock) pthread_spin_lock(lock)
#define fastlock_tryacquire_(lock) pthread_spin_trylock(lock)
#define fastlock_release_(lock) pthread_spin_unlock(lock)
#else
#define fastlock_t_ pthread_mutex_t
#define fastlock_init_(lock) pthread_mutex_init(lock, NULL)
#define fastlock_destroy_(lock) pthread_mutex_destroy(lock)
#define fastlock_acquire_(lock) pthread_mutex_lock(lock)
#define fastlock_tryacquire_(lock) pthread_mutex_trylock(lock)
#define fastlock_release_(lock) pthread_mutex_unlock(lock)
#endif
#if ENABLE_DEBUG
typedef struct {
fastlock_t_ impl;
int is_initialized;
} fastlock_t;
static inline int fastlock_init(fastlock_t *lock)
{
int ret;
ret = fastlock_init_(&lock->impl);
lock->is_initialized = !ret;
return ret;
}
static inline void fastlock_destroy(fastlock_t *lock)
{
int ret;
assert(lock->is_initialized);
lock->is_initialized = 0;
ret = fastlock_destroy_(&lock->impl);
assert(!ret);
}
static inline void fastlock_acquire(fastlock_t *lock)
{
int ret;
assert(lock->is_initialized);
ret = fastlock_acquire_(&lock->impl);
assert(!ret);
}
static inline int fastlock_tryacquire(fastlock_t *lock)
{
assert(lock->is_initialized);
return fastlock_tryacquire_(&lock->impl);
}
static inline void fastlock_release(fastlock_t *lock)
{
int ret;
assert(lock->is_initialized);
ret = fastlock_release_(&lock->impl);
assert(!ret);
}
#else
# define fastlock_t fastlock_t_
# define fastlock_init(lock) fastlock_init_(lock)
# define fastlock_destroy(lock) fastlock_destroy_(lock)
# define fastlock_acquire(lock) fastlock_acquire_(lock)
# define fastlock_tryacquire(lock) fastlock_tryacquire_(lock)
# define fastlock_release(lock) fastlock_release_(lock)
#endif
#ifdef __cplusplus
}
#endif
#endif