2000-02-18 03:38:33 +08:00
|
|
|
/*
|
|
|
|
* mthr.c
|
|
|
|
*
|
|
|
|
* Implement Mingw thread-support DLL .
|
|
|
|
*
|
|
|
|
* This file is used iff the following conditions are met:
|
|
|
|
* - gcc uses -mthreads option
|
|
|
|
* - user code uses C++ exceptions
|
|
|
|
*
|
|
|
|
* The sole job of the Mingw thread support DLL (MingwThr) is to catch
|
|
|
|
* all the dying threads and clean up the data allocated in the TLSs
|
|
|
|
* for exception contexts during C++ EH. Posix threads have key dtors,
|
|
|
|
* but win32 TLS keys do not, hence the magic. Without this, there's at
|
|
|
|
* least `6 * sizeof (void*)' bytes leaks for each catch/throw in each
|
|
|
|
* thread. The only public interface is __mingwthr_key_dtor().
|
|
|
|
*
|
|
|
|
* Created by Mumit Khan <khan@nanotech.wisc.edu>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#undef WIN32_LEAN_AND_MEAN
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/* To protect the thread/key association data structure modifications. */
|
|
|
|
CRITICAL_SECTION __mingwthr_cs;
|
|
|
|
|
|
|
|
typedef struct __mingwthr_key __mingwthr_key_t;
|
|
|
|
|
|
|
|
/* The list of threads active with key/dtor pairs. */
|
|
|
|
struct __mingwthr_key {
|
|
|
|
DWORD key;
|
|
|
|
void (*dtor) (void *);
|
|
|
|
__mingwthr_key_t *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-12-05 23:01:06 +08:00
|
|
|
static __mingwthr_key_t *key_dtor_list;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* __mingwthr_key_add:
|
|
|
|
*
|
|
|
|
* Add key/dtor association for this thread. If the thread entry does not
|
|
|
|
* exist, create a new one and add to the head of the threads list; add
|
|
|
|
* the new assoc at the head of the keys list.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
2001-12-05 23:01:06 +08:00
|
|
|
___mingwthr_add_key_dtor ( DWORD key, void (*dtor) (void *))
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
__mingwthr_key_t *new_key;
|
|
|
|
|
|
|
|
new_key = (__mingwthr_key_t *) calloc (1, sizeof (__mingwthr_key_t));
|
|
|
|
if (new_key == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
new_key->key = key;
|
|
|
|
new_key->dtor = dtor;
|
|
|
|
|
|
|
|
EnterCriticalSection (&__mingwthr_cs);
|
|
|
|
|
2001-12-05 23:01:06 +08:00
|
|
|
new_key->next = key_dtor_list;
|
|
|
|
key_dtor_list = new_key;
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
LeaveCriticalSection (&__mingwthr_cs);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2001-12-05 23:01:06 +08:00
|
|
|
printf ("%s: allocating: (%ld, %x)\n",
|
|
|
|
__FUNCTION__, key, dtor);
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-12-05 23:01:06 +08:00
|
|
|
static int
|
|
|
|
___mingwthr_remove_key_dtor ( DWORD key )
|
|
|
|
{
|
|
|
|
__mingwthr_key_t *prev_key;
|
|
|
|
__mingwthr_key_t *cur_key;
|
|
|
|
|
|
|
|
EnterCriticalSection (&__mingwthr_cs);
|
|
|
|
|
|
|
|
prev_key = NULL;
|
|
|
|
cur_key = key_dtor_list;
|
|
|
|
|
|
|
|
while( cur_key != NULL )
|
|
|
|
{
|
|
|
|
if( cur_key->key == key )
|
|
|
|
{
|
|
|
|
// take key/dtor out of list
|
|
|
|
if( prev_key == NULL )
|
|
|
|
{
|
|
|
|
key_dtor_list = cur_key->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev_key->next = cur_key->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf ("%s: removing: (%ld)\n",
|
|
|
|
__FUNCTION__, key );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
free( cur_key );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_key = cur_key;
|
|
|
|
cur_key = cur_key->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection (&__mingwthr_cs);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-02-18 03:38:33 +08:00
|
|
|
/*
|
2001-12-05 23:01:06 +08:00
|
|
|
* __mingwthr_run_key_dtors (void):
|
2000-02-18 03:38:33 +08:00
|
|
|
*
|
|
|
|
* Callback from DllMain when thread detaches to clean up the key
|
|
|
|
* storage.
|
|
|
|
*
|
|
|
|
* Note that this does not delete the key itself, but just runs
|
|
|
|
* the dtor if the current value are both non-NULL. Note that the
|
|
|
|
* keys with NULL dtors are not added by __mingwthr_key_dtor, the
|
|
|
|
* only public interface, so we don't need to check.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2001-12-05 23:01:06 +08:00
|
|
|
__mingwthr_run_key_dtors (void)
|
2000-02-18 03:38:33 +08:00
|
|
|
{
|
|
|
|
__mingwthr_key_t *keyp;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2001-12-05 23:01:06 +08:00
|
|
|
printf ("%s: Entering Thread id %ld\n", __FUNCTION__, GetCurrentThreadId() );
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
EnterCriticalSection (&__mingwthr_cs);
|
|
|
|
|
2001-12-05 23:01:06 +08:00
|
|
|
for (keyp = key_dtor_list; keyp; )
|
|
|
|
{
|
|
|
|
LPVOID value = TlsGetValue (keyp->key);
|
|
|
|
if (GetLastError () == ERROR_SUCCESS)
|
|
|
|
{
|
2000-02-18 03:38:33 +08:00
|
|
|
#ifdef DEBUG
|
2001-12-05 23:01:06 +08:00
|
|
|
printf (" (%ld, %x)\n", keyp->key, keyp->dtor);
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif
|
2001-12-05 23:01:06 +08:00
|
|
|
if (value)
|
|
|
|
(*keyp->dtor) (value);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
#ifdef DEBUG
|
2001-12-05 23:01:06 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
printf (" TlsGetValue FAILED (%ld, %x)\n",
|
|
|
|
keyp->key, keyp->dtor);
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif
|
2001-12-05 23:01:06 +08:00
|
|
|
keyp = keyp->next;
|
|
|
|
}
|
2000-02-18 03:38:33 +08:00
|
|
|
|
2001-12-05 23:01:06 +08:00
|
|
|
LeaveCriticalSection (&__mingwthr_cs);
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2001-12-05 23:01:06 +08:00
|
|
|
printf ("%s: Exiting Thread id %ld\n", __FUNCTION__, GetCurrentThreadId() );
|
2000-02-18 03:38:33 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* __mingwthr_register_key_dtor (DWORD key, void (*dtor) (void *))
|
|
|
|
*
|
|
|
|
* Public interface called by C++ exception handling mechanism in
|
|
|
|
* libgcc (cf: __gthread_key_create).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
__declspec(dllexport)
|
|
|
|
int
|
|
|
|
__mingwthr_key_dtor (DWORD key, void (*dtor) (void *))
|
|
|
|
{
|
|
|
|
if (dtor)
|
|
|
|
{
|
2001-12-05 23:01:06 +08:00
|
|
|
return ___mingwthr_add_key_dtor (key, dtor);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-12-05 23:01:06 +08:00
|
|
|
__declspec(dllexport)
|
|
|
|
int
|
|
|
|
__mingwthr_remove_key_dtor (DWORD key )
|
|
|
|
{
|
|
|
|
return ___mingwthr_remove_key_dtor ( key );
|
|
|
|
}
|