Skip to content

Instantly share code, notes, and snippets.

@AmesianX
Forked from rossy/tls-demo.c
Created November 25, 2018 19:47
Show Gist options
  • Select an option

  • Save AmesianX/25ff985af232f9000a6462f8a574ca52 to your computer and use it in GitHub Desktop.

Select an option

Save AmesianX/25ff985af232f9000a6462f8a574ca52 to your computer and use it in GitHub Desktop.
#include <windows.h>
#include <stdlib.h>
struct m_thread_info {
DWORD tid;
};
typedef struct m_thread_info *pthread_t;
static DWORD mpv_tls_index = FLS_OUT_OF_INDEXES;
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved)
{
switch (reason) {
case DLL_PROCESS_ATTACH:
mpv_tls_index = FlsAlloc(NULL);
if (mpv_tls_index == FLS_OUT_OF_INDEXES)
return FALSE;
break;
case DLL_PROCESS_DETACH:
if (mpv_tls_index != FLS_OUT_OF_INDEXES)
FlsFree(mpv_tls_index);
break;
case DLL_THREAD_ATTACH: {
struct m_thread_info *td = malloc(sizeof td);
*td = (struct m_thread_info) { GetCurrentThreadId() };
FlsSetValue(mpv_tls_index, td);
break;
}
case DLL_THREAD_DETACH: {
struct m_thread_info *td = FlsGetValue(mpv_tls_index);
free(td);
break;
}
}
return TRUE;
}
pthread_t pthread_self(void)
{
return FlsGetValue(mpv_tls_index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment