Skip to content

Commit

Permalink
Spinlocks can be replaced by mutexes if not supported.
Browse files Browse the repository at this point in the history
  • Loading branch information
m.petschow committed Nov 23, 2010
1 parent 39eb1f5 commit d456606
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 14 deletions.
4 changes: 4 additions & 0 deletions INCLUDE/counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@

typedef struct {
int value;
#ifdef NOSPINLOCKS
pthread_mutex_t lock;
#else
pthread_spinlock_t lock;
#endif
} counter_t;

counter_t *PMR_create_counter(int init_value);
Expand Down
4 changes: 4 additions & 0 deletions INCLUDE/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ typedef struct {
int num_tasks;
task_t *head;
task_t *back;
#ifdef NOSPINLOCKS
pthread_mutex_t lock;
#else
pthread_spinlock_t lock;
#endif
} queue_t;


Expand Down
10 changes: 7 additions & 3 deletions INSTALL/make.inc.gnu
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ ARFLAGS = rcs
# to 0 by default
COMPLEX_SUPPORT = 0

# Compile routines without adding necessary LAPACK routines
# to 'libmrrr.a'; default = 0
NOLAPACK = 0
# To build 'libmrrr.a' without adding necessary LAPACK routines
# for the to the archive set value to 0; default value is 1
INCLAPACK = 1

# On some systems 'spinlocks' are not supported, therefore
# here the flag to use 'mutexes' instead; default value is 1
SPINLOCK_SUPPORT = 1
10 changes: 7 additions & 3 deletions INSTALL/make.inc.ibm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ ARFLAGS = rcs
# to 0 by default
COMPLEX_SUPPORT = 0

# Compile routines without adding necessary LAPACK routines
# to 'libmrrr.a'; default = 0
NOLAPACK = 0
# To build 'libmrrr.a' without adding necessary LAPACK routines
# for the to the archive set value to 0; default value is 1
INCLAPACK = 1

# On some systems 'spinlocks' are not supported, therefore
# here the flag to use 'mutexes' instead; default value is 1
SPINLOCK_SUPPORT = 1
12 changes: 8 additions & 4 deletions INSTALL/make.inc.intel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FC = ifort

# Compiler flags
CFLAGS = -pthread -O3 -restrict
FFLAGS = -nofor-main -O3
FFLAGS = -nofor-main -O3 -assume underscore

# Archiver and flags used when building the archive
AR = /usr/bin/ar
Expand All @@ -16,6 +16,10 @@ ARFLAGS = rcs
# to 0 by default
COMPLEX_SUPPORT = 0

# Compile routines without adding necessary LAPACK routines
# to 'libmrrr.a'; default = 0
NOLAPACK = 0
# To build 'libmrrr.a' without adding necessary LAPACK routines
# for the to the archive set value to 0; default value is 1
INCLAPACK = 1

# On some systems 'spinlocks' are not supported, therefore
# here the flag to use 'mutexes' instead; default value is 1
SPINLOCK_SUPPORT = 1
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ ifeq ($(COMPLEX_SUPPORT),1)
CFLAGS += -DCOMPLEX_SUPPORTED
endif

ifeq ($(SPINLOCK_SUPPORT),0)
CFLAGS += -DNOSPINLOCKS
endif

# Source files
HEADERS := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.h))
CSRCS := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.c))
COBJS = $(CSRCS:.c=.o)

ifeq ($(NOLAPACK),1)
ifeq ($(INCLAPACK),0)
FSRCS := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/odscal.f))
FOBJS = $(FSRCS:.f=.o)
else
Expand Down
32 changes: 32 additions & 0 deletions SRC/counter.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ counter_t *PMR_create_counter(int init_value)
counter = (counter_t *) malloc( sizeof(counter_t) );

counter->value = init_value;
#ifdef NOSPINLOCKS
info = pthread_mutex_init(&counter->lock, NULL);
#else
info = pthread_spin_init(&counter->lock, PTHREAD_PROCESS_PRIVATE);
#endif
assert(info == 0);

return(counter);
Expand All @@ -69,7 +73,11 @@ counter_t *PMR_create_counter(int init_value)

inline void PMR_destroy_counter(counter_t *counter)
{
#ifdef NOSPINLOCKS
pthread_mutex_destroy(&counter->lock);
#else
pthread_spin_destroy(&counter->lock);
#endif
free(counter);
}

