Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lock: use rwlock by default #232

Merged
merged 2 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 38 additions & 66 deletions src/lock/lock.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* @file lock/lock.c Pthread mutex locking
* @file lock.c Pthread read/write locking
*
* Copyright (C) 2010 Creytiv.com
*/
#define _DEFAULT_SOURCE 1
#define __USE_UNIX98 1
#define _GNU_SOURCE 1
#include <pthread.h>
#include <re_types.h>
#include <re_mem.h>
Expand All @@ -16,39 +15,26 @@
#include <re_dbg.h>


#ifndef RELEASE
#define LOCK_DEBUG 0
#endif


/** Defines a lock */
struct lock {
pthread_mutex_t m;
pthread_rwlock_t lock;
};


static void lock_destructor(void *data)
{
struct lock *l = data;

int err = pthread_mutex_destroy(&l->m);
int err = pthread_rwlock_destroy(&l->lock);
if (err) {
DEBUG_WARNING("pthread_mutex_destroy: %m\n", err);
DEBUG_WARNING("pthread_rwlock_destroy: %m\n", err);
}
}


/**
* Allocate a new lock
*
* @param lp Pointer to allocated lock object
*
* @return 0 if success, otherwise errorcode
*/
int lock_alloc(struct lock **lp)
{
pthread_mutexattr_t attr;
struct lock *l;
int err;

if (!lp)
return EINVAL;
Expand All @@ -57,85 +43,71 @@ int lock_alloc(struct lock **lp)
if (!l)
return ENOMEM;

(void)pthread_mutex_init(&l->m, NULL);

pthread_mutexattr_init(&attr);

#if LOCK_DEBUG
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
DEBUG_NOTICE("init debug lock\n");
#else
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
#endif
pthread_mutex_init(&l->m, &attr);
err = pthread_rwlock_init(&l->lock, NULL);
if (err)
goto out;

*lp = l;
return 0;

out:
if (err)
mem_deref(l);
return err;
}


/**
* Get the lock for reading
*
* @param l Lock object
*/
void lock_read_get(struct lock *l)
{
const int err = pthread_mutex_lock(&l->m);
int err;

if (!l)
return;

err = pthread_rwlock_rdlock(&l->lock);
if (err) {
DEBUG_WARNING("lock_read_get: %m\n", err);
}
}


/**
* Get the lock for writing
*
* @param l Lock object
*/
void lock_write_get(struct lock *l)
{
const int err = pthread_mutex_lock(&l->m);
int err;

if (!l)
return;

err = pthread_rwlock_wrlock(&l->lock);
if (err) {
DEBUG_WARNING("lock_write_get: %m\n", err);
}
}


/**
* Attempt to get a lock for reading
*
* @param l Lock object
*
* @return 0 if success, otherwise errorcode
*/
int lock_read_try(struct lock *l)
{
return pthread_mutex_trylock(&l->m);
if (!l)
return EINVAL;
return pthread_rwlock_tryrdlock(&l->lock);
}


/**
* Attempt to get a lock for writing
*
* @param l Lock object
*
* @return 0 if success, otherwise errorcode
*/
int lock_write_try(struct lock *l)
{
return pthread_mutex_trylock(&l->m);
if (!l)
return EINVAL;
return pthread_rwlock_trywrlock(&l->lock);
}


/**
* Release a lock
*
* @param l Lock object
*/
void lock_rel(struct lock *l)
{
const int err = pthread_mutex_unlock(&l->m);
int err;

if (!l)
return;

err = pthread_rwlock_unlock(&l->lock);
if (err) {
DEBUG_WARNING("lock_rel: %m\n", err);
}
Expand Down
4 changes: 1 addition & 3 deletions src/lock/mod.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
# Copyright (C) 2010 Creytiv.com
#

ifdef HAVE_PTHREAD_RWLOCK
SRCS += lock/rwlock.c
else ifdef HAVE_PTHREAD
ifdef HAVE_PTHREAD
SRCS += lock/lock.c
else ifeq ($(OS),win32)
SRCS += lock/win32/lock.c
Expand Down
114 changes: 0 additions & 114 deletions src/lock/rwlock.c

This file was deleted.