Add spin locks for critical sections in hdb api.

git-svn-id: http://svn.fedorahosted.org/svn/corosync/trunk@2050 fd59a12c-fef9-0310-b244-a6a79926bd2f
This commit is contained in:
Steven Dake 2009-04-10 02:24:01 +00:00
parent e26aedf84f
commit 9141188ee8
8 changed files with 101 additions and 66 deletions

View File

@ -63,14 +63,7 @@ struct poll_instance {
int stop_requested;
};
/*
* All instances in one database
*/
static struct hdb_handle_database poll_instance_database = {
.handle_count = 0,
.handles = 0,
.iterator = 0
};
DECLARE_HDB_DATABASE (poll_instance_database);
hdb_handle_t poll_create (void)
{

View File

@ -98,20 +98,9 @@ static pthread_rwlock_t reload_lock;
static pthread_t lock_thread;
static pthread_mutex_t meta_lock;
static struct hdb_handle_database object_instance_database = {
.handle_count = 0,
.handles = 0,
.iterator = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER
};
static struct hdb_handle_database object_find_instance_database = {
.handle_count = 0,
.handles = 0,
.iterator = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER
};
DECLARE_HDB_DATABASE (object_instance_database);
DECLARE_HDB_DATABASE (object_find_instance_database);
static void objdb_wrlock(void)
{

View File

@ -206,15 +206,7 @@ static int totemnet_build_sockets (
static struct totem_ip_address localhost;
/*
* All instances in one database
*/
static struct hdb_handle_database totemnet_instance_database = {
.handle_count = 0,
.handles = 0,
.iterator = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER
};
DECLARE_HDB_DATABASE (totemnet_instance_database);
static void totemnet_instance_initialize (struct totemnet_instance *instance)
{

View File

@ -227,12 +227,7 @@ struct totempg_group_instance {
int groups_cnt;
};
static struct hdb_handle_database totempg_groups_instance_database = {
.handle_count = 0,
.handles = 0,
.iterator = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER
};
DECLARE_HDB_DATABASE (totempg_groups_instance_database);
static unsigned char next_fragment = 1;

View File

@ -467,13 +467,7 @@ struct rrp_algo *rrp_algos[] = {
/*
* All instances in one database
*/
static struct hdb_handle_database totemrrp_instance_database = {
.handle_count = 0,
.handles = 0,
.iterator = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER
};
DECLARE_HDB_DATABASE (totemrrp_instance_database);
#define log_printf(level, format, args...) \
do { \

View File

@ -612,12 +612,8 @@ void main_iface_change_fn (
/*
* All instances in one database
*/
static struct hdb_handle_database totemsrp_instance_database = {
.handle_count = 0,
.handles = 0,
.iterator = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER
};
DECLARE_HDB_DATABASE (totemsrp_instance_database);
struct message_handlers totemsrp_message_handlers = {
6,
{

View File

@ -60,14 +60,80 @@ struct hdb_handle_database {
unsigned int handle_count;
struct hdb_handle *handles;
unsigned int iterator;
pthread_mutex_t mutex;
#if defined(HAVE_PTHREAD_SPIN_LOCK)
pthread_spinlock_t lock;
#else
pthread_mutex_t lock;
#endif
unsigned int first_run;
};
#if defined(HAVE_PTHREAD_SPIN_LOCK)
static inline void hdb_database_lock (pthread_spinlock_t *spinlock)
{
pthread_spin_lock (spinlock);
}
static inline void hdb_database_unlock (pthread_spinlock_t *spinlock)
{
pthread_spin_unlock (spinlock);
}
static inline void hdb_database_lock_init (pthread_spinlock_t *spinlock)
{
pthread_spin_init (spinlock, 0);
}
static inline void hdb_database_lock_destroy (pthread_spinlock_t *spinlock)
{
pthread_spin_destroy (spinlock);
}
#else
static inline void hdb_database_lock (pthread_mutex_t *mutex)
{
pthread_mutex_lock (mutex);
}
static inline void hdb_database_unlock (pthread_mutex_t *mutex)
{
pthread_mutex_unlock (mutex);
}
static inline void hdb_database_lock_init (pthread_mutex_t *mutex)
{
pthread_mutex_init (mutex, NULL);
}
static inline void hdb_database_lock_destroy (pthread_mutex_t *mutex)
{
pthread_mutex_destroy (mutex);
}
#endif
#define DECLARE_HDB_DATABASE(database_name) \
static struct hdb_handle_database (database_name); \
static void database_name##_init(void)__attribute__((constructor)); \
static void database_name##_init(void) \
{ \
memset (&(database_name), 0, sizeof (struct hdb_handle_database));\
hdb_database_lock_init (&(database_name).lock); \
}
#define DECLARE_HDB_DATABASE_FIRSTRUN(database_name) \
static struct hdb_handle_database (database_name) = { \
.first_run = 1, \
}; \
static void database_name##_init(void)__attribute__((constructor)); \
static void database_name##_init(void) \
{ \
memset (&(database_name), 0, sizeof (struct hdb_handle_database));\
hdb_database_lock_init (&(database_name).lock); \
}
static inline void hdb_create (
struct hdb_handle_database *handle_database)
{
memset (handle_database, 0, sizeof (struct hdb_handle_database));
pthread_mutex_init (&handle_database->mutex, NULL);
hdb_database_lock_init (&handle_database->lock);
}
static inline void hdb_destroy (
@ -76,7 +142,7 @@ static inline void hdb_destroy (
if (handle_database->handles) {
free (handle_database->handles);
}
pthread_mutex_destroy (&handle_database->mutex);
hdb_database_lock_destroy (&handle_database->lock);
memset (handle_database, 0, sizeof (struct hdb_handle_database));
}
@ -93,7 +159,11 @@ static inline int hdb_handle_create (
void *instance;
int i;
pthread_mutex_lock (&handle_database->mutex);
if (handle_database->first_run == 1) {
memset (handle_database, 0, sizeof (struct hdb_handle_database));
hdb_database_lock_init (&handle_database->lock);
}
hdb_database_lock (&handle_database->lock);
for (handle = 0; handle < handle_database->handle_count; handle++) {
if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
@ -107,7 +177,7 @@ static inline int hdb_handle_create (
new_handles = (struct hdb_handle *)realloc (handle_database->handles,
sizeof (struct hdb_handle) * handle_database->handle_count);
if (new_handles == NULL) {
pthread_mutex_unlock (&handle_database->mutex);
hdb_database_unlock (&handle_database->lock);
return (-1);
}
handle_database->handles = new_handles;
@ -143,7 +213,7 @@ static inline int hdb_handle_create (
*handle_id_out = (((unsigned long long)(check)) << 32) | handle;
pthread_mutex_unlock (&handle_database->mutex);
hdb_database_unlock (&handle_database->lock);
return (0);
}
@ -156,23 +226,23 @@ static inline int hdb_handle_get (
unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
unsigned int handle = handle_in & 0xffffffff;
pthread_mutex_lock (&handle_database->mutex);
hdb_database_lock (&handle_database->lock);
if (check != 0xffffffff &&
check != handle_database->handles[handle].check) {
pthread_mutex_unlock (&handle_database->mutex);
hdb_database_unlock (&handle_database->lock);
return (-1);
}
*instance = NULL;
if (handle >= handle_database->handle_count) {
pthread_mutex_unlock (&handle_database->mutex);
hdb_database_unlock (&handle_database->lock);
return (-1);
}
if (handle_database->handles[handle].state != HDB_HANDLE_STATE_ACTIVE) {
pthread_mutex_unlock (&handle_database->mutex);
hdb_database_unlock (&handle_database->lock);
return (-1);
}
@ -180,7 +250,7 @@ static inline int hdb_handle_get (
handle_database->handles[handle].ref_count += 1;
pthread_mutex_unlock (&handle_database->mutex);
hdb_database_unlock (&handle_database->lock);
return (0);
}
@ -191,12 +261,12 @@ static inline int hdb_handle_put (
unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
unsigned int handle = handle_in & 0xffffffff;
pthread_mutex_lock (&handle_database->mutex);
hdb_database_lock (&handle_database->lock);
if (check != 0xffffffff &&
check != handle_database->handles[handle].check) {
pthread_mutex_unlock (&handle_database->mutex);
hdb_database_unlock (&handle_database->lock);
return (-1);
}
@ -207,7 +277,7 @@ static inline int hdb_handle_put (
free (handle_database->handles[handle].instance);
memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
}
pthread_mutex_unlock (&handle_database->mutex);
hdb_database_unlock (&handle_database->lock);
return (0);
}
@ -219,16 +289,16 @@ static inline int hdb_handle_destroy (
unsigned int handle = handle_in & 0xffffffff;
int res;
pthread_mutex_lock (&handle_database->mutex);
hdb_database_lock (&handle_database->lock);
if (check != 0xffffffff &&
check != handle_database->handles[handle].check) {
pthread_mutex_unlock (&handle_database->mutex);
hdb_database_unlock (&handle_database->lock);
return (-1);
}
handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
pthread_mutex_unlock (&handle_database->mutex);
hdb_database_unlock (&handle_database->lock);
res = hdb_handle_put (handle_database, handle);
return (res);
}

View File

@ -61,6 +61,11 @@ struct lcr_iface_instance {
void (*destructor) (void *context);
};
DECLARE_HDB_DATABASE_FIRSTRUN (lcr_component_instance_database);
DECLARE_HDB_DATABASE_FIRSTRUN (lcr_iface_instance_database);
/*
static struct hdb_handle_database lcr_component_instance_database = {
.handle_count = 0,
.handles = 0,
@ -72,6 +77,7 @@ static struct hdb_handle_database lcr_iface_instance_database = {
.handles = 0,
.iterator = 0
};
*/
static hdb_handle_t g_component_handle = 0xFFFFFFFF;