Release 960712
[wine] / win32 / thread.c
1 /*
2  * Win32 kernel functions
3  *
4  * Copyright 1995 Martin von Loewis
5  */
6
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 "kernel32.h"
14 #include "stddebug.h"
15 #include "debug.h"
16 #include "xmalloc.h"
17
18 /***********************************************************************
19  *           GetCurrentThreadId   (KERNEL32.200)
20  */
21
22 int GetCurrentThreadId(void)
23 {
24         return getpid();
25 }
26
27 /***********************************************************************
28  *           GetThreadContext         (KERNEL32.294)
29  */
30 BOOL GetThreadContext(HANDLE hThread, void *lpContext)
31 {
32         return FALSE;
33 }
34 /***********************************************************************
35  *           GetCurrentThread    (KERNEL32.200)
36  */
37 HANDLE GetCurrentThread(void)
38 {
39         return 0;
40 }
41
42 /**********************************************************************
43  *          Critical Sections are currently ignored
44  */
45 void InitializeCriticalSection(CRITICAL_SECTION *lpCrit)
46 {
47         memset(lpCrit,0,sizeof(CRITICAL_SECTION));
48 }
49
50 void EnterCriticalSection(CRITICAL_SECTION* lpCrit)
51 {
52     if (lpCrit->LockCount)
53         fprintf( stderr, "Error: re-entering critical section %08lx\n",
54                  (DWORD)lpCrit );
55     lpCrit->LockCount++;
56 }
57
58 void LeaveCriticalSection(CRITICAL_SECTION* lpCrit)
59 {
60     if (!lpCrit->LockCount)
61         fprintf( stderr, "Error: leaving critical section %08lx again\n",
62                  (DWORD)lpCrit );
63     lpCrit->LockCount--;
64 }
65
66 void DeleteCriticalSection(CRITICAL_SECTION* lpCrit)
67 {
68         return;
69 }
70
71 /***********************************************************************
72  *           Tls is available only for the single thread
73  */
74 static LPVOID* Tls=0;
75 static int TlsCount=0;
76
77 DWORD TlsAlloc()
78 {
79         if(!Tls){
80                 TlsCount++;
81                 Tls=xmalloc(sizeof(LPVOID));
82                 /* Tls needs to be zero initialized */
83                 Tls[0]=0;
84                 return 0;
85         }
86         Tls=xrealloc(Tls,sizeof(LPVOID)*(++TlsCount));
87         Tls[TlsCount-1]=0;
88         return TlsCount-1;
89 }
90
91 void TlsFree(DWORD index)
92 {
93         /*FIXME: should remember that it has been freed */
94         return;
95 }
96
97 LPVOID TlsGetValue(DWORD index)
98 {
99         if(index>=TlsCount)
100         {
101                 /* FIXME: Set last error*/
102                 return 0;
103         }
104         return Tls[index];
105 }
106
107 void TlsSetValue(DWORD index,LPVOID value)
108 {
109         if(index>=TlsCount)
110         {
111                 /* FIXME: Set last error*/
112                 return;
113         }
114         Tls[index]=value;
115 }
116
117 /* FIXME: This is required to work cross-addres space as well */
118 static CRITICAL_SECTION interlocked;
119 static int interlocked_init;
120
121 static void get_interlocked()
122 {
123         if(!interlocked_init)
124                 InitializeCriticalSection(&interlocked);
125         interlocked_init=1;
126         EnterCriticalSection(&interlocked);
127 }
128
129 static void release_interlocked()
130 {
131         LeaveCriticalSection(&interlocked);
132 }
133
134 /***********************************************************************
135  *           InterlockedIncrement
136  */
137 LONG InterlockedIncrement(LPLONG lpAddend)
138 {
139         int ret;
140         get_interlocked();
141         (*lpAddend)++;
142         ret=*lpAddend;
143         release_interlocked();
144         return ret;
145 }
146
147 /***********************************************************************
148  *           InterlockedDecrement
149  */
150 LONG InterlockedDecrement(LPLONG lpAddend)
151 {
152         int ret;
153         get_interlocked();
154         (*lpAddend)--;
155         ret=*lpAddend;
156         release_interlocked();
157         return ret;
158 }
159
160 /***********************************************************************
161  *           InterlockedExchange
162  */
163 LONG InterlockedExchange(LPLONG target, LONG value)
164 {
165         int ret;
166         get_interlocked();
167         ret=*target;
168         *target=value;
169         release_interlocked();
170         return ret;
171 }