Fix Windows build for 1.7 release (pthread stuff)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nicolas Williams · committed by Nicolas Williams · parent d18b2d0 · Browse files · 4 files·+420 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+420 added−6 removed
- other:+420 added−6 removed
Provenance
- Commit
34182cc- Tree
847a896- Author
Nicolas Williams <nico@cryptonector.com>- Committer
Nicolas Williams <nico@cryptonector.com>- Parent
d18b2d0
4 files changed+420 added−6 removed
modified:src/jv_dtoa_tsd.c+17 added-6 removed
1 #include <stdlib.h>2 #include <stdio.h>3-#include <pthread.h>3 4+#include "jv_thread.h"5 #include "jv_dtoa_tsd.h"6 #include "jv_dtoa.h"7 #include "jv_alloc.h"8 9-10-static pthread_key_t dtoa_ctx_key;9+#ifndef WIN3210 static pthread_once_t dtoa_ctx_once = PTHREAD_ONCE_INIT;11+#endif12 13+static pthread_key_t dtoa_ctx_key;14 static void tsd_dtoa_ctx_dtor(void *ctx) {15 if (ctx) {16 jvp_dtoa_context_free((struct dtoa_context *)ctx);17 jv_mem_free(ctx);18 }19 }20 21+#ifndef WIN3222+static23+#endif20-static void tsd_dtoa_ctx_fini() {24+void tsd_dtoa_ctx_fini() {25 struct dtoa_context *ctx = pthread_getspecific(dtoa_ctx_key);26 tsd_dtoa_ctx_dtor(ctx);27 pthread_setspecific(dtoa_ctx_key, NULL);28 }29 30+#ifndef WIN3231+static32+#endif26-static void tsd_dtoa_ctx_init() {33+void tsd_dtoa_ctx_init() {34 if (pthread_key_create(&dtoa_ctx_key, tsd_dtoa_ctx_dtor) != 0) {35 fprintf(stderr, "error: cannot create thread specific key");36 abort();37 }38+#ifndef WIN3239 atexit(tsd_dtoa_ctx_fini);40+#endif41 }42 43 inline struct dtoa_context *tsd_dtoa_context_get() {44+#ifndef WIN3245 pthread_once(&dtoa_ctx_once, tsd_dtoa_ctx_init); // cannot fail46+#endif47 struct dtoa_context *ctx = (struct dtoa_context*)pthread_getspecific(dtoa_ctx_key);48 if (!ctx) {49 ctx = malloc(sizeof(struct dtoa_context));50 jvp_dtoa_context_init(ctx);2 unmodified lines53 abort();54 }55 }56 return ctx;46-}57+}added:src/jv_thread.h+72 added
1+#ifndef JV_THREAD_H2+#define JV_THREAD_H3+4+#ifdef WIN325+#include <windows.h>6+#include <winnt.h>7+#include <errno.h>8+9+/* Copied from Heimdal: pthread-like mutexes for WIN32 -- see lib/base/heimbase.h in Heimdal */10+typedef struct pthread_mutex {11+ HANDLE h;12+} pthread_mutex_t;13+14+#define PTHREAD_MUTEX_INITIALIZER { INVALID_HANDLE_VALUE }15+16+static inline int17+pthread_mutex_init(pthread_mutex_t *m)18+{19+ m->h = CreateSemaphore(NULL, 1, 1, NULL);20+ if (m->h == INVALID_HANDLE_VALUE)21+ return EAGAIN;22+ return 0;23+}24+25+static inline int26+pthread_mutex_lock(pthread_mutex_t *m)27+{28+ HANDLE h, new_h;29+ int created = 0;30+31+ h = InterlockedCompareExchangePointer(&m->h, m->h, m->h);32+ if (h == INVALID_HANDLE_VALUE || h == NULL) {33+ created = 1;34+ new_h = CreateSemaphore(NULL, 0, 1, NULL);35+ if (new_h == INVALID_HANDLE_VALUE)36+ return EAGAIN;37+ if (InterlockedCompareExchangePointer(&m->h, new_h, h) != h) {38+ created = 0;39+ CloseHandle(new_h);40+ }41+ }42+ if (!created)43+ WaitForSingleObject(m->h, INFINITE);44+ return 0;45+}46+47+static inline int48+pthread_mutex_unlock(pthread_mutex_t *m)49+{50+ if (ReleaseSemaphore(m->h, 1, NULL) == FALSE)51+ return EPERM;52+ return 0;53+}54+static inline int55+pthread_mutex_destroy(pthread_mutex_t *m)56+{57+ HANDLE h;58+59+ h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h);60+ if (h != INVALID_HANDLE_VALUE)61+ CloseHandle(h);62+ return 0;63+}64+65+typedef unsigned long pthread_key_t;66+int pthread_key_create(pthread_key_t *, void (*)(void *));67+int pthread_setspecific(pthread_key_t, void *);68+void *pthread_getspecific(pthread_key_t);69+#else70+#include <pthread.h>71+#endif72+#endif /* JV_THREAD_H */modified:src/jv.c+303 added
1+/*2+ * Portions Copyright (c) 2016 Kungliga Tekniska Högskolan3+ * (Royal Institute of Technology, Stockholm, Sweden).4+ * All rights reserved.5+ *6+ * Redistribution and use in source and binary forms, with or without7+ * modification, are permitted provided that the following conditions8+ * are met:9+ *10+ * 1. Redistributions of source code must retain the above copyright11+ * notice, this list of conditions and the following disclaimer.12+ *13+ * 2. Redistributions in binary form must reproduce the above copyright14+ * notice, this list of conditions and the following disclaimer in the15+ * documentation and/or other materials provided with the distribution.16+ *17+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS20+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE21+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,22+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES23+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR24+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,26+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)27+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED28+ * OF THE POSSIBILITY OF SUCH DAMAGE.29+ *30+ */31+32+33 #include <stdint.h>34 #include <stddef.h>35 #include <assert.h>36 #include <stdlib.h>187 unmodified lines224 // the decimal precision of binary double225 #define BIN64_DEC_PRECISION (17)226 #define DEC_NUMBER_STRING_GUARD (14)227 228+#include <jv_thread.h>229+#ifdef WIN32230+/* Copied from Heimdal: thread-specific keys; see lib/base/dll.c in Heimdal */231+232+/*233+ * This is an implementation of thread-specific storage with234+ * destructors. WIN32 doesn't quite have this. Instead it has235+ * DllMain(), an entry point in every DLL that gets called to notify the236+ * DLL of thread/process "attach"/"detach" events.237+ *238+ * We use __thread (or __declspec(thread)) for the thread-local itself239+ * and DllMain() DLL_THREAD_DETACH events to drive destruction of240+ * thread-local values.241+ *242+ * When building in maintainer mode on non-Windows pthread systems this243+ * uses a single pthread key instead to implement multiple keys. This244+ * keeps the code from rotting when modified by non-Windows developers.245+ */246+247+/* Logical array of keys that grows lock-lessly */248+typedef struct tls_keys tls_keys;249+struct tls_keys {250+ void (**keys_dtors)(void *); /* array of destructors */251+ size_t keys_start_idx; /* index of first destructor */252+ size_t keys_num;253+ tls_keys *keys_next;254+};255+256+/*257+ * Well, not quite locklessly. We need synchronization primitives to do258+ * this locklessly. An atomic CAS will do.259+ */260+static pthread_mutex_t tls_key_defs_lock = PTHREAD_MUTEX_INITIALIZER;261+static tls_keys *tls_key_defs;262+263+/* Logical array of values (per-thread; no locking needed here) */264+struct tls_values {265+ void **values; /* realloc()ed */266+ size_t values_num;267+};268+269+#ifdef _MSC_VER270+static __declspec(thread) struct nomem_handler nomem_handler;271+#else272+static __thread struct tls_values values;273+#endif274+275+#define DEAD_KEY ((void *)8)276+277+static void278+w32_service_thread_detach(void *unused)279+{280+ tls_keys *key_defs;281+ void (*dtor)(void*);282+ size_t i;283+284+ pthread_mutex_lock(&tls_key_defs_lock);285+ key_defs = tls_key_defs;286+ pthread_mutex_unlock(&tls_key_defs_lock);287+288+ if (key_defs == NULL)289+ return;290+291+ for (i = 0; i < values.values_num; i++) {292+ assert(i >= key_defs->keys_start_idx);293+ if (i >= key_defs->keys_start_idx + key_defs->keys_num) {294+ pthread_mutex_lock(&tls_key_defs_lock);295+ key_defs = key_defs->keys_next;296+ pthread_mutex_unlock(&tls_key_defs_lock);297+298+ assert(key_defs != NULL);299+ assert(i >= key_defs->keys_start_idx);300+ assert(i < key_defs->keys_start_idx + key_defs->keys_num);301+ }302+ dtor = key_defs->keys_dtors[i - key_defs->keys_start_idx];303+ if (values.values[i] != NULL && dtor != NULL && dtor != DEAD_KEY)304+ dtor(values.values[i]);305+ values.values[i] = NULL;306+ }307+}308+309+extern void tsd_dtoa_ctx_init();310+extern void tsd_dtoa_ctx_fini();311+static void tsd_dec_ctx_fini();312+static void tsd_dec_ctx_init();313+314+BOOL WINAPI DllMain(HINSTANCE hinstDLL,315+ DWORD fdwReason,316+ LPVOID lpvReserved)317+{318+ switch (fdwReason) {319+ case DLL_PROCESS_ATTACH:320+ /*create_pt_key();*/321+ tsd_dtoa_ctx_init();322+ tsd_dec_ctx_init();323+ return 1;324+ case DLL_PROCESS_DETACH:325+ tsd_dtoa_ctx_fini();326+ tsd_dec_ctx_fini();327+ return 0;328+ case DLL_THREAD_ATTACH: return 0;329+ case DLL_THREAD_DETACH:330+ w32_service_thread_detach(NULL);331+ return 0;332+ default: return 0;333+ }334+}335+336+int337+pthread_key_create(pthread_key_t *key, void (*dtor)(void *))338+{339+ tls_keys *key_defs, *new_key_defs;340+ size_t i, k;341+ int ret = ENOMEM;342+343+ pthread_mutex_lock(&tls_key_defs_lock);344+ if (tls_key_defs == NULL) {345+ /* First key */346+ new_key_defs = calloc(1, sizeof(*new_key_defs));347+ if (new_key_defs == NULL) {348+ pthread_mutex_unlock(&tls_key_defs_lock);349+ return ENOMEM;350+ }351+ new_key_defs->keys_num = 8;352+ new_key_defs->keys_dtors = calloc(new_key_defs->keys_num,353+ sizeof(*new_key_defs->keys_dtors));354+ if (new_key_defs->keys_dtors == NULL) {355+ pthread_mutex_unlock(&tls_key_defs_lock);356+ free(new_key_defs);357+ return ENOMEM;358+ }359+ tls_key_defs = new_key_defs;360+ new_key_defs->keys_dtors[0] = dtor;361+ for (i = 1; i < new_key_defs->keys_num; i++)362+ new_key_defs->keys_dtors[i] = NULL;363+ pthread_mutex_unlock(&tls_key_defs_lock);364+ return 0;365+ }366+367+ for (key_defs = tls_key_defs;368+ key_defs != NULL;369+ key_defs = key_defs->keys_next) {370+ k = key_defs->keys_start_idx;371+ for (i = 0; i < key_defs->keys_num; i++, k++) {372+ if (key_defs->keys_dtors[i] == NULL) {373+ /* Found free slot; use it */374+ key_defs->keys_dtors[i] = dtor;375+ *key = k;376+ pthread_mutex_unlock(&tls_key_defs_lock);377+ return 0;378+ }379+ }380+ if (key_defs->keys_next != NULL)381+ continue;382+383+ /* Grow the registration array */384+ /* XXX DRY */385+ new_key_defs = calloc(1, sizeof(*new_key_defs));386+ if (new_key_defs == NULL)387+ break;388+389+ new_key_defs->keys_dtors =390+ calloc(key_defs->keys_num + key_defs->keys_num / 2,391+ sizeof(*new_key_defs->keys_dtors));392+ if (new_key_defs->keys_dtors == NULL) {393+ free(new_key_defs);394+ break;395+ }396+ new_key_defs->keys_start_idx = key_defs->keys_start_idx +397+ key_defs->keys_num;398+ new_key_defs->keys_num = key_defs->keys_num + key_defs->keys_num / 2;399+ new_key_defs->keys_dtors[i] = dtor;400+ for (i = 1; i < new_key_defs->keys_num; i++)401+ new_key_defs->keys_dtors[i] = NULL;402+ key_defs->keys_next = new_key_defs;403+ ret = 0;404+ break;405+ }406+ pthread_mutex_unlock(&tls_key_defs_lock);407+ return ret;408+}409+410+static void411+key_lookup(pthread_key_t key, tls_keys **kd,412+ size_t *dtor_idx, void (**dtor)(void *))413+{414+ tls_keys *key_defs;415+416+ if (kd != NULL)417+ *kd = NULL;418+ if (dtor_idx != NULL)419+ *dtor_idx = 0;420+ if (dtor != NULL)421+ *dtor = NULL;422+423+ pthread_mutex_lock(&tls_key_defs_lock);424+ key_defs = tls_key_defs;425+ pthread_mutex_unlock(&tls_key_defs_lock);426+427+ while (key_defs != NULL) {428+ if (key >= key_defs->keys_start_idx &&429+ key < key_defs->keys_start_idx + key_defs->keys_num) {430+ if (kd != NULL)431+ *kd = key_defs;432+ if (dtor_idx != NULL)433+ *dtor_idx = key - key_defs->keys_start_idx;434+ if (dtor != NULL)435+ *dtor = key_defs->keys_dtors[key - key_defs->keys_start_idx];436+ return;437+ }438+439+ pthread_mutex_lock(&tls_key_defs_lock);440+ key_defs = key_defs->keys_next;441+ pthread_mutex_unlock(&tls_key_defs_lock);442+ assert(key_defs != NULL);443+ assert(key >= key_defs->keys_start_idx);444+ }445+}446+447+int448+pthread_setspecific(pthread_key_t key, void *value)449+{450+ void **new_values;451+ size_t new_num;452+ void (*dtor)(void *);453+ size_t i;454+455+ key_lookup(key, NULL, NULL, &dtor);456+ if (dtor == NULL)457+ return EINVAL;458+459+ if (key >= values.values_num) {460+ if (values.values_num == 0) {461+ values.values = NULL;462+ new_num = 8;463+ } else {464+ new_num = (values.values_num + values.values_num / 2);465+ }466+ new_values = realloc(values.values, sizeof(void *) * new_num);467+ if (new_values == NULL)468+ return ENOMEM;469+ for (i = values.values_num; i < new_num; i++)470+ new_values[i] = NULL;471+ values.values = new_values;472+ values.values_num = new_num;473+ }474+475+ assert(key < values.values_num);476+477+ if (values.values[key] != NULL && dtor != NULL && dtor != DEAD_KEY)478+ dtor(values.values[key]);479+480+ values.values[key] = value;481+ return 0;482+}483+484+void *485+pthread_getspecific(pthread_key_t key)486+{487+ if (key >= values.values_num)488+ return NULL;489+ return values.values[key];490+}491+#else492 #include <pthread.h>493+#endif494 495 static pthread_key_t dec_ctx_key;496 static pthread_key_t dec_ctx_dbl_key;497+#ifndef WIN32498 static pthread_once_t dec_ctx_once = PTHREAD_ONCE_INIT;499+#endif500 501 #define DEC_CONTEXT() tsd_dec_ctx_get(&dec_ctx_key)502 #define DEC_CONTEXT_TO_DOUBLE() tsd_dec_ctx_get(&dec_ctx_dbl_key)503 14 unmodified lines518 if (pthread_key_create(&dec_ctx_dbl_key, jv_mem_free) != 0) {519 fprintf(stderr, "error: cannot create thread specific key");520 abort();521 }522+#ifndef WIN32523 atexit(tsd_dec_ctx_fini);524+#endif525 }526 527 static decContext* tsd_dec_ctx_get(pthread_key_t *key) {528+#ifndef WIN32529 pthread_once(&dec_ctx_once, tsd_dec_ctx_init); // cannot fail530+#endif531 decContext *ctx = (decContext*)pthread_getspecific(*key);532 if (ctx) {533 return ctx;534 }1457 unmodified linesmodified:COPYING+28 added
104 unmodified lines105 shall not be used in advertising or otherwise to promote the sale, use106 or other dealings in this Software without prior written authorization107 of the copyright holder.108 109+Portions Copyright (c) 2016 Kungliga Tekniska Högskolan110+(Royal Institute of Technology, Stockholm, Sweden).111+All rights reserved.112+113+Redistribution and use in source and binary forms, with or without114+modification, are permitted provided that the following conditions115+are met:116+117+1. Redistributions of source code must retain the above copyright118+ notice, this list of conditions and the following disclaimer.119+120+2. Redistributions in binary form must reproduce the above copyright121+ notice, this list of conditions and the following disclaimer in the122+ documentation and/or other materials provided with the distribution.123+124+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS125+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT126+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS127+FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE128+COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,129+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES130+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR131+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)132+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,133+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)134+ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED135+OF THE POSSIBILITY OF SUCH DAMAGE.136+