Separated the public interfaces of the threading primitives.

This commit is contained in:
Tobias Brunner
2009-12-23 17:01:53 +01:00
parent f36143b0ba
commit eba64cef41
50 changed files with 361 additions and 371 deletions
+18 -1
View File
@@ -20,13 +20,30 @@
#include <time.h>
#include <errno.h>
#include <threading.h>
#include <library.h>
#include <debug.h>
#include "condvar.h"
#include "mutex.h"
typedef struct private_condvar_t private_condvar_t;
/**
* private data of condvar
*/
struct private_condvar_t {
/**
* public functions
*/
condvar_t public;
/**
* wrapped pthread condvar
*/
pthread_cond_t condvar;
};
/**
* Implementation of condvar_t.wait.
*/
+72 -15
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2008 Tobias Brunner
* Copyright (C) 2008-2009 Tobias Brunner
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
@@ -14,26 +14,83 @@
* for more details.
*/
/**
* @defgroup condvar condvar
* @{ @ingroup threading
*/
#ifndef THREADING_CONDVAR_H_
#define THREADING_CONDVAR_H_
typedef struct private_condvar_t private_condvar_t;
typedef struct condvar_t condvar_t;
typedef enum condvar_type_t condvar_type_t;
#include "mutex.h"
/**
* private data of condvar
* Type of condvar.
*/
struct private_condvar_t {
/**
* public functions
*/
condvar_t public;
/**
* wrapped pthread condvar
*/
pthread_cond_t condvar;
enum condvar_type_t {
/** default condvar */
CONDVAR_TYPE_DEFAULT = 0,
};
#endif /* THREADING_CONDVAR_H_ */
/**
* Condvar wrapper to use in conjunction with mutex_t.
*/
struct condvar_t {
/**
* Wait on a condvar until it gets signalized.
*
* @param mutex mutex to release while waiting
*/
void (*wait)(condvar_t *this, mutex_t *mutex);
/**
* Wait on a condvar until it gets signalized, or times out.
*
* @param mutex mutex to release while waiting
* @param timeout timeout im ms
* @return TRUE if timed out, FALSE otherwise
*/
bool (*timed_wait)(condvar_t *this, mutex_t *mutex, u_int timeout);
/**
* Wait on a condvar until it gets signalized, or times out.
*
* The passed timeval should be calculated based on the time_monotonic()
* function.
*
* @param mutex mutex to release while waiting
* @param tv absolute time until timeout
* @return TRUE if timed out, FALSE otherwise
*/
bool (*timed_wait_abs)(condvar_t *this, mutex_t *mutex, timeval_t tv);
/**
* Wake up a single thread in a condvar.
*/
void (*signal)(condvar_t *this);
/**
* Wake up all threads in a condvar.
*/
void (*broadcast)(condvar_t *this);
/**
* Destroy a condvar and free its resources.
*/
void (*destroy)(condvar_t *this);
};
/**
* Create a condvar instance.
*
* @param type type of condvar to create
* @return condvar instance
*/
condvar_t *condvar_create(condvar_type_t type);
#endif /** THREADING_CONDVAR_H_ @} */
+50 -1
View File
@@ -17,13 +17,62 @@
#define _GNU_SOURCE
#include <pthread.h>
#include <threading.h>
#include <library.h>
#include <debug.h>
#include "mutex.h"
#include "lock_profiler.h"
typedef struct private_mutex_t private_mutex_t;
typedef struct private_r_mutex_t private_r_mutex_t;
/**
* private data of mutex
*/
struct private_mutex_t {
/**
* public functions
*/
mutex_t public;
/**
* wrapped pthread mutex
*/
pthread_mutex_t mutex;
/**
* is this a recursiv emutex, implementing private_r_mutex_t?
*/
bool recursive;
/**
* profiling info, if enabled
*/
lock_profile_t profile;
};
/**
* private data of mutex, extended by recursive locking information
*/
struct private_r_mutex_t {
/**
* Extends private_mutex_t
*/
private_mutex_t generic;
/**
* thread which currently owns mutex
*/
pthread_t thread;
/**
* times we have locked the lock, stored per thread
*/
pthread_key_t times;
};
/**
* Implementation of mutex_t.lock.
*/
+59 -35
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2008 Tobias Brunner
* Copyright (C) 2008-2009 Tobias Brunner
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
@@ -14,60 +14,84 @@
* for more details.
*/
/**
* @defgroup mutex mutex
* @{ @ingroup threading
*/
#ifndef THREADING_MUTEX_H_
#define THREADING_MUTEX_H_
#include "lock_profiler.h"
typedef struct mutex_t mutex_t;
typedef enum mutex_type_t mutex_type_t;
typedef struct private_mutex_t private_mutex_t;
typedef struct private_r_mutex_t private_r_mutex_t;
#include "condvar.h"
#ifdef __APPLE__
/* on Mac OS X 10.5 several system calls we use are no cancellation points.
* fortunately, select isn't one of them, so we wrap some of the others with
* calls to select(2).
*/
#include <sys/socket.h>
#include <sys/select.h>
#define WRAP_WITH_SELECT(func, socket, ...)\
fd_set rfds; FD_ZERO(&rfds); FD_SET(socket, &rfds);\
if (select(socket + 1, &rfds, NULL, NULL, NULL) <= 0) { return -1; }\
return func(socket, __VA_ARGS__)
static inline int cancellable_accept(int socket, struct sockaddr *address,
socklen_t *address_len)
{
WRAP_WITH_SELECT(accept, socket, address, address_len);
}
#define accept cancellable_accept
static inline int cancellable_recvfrom(int socket, void *buffer, size_t length,
int flags, struct sockaddr *address, socklen_t *address_len)
{
WRAP_WITH_SELECT(recvfrom, socket, buffer, length, flags, address, address_len);
}
#define recvfrom cancellable_recvfrom
#endif /* __APPLE__ */
/**
* private data of mutex
* Type of mutex.
*/
struct private_mutex_t {
/**
* public functions
*/
mutex_t public;
/**
* wrapped pthread mutex
*/
pthread_mutex_t mutex;
/**
* is this a recursiv emutex, implementing private_r_mutex_t?
*/
bool recursive;
/**
* profiling info, if enabled
*/
lock_profile_t profile;
enum mutex_type_t {
/** default mutex */
MUTEX_TYPE_DEFAULT = 0,
/** allow recursive locking of the mutex */
MUTEX_TYPE_RECURSIVE = 1,
};
/**
* private data of mutex, extended by recursive locking information
* Mutex wrapper implements simple, portable and advanced mutex functions.
*/
struct private_r_mutex_t {
struct mutex_t {
/**
* Extends private_mutex_t
* Acquire the lock to the mutex.
*/
private_mutex_t generic;
void (*lock)(mutex_t *this);
/**
* thread which currently owns mutex
* Release the lock on the mutex.
*/
pthread_t thread;
void (*unlock)(mutex_t *this);
/**
* times we have locked the lock, stored per thread
* Destroy a mutex instance.
*/
pthread_key_t times;
void (*destroy)(mutex_t *this);
};
#endif /* THREADING_MUTEX_H_ */
/**
* Create a mutex instance.
*
* @param type type of mutex to create
* @return unlocked mutex instance
*/
mutex_t *mutex_create(mutex_type_t type);
#endif /** THREADING_MUTEX_H_ @} */
+62 -1
View File
@@ -17,13 +17,74 @@
#define _GNU_SOURCE
#include <pthread.h>
#include <threading.h>
#include <library.h>
#include <debug.h>
#include "rwlock.h"
#include "condvar.h"
#include "mutex.h"
#include "lock_profiler.h"
typedef struct private_rwlock_t private_rwlock_t;
/**
* private data of rwlock
*/
struct private_rwlock_t {
/**
* public functions
*/
rwlock_t public;
#ifdef HAVE_PTHREAD_RWLOCK_INIT
/**
* wrapped pthread rwlock
*/
pthread_rwlock_t rwlock;
#else
/**
* mutex to emulate a native rwlock
*/
mutex_t *mutex;
/**
* condvar to handle writers
*/
condvar_t *writers;
/**
* condvar to handle readers
*/
condvar_t *readers;
/**
* number of waiting writers
*/
u_int waiting_writers;
/**
* number of readers holding the lock
*/
u_int reader_count;
/**
* current writer thread, if any
*/
pthread_t writer;
#endif /* HAVE_PTHREAD_RWLOCK_INIT */
/**
* profiling info, if enabled
*/
lock_profile_t profile;
};
#ifdef HAVE_PTHREAD_RWLOCK_INIT
/**
+56 -58
View File
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2008 Tobias Brunner
* Copyright (C) 2008-2009 Tobias Brunner
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
@@ -14,69 +14,67 @@
* for more details.
*/
/**
* @defgroup rwlock rwlock
* @{ @ingroup threading
*/
#ifndef THREADING_RWLOCK_H_
#define THREADING_RWLOCK_H_
#include "lock_profiler.h"
typedef struct private_rwlock_t private_rwlock_t;
typedef struct rwlock_t rwlock_t;
typedef enum rwlock_type_t rwlock_type_t;
/**
* private data of rwlock
* Type of read-write lock.
*/
struct private_rwlock_t {
/**
* public functions
*/
rwlock_t public;
#ifdef HAVE_PTHREAD_RWLOCK_INIT
/**
* wrapped pthread rwlock
*/
pthread_rwlock_t rwlock;
#else
/**
* mutex to emulate a native rwlock
*/
mutex_t *mutex;
/**
* condvar to handle writers
*/
condvar_t *writers;
/**
* condvar to handle readers
*/
condvar_t *readers;
/**
* number of waiting writers
*/
u_int waiting_writers;
/**
* number of readers holding the lock
*/
u_int reader_count;
/**
* current writer thread, if any
*/
pthread_t writer;
#endif /* HAVE_PTHREAD_RWLOCK_INIT */
/**
* profiling info, if enabled
*/
lock_profile_t profile;
enum rwlock_type_t {
/** default condvar */
RWLOCK_TYPE_DEFAULT = 0,
};
#endif /* THREADING_THREADING_H_ */
/**
* Read-Write lock wrapper.
*/
struct rwlock_t {
/**
* Acquire the read lock.
*/
void (*read_lock)(rwlock_t *this);
/**
* Acquire the write lock.
*/
void (*write_lock)(rwlock_t *this);
/**
* Try to acquire the write lock.
*
* Never blocks, but returns FALSE if the lock was already occupied.
*
* @return TRUE if lock acquired
*/
bool (*try_write_lock)(rwlock_t *this);
/**
* Release any acquired lock.
*/
void (*unlock)(rwlock_t *this);
/**
* Destroy the read-write lock.
*/
void (*destroy)(rwlock_t *this);
};
/**
* Create a read-write lock instance.
*
* @param type type of rwlock to create
* @return unlocked rwlock instance
*/
rwlock_t *rwlock_create(rwlock_type_t type);
#endif /** THREADING_RWLOCK_H_ @} */