| 1 |
#ifndef ULIB__UMTX_H |
| 2 |
#define ULIB__UMTX_H |
| 3 |
/* |
| 4 |
The entire need for locking in UUID springs from the fact that some |
| 5 |
are using it in forked or threaded environments, not to mention the |
| 6 |
"thread-safety" aspects. |
| 7 |
|
| 8 |
The first complaint was that UUIDv4 would repeat after creating new |
| 9 |
threads. This was solved by moving the RNG context into a thread-safe |
| 10 |
container and reseeding at clone time. |
| 11 |
|
| 12 |
The next complaint was that UUIDv4 would repeat after forking new |
| 13 |
processes. This was solved by saving the process id and comparing it |
| 14 |
to the live pid on every call. If they differed, reseed the RNG. |
| 15 |
|
| 16 |
The next complaint, which has not appeared yet but will in time as |
| 17 |
CPUs get faster, will be that UUIDv1 repeats due to timestamp overlap. |
| 18 |
This can already be seen in testing and has been partially mitigated |
| 19 |
by implementing a counter on top of the raw time value and spinning |
| 20 |
the clock_seq field as needed. |
| 21 |
|
| 22 |
However, in light of the first two complaints, this too shall fail. |
| 23 |
The real solution is to move both random number and time contexts into |
| 24 |
a shared memory space that can be seen by both threads and forks, |
| 25 |
which of course presents us with a serious need for a fast, reliable |
| 26 |
lock. |
| 27 |
|
| 28 |
There does not appear to be a portable solution, so we get this. |
| 29 |
*/ |
| 30 |
|
| 31 |
#if defined(USE_WIN32_ALIEN) || defined(USE_WIN32_NATIVE) |
| 32 |
|
| 33 |
# ifdef HAVE_SRWLOCK |
| 34 |
# define UMTX_init InitializeSRWLock(&SMEM->LOCK) |
| 35 |
# define UMTX_lock AcquireSRWLockExclusive(&SMEM->LOCK) |
| 36 |
# define UMTX_unlock ReleaseSRWLockExclusive(&SMEM->LOCK) |
| 37 |
# else |
| 38 |
# define UMTX_init InitializeCriticalSection(&SMEM->LOCK) |
| 39 |
# define UMTX_lock EnterCriticalSection(&SMEM->LOCK) |
| 40 |
# define UMTX_unlock LeaveCriticalSection(&SMEM->LOCK) |
| 41 |
# endif |
| 42 |
|
| 43 |
#elif defined(__OpenBSD__) |
| 44 |
|
| 45 |
/* sem_init() broken when pshared != 0 */ |
| 46 |
#include "FUTX.h" |
| 47 |
#include "atomic.h" |
| 48 |
#define UMTX_init SMEM->LOCK = 0 |
| 49 |
#define UMTX_lock { \ |
| 50 |
long c; \ |
| 51 |
if ((c = cmpxchg(&SMEM->LOCK, 0, 1)) != 0) { \ |
| 52 |
if (c != 2) \ |
| 53 |
c = xchg(&SMEM->LOCK, 2); \ |
| 54 |
while (c != 0) { \ |
| 55 |
futex_wait(&SMEM->LOCK, 2, 0); \ |
| 56 |
c = xchg(&SMEM->LOCK, 2); \ |
| 57 |
} \ |
| 58 |
} \ |
| 59 |
} |
| 60 |
#define UMTX_unlock { \ |
| 61 |
if (xdec(&SMEM->LOCK) != 1) { \ |
| 62 |
SMEM->LOCK = 0; \ |
| 63 |
futex_wake(&SMEM->LOCK, 1); \ |
| 64 |
} \ |
| 65 |
} |
| 66 |
|
| 67 |
#elif defined(__APPLE__) && defined(HAVE_DISPATCH_DISPATCH_H) |
| 68 |
|
| 69 |
/* See this link for a nice discussion on options here. |
| 70 |
https://www.codestudy.net/blog/why-are-sem-init-sem-getvalue-sem-destroy-deprecated-on-mac-os-x-and-what-replaces-them/ |
| 71 |
* Note that one of the workarounds is with pthreads. Their call to |
| 72 |
* pthread_mutex_create() discards the pshared argument, which is really |
| 73 |
* where all our troubles began with pthreads in general. Notably that |
| 74 |
* once you start needing pshared=!0, various platforms display differing |
| 75 |
* states of brokenness. One platform (*cough* OpenBSD) going so far as to |
| 76 |
* say that removing process-shared mutex was POSIX-conformant. And while |
| 77 |
* that's probably true, our need is not diminished. |
| 78 |
*/ |
| 79 |
/* For OSX and archlinux but arch seems to have a working sem_init/wait */ |
| 80 |
/* dispatch_semaphore_t is a pointer type */ |
| 81 |
#define UMTX_init SMEM->LOCK = dispatch_semaphore_create(1) |
| 82 |
#define UMTX_lock dispatch_semaphore_wait(SMEM->LOCK, DISPATCH_TIME_FOREVER) |
| 83 |
#define UMTX_unlock dispatch_semaphore_signal(SMEM->LOCK) |
| 84 |
|
| 85 |
#else |
| 86 |
|
| 87 |
#define UMTX_init do { \ |
| 88 |
if (sem_init(&SMEM->LOCK, 1, 1) == 0) break; \ |
| 89 |
if (errno == EINVAL) Perl_croak_nocontext("panic: sem_init (EINVAL) at %s line %d\n", __FILE__, __LINE__); \ |
| 90 |
Perl_croak_nocontext("panic: sem_init (%d) at %s line %d\n", errno, __FILE__, __LINE__); \ |
| 91 |
} while (1); |
| 92 |
#define UMTX_lock do { \ |
| 93 |
if (sem_wait(&SMEM->LOCK) == 0) break; \ |
| 94 |
if (errno == EINTR) continue; \ |
| 95 |
if (errno == EINVAL) Perl_croak_nocontext("panic: sem_wait (EINVAL) at %s line %d\n", __FILE__, __LINE__); \ |
| 96 |
Perl_croak_nocontext("panic: sem_wait (%d) at %s line %d\n", errno, __FILE__, __LINE__); \ |
| 97 |
} while (1); |
| 98 |
#define UMTX_unlock do { \ |
| 99 |
if (sem_post(&SMEM->LOCK) == 0) break; \ |
| 100 |
if (errno == EINVAL) Perl_croak_nocontext("panic: sem_post (EINVAL) at %s line %d\n", __FILE__, __LINE__); \ |
| 101 |
Perl_croak_nocontext("panic: sem_post (%d) at %s line %d\n", errno, __FILE__, __LINE__); \ |
| 102 |
} while (1); |
| 103 |
|
| 104 |
#endif |
| 105 |
|
| 106 |
|
| 107 |
/* This is try/catch from XSUB.h, copied here for reference. */ |
| 108 |
#ifdef NO_XSLOCKSxxx |
| 109 |
# define dXCPT dJMPENV; int rEtV = 0 |
| 110 |
# define XCPT_TRY_START JMPENV_PUSH(rEtV); if (rEtV == 0) |
| 111 |
# define XCPT_TRY_END JMPENV_POP; |
| 112 |
# define XCPT_CATCH if (rEtV != 0) |
| 113 |
# define XCPT_RETHROW JMPENV_JUMP(rEtV) |
| 114 |
#endif |
| 115 |
|
| 116 |
#define UMTX_INIT UMTX_init |
| 117 |
#define UMTX_LOCK dJMPENV; int rEtV = 0; UMTX_lock; JMPENV_PUSH(rEtV); if (rEtV == 0) |
| 118 |
#define UMTX_UNLOCK JMPENV_POP; UMTX_unlock; if (rEtV != 0) { JMPENV_JUMP(rEtV); } |
| 119 |
|
| 120 |
#endif |
| 121 |
/* ex:set ts=2 sw=2 itab=spaces: */ |