Skip to content

Commit

Permalink
mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas-Krebbel committed Jul 18, 2024
1 parent 4e08851 commit 4d39279
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/libm/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,46 @@ static sigjmp_buf sigjmp;
#define LONGJMP siglongjmp
#endif

// Protect sigjmp from parallel access
// FIXME: Static initializers will not work on Windows and MacOS
#ifdef __linux__
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
#else
#error "pthread initializer not supported"
#endif

static void sighandler(int signum) {
LONGJMP(sigjmp, 1);
}

// On Linux we prefer the more robust sigaction over signal
#ifdef __linux__
#define SIGNAL(SIGNUM, SIGHANDLER, ORIG) \
{ \
struct sigaction sigact = { .sa_handler = (SIGHANDLER) }; \
sigaction((SIGNUM), &sigact, (ORIG)); \
}
#define RESTORE_SIGNAL(SIGNUM, ORIG) \
sigaction((SIGNUM), (ORIG), NULL);
#else
#define SIGNAL(SIGNUM, SIGHANDLER, ORIG) \
(ORIG) = signal((SIGNUM), (SIGHANDLER));
#define RESTORE_SIGNAL(SIGNUM, ORIG) \
signal((SIGNUM), (ORIG));
#endif

static int cpuSupportsExt(void (*tryExt)()) {
// FIXME: Strictly speaking this should be an atomic_int
// But int should be atomic on all supported targets anyway?!
static int cache = -1;

// This is not atomic. However, since the only transition is from -1
// to either 0 or 1 and never back, this should be ok.
if (cache != -1) return cache;
pthread_mutex_lock(&mtx);

void (*org);
org = signal(SIGILL, sighandler);
void *org = NULL;
SIGNAL(SIGILL, sighandler, org);

if (SETJMP(sigjmp) == 0) {
(*tryExt)();
Expand All @@ -37,7 +67,9 @@ static int cpuSupportsExt(void (*tryExt)()) {
cache = 0;
}

signal(SIGILL, org);
RESTORE_SIGNAL(SIGILL, org);

pthread_mutex_unlock(&mtx);
return cache;
}

Expand Down
1 change: 1 addition & 0 deletions src/libm/dispavx.c.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <assert.h>
#include <signal.h>
#include <setjmp.h>
#include <pthread.h>

#include "misc.h"

Expand Down
1 change: 1 addition & 0 deletions src/libm/disppower_128.c.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <assert.h>
#include <signal.h>
#include <setjmp.h>
#include <pthread.h>

#include "misc.h"

Expand Down
1 change: 1 addition & 0 deletions src/libm/disps390x_128.c.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <assert.h>
#include <signal.h>
#include <setjmp.h>
#include <pthread.h>

#include "misc.h"

Expand Down
1 change: 1 addition & 0 deletions src/libm/dispscalar.c.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <assert.h>
#include <signal.h>
#include <setjmp.h>
#include <pthread.h>

#include "misc.h"

Expand Down
1 change: 1 addition & 0 deletions src/libm/dispsse.c.org
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <assert.h>
#include <signal.h>
#include <setjmp.h>
#include <pthread.h>

#include "misc.h"

Expand Down

0 comments on commit 4d39279

Please sign in to comment.