mirror of
https://github.com/reactos/reactos.git
synced 2024-09-21 10:07:17 +00:00
103 lines
1.7 KiB
C
103 lines
1.7 KiB
C
#include <precomp.h>
|
|
#include <stdlib.h>
|
|
#include <internal/tls.h>
|
|
#include <internal/rterror.h>
|
|
|
|
|
|
static unsigned long TlsIndex = (unsigned long)-1;
|
|
|
|
|
|
static void InitThreadData(PTHREADDATA ThreadData)
|
|
{
|
|
ThreadData->terrno = 0;
|
|
ThreadData->tdoserrno = 0;
|
|
|
|
ThreadData->fpecode = 0;
|
|
|
|
ThreadData->tnext = 1;
|
|
|
|
/* FIXME: init more thread local data */
|
|
|
|
}
|
|
|
|
|
|
int CreateThreadData(void)
|
|
{
|
|
PTHREADDATA ThreadData;
|
|
|
|
TlsIndex = TlsAlloc();
|
|
if (TlsIndex == (unsigned long)-1)
|
|
return FALSE;
|
|
|
|
ThreadData = (PTHREADDATA)calloc(1, sizeof(THREADDATA));
|
|
if (ThreadData == NULL)
|
|
return FALSE;
|
|
|
|
if(!TlsSetValue(TlsIndex, (LPVOID)ThreadData))
|
|
return FALSE;
|
|
|
|
InitThreadData(ThreadData);
|
|
|
|
return TRUE;
|
|
}
|
|
|
|
|
|
void DestroyThreadData(void)
|
|
{
|
|
if (TlsIndex != (unsigned long)-1)
|
|
{
|
|
TlsFree(TlsIndex);
|
|
TlsIndex = (unsigned long)-1;
|
|
}
|
|
}
|
|
|
|
|
|
void FreeThreadData(PTHREADDATA ThreadData)
|
|
{
|
|
if (TlsIndex != (unsigned long)-1)
|
|
{
|
|
if (ThreadData == NULL)
|
|
ThreadData = TlsGetValue(TlsIndex);
|
|
|
|
if (ThreadData != NULL)
|
|
{
|
|
/* FIXME: free more thread local data */
|
|
|
|
free(ThreadData);
|
|
}
|
|
|
|
TlsSetValue(TlsIndex, NULL);
|
|
}
|
|
}
|
|
|
|
|
|
PTHREADDATA GetThreadData(void)
|
|
{
|
|
PTHREADDATA ThreadData;
|
|
DWORD LastError;
|
|
|
|
LastError = GetLastError();
|
|
ThreadData = TlsGetValue(TlsIndex);
|
|
if (ThreadData == NULL)
|
|
{
|
|
ThreadData = (PTHREADDATA)calloc(1, sizeof(THREADDATA));
|
|
if (ThreadData != NULL)
|
|
{
|
|
TlsSetValue(TlsIndex, (LPVOID)ThreadData);
|
|
|
|
InitThreadData(ThreadData);
|
|
}
|
|
else
|
|
{
|
|
_amsg_exit(_RT_THREAD); /* write message and die */
|
|
}
|
|
}
|
|
|
|
SetLastError(LastError);
|
|
|
|
return ThreadData;
|
|
}
|
|
|
|
/* EOF */
|
|
|