Skip to content

Commit

Permalink
Use pthread mutex instead of semaphore
Browse files Browse the repository at this point in the history
(the latter need -lpthread on modern Linux anyway)
  • Loading branch information
attipaci committed Jan 16, 2025
1 parent 454ded6 commit 21a52dd
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ include config.mk
# The version of the shared .so libraries
SO_VERSION := 1

# Link with math libs (NAN) and pthread (mutex)
LDFLAGS += -lm -lpthread

# Check if there is a doxygen we can run
ifndef DOXYGEN
DOXYGEN := $(shell which doxygen)
Expand Down Expand Up @@ -90,6 +93,7 @@ SOURCES = $(SRC)/xchange.c $(SRC)/xstruct.c $(SRC)/xlookup.c $(SRC)/xjson.c
# Generate a list of object (obj/*.o) files from the input sources
OBJECTS := $(subst .c,.o,$(subst $(SRC),$(OBJ),$(SOURCES)))


$(LIB)/libxchange.so: $(LIB)/libxchange.so.$(SO_VERSION)

# Shared library
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ prior to invoking `make`. The following build variables can be configured:

- `WEXTRA`: If set to 1, `-Wextra` is added to `CFLAGS` automatically.

- `LDFLAGS`: Extra linker flags (default: _not set_). Note, `-lm` will be added automatically.
- `LDFLAGS`: Extra linker flags (default: _not set_). Note, `-lm -lpthread` will be added automatically.

- `CHECKEXTRA`: Extra options to pass to `cppcheck` for the `make check` target

Expand Down
3 changes: 0 additions & 3 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ CHECKOPTS ?= --enable=performance,warning,portability,style --language=c \
# Below are some generated constants based on the one that were set above
# ============================================================================

# Link against math libs (for e.g. isnan())
LDFLAGS += -lm

# Search for files in the designated locations
vpath %.h $(INC)
vpath %.c $(SRC)
Expand Down
28 changes: 14 additions & 14 deletions src/xlookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <semaphore.h>
#include <pthread.h>

#define __XCHANGE_INTERNAL_API__ ///< Use internal definitions
#include "xchange.h"
Expand All @@ -29,7 +29,7 @@ typedef struct {
XLookupEntry **table;
int nBins;
int nEntries;
sem_t sem;
pthread_mutex_t mutex;
} XLookupPrivate;

/// \endcond
Expand Down Expand Up @@ -148,10 +148,10 @@ int xLookupPut(XLookupTable *tab, const char *prefix, const XField *field, XFiel
p = (XLookupPrivate *) tab->priv;
if(!p) return x_error(0, EINVAL, "xGetLookupEntryAsync", "lookup table not initialized");

if(sem_wait(&p->sem) != 0) return x_error(X_FAILURE, errno, fn, "sem_wait() error");
if(pthread_mutex_lock(&p->mutex) != 0) return x_error(X_FAILURE, errno, fn, "sem_wait() error");

res = xLookupPutAsync(tab, prefix, field, oldValue);
sem_post(&p->sem);
pthread_mutex_unlock(&p->mutex);

return res;
}
Expand Down Expand Up @@ -183,13 +183,13 @@ XField *xLookupRemove(XLookupTable *tab, const char *id) {
return NULL;
}

if(sem_wait(&p->sem) != 0) {
if(pthread_mutex_lock(&p->mutex) != 0) {
x_error(0, errno, fn, "sem_wait() error");
return NULL;
}

f = xLookupRemoveAsync(tab, id);
sem_post(&p->sem);
pthread_mutex_unlock(&p->mutex);

return f;
}
Expand Down Expand Up @@ -295,10 +295,10 @@ int xLookupPutAll(XLookupTable *tab, const char *prefix, const XStructure *s, bo
p = (XLookupPrivate *) tab->priv;
if(!p) return x_error(0, EINVAL, "xGetLookupEntryAsync", "lookup table not initialized");

if(sem_wait(&p->sem) != 0) return x_error(X_FAILURE, errno, fn, "sem_wait() error");
if(pthread_mutex_lock(&p->mutex) != 0) return x_error(X_FAILURE, errno, fn, "sem_wait() error");

n = xLookupPutAllAsync(tab, prefix, s, recursive);
sem_post(&p->sem);
pthread_mutex_unlock(&p->mutex);

prop_error(fn, n);
return n;
Expand Down Expand Up @@ -330,10 +330,10 @@ int xLookupRemoveAll(XLookupTable *tab, const char *prefix, const XStructure *s,
p = (XLookupPrivate *) tab->priv;
if(!p) return x_error(0, EINVAL, "xGetLookupEntryAsync", "lookup table not initialized");

if(sem_wait(&p->sem) != 0) return x_error(X_FAILURE, errno, fn, "sem_wait() error");
if(pthread_mutex_lock(&p->mutex) != 0) return x_error(X_FAILURE, errno, fn, "sem_wait() error");

n = xLookupRemoveAllAsync(tab, prefix, s, recursive);
sem_post(&p->sem);
pthread_mutex_unlock(&p->mutex);

prop_error(fn, n);
return n;
Expand Down Expand Up @@ -371,7 +371,7 @@ XLookupTable *xAllocLookup(unsigned int size) {
x_check_alloc(p->table);

p->nBins = n;
sem_init(&p->sem, FALSE, 1);
pthread_mutex_init(&p->mutex, NULL);

tab = (XLookupTable *) calloc(1, sizeof(XLookupTable));
x_check_alloc(tab);
Expand Down Expand Up @@ -453,13 +453,13 @@ XField *xLookupField(const XLookupTable *tab, const char *id) {
return NULL;
}

if(sem_wait(&p->sem) != 0) {
if(pthread_mutex_lock(&p->mutex) != 0) {
x_error(0, errno, fn, "sem_wait() error");
return NULL;
}

e = xGetLookupEntryAsync(tab, id, xGetHash(id));
sem_post(&p->sem);
pthread_mutex_unlock(&p->mutex);

return e ? e->field : NULL;
}
Expand Down Expand Up @@ -495,7 +495,7 @@ void xDestroyLookup(XLookupTable *tab) {
}

free(p->table);
sem_destroy(&p->sem);
pthread_mutex_destroy(&p->mutex);
}

free(tab);
Expand Down
2 changes: 2 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
INC = ../include
BUILD_MODE = debug

LDFLAGS += -lm

# Load the common Makefile definitions...
include ../config.mk

Expand Down

0 comments on commit 21a52dd

Please sign in to comment.