Release 970101
[wine] / scheduler / thread.c
1 /*
2  * Win32 threads
3  *
4  * Copyright 1996 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include "thread.h"
9 #include "winerror.h"
10 #include "heap.h"
11 #include "selectors.h"
12 #include "winnt.h"
13
14 THDB *pCurrentThread = NULL;
15
16
17 /***********************************************************************
18  *           THREAD_Create
19  */
20 THDB *THREAD_Create( PDB32 *pdb, DWORD stack_size )
21 {
22     THDB *thdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(THDB) );
23     if (!thdb) return NULL;
24     thdb->header.type     = K32OBJ_THREAD;
25     thdb->header.refcount = 1;
26     thdb->process         = pdb;
27     thdb->teb.except      = (void *)-1;
28     thdb->teb.htask16     = 0; /* FIXME */
29     thdb->teb.stack_sel   = 0; /* FIXME */
30     thdb->teb.self        = &thdb->teb;
31     thdb->teb.tls_ptr     = thdb->tls_array;
32     thdb->process2        = pdb;
33     thdb->exit_code       = 0x103; /* STILL_ACTIVE */
34
35     /* Allocate the stack */
36
37     if (!stack_size) stack_size = 1024 * 1024;  /* default size = 1Mb */
38     thdb->stack_base = VirtualAlloc( NULL, stack_size, MEM_COMMIT,
39                                      PAGE_EXECUTE_READWRITE );
40     if (!thdb->stack_base) goto error;
41     /* Un-commit the first page (FIXME: should use PAGE_GUARD instead) */
42     VirtualFree( thdb->stack_base, 1, MEM_DECOMMIT );
43     thdb->teb.stack_top   = (char *)thdb->stack_base + stack_size;
44     thdb->teb.stack_low   = thdb->teb.stack_top;
45     thdb->exit_stack      = thdb->teb.stack_top;
46
47     /* Allocate the TEB selector (%fs register) */
48
49     thdb->teb_sel = SELECTOR_AllocBlock( &thdb->teb, 0x1000, SEGMENT_DATA,
50                                          TRUE, FALSE );
51     if (!thdb->teb_sel) goto error;
52     return thdb;
53
54 error:
55     if (thdb->teb_sel) SELECTOR_FreeBlock( thdb->teb_sel, 1 );
56     if (thdb->stack_base) VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
57     HeapFree( SystemHeap, 0, thdb );
58     return NULL;
59 }
60
61
62 /***********************************************************************
63  *           THREAD_Destroy
64  */
65 void THREAD_Destroy( K32OBJ *ptr )
66 {
67     THDB *thdb = (THDB *)ptr;
68     assert( ptr->type == K32OBJ_THREAD );
69     ptr->type = K32OBJ_UNKNOWN;
70     SELECTOR_FreeBlock( thdb->teb_sel, 1 );
71     HeapFree( SystemHeap, 0, thdb );
72 }
73
74
75 /***********************************************************************
76  *           GetCurrentThread   (KERNEL32.200)
77  */
78 HANDLE32 GetCurrentThread(void)
79 {
80     return 0xFFFFFFFE;
81 }
82
83
84 /***********************************************************************
85  *           GetCurrentThreadId   (KERNEL32.201)
86  */
87 DWORD GetCurrentThreadId(void)
88 {
89     /* FIXME: should probably use %fs register here */
90     assert( pCurrentThread );
91     return (DWORD)pCurrentThread;
92 }
93
94
95 /**********************************************************************
96  *           GetLastError   (KERNEL.148) (KERNEL32.227)
97  */
98 DWORD GetLastError(void)
99 {
100     THDB *thread = (THDB *)GetCurrentThreadId();
101     return thread->last_error;
102 }
103
104
105 /**********************************************************************
106  *           SetLastError   (KERNEL.147) (KERNEL32.497)
107  */
108 void SetLastError( DWORD error )
109 {
110     THDB *thread;
111     if (!pCurrentThread) return;  /* FIXME */
112     thread = (THDB *)GetCurrentThreadId();
113     thread->last_error = error;
114 }
115
116
117 /**********************************************************************
118  *           SetLastErrorEx   (USER32.484)
119  */
120 void SetLastErrorEx( DWORD error, DWORD type )
121 {
122     /* FIXME: what about 'type'? */
123     SetLastError( error );
124 }
125
126
127 /**********************************************************************
128  *           TlsAlloc   (KERNEL32.530)
129  */
130 DWORD TlsAlloc(void)
131 {
132     DWORD i, mask, ret = 0;
133     THDB *thread = (THDB *)GetCurrentThreadId();
134     DWORD *bits = thread->process->tls_bits;
135     EnterCriticalSection( &thread->process->crit_section );
136     if (*bits == 0xffffffff)
137     {
138         bits++;
139         ret = 32;
140         if (*bits == 0xffffffff)
141         {
142             LeaveCriticalSection( &thread->process->crit_section );
143             SetLastError( ERROR_NO_MORE_ITEMS );
144             return 0xffffffff;
145         }
146     }
147     for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
148     *bits |= mask;
149     LeaveCriticalSection( &thread->process->crit_section );
150     return ret + i;
151 }
152
153
154 /**********************************************************************
155  *           TlsFree   (KERNEL32.531)
156  */
157 BOOL32 TlsFree( DWORD index )
158 {
159     DWORD mask;
160     THDB *thread = (THDB *)GetCurrentThreadId();
161     DWORD *bits = thread->process->tls_bits;
162     if (index >= 64)
163     {
164         SetLastError( ERROR_INVALID_PARAMETER );
165         return FALSE;
166     }
167     EnterCriticalSection( &thread->process->crit_section );
168     if (index >= 32) bits++;
169     mask = (1 << (index & 31));
170     if (!(*bits & mask))  /* already free? */
171     {
172         LeaveCriticalSection( &thread->process->crit_section );
173         SetLastError( ERROR_INVALID_PARAMETER );
174         return FALSE;
175     }
176     *bits &= ~mask;
177     thread->tls_array[index] = 0;
178     /* FIXME: should zero all other thread values */
179     LeaveCriticalSection( &thread->process->crit_section );
180     return TRUE;
181 }
182
183
184 /**********************************************************************
185  *           TlsGetValue   (KERNEL32.532)
186  */
187 LPVOID TlsGetValue( DWORD index )
188 {
189     THDB *thread = (THDB *)GetCurrentThreadId();
190     if (index >= 64)
191     {
192         SetLastError( ERROR_INVALID_PARAMETER );
193         return NULL;
194     }
195     SetLastError( ERROR_SUCCESS );
196     return thread->tls_array[index];
197 }
198
199
200 /**********************************************************************
201  *           TlsSetValue   (KERNEL32.533)
202  */
203 BOOL32 TlsSetValue( DWORD index, LPVOID value )
204 {
205     THDB *thread = (THDB *)GetCurrentThreadId();
206     if (index >= 64)
207     {
208         SetLastError( ERROR_INVALID_PARAMETER );
209         return FALSE;
210     }
211     thread->tls_array[index] = value;
212     return TRUE;
213 }
214
215
216 /***********************************************************************
217  *           GetThreadContext   (KERNEL32.294)
218  */
219 BOOL32 GetThreadContext( HANDLE32 handle, CONTEXT *context )
220 {
221     return FALSE;
222 }
223
224
225 /**********************************************************************
226  *           NtCurrentTeb   (NTDLL.89)
227  */
228 void NtCurrentTeb( CONTEXT *context )
229 {
230     EAX_reg(context) = GetSelectorBase( FS_reg(context) );
231 }