Commit 2b8ecbb8 authored by Martin Karsten's avatar Martin Karsten

- change locks nomenclature

parent 32874d4f
......@@ -37,10 +37,10 @@ class Cluster : public Scheduler {
PollerType* pollVec;
size_t pollCount;
BaseProcessor* pauseProc;
TaskSemaphore pauseSem;
WorkerSemaphore confirmSem;
WorkerSemaphore sleepSem;
BaseProcessor* pauseProc;
Semaphore<WorkerLock,false> pauseSem;
WorkerSemaphore confirmSem;
WorkerSemaphore sleepSem;
ClusterStats* stats;
......
......@@ -28,25 +28,24 @@
#include <sys/types.h>
#include <sys/socket.h>
// A vector for FDs works well here in principle, because POSIX guarantees lowest-numbered FDs:
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_14
// A fixed-size array based on 'getrlimit' is somewhat brute-force, but simple and fast.
/**
An EventScope object holds a set of Clusters and provides a common I/O
scope. Multiple EventScope objects can be used to take advantage of
partitioned kernel file descriptor tables on Linux.
*/
class EventScope {
// A vector for FDs works well here in principle, because POSIX guarantees lowest-numbered FDs:
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_14
// A fixed-size array based on 'getrlimit' is somewhat brute-force, but simple and fast.
struct SyncSem {
TaskLock mtx;
TaskBinarySemaphore sem;
FastMutex mtx;
Semaphore<WorkerLock,true> sem;
};
struct SyncFD {
SyncSem RD;
SyncSem WR;
#if TESTING_LAZY_FD_REGISTRATION
TaskLock lock;
FastMutex lock;
BasePoller* poller;
size_t status;
SyncFD() : poller(nullptr), status(0) {}
......@@ -166,7 +165,7 @@ private:
RASSERT0(fd >= 0 && fd < fdCount);
SyncFD& fdsync = fdSyncVector[fd];
if ((fdsync.status & target) == target) return false; // outside of lock: faster, but double regs possible...
ScopedLock<TaskLock> sl(fdsync.lock);
ScopedLock<FastMutex> sl(fdsync.lock);
RASSERT0((bool)fdsync.status == (bool)fdsync.poller)
fdsync.status |= target;
#endif
......@@ -206,7 +205,7 @@ public:
RASSERT0(fdsync.RD.sem.empty());
RASSERT0(fdsync.WR.sem.empty());
#if TESTING_LAZY_FD_REGISTRATION
ScopedLock<TaskLock> sl(fdsync.lock);
ScopedLock<FastMutex> sl(fdsync.lock);
fdsync.status = 0;
if (RemoveFromPollSet) { // only called from lfConnect w/ TESTING_LAZY_FD_REGISTRATION
RASSERT0(fdsync.poller)
......@@ -236,7 +235,7 @@ public:
void blockPollFD(int fd) {
RASSERT0(fd >= 0 && fd < fdCount);
masterPoller->setupPollFD(fd, true); // reset using ONESHOT to reduce polling
ScopedLock<TaskLock> sl(fdSyncVector[fd].RD.mtx);
ScopedLock<FastMutex> sl(fdSyncVector[fd].RD.mtx);
fdSyncVector[fd].RD.sem.P();
}
......@@ -261,7 +260,7 @@ public:
void block(int fd) {
RASSERT0(fd >= 0 && fd < fdCount);
SyncSem& sync = Input ? fdSyncVector[fd].RD : fdSyncVector[fd].WR;
ScopedLock<TaskLock> sl(sync.mtx);
ScopedLock<FastMutex> sl(sync.mtx);
sync.sem.P();
}
......@@ -314,7 +313,7 @@ public:
}
#endif
SyncSem& sync = Input ? fdSyncVector[fd].RD : fdSyncVector[fd].WR;
ScopedLock<TaskLock> sl(sync.mtx);
ScopedLock<FastMutex> sl(sync.mtx);
for (;;) {
sync.sem.P();
ret = iofunc(fd, a...);
......
......@@ -23,16 +23,15 @@ struct _cfibre_mutex_t : public fibre_mutex_t {};
struct _cfibre_cond_t : public fibre_cond_t {};
struct _cfibre_rwlock_t : public fibre_rwlock_t {};
struct _cfibre_barrier_t : public fibre_barrier_t {};
struct _cfast_mutex_t : public fast_mutex_t {};
struct _cfast_cond_t : public fast_cond_t {};
struct _cfibre_attr_t : public fibre_attr_t {};
struct _cfibre_mutexattr_t : public fibre_mutexattr_t {};
struct _cfibre_condattr_t : public fibre_condattr_t {};
struct _cfibre_rwlockattr_t : public fibre_rwlockattr_t {};
struct _cfibre_barrierattr_t : public fibre_barrierattr_t {};
struct _cfast_mutexattr_t : public fast_mutexattr_t {};
struct _cfast_condattr_t : public fast_condattr_t {};
struct _cfibre_fastmutex_t : public fibre_fastmutex_t {};
struct _cfibre_fastmutexattr_t : public fibre_fastmutexattr_t {};
struct _cfibre_cluster_t : public Cluster {};
......@@ -294,56 +293,36 @@ extern "C" int cfibre_barrier_wait(cfibre_barrier_t *barrier) {
return fibre_barrier_wait(*barrier);
}
extern "C" int cfast_mutex_init(cfast_mutex_t *restrict mutex, const cfast_mutexattr_t *restrict attr) {
*mutex = (cfast_mutex_t)new fast_mutex_t;
return fast_mutex_init(*mutex, (fast_mutexattr_t*)attr);
extern "C" int cfibre_fastmutex_init(cfibre_fastmutex_t *restrict mutex, const cfibre_fastmutexattr_t *restrict attr) {
*mutex = (cfibre_fastmutex_t)new fibre_fastmutex_t;
return fibre_fastmutex_init(*mutex, (fibre_fastmutexattr_t*)attr);
}
extern "C" int cfast_mutex_destroy(cfast_mutex_t *mutex) {
int ret = fast_mutex_destroy(*mutex);
extern "C" int cfibre_fastmutex_destroy(cfibre_fastmutex_t *mutex) {
int ret = fibre_fastmutex_destroy(*mutex);
delete *mutex;
*mutex = nullptr;
return ret;
}
extern "C" int cfast_mutex_lock(cfast_mutex_t *mutex) {
return fast_mutex_lock(*mutex);
}
extern "C" int cfast_mutex_trylock(cfast_mutex_t *mutex) {
return fast_mutex_trylock(*mutex);
}
extern "C" int cfast_mutex_unlock(cfast_mutex_t *mutex) {
return fast_mutex_unlock(*mutex);
}
extern "C" int cfast_cond_init(cfast_cond_t *restrict cond, const cfast_condattr_t *restrict attr) {
*cond = (cfast_cond_t)new fast_cond_t;
return fast_cond_init(*cond, (fast_condattr_t*)attr);
}
extern "C" int cfast_cond_destroy(cfast_cond_t *cond) {
int ret = fast_cond_destroy(*cond);
delete *cond;
*cond = nullptr;
return ret;
extern "C" int cfibre_fastmutex_lock(cfibre_fastmutex_t *mutex) {
return fibre_fastmutex_lock(*mutex);
}
extern "C" int cfast_cond_wait(cfast_cond_t *restrict cond, cfast_mutex_t *restrict mutex) {
return fast_cond_wait(*cond, *mutex);
extern "C" int cfibre_fastmutex_trylock(cfibre_fastmutex_t *mutex) {
return fibre_fastmutex_trylock(*mutex);
}
extern "C" int cfast_cond_timedwait(cfast_cond_t *restrict cond, cfast_mutex_t *restrict mutex, const struct timespec *restrict abstime) {
return fast_cond_timedwait(*cond, *mutex, abstime);
extern "C" int cfibre_fastmutex_unlock(cfibre_fastmutex_t *mutex) {
return fibre_fastmutex_unlock(*mutex);
}
extern "C" int cfast_cond_signal(cfast_cond_t *cond) {
return fast_cond_signal(*cond);
extern "C" int cfibre_fastcond_wait(cfibre_cond_t *restrict cond, cfibre_fastmutex_t *restrict mutex) {
return fibre_fastcond_wait(*cond, *mutex);
}
extern "C" int cfast_cond_broadcast(cfast_cond_t *cond) {
return fast_cond_broadcast(*cond);
extern "C" int cfibre_fastcond_timedwait(cfibre_cond_t *restrict cond, cfibre_fastmutex_t *restrict mutex, const struct timespec *restrict abstime) {
return fibre_fastcond_timedwait(*cond, *mutex, abstime);
}
extern "C" int cfibre_socket(int domain, int type, int protocol) {
......
......@@ -27,24 +27,23 @@
#include <sys/socket.h> // socket types
#include <pthread.h>
typedef struct _cfibre_t* cfibre_t;
typedef struct _cfibre_sem_t* cfibre_sem_t;
typedef struct _cfibre_mutex_t* cfibre_mutex_t;
typedef struct _cfibre_cond_t* cfibre_cond_t;
typedef struct _cfibre_rwlock_t* cfibre_rwlock_t;
typedef struct _cfibre_barrier_t* cfibre_barrier_t;
typedef struct _cfibre_t* cfibre_t;
typedef struct _cfibre_sem_t* cfibre_sem_t;
typedef struct _cfibre_mutex_t* cfibre_mutex_t;
typedef struct _cfibre_cond_t* cfibre_cond_t;
typedef struct _cfibre_rwlock_t* cfibre_rwlock_t;
typedef struct _cfibre_barrier_t* cfibre_barrier_t;
typedef struct _cfibre_attr_t* cfibre_attr_t;
typedef struct _cfibre_mutexattr_t* cfibre_mutexattr_t;
typedef struct _cfibre_condattr_t* cfibre_condattr_t;
typedef struct _cfibre_rwlockattr_t* cfibre_rwlockattr_t;
typedef struct _cfibre_barrierattr_t* cfibre_barrierattr_t;
typedef struct _cfast_mutex_t* cfast_mutex_t;
typedef struct _cfast_cond_t* cfast_cond_t;
typedef struct _cfast_mutexattr_t* cfast_mutexattr_t;
typedef struct _cfast_condattr_t* cfast_condattr_t;
typedef struct _cfibre_fastmutex_t* cfibre_fastmutex_t;
typedef struct _cfibre_fastmutexattr_t* cfibre_fastmutexattr_t;
typedef struct _cfibre_cluster_t* cfibre_cluster_t;
typedef struct _cfibre_cluster_t* cfibre_cluster_t;
#ifdef __cplusplus
extern "C" {
......@@ -128,18 +127,14 @@ int cfibre_barrier_init(cfibre_barrier_t *restrict barrier, const cfibre_barrier
int cfibre_barrier_destroy(cfibre_barrier_t *barrier);
int cfibre_barrier_wait(cfibre_barrier_t *barrier);
int cfast_mutex_init(cfast_mutex_t *restrict mutex, const cfast_mutexattr_t *restrict attr);
int cfast_mutex_destroy(cfast_mutex_t *mutex);
int cfast_mutex_lock(cfast_mutex_t *mutex);
int cfast_mutex_trylock(cfast_mutex_t *mutex);
int cfast_mutex_unlock(cfast_mutex_t *mutex);
int cfast_cond_init(cfast_cond_t *restrict cond, const cfast_condattr_t *restrict attr);
int cfast_cond_destroy(cfast_cond_t *cond);
int cfast_cond_wait(cfast_cond_t *restrict cond, cfast_mutex_t *restrict mutex);
int cfast_cond_timedwait(cfast_cond_t *restrict cond, cfast_mutex_t *restrict mutex, const struct timespec *restrict abstime);
int cfast_cond_signal(cfast_cond_t *cond);
int cfast_cond_broadcast(cfast_cond_t *cond);
int cfibre_fastmutex_init(cfibre_fastmutex_t *restrict mutex, const cfibre_fastmutexattr_t *restrict attr);
int cfibre_fastmutex_destroy(cfibre_fastmutex_t *mutex);
int cfibre_fastmutex_lock(cfibre_fastmutex_t *mutex);
int cfibre_fastmutex_trylock(cfibre_fastmutex_t *mutex);
int cfibre_fastmutex_unlock(cfibre_fastmutex_t *mutex);
int cfibre_fastcond_wait(cfibre_cond_t *restrict cond, cfibre_fastmutex_t *restrict mutex);
int cfibre_fastcond_timedwait(cfibre_cond_t *restrict cond, cfibre_fastmutex_t *restrict mutex, const struct timespec *restrict abstime);
/** @brief Create socket. (`socket`). */
int cfibre_socket(int domain, int type, int protocol);
......
......@@ -32,11 +32,11 @@ static struct _Bootstrapper {
// EventScope.h pulls in everything else
#include "libfibre/EventScope.h"
typedef TaskLock FibreMutex;
typedef TaskCondition FibreCondition;
typedef TaskSemaphore FibreSemaphore;
typedef TaskLockRW FibreLockRW;
typedef TaskBarrier FibreBarrier;
typedef Mutex<WorkerLock> FibreMutex;
typedef Condition<> FibreCondition;
typedef Semaphore<WorkerLock,false> FibreSemaphore;
typedef LockRW<WorkerLock> FibreLockRW;
typedef Barrier<WorkerLock> FibreBarrier;
typedef Fibre* fibre_t;
typedef FibreMutex fibre_mutex_t;
......@@ -45,8 +45,7 @@ typedef FibreSemaphore fibre_sem_t;
typedef FibreLockRW fibre_rwlock_t;
typedef FibreBarrier fibre_barrier_t;
typedef FastMutex fast_mutex_t;
typedef Condition<> fast_cond_t;
typedef FastMutex fibre_fastmutex_t;
struct fibre_attr_t {
size_t stackSize;
......@@ -66,8 +65,7 @@ struct fibre_condattr_t {};
struct fibre_rwlockattr_t {};
struct fibre_barrierattr_t {};
struct fast_mutexattr_t {};
struct fast_condattr_t {};
struct fibre_fastmutexattr_t {};
#ifdef __GNUC__
#define restrict __restrict__
......@@ -353,70 +351,47 @@ inline int fibre_barrier_wait(fibre_barrier_t *barrier) {
}
/** @brief Initialize mutex lock. (`pthread_mutex_init`) */
inline int fast_mutex_init(fast_mutex_t *restrict mutex, const fast_mutexattr_t *restrict attr) {
inline int fibre_fastmutex_init(fibre_fastmutex_t *restrict mutex, const fibre_fastmutexattr_t *restrict attr) {
RASSERT0(attr == nullptr);
return 0;
}
/** @brief Destroy mutex lock. (`pthread_mutex_destroy`) */
inline int fast_mutex_destroy(fast_mutex_t *mutex) {
inline int fibre_fastmutex_destroy(fibre_fastmutex_t *mutex) {
return 0;
}
/** @brief Acquire mutex lock. Block, if necessary. (`pthread_mutex_lock`) */
inline int fast_mutex_lock(fast_mutex_t *mutex) {
inline int fibre_fastmutex_lock(fibre_fastmutex_t *mutex) {
mutex->acquire();
return 0;
}
/** @brief Perform non-blocking attempt to acquire mutex lock. (`pthread_mutex_trylock`) */
inline int fast_mutex_trylock(fast_mutex_t *mutex) {
inline int fibre_fastmutex_trylock(fibre_fastmutex_t *mutex) {
return mutex->tryAcquire() ? 0 : EBUSY;
}
/** @brief Release mutex lock. Block, if necessary. (`pthread_mutex_unlock`) */
inline int fast_mutex_unlock(fast_mutex_t *mutex) {
inline int fibre_fastmutex_unlock(fibre_fastmutex_t *mutex) {
mutex->release();
return 0;
}
/** @brief Initialize condition variable. (`pthread_cond_init`) */
inline int fast_cond_init(fast_cond_t *restrict cond, const fast_condattr_t *restrict attr) {
RASSERT0(attr == nullptr);
return 0;
}
/** @brief Destroy condition variable. (`pthread_cond_init`) */
inline int fast_cond_destroy(fast_cond_t *cond) {
return 0;
}
/** @brief Wait on condition variable. (`pthread_cond_wait`) */
inline int fast_cond_wait(fast_cond_t *restrict cond, fast_mutex_t *restrict mutex) {
/** @brief Wait on condition variable using fast mutex. (`pthread_cond_wait`) */
inline int fibre_fastcond_wait(fibre_cond_t *restrict cond, fibre_fastmutex_t *restrict mutex) {
cond->wait(*mutex);
mutex->acquire();
return 0;
}
/** @brief Perform wait attempt on condition variable with timeout. (`pthread_cond_timedwait`) */
inline int fast_cond_timedwait(fast_cond_t *restrict cond, fast_mutex_t *restrict mutex, const struct timespec *restrict abstime) {
/** @brief Perform wait attempt on condition variable with timeout using fast mutex. (`pthread_cond_timedwait`) */
inline int fibre_fastcond_timedwait(fibre_cond_t *restrict cond, fibre_fastmutex_t *restrict mutex, const struct timespec *restrict abstime) {
int retcode = cond->wait(*mutex, *abstime) ? 0 : ETIMEDOUT;
mutex->acquire();
return retcode;
}
/** @brief Signal one waiting fibre on condition variable. (`pthread_cond_signal`) */
inline int fast_cond_signal(fast_cond_t *cond) {
cond->signal();
return 0;
}
/** @brief Signal all waiting fibres on condition variable. (`pthread_cond_broadcast`) */
inline int fast_cond_broadcast(fast_cond_t *cond) {
cond->signal<true>();
return 0;
}
#endif /* _fibre_h_ */
/**
......
......@@ -390,14 +390,14 @@ public:
void release() { return V(); }
};
template<typename Lock, bool OwnerLock, bool Fifo, typename BQ = BlockingQueue>
template<typename Lock, bool Fifo, typename BQ = BlockingQueue>
class LockedMutex {
Lock lock;
StackContext* owner;
BQ bq;
protected:
template<typename... Args>
template<bool OwnerLock, typename... Args>
bool internalAcquire(const Args&... args) {
StackContext* cs = Context::CurrStack();
if (OwnerLock && cs == owner) return true;
......@@ -420,8 +420,8 @@ public:
bool test() const { return owner != nullptr; }
template<typename... Args>
bool acquire(const Args&... args) { return internalAcquire(args...); }
bool tryAcquire() { return acquire(false); }
bool acquire(const Args&... args) { return internalAcquire<false>(args...); }
bool tryAcquire() { return acquire<false>(false); }
void release() {
ScopedLock<Lock> al(lock);
......@@ -435,7 +435,7 @@ public:
}
};
template<typename Semaphore, bool OwnerLock, size_t SpinStart, size_t SpinEnd, size_t SpinCount>
template<typename Semaphore, size_t SpinStart, size_t SpinEnd, size_t SpinCount>
class SpinMutex {
StackContext* owner;
Semaphore sem;
......@@ -452,7 +452,7 @@ class SpinMutex {
}
protected:
template<typename... Args>
template<bool OwnerLock, typename... Args>
bool internalAcquire(const Args&... args) {
StackContext* cs = Context::CurrStack();
if (OwnerLock && cs == owner) return true;
......@@ -479,7 +479,7 @@ public:
bool test() const { return owner != nullptr; }
template<typename... Args>
bool acquire(const Args&... args) { return internalAcquire(args...); }
bool acquire(const Args&... args) { return internalAcquire<false>(args...); }
bool tryAcquire() { return acquire(false); }
void release() {
......@@ -499,7 +499,7 @@ public:
template<typename... Args>
size_t acquire(const Args&... args) {
if (BaseMutex::internalAcquire(args...)) return ++counter; else return 0;
if (BaseMutex::template internalAcquire<true>(args...)) return ++counter; else return 0;
}
size_t tryAcquire() { return acquire(false); }
......@@ -510,21 +510,21 @@ public:
}
};
template<typename Lock, bool OwnerLock = false>
template<typename Lock>
#if TESTING_MUTEX_FIFO
class Mutex : public LockedMutex<Lock, OwnerLock, true> {};
class Mutex : public LockedMutex<Lock, true> {};
#elif TESTING_MUTEX_BARGING
class Mutex : public LockedMutex<Lock, OwnerLock, false> {};
class Mutex : public LockedMutex<Lock, false> {};
#elif TESTING_MUTEX_SPIN
class Mutex : public SpinMutex<Semaphore<Lock, true>, OwnerLock, 4, 1024, 16> {};
class Mutex : public SpinMutex<Semaphore<Lock, true>, 4, 1024, 16> {};
#else
class Mutex : public SpinMutex<Semaphore<Lock, true>, OwnerLock, 0, 0, 0> {};
class Mutex : public SpinMutex<Semaphore<Lock, true>, 0, 0, 0> {};
#endif
#if TESTING_MUTEX_SPIN
typedef SpinMutex<BinaryBenaphore<LimitedSemaphore>,false, 4, 1024, 16> FastMutex;
typedef SpinMutex<BinaryBenaphore<LimitedSemaphore>, 4, 1024, 16> FastMutex;
#else
typedef SpinMutex<BinaryBenaphore<LimitedSemaphore>,false, 0, 0, 0> FastMutex;
typedef SpinMutex<BinaryBenaphore<LimitedSemaphore>, 0, 0, 0> FastMutex;
#endif
// simple blocking RW lock: release alternates; new readers block when writer waits -> no starvation
......@@ -721,11 +721,4 @@ public:
void detach() { ScopedLock<Lock> al(lock); Baseclass::detach(); }
};
typedef Mutex<WorkerLock> TaskLock;
typedef Condition<> TaskCondition;
typedef Semaphore<WorkerLock,false> TaskSemaphore;
typedef Semaphore<WorkerLock,true> TaskBinarySemaphore;
typedef LockRW<WorkerLock> TaskLockRW;
typedef Barrier<WorkerLock> TaskBarrier;
#endif /* _BlockingSync_h_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment