Release 961208
[wine/multimedia.git] / win32 / thread.c
blobae020f7aa8ee587d58080d6b749c7a8291d97864
1 /*
2 * Win32 kernel functions
4 * Copyright 1995 Martin von Loewis
5 */
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include "windows.h"
11 #include "winbase.h"
12 #include "winerror.h"
13 #include "stddebug.h"
14 #include "debug.h"
15 #include "xmalloc.h"
17 /***********************************************************************
18 * GetCurrentThreadId (KERNEL32.201)
21 DWORD GetCurrentThreadId(void)
23 /* Windows 95 returns the address of the thread database (sorta) */
24 return MAKELONG(GetCurrentTask(), 0);
27 /***********************************************************************
28 * GetThreadContext (KERNEL32.294)
30 BOOL GetThreadContext(HANDLE32 hThread, void *lpContext)
32 return FALSE;
34 /***********************************************************************
35 * GetCurrentThread (KERNEL32.200)
37 HANDLE32 GetCurrentThread(void)
39 return 0xFFFFFFFE; /* that's -2 */
42 /**********************************************************************
43 * Critical Sections are currently ignored
45 void InitializeCriticalSection(CRITICAL_SECTION *lpCrit)
47 memset(lpCrit,0,sizeof(CRITICAL_SECTION));
50 void EnterCriticalSection(CRITICAL_SECTION* lpCrit)
52 if (lpCrit->LockCount)
53 fprintf( stderr, "Error: re-entering critical section %08lx\n",
54 (DWORD)lpCrit );
55 lpCrit->LockCount++;
58 void LeaveCriticalSection(CRITICAL_SECTION* lpCrit)
60 if (!lpCrit->LockCount)
61 fprintf( stderr, "Error: leaving critical section %08lx again\n",
62 (DWORD)lpCrit );
63 lpCrit->LockCount--;
66 void DeleteCriticalSection(CRITICAL_SECTION* lpCrit)
68 return;
71 void ReinitializeCriticalSection(CRITICAL_SECTION *lpCrit) {
72 /* hmm */
75 void MakeCriticalSectionGlobal(CRITICAL_SECTION *lpCrit) {
76 /* hmm */
79 /***********************************************************************
80 * Tls is available only for the single thread
81 * (BTW: TLS means Thread Local Storage)
83 static LPVOID* Tls=0;
84 static int TlsCount=0;
86 DWORD TlsAlloc()
88 if(!Tls){
89 TlsCount++;
90 Tls=xmalloc(sizeof(LPVOID));
91 /* Tls needs to be zero initialized */
92 Tls[0]=0;
93 return 0;
95 Tls=xrealloc(Tls,sizeof(LPVOID)*(++TlsCount));
96 Tls[TlsCount-1]=0;
97 return TlsCount-1;
100 void TlsFree(DWORD index)
102 /*FIXME: should remember that it has been freed */
103 return;
106 LPVOID TlsGetValue(DWORD index)
108 if(index>=TlsCount)
110 /* FIXME: Set last error*/
111 return 0;
113 return Tls[index];
116 BOOL32 TlsSetValue(DWORD index,LPVOID value)
118 if(index>=TlsCount)
120 /* FIXME: Set last error*/
121 return FALSE;
123 Tls[index]=value;
124 return TRUE;
127 /* FIXME: This is required to work cross-addres space as well */
128 static CRITICAL_SECTION interlocked;
129 static int interlocked_init;
131 static void get_interlocked()
133 if(!interlocked_init)
134 InitializeCriticalSection(&interlocked);
135 interlocked_init=1;
136 EnterCriticalSection(&interlocked);
139 static void release_interlocked()
141 LeaveCriticalSection(&interlocked);
144 /***********************************************************************
145 * InterlockedIncrement
147 LONG InterlockedIncrement(LPLONG lpAddend)
149 int ret;
150 get_interlocked();
151 (*lpAddend)++;
152 ret=*lpAddend;
153 release_interlocked();
154 return ret;
157 /***********************************************************************
158 * InterlockedDecrement
160 LONG InterlockedDecrement(LPLONG lpAddend)
162 int ret;
163 get_interlocked();
164 (*lpAddend)--;
165 ret=*lpAddend;
166 release_interlocked();
167 return ret;
170 /***********************************************************************
171 * InterlockedExchange
173 LONG InterlockedExchange(LPLONG target, LONG value)
175 int ret;
176 get_interlocked();
177 ret=*target;
178 *target=value;
179 release_interlocked();
180 return ret;