Skip to content

Commit

Permalink
Implement new locking method based around FreeBSD futex-like
Browse files Browse the repository at this point in the history
_umtx_op(2) locking primitives, which allows for process-shared
kernel-assisted mutexes.
  • Loading branch information
sobomax committed Mar 8, 2017
1 parent 4f835cc commit 4fe5237
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile.conf.template
Expand Up @@ -84,6 +84,7 @@ DEFS+= -DDBG_MALLOC #Enables debugging for memory allocators
#DEFS+= -DUSE_FUTEX #Uses linux futexs with fast architecture specific locking
#DEFS+= -DUSE_SYSV_SEM #Uses SYSV sems for locking ( slower & limited number of locks
#DEFS+= -DUSE_PTHREAD_MUTEX #Uses pthread mutexes for locking
#DEFS+= -DUSE_UMUTEX_MUTEX #Uses FreeBSD-specific low-level mutexes for locking
#DEFS+= -DUSE_POSIX_SEM #Uses POSIX sems for locking
#DEFS+= -DBUSY_WAIT #Uses busy waiting on the lock
#DEFS+= -DDBG_LOCK #Attach debug info to all lock structures
Expand Down
5 changes: 5 additions & 0 deletions Makefile.defs
Expand Up @@ -752,6 +752,7 @@ ifeq ($(ARCH), alpha)
endif

# decide on the locking type
ifeq (,$(findstring USE_UMUTEX_MUTEX, $(DEFS)))
ifeq (,$(findstring USE_PTHREAD_MUTEX, $(DEFS)))
ifeq (,$(findstring USE_POSIX_SEM, $(DEFS)))
ifeq (,$(findstring USE_SYSV_SEM, $(DEFS)))
Expand All @@ -771,6 +772,10 @@ else
## USE_PTHREAD_MUTEX was manually forced from .conf
found_lock_method=yes
endif
else
## USE_UMUTEX_MUTEX was manually forced from .conf
found_lock_method=yes
endif

CFLAGS=$(shell echo "$${CFLAGS}")
LDFLAGS+=
Expand Down
2 changes: 1 addition & 1 deletion lock_alloc.h
Expand Up @@ -54,7 +54,7 @@
#include "mem/mem.h"
#include "mem/shm_mem.h"

#if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM)
#if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM) || defined(USE_UMUTEX_MUTEX)
/* simple locks*/
#define lock_alloc() shm_malloc(sizeof(gen_lock_t))
#define lock_dealloc(lock) shm_free((void*)lock)
Expand Down
51 changes: 50 additions & 1 deletion lock_ops.h
Expand Up @@ -127,6 +127,55 @@ inline static gen_lock_t* lock_init(gen_lock_t* lock)
#define lock_get(lock) pthread_mutex_lock(lock)
#define lock_release(lock) pthread_mutex_unlock(lock)

#elif defined USE_UMUTEX_MUTEX
# if !defined(USE_UMUTEX_MUTEX_DECL)
# define USE_UMUTEX_MUTEX_DECL 1
#include <sys/types.h>

#include <errno.h>
#include <string.h>

#define u_long unsigned long
#include <sys/umtx.h>

typedef struct umutex gen_lock_t;

#define lock_destroy(lock) /* do nothing */

inline static int
_umtx_op_err(void *obj, int op, u_long val, void *uaddr, void *uaddr2)
{

if (_umtx_op(obj, op, val, uaddr, uaddr2) == -1)
return (errno);
return (0);
}

inline static gen_lock_t *
lock_init(gen_lock_t *lock)
{

memset(lock, '\0', sizeof(gen_lock_t));
lock->m_flags = USYNC_PROCESS_SHARED;

return (lock);
}

inline static int
lock_get(gen_lock_t *lock)
{

return (_umtx_op_err(lock, UMTX_OP_MUTEX_LOCK, 0, 0, 0));
}

inline static int
lock_release(gen_lock_t *lock)
{

return (_umtx_op_err(lock, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0));
}

# endif /* USE_UMUTEX_MUTEX_DECL */
#elif defined USE_POSIX_SEM
#include <semaphore.h>

Expand Down Expand Up @@ -248,7 +297,7 @@ inline static void lock_release(gen_lock_t* lock)

/* lock sets */

#if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM)
#if defined(FAST_LOCK) || defined(USE_PTHREAD_MUTEX) || defined(USE_POSIX_SEM) || defined(USE_UMUTEX_MUTEX)
#define GEN_LOCK_T_PREFERED

struct gen_lock_set_t_ {
Expand Down
9 changes: 8 additions & 1 deletion version.h
Expand Up @@ -153,6 +153,12 @@
#define USE_PTHREAD_MUTEX_STR ""
#endif

#ifdef USE_UMUTEX_MUTEX
#define USE_UMUTEX_MUTEX_STR ", USE_UMUTEX_MUTEX"
#else
#define USE_UMUTEX_MUTEX_STR ""
#endif

#ifdef USE_POSIX_SEM
#define USE_POSIX_SEM_STR ", USE_POSIX_SEM"
#else
Expand Down Expand Up @@ -183,7 +189,8 @@
SHM_MMAP_STR PKG_MALLOC_STR VQ_MALLOC_STR QM_MALLOC_STR F_MALLOC_STR \
HP_MALLOC_STR USE_SHM_MEM_STR DBG_MALLOC_STR \
DEBUG_DMALLOC_STR QM_JOIN_FREE_STR FAST_LOCK_STR NOSMP_STR \
USE_PTHREAD_MUTEX_STR USE_POSIX_SEM_STR USE_SYSV_SEM_STR DBG_LOCK_STR
USE_PTHREAD_MUTEX_STR USE_UMUTEX_MUTEX_STR USE_POSIX_SEM_STR \
USE_SYSV_SEM_STR DBG_LOCK_STR


#endif

0 comments on commit 4fe5237

Please sign in to comment.