Author: Armin Rigo <ar...@tunes.org> Branch: windows-tls Changeset: r2521:d7e149c4dc3d Date: 2016-01-03 12:27 +0100 http://bitbucket.org/cffi/cffi/changeset/d7e149c4dc3d/
Log: trying out with a DllMain function diff --git a/c/misc_win32.h b/c/misc_win32.h --- a/c/misc_win32.h +++ b/c/misc_win32.h @@ -4,12 +4,53 @@ /* errno and GetLastError support */ struct cffi_errno_s { + /* The locally-made thread state. This is only non-null in case + we build the thread state here. It remains null if this thread + had already a thread state provided by CPython. */ + PyThreadState *local_thread_state; + + /* The saved errno and lasterror. */ int saved_errno; int saved_lasterror; }; static DWORD cffi_tls_index = TLS_OUT_OF_INDEXES; +BOOL WINAPI DllMain(HINSTANCE hinstDLL, + DWORD reason_for_call, + LPVOID reserved) +{ + LPVOID tls; + + switch (reason_for_call) { + + case DLL_THREAD_DETACH: + if (cffi_tls_index != TLS_OUT_OF_INDEXES) { + tls = TlsGetValue(cffi_tls_index); + if (tls != NULL) { + fprintf(stderr, "thread shutting down! %p\n", + tls->local_thread_state); + TlsSetValue(cffi_tls_index, NULL); + + if (tls->local_thread_state != NULL) { + /* We need to re-acquire the GIL temporarily to free the + thread state. I hope it is not a problem to do it in + DLL_THREAD_DETACH. + */ + PyEval_RestoreThread(tls->local_thread_state); + PyThreadState_DeleteCurrent(); + } + free(tls); + } + } + break; + + default: + break; + } + return TRUE; +} + static void init_cffi_tls(void) { if (cffi_tls_index == TLS_OUT_OF_INDEXES) { @@ -24,7 +65,6 @@ LPVOID p = TlsGetValue(cffi_tls_index); if (p == NULL) { - /* XXX this malloc() leaks */ p = malloc(sizeof(struct cffi_errno_s)); if (p == NULL) return NULL; _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit