Skip to content
Myr

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

4 files+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>34#include "jv_thread.h"5#include "jv_dtoa_tsd.h"6#include "jv_dtoa.h"7#include "jv_alloc.h"8910static pthread_key_t dtoa_ctx_key;9#ifndef WIN3210static pthread_once_t dtoa_ctx_once = PTHREAD_ONCE_INIT;11#endif1213static pthread_key_t dtoa_ctx_key;14static 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}2021#ifndef WIN3222static23#endif20static void tsd_dtoa_ctx_fini() {24void 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}2930#ifndef WIN3231static32#endif26static void tsd_dtoa_ctx_init() {33void 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}4243inline 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 lines
53 abort();54 }55 }56 return ctx;46}57}
added:src/jv_thread.h+72 added
1#ifndef JV_THREAD_H2#define JV_THREAD_H34#ifdef WIN325#include <windows.h>6#include <winnt.h>7#include <errno.h>89/* Copied from Heimdal: pthread-like mutexes for WIN32 -- see lib/base/heimbase.h in Heimdal */10typedef struct pthread_mutex {11 HANDLE h;12} pthread_mutex_t;1314#define PTHREAD_MUTEX_INITIALIZER { INVALID_HANDLE_VALUE }1516static inline int17pthread_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}2425static inline int26pthread_mutex_lock(pthread_mutex_t *m)27{28 HANDLE h, new_h;29 int created = 0;3031 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}4647static inline int48pthread_mutex_unlock(pthread_mutex_t *m)49{50 if (ReleaseSemaphore(m->h, 1, NULL) == FALSE)51 return EPERM;52 return 0;53}54static inline int55pthread_mutex_destroy(pthread_mutex_t *m)56{57 HANDLE h;5859 h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h);60 if (h != INVALID_HANDLE_VALUE)61 CloseHandle(h);62 return 0;63}6465typedef unsigned long pthread_key_t;66int pthread_key_create(pthread_key_t *, void (*)(void *));67int pthread_setspecific(pthread_key_t, void *);68void *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 */313233#include <stdint.h>34#include <stddef.h>35#include <assert.h>36#include <stdlib.h>
187 unmodified lines
224// the decimal precision of binary double225#define BIN64_DEC_PRECISION (17)226#define DEC_NUMBER_STRING_GUARD (14)227228#include <jv_thread.h>229#ifdef WIN32230/* Copied from Heimdal: thread-specific keys; see lib/base/dll.c in Heimdal */231232/*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 */246247/* Logical array of keys that grows lock-lessly */248typedef struct tls_keys tls_keys;249struct 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};255256/*257 * Well, not quite locklessly. We need synchronization primitives to do258 * this locklessly. An atomic CAS will do.259 */260static pthread_mutex_t tls_key_defs_lock = PTHREAD_MUTEX_INITIALIZER;261static tls_keys *tls_key_defs;262263/* Logical array of values (per-thread; no locking needed here) */264struct tls_values {265 void **values; /* realloc()ed */266 size_t values_num;267};268269#ifdef _MSC_VER270static __declspec(thread) struct nomem_handler nomem_handler;271#else272static __thread struct tls_values values;273#endif274275#define DEAD_KEY ((void *)8)276277static void278w32_service_thread_detach(void *unused)279{280 tls_keys *key_defs;281 void (*dtor)(void*);282 size_t i;283284 pthread_mutex_lock(&tls_key_defs_lock);285 key_defs = tls_key_defs;286 pthread_mutex_unlock(&tls_key_defs_lock);287288 if (key_defs == NULL)289 return;290291 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);297298 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}308309extern void tsd_dtoa_ctx_init();310extern void tsd_dtoa_ctx_fini();311static void tsd_dec_ctx_fini();312static void tsd_dec_ctx_init();313314BOOL 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}335336int337pthread_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;342343 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 }366367 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;382383 /* 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;388389 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}409410static void411key_lookup(pthread_key_t key, tls_keys **kd,412 size_t *dtor_idx, void (**dtor)(void *))413{414 tls_keys *key_defs;415416 if (kd != NULL)417 *kd = NULL;418 if (dtor_idx != NULL)419 *dtor_idx = 0;420 if (dtor != NULL)421 *dtor = NULL;422423 pthread_mutex_lock(&tls_key_defs_lock);424 key_defs = tls_key_defs;425 pthread_mutex_unlock(&tls_key_defs_lock);426427 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 }438439 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}446447int448pthread_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;454455 key_lookup(key, NULL, NULL, &dtor);456 if (dtor == NULL)457 return EINVAL;458459 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 }474475 assert(key < values.values_num);476477 if (values.values[key] != NULL && dtor != NULL && dtor != DEAD_KEY)478 dtor(values.values[key]);479480 values.values[key] = value;481 return 0;482}483484void *485pthread_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#endif494495static pthread_key_t dec_ctx_key;496static pthread_key_t dec_ctx_dbl_key;497#ifndef WIN32498static pthread_once_t dec_ctx_once = PTHREAD_ONCE_INIT;499#endif500501#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 lines
518 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}526527static 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 lines
modified:COPYING+28 added
104 unmodified lines
105shall not be used in advertising or otherwise to promote the sale, use106or other dealings in this Software without prior written authorization107of the copyright holder.108109Portions Copyright (c) 2016 Kungliga Tekniska Högskolan110(Royal Institute of Technology, Stockholm, Sweden).111All rights reserved.112113Redistribution and use in source and binary forms, with or without114modification, are permitted provided that the following conditions115are met:1161171. Redistributions of source code must retain the above copyright118 notice, this list of conditions and the following disclaimer.1191202. 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.123124THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS125"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT126LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS127FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE128COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,129INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES130(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR131SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)132HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,133STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)134ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED135OF THE POSSIBILITY OF SUCH DAMAGE.136