4 * Copyright 1996 Alexandre Julliard
14 #include "selectors.h"
23 static BOOL32
THREAD_Signaled( K32OBJ
*obj
, DWORD thread_id
);
24 static BOOL32
THREAD_Satisfied( K32OBJ
*obj
, DWORD thread_id
);
25 static void THREAD_AddWait( K32OBJ
*obj
, DWORD thread_id
);
26 static void THREAD_RemoveWait( K32OBJ
*obj
, DWORD thread_id
);
27 static void THREAD_Destroy( K32OBJ
*obj
);
29 const K32OBJ_OPS THREAD_Ops
=
31 THREAD_Signaled
, /* signaled */
32 THREAD_Satisfied
, /* satisfied */
33 THREAD_AddWait
, /* add_wait */
34 THREAD_RemoveWait
, /* remove_wait */
37 THREAD_Destroy
/* destroy */
40 /* The pseudohandle used for the current thread, see GetCurrentThread */
41 #define CURRENT_THREAD_PSEUDOHANDLE 0xfffffffe
43 /* Is threading code initialized? */
44 BOOL32 THREAD_InitDone
= FALSE
;
46 /**********************************************************************
49 * Return a pointer to a thread object. The object count must be decremented
50 * when no longer used.
52 THDB
*THREAD_GetPtr( HANDLE32 handle
, DWORD access
)
56 if (handle
== CURRENT_THREAD_PSEUDOHANDLE
) /* Self-thread handle */
58 thread
= THREAD_Current();
59 K32OBJ_IncCount( &thread
->header
);
61 else thread
= (THDB
*)HANDLE_GetObjPtr( PROCESS_Current(), handle
,
62 K32OBJ_THREAD
, access
);
67 /***********************************************************************
70 * Return the current thread THDB pointer.
72 THDB
*THREAD_Current(void)
74 if (!THREAD_InitDone
) return NULL
;
75 return (THDB
*)((char *)NtCurrentTeb() - (int)&((THDB
*)0)->teb
);
79 /***********************************************************************
82 * Add a thread to a queue.
84 void THREAD_AddQueue( THREAD_QUEUE
*queue
, THDB
*thread
)
86 THREAD_ENTRY
*entry
= HeapAlloc( SystemHeap
, HEAP_NO_SERIALIZE
,
90 entry
->thread
= thread
;
93 entry
->next
= (*queue
)->next
;
94 (*queue
)->next
= entry
;
96 else entry
->next
= entry
;
101 /***********************************************************************
104 * Remove a thread from a queue.
106 void THREAD_RemoveQueue( THREAD_QUEUE
*queue
, THDB
*thread
)
108 THREAD_ENTRY
*entry
= *queue
;
110 if (entry
->next
== entry
) /* Only one element in the queue */
112 assert( entry
->thread
== thread
);
118 while (entry
->next
->thread
!= thread
)
121 assert( entry
!= *queue
); /* Have we come all the way around? */
123 if ((next
= entry
->next
) == *queue
) *queue
= entry
;
124 entry
->next
= entry
->next
->next
;
125 entry
= next
; /* This is the one we want to free */
127 HeapFree( SystemHeap
, 0, entry
);
132 /***********************************************************************
135 THDB
*THREAD_Create( PDB32
*pdb
, DWORD stack_size
, BOOL32 alloc_stack16
,
136 LPTHREAD_START_ROUTINE start_addr
, LPVOID param
)
141 THDB
*thdb
= HeapAlloc( SystemHeap
, HEAP_ZERO_MEMORY
, sizeof(THDB
) );
142 if (!thdb
) return NULL
;
143 thdb
->header
.type
= K32OBJ_THREAD
;
144 thdb
->header
.refcount
= 1;
146 thdb
->teb
.except
= (void *)-1;
147 thdb
->teb
.htask16
= 0; /* FIXME */
148 thdb
->teb
.self
= &thdb
->teb
;
149 thdb
->teb
.tls_ptr
= thdb
->tls_array
;
150 thdb
->teb
.process
= pdb
;
151 thdb
->wait_list
= &thdb
->wait_struct
;
152 thdb
->exit_code
= 0x103; /* STILL_ACTIVE */
153 thdb
->entry_point
= start_addr
;
154 thdb
->entry_arg
= param
;
156 /* Allocate the stack */
159 * If stacksize smaller than 1 MB, allocate 1MB
160 * (one program wanted only 10 kB, which is recommendable, but some WINE
161 * functions, noteably in the files subdir, push HUGE structures and
162 * arrays on the stack. They probably shouldn't.)
163 * If stacksize larger than 16 MB, warn the user. (We could shrink the stack
164 * but this could give more or less unexplainable crashes.)
166 if (stack_size
<1024*1024)
167 stack_size
= 1024 * 1024;
168 if (stack_size
>= 16*1024*1024)
169 WARN(thread
,"Thread stack size is %ld MB.\n",stack_size
/1024/1024);
170 thdb
->stack_base
= VirtualAlloc(NULL
,
171 stack_size
+ (alloc_stack16
? 0x10000 : 0),
172 MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
173 if (!thdb
->stack_base
) goto error
;
174 /* Set a guard page at the bottom of the stack */
175 VirtualProtect( thdb
->stack_base
, 1, PAGE_EXECUTE_READWRITE
| PAGE_GUARD
,
177 thdb
->teb
.stack_top
= (char *)thdb
->stack_base
+ stack_size
;
178 thdb
->teb
.stack_low
= thdb
->stack_base
;
179 thdb
->exit_stack
= thdb
->teb
.stack_top
;
181 /* Allocate the TEB selector (%fs register) */
183 thdb
->teb_sel
= SELECTOR_AllocBlock( &thdb
->teb
, 0x1000, SEGMENT_DATA
,
185 if (!thdb
->teb_sel
) goto error
;
187 /* Allocate the 16-bit stack selector */
191 thdb
->teb
.stack_sel
= SELECTOR_AllocBlock( thdb
->teb
.stack_top
,
192 0x10000, SEGMENT_DATA
,
194 if (!thdb
->teb
.stack_sel
) goto error
;
195 thdb
->cur_stack
= PTR_SEG_OFF_TO_SEGPTR( thdb
->teb
.stack_sel
, 0xfffc );
198 /* Allocate the event */
200 if (!(thdb
->event
= EVENT_Create( TRUE
, FALSE
))) goto error
;
202 /* Initialize the thread context */
206 thdb
->pcontext
= &thdb
->context
;
207 thdb
->context
.SegCs
= cs
;
208 thdb
->context
.SegDs
= ds
;
209 thdb
->context
.SegEs
= ds
;
210 thdb
->context
.SegGs
= ds
;
211 thdb
->context
.SegSs
= ds
;
212 thdb
->context
.SegFs
= thdb
->teb_sel
;
213 thdb
->context
.Eip
= (DWORD
)start_addr
;
214 thdb
->context
.Esp
= (DWORD
)thdb
->teb
.stack_top
;
219 if (thdb
->event
) K32OBJ_DecCount( thdb
->event
);
220 if (thdb
->teb
.stack_sel
) SELECTOR_FreeBlock( thdb
->teb
.stack_sel
, 1 );
221 if (thdb
->teb_sel
) SELECTOR_FreeBlock( thdb
->teb_sel
, 1 );
222 if (thdb
->stack_base
) VirtualFree( thdb
->stack_base
, 0, MEM_RELEASE
);
223 HeapFree( SystemHeap
, 0, thdb
);
228 /***********************************************************************
231 static BOOL32
THREAD_Signaled( K32OBJ
*obj
, DWORD thread_id
)
233 THDB
*thdb
= (THDB
*)obj
;
234 assert( obj
->type
== K32OBJ_THREAD
);
235 return K32OBJ_OPS( thdb
->event
)->signaled( thdb
->event
, thread_id
);
239 /***********************************************************************
242 * Wait on this object has been satisfied.
244 static BOOL32
THREAD_Satisfied( K32OBJ
*obj
, DWORD thread_id
)
246 THDB
*thdb
= (THDB
*)obj
;
247 assert( obj
->type
== K32OBJ_THREAD
);
248 return K32OBJ_OPS( thdb
->event
)->satisfied( thdb
->event
, thread_id
);
252 /***********************************************************************
255 * Add thread to object wait queue.
257 static void THREAD_AddWait( K32OBJ
*obj
, DWORD thread_id
)
259 THDB
*thdb
= (THDB
*)obj
;
260 assert( obj
->type
== K32OBJ_THREAD
);
261 return K32OBJ_OPS( thdb
->event
)->add_wait( thdb
->event
, thread_id
);
265 /***********************************************************************
268 * Remove thread from object wait queue.
270 static void THREAD_RemoveWait( K32OBJ
*obj
, DWORD thread_id
)
272 THDB
*thdb
= (THDB
*)obj
;
273 assert( obj
->type
== K32OBJ_THREAD
);
274 return K32OBJ_OPS( thdb
->event
)->remove_wait( thdb
->event
, thread_id
);
278 /***********************************************************************
281 static void THREAD_Destroy( K32OBJ
*ptr
)
283 THDB
*thdb
= (THDB
*)ptr
;
285 assert( ptr
->type
== K32OBJ_THREAD
);
286 ptr
->type
= K32OBJ_UNKNOWN
;
288 /* Free the associated memory */
292 /* Check if we are deleting the current thread */
295 if (fs
== thdb
->teb_sel
)
302 K32OBJ_DecCount( thdb
->event
);
303 SELECTOR_FreeBlock( thdb
->teb_sel
, 1 );
304 if (thdb
->teb
.stack_sel
) SELECTOR_FreeBlock( thdb
->teb
.stack_sel
, 1 );
305 HeapFree( SystemHeap
, 0, thdb
);
310 /***********************************************************************
311 * CreateThread (KERNEL32.63)
313 HANDLE32 WINAPI
CreateThread( SECURITY_ATTRIBUTES
*sa
, DWORD stack
,
314 LPTHREAD_START_ROUTINE start
, LPVOID param
,
315 DWORD flags
, LPDWORD id
)
318 BOOL32 inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
320 THDB
*thread
= THREAD_Create( PROCESS_Current(), stack
,
321 TRUE
, start
, param
);
322 if (!thread
) return INVALID_HANDLE_VALUE32
;
323 handle
= HANDLE_Alloc( PROCESS_Current(), &thread
->header
,
324 THREAD_ALL_ACCESS
, inherit
);
325 if (handle
== INVALID_HANDLE_VALUE32
) goto error
;
326 if (SYSDEPS_SpawnThread( thread
) == -1) goto error
;
327 *id
= THDB_TO_THREAD_ID( thread
);
331 K32OBJ_DecCount( &thread
->header
);
332 return INVALID_HANDLE_VALUE32
;
336 /***********************************************************************
337 * ExitThread [KERNEL32.215] Ends a thread
342 void WINAPI
ExitThread(
343 DWORD code
) /* [in] Exit code for this thread */
345 THDB
*thdb
= THREAD_Current();
349 thdb
->exit_code
= code
;
350 EVENT_Set( thdb
->event
);
352 /* Abandon all owned mutexes */
353 while (thdb
->mutex_list
) MUTEX_Abandon( thdb
->mutex_list
);
355 /* FIXME: should free the stack somehow */
357 /* FIXME: We cannot do this; once the current thread is destroyed,
358 synchronization primitives do not work properly. */
359 K32OBJ_DecCount( &thdb
->header
);
361 /* Completely unlock the system lock just in case */
362 count
= SYSTEM_LOCK_COUNT();
363 while (count
--) SYSTEM_UNLOCK();
364 SYSDEPS_ExitThread();
368 /***********************************************************************
369 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
372 * Pseudohandle for the current thread
374 HANDLE32 WINAPI
GetCurrentThread(void)
376 return CURRENT_THREAD_PSEUDOHANDLE
;
380 /***********************************************************************
381 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
384 * Thread identifier of calling thread
386 DWORD WINAPI
GetCurrentThreadId(void)
388 return THDB_TO_THREAD_ID( THREAD_Current() );
392 /**********************************************************************
393 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
396 * Calling thread's last error code value.
398 DWORD WINAPI
GetLastError(void)
400 THDB
*thread
= THREAD_Current();
401 return thread
->last_error
;
405 /**********************************************************************
406 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
411 void WINAPI
SetLastError(
412 DWORD error
) /* [in] Per-thread error code */
414 THDB
*thread
= THREAD_Current();
415 /* This one must work before we have a thread (FIXME) */
416 if (thread
) thread
->last_error
= error
;
420 /**********************************************************************
421 * SetLastErrorEx [USER32.485] Sets the last-error code.
426 void WINAPI
SetLastErrorEx(
427 DWORD error
, /* [in] Per-thread error code */
428 DWORD type
) /* [in] Error type */
430 TRACE(thread
, "(%08lx, %08lx)\n", error
,type
);
437 /* Fall through for now */
439 FIXME(thread
, "(error=%08lx, type=%08lx): Unhandled type\n", error
,type
);
442 SetLastError( error
);
446 /**********************************************************************
449 DWORD
THREAD_TlsAlloc(THDB
*thread
)
451 DWORD i
, mask
, ret
= 0;
452 DWORD
*bits
= thread
->process
->tls_bits
;
453 EnterCriticalSection( &thread
->process
->crit_section
);
454 if (*bits
== 0xffffffff)
458 if (*bits
== 0xffffffff)
460 LeaveCriticalSection( &thread
->process
->crit_section
);
461 SetLastError( ERROR_NO_MORE_ITEMS
);
465 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
467 LeaveCriticalSection( &thread
->process
->crit_section
);
472 /**********************************************************************
473 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
475 * Allocates a thread local storage index
479 * Failure: 0xFFFFFFFF
481 DWORD WINAPI
TlsAlloc(void)
483 return THREAD_TlsAlloc(THREAD_Current());
487 /**********************************************************************
488 * TlsFree [KERNEL32.531] Releases a TLS index.
490 * Releases a thread local storage index, making it available for reuse
496 BOOL32 WINAPI
TlsFree(
497 DWORD index
) /* [in] TLS Index to free */
500 THDB
*thread
= THREAD_Current();
501 DWORD
*bits
= thread
->process
->tls_bits
;
504 SetLastError( ERROR_INVALID_PARAMETER
);
507 EnterCriticalSection( &thread
->process
->crit_section
);
508 if (index
>= 32) bits
++;
509 mask
= (1 << (index
& 31));
510 if (!(*bits
& mask
)) /* already free? */
512 LeaveCriticalSection( &thread
->process
->crit_section
);
513 SetLastError( ERROR_INVALID_PARAMETER
);
517 thread
->tls_array
[index
] = 0;
518 /* FIXME: should zero all other thread values */
519 LeaveCriticalSection( &thread
->process
->crit_section
);
524 /**********************************************************************
525 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
528 * Success: Value stored in calling thread's TLS slot for index
529 * Failure: 0 and GetLastError returns NO_ERROR
531 LPVOID WINAPI
TlsGetValue(
532 DWORD index
) /* [in] TLS index to retrieve value for */
534 THDB
*thread
= THREAD_Current();
537 SetLastError( ERROR_INVALID_PARAMETER
);
540 SetLastError( ERROR_SUCCESS
);
541 return thread
->tls_array
[index
];
545 /**********************************************************************
546 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
552 BOOL32 WINAPI
TlsSetValue(
553 DWORD index
, /* [in] TLS index to set value for */
554 LPVOID value
) /* [in] Value to be stored */
556 THDB
*thread
= THREAD_Current();
559 SetLastError( ERROR_INVALID_PARAMETER
);
562 thread
->tls_array
[index
] = value
;
567 /***********************************************************************
568 * SetThreadContext [KERNEL32.670] Sets context of thread.
574 BOOL32 WINAPI
SetThreadContext(
575 HANDLE32 handle
, /* [in] Handle to thread with context */
576 CONTEXT
*context
) /* [out] Address of context structure */
578 THDB
*thread
= THREAD_GetPtr( handle
, THREAD_GET_CONTEXT
);
579 if (!thread
) return FALSE
;
580 *context
= thread
->context
;
581 K32OBJ_DecCount( &thread
->header
);
585 /***********************************************************************
586 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
592 BOOL32 WINAPI
GetThreadContext(
593 HANDLE32 handle
, /* [in] Handle to thread with context */
594 CONTEXT
*context
) /* [out] Address of context structure */
596 THDB
*thread
= THREAD_GetPtr( handle
, THREAD_GET_CONTEXT
);
597 if (!thread
) return FALSE
;
598 *context
= thread
->context
;
599 K32OBJ_DecCount( &thread
->header
);
604 /**********************************************************************
605 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
608 * Success: Thread's priority level.
609 * Failure: THREAD_PRIORITY_ERROR_RETURN
611 INT32 WINAPI
GetThreadPriority(
612 HANDLE32 hthread
) /* [in] Handle to thread */
617 if (!(thread
= THREAD_GetPtr( hthread
, THREAD_QUERY_INFORMATION
)))
618 return THREAD_PRIORITY_ERROR_RETURN
;
619 ret
= thread
->delta_priority
;
620 K32OBJ_DecCount( &thread
->header
);
625 /**********************************************************************
626 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
632 BOOL32 WINAPI
SetThreadPriority(
633 HANDLE32 hthread
, /* [in] Handle to thread */
634 INT32 priority
) /* [in] Thread priority level */
638 if (!(thread
= THREAD_GetPtr( hthread
, THREAD_SET_INFORMATION
)))
640 thread
->delta_priority
= priority
;
641 K32OBJ_DecCount( &thread
->header
);
646 /**********************************************************************
647 * TerminateThread [KERNEL32.???] Terminates a thread
653 BOOL32 WINAPI
TerminateThread(
654 HANDLE32 handle
, /* [in] Handle to thread */
655 DWORD exitcode
) /* [in] Exit code for thread */
657 FIXME(thread
,"(0x%08x,%ld): stub\n",handle
,exitcode
);
658 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
663 /**********************************************************************
664 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
670 BOOL32 WINAPI
GetExitCodeThread(
671 HANDLE32 hthread
, /* [in] Handle to thread */
672 LPDWORD exitcode
) /* [out] Address to receive termination status */
676 if (!(thread
= THREAD_GetPtr( hthread
, THREAD_QUERY_INFORMATION
)))
678 if (exitcode
) *exitcode
= thread
->exit_code
;
679 K32OBJ_DecCount( &thread
->header
);
684 /**********************************************************************
685 * ResumeThread [KERNEL32.587] Resumes a thread.
687 * Decrements a thread's suspend count. When count is zero, the
688 * execution of the thread is resumed.
691 * Success: Previous suspend count
692 * Failure: 0xFFFFFFFF
695 DWORD WINAPI
ResumeThread(
696 HANDLE32 hthread
) /* [in] Indentifies thread to restart */
702 if (!(thread
= THREAD_GetPtr( hthread
, THREAD_QUERY_INFORMATION
)))
705 WARN(thread
, "Invalid thread handle\n");
708 if ((oldcount
= thread
->suspend_count
) != 0)
710 if (!--thread
->suspend_count
)
712 if (kill(thread
->unix_pid
, SIGCONT
))
714 WARN(thread
, "Unable to CONTinue pid: %04x\n",
716 oldcount
= 0xFFFFFFFF;
720 K32OBJ_DecCount(&thread
->header
);
726 /**********************************************************************
727 * SuspendThread [KERNEL32.681] Suspends a thread.
730 * Success: Previous suspend count
731 * Failure: 0xFFFFFFFF
733 DWORD WINAPI
SuspendThread(
734 HANDLE32 hthread
) /* [in] Handle to the thread */
740 if (!(thread
= THREAD_GetPtr( hthread
, THREAD_QUERY_INFORMATION
)))
743 WARN(thread
, "Invalid thread handle\n");
747 if (!(oldcount
= thread
->suspend_count
))
749 if (thread
->unix_pid
== getpid())
750 WARN(thread
, "Attempting to suspend myself\n" );
753 if (kill(thread
->unix_pid
, SIGSTOP
))
755 WARN(thread
, "Unable to STOP pid: %04x\n",
757 oldcount
= 0xFFFFFFFF;
759 else thread
->suspend_count
++;
762 else thread
->suspend_count
++;
763 K32OBJ_DecCount( &thread
->header
);
770 /**********************************************************************
771 * GetThreadTimes [KERNEL32.???] Obtains timing information.
774 * What are the fields where these values are stored?
780 BOOL32 WINAPI
GetThreadTimes(
781 HANDLE32 thread
, /* [in] Specifies the thread of interest */
782 LPFILETIME creationtime
, /* [out] When the thread was created */
783 LPFILETIME exittime
, /* [out] When the thread was destroyed */
784 LPFILETIME kerneltime
, /* [out] Time thread spent in kernel mode */
785 LPFILETIME usertime
) /* [out] Time thread spent in user mode */
787 FIXME(thread
,"(0x%08x): stub\n",thread
);
788 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
793 /**********************************************************************
794 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
796 * Attaches the input processing mechanism of one thread to that of
803 BOOL32 WINAPI
AttachThreadInput(
804 DWORD idAttach
, /* [in] Thread to attach */
805 DWORD idAttachTo
, /* [in] Thread to attach to */
806 BOOL32 fAttach
) /* [in] Attach or detach */
810 FIXME(thread
, "(0x%08lx,0x%08lx,%d): stub\n",idAttach
,idAttachTo
,fAttach
);
811 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);