aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2015-06-18 13:46:01 +0200
committerThomas White <taw@physics.org>2015-06-18 13:47:09 +0200
commitedf2ad46f4c0f403b90d3058018a295baa9d0753 (patch)
tree059e668a733ec62f55137c8eb702075821e427d3 /src
parent14d8651e663538be7445e74cef9206ce9ab4cf36 (diff)
Replace semaphore with a mutex
A mutex (in a shared memory segment) is the correct synchronisation primitive here. I had confused myself...
Diffstat (limited to 'src')
-rw-r--r--src/im-sandbox.c35
-rw-r--r--src/process_image.c4
-rw-r--r--src/process_image.h2
3 files changed, 27 insertions, 14 deletions
diff --git a/src/im-sandbox.c b/src/im-sandbox.c
index b800f5b4..df50199c 100644
--- a/src/im-sandbox.c
+++ b/src/im-sandbox.c
@@ -3,13 +3,13 @@
*
* Sandbox for indexing
*
- * Copyright © 2012-2014 Deutsches Elektronen-Synchrotron DESY,
+ * Copyright © 2012-2015 Deutsches Elektronen-Synchrotron DESY,
* a research centre of the Helmholtz Association.
* Copyright © 2012 Richard Kirian
* Copyright © 2012 Lorenzo Galli
*
* Authors:
- * 2010-2014 Thomas White <taw@physics.org>
+ * 2010-2015 Thomas White <taw@physics.org>
* 2014 Valerio Mariani
* 2011 Richard Kirian
* 2012 Lorenzo Galli
@@ -49,7 +49,6 @@
#include <sys/stat.h>
#include <assert.h>
#include <sys/mman.h>
-#include <semaphore.h>
#ifdef HAVE_CLOCK_GETTIME
#include <time.h>
@@ -89,7 +88,7 @@ struct sb_reader
struct sb_shm
{
- sem_t term_sem;
+ pthread_mutex_t term_lock;
};
@@ -368,7 +367,7 @@ static int read_fpe_data(struct buffer_data *bd)
static void run_work(const struct index_args *iargs,
int filename_pipe, int results_pipe, Stream *st,
- int cookie, const char *tmpdir, sem_t *term_sem)
+ int cookie, const char *tmpdir, pthread_mutex_t *term_lock)
{
FILE *fh;
int allDone = 0;
@@ -503,7 +502,7 @@ static void run_work(const struct index_args *iargs,
pargs.n_crystals = 0;
process_image(iargs, &pargs, st, cookie, tmpdir,
- results_pipe, ser, term_sem);
+ results_pipe, ser, term_lock);
/* Request another image */
c = sprintf(buf, "%i\n", pargs.n_crystals);
@@ -824,7 +823,7 @@ static void start_worker_process(struct sandbox *sb, int slot)
st = open_stream_fd_for_write(stream_pipe[1]);
run_work(sb->iargs, filename_pipe[0], result_pipe[1],
- st, slot, tmp, &sb->shared->term_sem);
+ st, slot, tmp, &sb->shared->term_lock);
close_stream(st);
//close(filename_pipe[0]);
@@ -906,6 +905,8 @@ static void handle_zombie(struct sandbox *sb)
static int setup_shm(struct sandbox *sb)
{
+ pthread_mutexattr_t attr;
+
sb->shared = mmap(NULL, sizeof(struct sb_shm), PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
@@ -914,11 +915,23 @@ static int setup_shm(struct sandbox *sb)
return 1;
}
- if ( sem_init(&sb->shared->term_sem, 1, 1) ) {
- ERROR("Terminal semaphore setup failed: %s\n", strerror(errno));
+ if ( pthread_mutexattr_init(&attr) ) {
+ ERROR("Failed to initialise mutex attr.\n");
+ return 1;
+ }
+
+ if ( pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) ) {
+ ERROR("Failed to set process shared attribute.\n");
return 1;
}
+ if ( pthread_mutex_init(&sb->shared->term_lock, &attr) ) {
+ ERROR("Terminal lock setup failed.\n");
+ return 1;
+ }
+
+ pthread_mutexattr_destroy(&attr);
+
return 0;
}
@@ -1230,7 +1243,7 @@ void create_sandbox(struct index_args *iargs, int n_proc, char *prefix,
/* Update progress */
lock_sandbox(sb);
tNow = get_monotonic_seconds();
- r = sem_trywait(&sb->shared->term_sem);
+ r = pthread_mutex_trylock(&sb->shared->term_lock);
if ((r==0) && (tNow >= sb->t_last_stats+STATS_EVERY_N_SECONDS))
{
@@ -1250,7 +1263,7 @@ void create_sandbox(struct index_args *iargs, int n_proc, char *prefix,
}
- if ( r == 0 ) sem_post(&sb->shared->term_sem);
+ if ( r == 0 ) pthread_mutex_unlock(&sb->shared->term_lock);
unlock_sandbox(sb);
allDone = 1;
diff --git a/src/process_image.c b/src/process_image.c
index 5ead2c61..783ca464 100644
--- a/src/process_image.c
+++ b/src/process_image.c
@@ -78,7 +78,7 @@ static void try_refine_autoR(struct image *image, Crystal *cr)
void process_image(const struct index_args *iargs, struct pattern_args *pargs,
Stream *st, int cookie, const char *tmpdir, int results_pipe,
- int serial, sem_t *term_sem)
+ int serial, pthread_mutex_t *term_lock)
{
float *data_for_measurement;
size_t data_size;
@@ -260,7 +260,7 @@ void process_image(const struct index_args *iargs, struct pattern_args *pargs,
iargs->ir_inn, iargs->ir_mid, iargs->ir_out,
iargs->int_diag, iargs->int_diag_h,
iargs->int_diag_k, iargs->int_diag_l,
- term_sem);
+ term_lock);
ret = write_chunk(st, &image, hdfile,
iargs->stream_peaks, iargs->stream_refls,
diff --git a/src/process_image.h b/src/process_image.h
index 6e44c173..d982c4f0 100644
--- a/src/process_image.h
+++ b/src/process_image.h
@@ -104,7 +104,7 @@ struct pattern_args
extern void process_image(const struct index_args *iargs,
struct pattern_args *pargs, Stream *st,
int cookie, const char *tmpdir, int results_pipe,
- int serial, sem_t *term_sem);
+ int serial, pthread_mutex_t *term_lock);
#endif /* PROCESS_IMAGEs_H */