Expand All @@ -79,12 +87,20 @@ inline int PMR_get_counter_value(counter_t *counter)
{
int value, info;

#ifdef NOSPINLOCKS
info = pthread_mutex_lock(&counter->lock);
#else
info = pthread_spin_lock(&counter->lock);
#endif
assert(info == 0);

value = counter->value;

#ifdef NOSPINLOCKS
info = pthread_mutex_unlock(&counter->lock);
#else
info = pthread_spin_unlock(&counter->lock);
#endif
assert(info == 0);

return(value);
Expand All @@ -96,12 +112,20 @@ inline int PMR_set_counter_value(counter_t *counter, int value)
{
int info;

#ifdef NOSPINLOCKS
info = pthread_mutex_lock(&counter->lock);
#else
info = pthread_spin_lock(&counter->lock);
#endif
assert(info == 0);

counter->value = value;

#ifdef NOSPINLOCKS
info = pthread_mutex_unlock(&counter->lock);
#else
info = pthread_spin_unlock(&counter->lock);
#endif
assert(info == 0);

return(value);
Expand All @@ -113,13 +137,21 @@ inline int PMR_decrement_counter(counter_t *counter, int amount)
{
int value, info;

#ifdef NOSPINLOCKS
info = pthread_mutex_lock(&counter->lock);
#else
info = pthread_spin_lock(&counter->lock);
#endif
assert(info == 0);

counter->value -= amount;
value = counter->value;

#ifdef NOSPINLOCKS
info = pthread_mutex_unlock(&counter->lock);
#else
info = pthread_spin_unlock(&counter->lock);
#endif
assert(info == 0);

return(value);
Expand Down
40 changes: 40 additions & 0 deletions SRC/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ queue_t *PMR_create_empty_queue(void)
queue->head = NULL;
queue->back = NULL;

#ifdef NOSPINLOCKS
info = pthread_mutex_init(&queue->lock, NULL);
#else
info = pthread_spin_init(&queue->lock, PTHREAD_PROCESS_PRIVATE);
#endif
assert(info == 0);

return(queue);
Expand All @@ -75,7 +79,11 @@ queue_t *PMR_create_empty_queue(void)

void PMR_destroy_queue(queue_t *queue)
{
#ifdef NOSPINLOCKS
pthread_mutex_destroy(&queue->lock);
#else
pthread_spin_destroy(&queue->lock);
#endif
free(queue);
}

Expand All @@ -85,12 +93,20 @@ int PMR_get_num_tasks(queue_t *queue)
{
int info, num_tasks;

#ifdef NOSPINLOCKS
info = pthread_mutex_lock(&queue->lock);
#else
info = pthread_spin_lock(&queue->lock);
#endif
assert(info == 0);

num_tasks = queue->num_tasks;

#ifdef NOSPINLOCKS
info = pthread_mutex_unlock(&queue->lock);
#else
info = pthread_spin_unlock(&queue->lock);
#endif
assert(info == 0);

return(num_tasks);
Expand Down Expand Up @@ -126,7 +142,11 @@ int PMR_insert_task_at_back(queue_t *queue, task_t *task)
{
int info;

#ifdef NOSPINLOCKS
info = pthread_mutex_lock(&queue->lock);
#else
info = pthread_spin_lock(&queue->lock);
#endif
assert(info == 0);

queue->num_tasks++;
Expand All @@ -139,7 +159,11 @@ int PMR_insert_task_at_back(queue_t *queue, task_t *task)
queue->back->next = task;
queue->back = task;

#ifdef NOSPINLOCKS
info = pthread_mutex_unlock(&queue->lock);
#else
info = pthread_spin_unlock(&queue->lock);
#endif
assert(info == 0);

return(info);
Expand All @@ -152,7 +176,11 @@ task_t *PMR_remove_task_at_front(queue_t *queue)
int info;
task_t *task;

#ifdef NOSPINLOCKS
info = pthread_mutex_lock(&queue->lock);
#else
info = pthread_spin_lock(&queue->lock);
#endif
assert(info == 0);

task = queue->head;
Expand All @@ -171,7 +199,11 @@ task_t *PMR_remove_task_at_front(queue_t *queue)
}
}

#ifdef NOSPINLOCKS
info = pthread_mutex_unlock(&queue->lock);
#else
info = pthread_spin_unlock(&queue->lock);
#endif
assert(info == 0);

return(task);
Expand All @@ -185,7 +217,11 @@ task_t *PMR_remove_task_at_back (queue_t *queue)
int info;
task_t *task;

#ifdef NOSPINLOCKS
info = pthread_mutex_lock(&queue->lock);
#else
info = pthread_spin_lock(&queue->lock);
#endif
assert(info == 0);

task = queue->back;
Expand All @@ -204,7 +240,11 @@ task_t *PMR_remove_task_at_back (queue_t *queue)
}
}

#ifdef NOSPINLOCKS
info = pthread_mutex_unlock(&queue->lock);
#else
info = pthread_spin_unlock(&queue->lock);
#endif
assert(info == 0);

return(task);
Expand Down
10 changes: 7 additions & 3 deletions make.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ ARFLAGS = rcs
# to 0 by default
COMPLEX_SUPPORT = 0

# Compile routines without adding necessary LAPACK routines
# to 'libmrrr.a'; default = 0
NOLAPACK = 0
# To build 'libmrrr.a' without adding necessary LAPACK routines
# for the to the archive set value to 0; default value is 1
INCLAPACK = 1

# On some systems 'spinlocks' are not supported, therefore
# here the flag to use 'mutexes' instead; default value is 1
SPINLOCK_SUPPORT = 1

0 comments on commit d456606

Please sign in to comment.