Skip to content
Myr

jv_dtoa_tsd.c

This file

Path

src/jv_dtoa_tsd.c

52 lines · 1.3 KiB · C

Last changed by

Use jv_mem_alloc to allocate dtoa_context in tsd_dtoa_context_get (#3356)엄태용before adoption2e69879

History of this file

Take it

1#include <stdlib.h>2#include <stdio.h>34#include "jv_thread.h"5#include "jv_dtoa_tsd.h"6#include "jv_dtoa.h"7#include "jv_alloc.h"89static pthread_once_t dtoa_ctx_once = PTHREAD_ONCE_INIT;1011static pthread_key_t dtoa_ctx_key;12static void tsd_dtoa_ctx_dtor(void *ctx) {13  if (ctx) {14    jvp_dtoa_context_free((struct dtoa_context *)ctx);15    jv_mem_free(ctx);16  }17}1819#ifndef WIN3220static21#endif22void jv_tsd_dtoa_ctx_fini(void) {23  struct dtoa_context *ctx = pthread_getspecific(dtoa_ctx_key);24  tsd_dtoa_ctx_dtor(ctx);25  pthread_setspecific(dtoa_ctx_key, NULL);26}2728#ifndef WIN3229static30#endif31void jv_tsd_dtoa_ctx_init(void) {32  if (pthread_key_create(&dtoa_ctx_key, tsd_dtoa_ctx_dtor) != 0) {33    fprintf(stderr, "error: cannot create thread specific key");34    abort();35  }36  atexit(jv_tsd_dtoa_ctx_fini);37}3839inline struct dtoa_context *tsd_dtoa_context_get(void) {40  pthread_once(&dtoa_ctx_once, jv_tsd_dtoa_ctx_init); // cannot fail41  struct dtoa_context *ctx = (struct dtoa_context*)pthread_getspecific(dtoa_ctx_key);42  if (!ctx) {43    ctx = jv_mem_alloc(sizeof(struct dtoa_context));44    jvp_dtoa_context_init(ctx);45    if (pthread_setspecific(dtoa_ctx_key, ctx) != 0) {46      jv_mem_free(ctx);47      fprintf(stderr, "error: cannot set thread specific data");48      abort();49    }50  }51  return ctx;52}