Blame jemalloc/include/jemalloc/internal/tsd_win.h

Packit 345191
#ifdef JEMALLOC_INTERNAL_TSD_WIN_H
Packit 345191
#error This file should be included only once, by tsd.h.
Packit 345191
#endif
Packit 345191
#define JEMALLOC_INTERNAL_TSD_WIN_H
Packit 345191
Packit 345191
typedef struct {
Packit 345191
	bool initialized;
Packit 345191
	tsd_t val;
Packit 345191
} tsd_wrapper_t;
Packit 345191
Packit 345191
extern DWORD tsd_tsd;
Packit 345191
extern tsd_wrapper_t tsd_boot_wrapper;
Packit 345191
extern bool tsd_booted;
Packit 345191
Packit 345191
/* Initialization/cleanup. */
Packit 345191
JEMALLOC_ALWAYS_INLINE bool
Packit 345191
tsd_cleanup_wrapper(void) {
Packit 345191
	DWORD error = GetLastError();
Packit 345191
	tsd_wrapper_t *wrapper = (tsd_wrapper_t *)TlsGetValue(tsd_tsd);
Packit 345191
	SetLastError(error);
Packit 345191
Packit 345191
	if (wrapper == NULL) {
Packit 345191
		return false;
Packit 345191
	}
Packit 345191
Packit 345191
	if (wrapper->initialized) {
Packit 345191
		wrapper->initialized = false;
Packit 345191
		tsd_cleanup(&wrapper->val);
Packit 345191
		if (wrapper->initialized) {
Packit 345191
			/* Trigger another cleanup round. */
Packit 345191
			return true;
Packit 345191
		}
Packit 345191
	}
Packit 345191
	malloc_tsd_dalloc(wrapper);
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE void
Packit 345191
tsd_wrapper_set(tsd_wrapper_t *wrapper) {
Packit 345191
	if (!TlsSetValue(tsd_tsd, (void *)wrapper)) {
Packit 345191
		malloc_write("<jemalloc>: Error setting TSD\n");
Packit 345191
		abort();
Packit 345191
	}
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE tsd_wrapper_t *
Packit 345191
tsd_wrapper_get(bool init) {
Packit 345191
	DWORD error = GetLastError();
Packit 345191
	tsd_wrapper_t *wrapper = (tsd_wrapper_t *) TlsGetValue(tsd_tsd);
Packit 345191
	SetLastError(error);
Packit 345191
Packit 345191
	if (init && unlikely(wrapper == NULL)) {
Packit 345191
		wrapper = (tsd_wrapper_t *)
Packit 345191
		    malloc_tsd_malloc(sizeof(tsd_wrapper_t));
Packit 345191
		if (wrapper == NULL) {
Packit 345191
			malloc_write("<jemalloc>: Error allocating TSD\n");
Packit 345191
			abort();
Packit 345191
		} else {
Packit 345191
			wrapper->initialized = false;
Packit 345191
			/* MSVC is finicky about aggregate initialization. */
Packit 345191
			tsd_t tsd_initializer = TSD_INITIALIZER;
Packit 345191
			wrapper->val = tsd_initializer;
Packit 345191
		}
Packit 345191
		tsd_wrapper_set(wrapper);
Packit 345191
	}
Packit 345191
	return wrapper;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE bool
Packit 345191
tsd_boot0(void) {
Packit 345191
	tsd_tsd = TlsAlloc();
Packit 345191
	if (tsd_tsd == TLS_OUT_OF_INDEXES) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
	malloc_tsd_cleanup_register(&tsd_cleanup_wrapper);
Packit 345191
	tsd_wrapper_set(&tsd_boot_wrapper);
Packit 345191
	tsd_booted = true;
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE void
Packit 345191
tsd_boot1(void) {
Packit 345191
	tsd_wrapper_t *wrapper;
Packit 345191
	wrapper = (tsd_wrapper_t *)
Packit 345191
	    malloc_tsd_malloc(sizeof(tsd_wrapper_t));
Packit 345191
	if (wrapper == NULL) {
Packit 345191
		malloc_write("<jemalloc>: Error allocating TSD\n");
Packit 345191
		abort();
Packit 345191
	}
Packit 345191
	tsd_boot_wrapper.initialized = false;
Packit 345191
	tsd_cleanup(&tsd_boot_wrapper.val);
Packit 345191
	wrapper->initialized = false;
Packit 345191
	tsd_t initializer = TSD_INITIALIZER;
Packit 345191
	wrapper->val = initializer;
Packit 345191
	tsd_wrapper_set(wrapper);
Packit 345191
}
Packit 345191
JEMALLOC_ALWAYS_INLINE bool
Packit 345191
tsd_boot(void) {
Packit 345191
	if (tsd_boot0()) {
Packit 345191
		return true;
Packit 345191
	}
Packit 345191
	tsd_boot1();
Packit 345191
	return false;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE bool
Packit 345191
tsd_booted_get(void) {
Packit 345191
	return tsd_booted;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE bool
Packit 345191
tsd_get_allocates(void) {
Packit 345191
	return true;
Packit 345191
}
Packit 345191
Packit 345191
/* Get/set. */
Packit 345191
JEMALLOC_ALWAYS_INLINE tsd_t *
Packit 345191
tsd_get(bool init) {
Packit 345191
	tsd_wrapper_t *wrapper;
Packit 345191
Packit 345191
	assert(tsd_booted);
Packit 345191
	wrapper = tsd_wrapper_get(init);
Packit 345191
	if (tsd_get_allocates() && !init && wrapper == NULL) {
Packit 345191
		return NULL;
Packit 345191
	}
Packit 345191
	return &wrapper->val;
Packit 345191
}
Packit 345191
Packit 345191
JEMALLOC_ALWAYS_INLINE void
Packit 345191
tsd_set(tsd_t *val) {
Packit 345191
	tsd_wrapper_t *wrapper;
Packit 345191
Packit 345191
	assert(tsd_booted);
Packit 345191
	wrapper = tsd_wrapper_get(true);
Packit 345191
	if (likely(&wrapper->val != val)) {
Packit 345191
		wrapper->val = *(val);
Packit 345191
	}
Packit 345191
	wrapper->initialized = true;
Packit 345191
}