4 * Copyright 1996 Alexandre Julliard
17 #include "selectors.h"
21 #include "stackframe.h"
28 static void THREAD_Destroy( K32OBJ
*obj
);
30 const K32OBJ_OPS THREAD_Ops
=
32 THREAD_Destroy
/* destroy */
35 /* Is threading code initialized? */
36 BOOL32 THREAD_InitDone
= FALSE
;
38 /**********************************************************************
41 * Return a pointer to a thread object. The object count must be decremented
42 * when no longer used.
44 THDB
*THREAD_GetPtr( HANDLE32 handle
, DWORD access
, int *server_handle
)
46 return (THDB
*)HANDLE_GetObjPtr( PROCESS_Current(), handle
,
47 K32OBJ_THREAD
, access
, server_handle
);
51 /***********************************************************************
54 * Return the current thread THDB pointer.
56 THDB
*THREAD_Current(void)
58 if (!THREAD_InitDone
) return NULL
;
59 return (THDB
*)((char *)NtCurrentTeb() - (int)&((THDB
*)0)->teb
);
62 /***********************************************************************
65 BOOL32
THREAD_IsWin16( THDB
*thdb
)
67 if (!thdb
|| !thdb
->process
)
71 TDB
* pTask
= (TDB
*)GlobalLock16( thdb
->process
->task
);
72 return !pTask
|| pTask
->thdb
== thdb
;
76 /***********************************************************************
79 * Convert a thread id to a THDB, making sure it is valid.
81 THDB
*THREAD_IdToTHDB( DWORD id
)
85 if (!id
) return THREAD_Current();
86 thdb
= THREAD_ID_TO_THDB( id
);
87 if (!K32OBJ_IsValid( &thdb
->header
, K32OBJ_THREAD
))
89 /* Allow task handles to be used; convert to main thread */
92 TDB
*pTask
= (TDB
*)GlobalLock16( id
);
93 if (pTask
) return pTask
->thdb
;
96 SetLastError( ERROR_INVALID_PARAMETER
);
103 /***********************************************************************
106 * Add a thread to a queue.
108 void THREAD_AddQueue( THREAD_QUEUE
*queue
, THDB
*thread
)
110 THREAD_ENTRY
*entry
= HeapAlloc( SystemHeap
, HEAP_NO_SERIALIZE
,
114 entry
->thread
= thread
;
117 entry
->next
= (*queue
)->next
;
118 (*queue
)->next
= entry
;
120 else entry
->next
= entry
;
125 /***********************************************************************
128 * Remove a thread from a queue.
130 void THREAD_RemoveQueue( THREAD_QUEUE
*queue
, THDB
*thread
)
132 THREAD_ENTRY
*entry
= *queue
;
134 if (entry
->next
== entry
) /* Only one element in the queue */
136 assert( entry
->thread
== thread
);
142 while (entry
->next
->thread
!= thread
)
145 assert( entry
!= *queue
); /* Have we come all the way around? */
147 if ((next
= entry
->next
) == *queue
) *queue
= entry
;
148 entry
->next
= entry
->next
->next
;
149 entry
= next
; /* This is the one we want to free */
151 HeapFree( SystemHeap
, 0, entry
);
156 /***********************************************************************
159 THDB
*THREAD_Create( PDB32
*pdb
, DWORD stack_size
, BOOL32 alloc_stack16
,
160 int *server_thandle
, int *server_phandle
,
161 LPTHREAD_START_ROUTINE start_addr
, LPVOID param
)
166 THDB
*thdb
= HeapAlloc( SystemHeap
, HEAP_ZERO_MEMORY
, sizeof(THDB
) );
167 if (!thdb
) return NULL
;
168 thdb
->header
.type
= K32OBJ_THREAD
;
169 thdb
->header
.refcount
= 1;
171 thdb
->teb
.except
= (void *)-1;
172 thdb
->teb
.htask16
= 0; /* FIXME */
173 thdb
->teb
.self
= &thdb
->teb
;
174 thdb
->teb
.flags
= (pdb
->flags
& PDB32_WIN16_PROC
)? 0 : TEBF_WIN32
;
175 thdb
->teb
.tls_ptr
= thdb
->tls_array
;
176 thdb
->teb
.process
= pdb
;
177 thdb
->wait_list
= &thdb
->wait_struct
;
178 thdb
->exit_code
= 0x103; /* STILL_ACTIVE */
179 thdb
->entry_point
= start_addr
;
180 thdb
->entry_arg
= param
;
183 /* Allocate the stack */
186 * If stacksize smaller than 1 MB, allocate 1MB
187 * (one program wanted only 10 kB, which is recommendable, but some WINE
188 * functions, noteably in the files subdir, push HUGE structures and
189 * arrays on the stack. They probably shouldn't.)
190 * If stacksize larger than 16 MB, warn the user. (We could shrink the stack
191 * but this could give more or less unexplainable crashes.)
193 if (stack_size
<1024*1024)
194 stack_size
= 1024 * 1024;
195 if (stack_size
>= 16*1024*1024)
196 WARN(thread
,"Thread stack size is %ld MB.\n",stack_size
/1024/1024);
197 thdb
->stack_base
= VirtualAlloc(NULL
,
198 stack_size
+ (alloc_stack16
? 0x10000 : 0),
199 MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
200 if (!thdb
->stack_base
) goto error
;
201 /* Set a guard page at the bottom of the stack */
202 VirtualProtect( thdb
->stack_base
, 1, PAGE_EXECUTE_READWRITE
| PAGE_GUARD
,
204 thdb
->teb
.stack_top
= (char *)thdb
->stack_base
+ stack_size
;
205 thdb
->teb
.stack_low
= thdb
->stack_base
;
206 thdb
->exit_stack
= thdb
->teb
.stack_top
;
208 /* Allocate the TEB selector (%fs register) */
210 thdb
->teb_sel
= SELECTOR_AllocBlock( &thdb
->teb
, 0x1000, SEGMENT_DATA
,
212 if (!thdb
->teb_sel
) goto error
;
214 /* Allocate the 16-bit stack selector */
218 thdb
->teb
.stack_sel
= SELECTOR_AllocBlock( thdb
->teb
.stack_top
,
219 0x10000, SEGMENT_DATA
,
221 if (!thdb
->teb
.stack_sel
) goto error
;
222 thdb
->cur_stack
= PTR_SEG_OFF_TO_SEGPTR( thdb
->teb
.stack_sel
,
223 0x10000 - sizeof(STACK16FRAME
) );
226 /* Create the thread socket */
228 if (CLIENT_NewThread( thdb
, server_thandle
, server_phandle
)) goto error
;
230 /* Add thread to process's list of threads */
232 THREAD_AddQueue( &pdb
->thread_list
, thdb
);
234 /* Create the thread event */
236 if (!(thdb
->event
= CreateEvent32A( NULL
, FALSE
, FALSE
, NULL
))) goto error
;
237 thdb
->event
= ConvertToGlobalHandle( thdb
->event
);
239 /* Initialize the thread context */
243 thdb
->pcontext
= &thdb
->context
;
244 thdb
->context
.SegCs
= cs
;
245 thdb
->context
.SegDs
= ds
;
246 thdb
->context
.SegEs
= ds
;
247 thdb
->context
.SegGs
= ds
;
248 thdb
->context
.SegSs
= ds
;
249 thdb
->context
.SegFs
= thdb
->teb_sel
;
250 thdb
->context
.Eip
= (DWORD
)start_addr
;
251 thdb
->context
.Esp
= (DWORD
)thdb
->teb
.stack_top
;
256 if (thdb
->socket
!= -1) close( thdb
->socket
);
257 if (thdb
->event
) CloseHandle( thdb
->event
);
258 if (thdb
->teb
.stack_sel
) SELECTOR_FreeBlock( thdb
->teb
.stack_sel
, 1 );
259 if (thdb
->teb_sel
) SELECTOR_FreeBlock( thdb
->teb_sel
, 1 );
260 if (thdb
->stack_base
) VirtualFree( thdb
->stack_base
, 0, MEM_RELEASE
);
261 HeapFree( SystemHeap
, 0, thdb
);
266 /***********************************************************************
269 static void THREAD_Destroy( K32OBJ
*ptr
)
271 THDB
*thdb
= (THDB
*)ptr
;
273 assert( ptr
->type
== K32OBJ_THREAD
);
274 ptr
->type
= K32OBJ_UNKNOWN
;
276 /* Free the associated memory */
280 /* Check if we are deleting the current thread */
283 if (fs
== thdb
->teb_sel
)
290 CloseHandle( thdb
->event
);
291 close( thdb
->socket
);
292 SELECTOR_FreeBlock( thdb
->teb_sel
, 1 );
293 if (thdb
->teb
.stack_sel
) SELECTOR_FreeBlock( thdb
->teb
.stack_sel
, 1 );
294 HeapFree( SystemHeap
, 0, thdb
);
299 /***********************************************************************
302 * Start execution of a newly created thread. Does not return.
304 void THREAD_Start( THDB
*thdb
)
306 LPTHREAD_START_ROUTINE func
= (LPTHREAD_START_ROUTINE
)thdb
->entry_point
;
307 assert( THREAD_Current() == thdb
);
309 MODULE_InitializeDLLs( thdb
->process
, 0, DLL_THREAD_ATTACH
, NULL
);
310 ExitThread( func( thdb
->entry_arg
) );
314 /***********************************************************************
315 * CreateThread (KERNEL32.63)
317 HANDLE32 WINAPI
CreateThread( SECURITY_ATTRIBUTES
*sa
, DWORD stack
,
318 LPTHREAD_START_ROUTINE start
, LPVOID param
,
319 DWORD flags
, LPDWORD id
)
321 int server_handle
= -1;
322 HANDLE32 handle
= INVALID_HANDLE_VALUE32
;
323 BOOL32 inherit
= (sa
&& (sa
->nLength
>=sizeof(*sa
)) && sa
->bInheritHandle
);
325 THDB
*thread
= THREAD_Create( PROCESS_Current(), stack
,
326 TRUE
, &server_handle
, NULL
, start
, param
);
327 if (!thread
) return INVALID_HANDLE_VALUE32
;
328 handle
= HANDLE_Alloc( PROCESS_Current(), &thread
->header
,
329 THREAD_ALL_ACCESS
, inherit
, server_handle
);
330 if (handle
== INVALID_HANDLE_VALUE32
) goto error
;
331 if (SYSDEPS_SpawnThread( thread
) == -1) goto error
;
332 if (id
) *id
= THDB_TO_THREAD_ID( thread
);
336 if (handle
!= INVALID_HANDLE_VALUE32
) CloseHandle( handle
);
337 K32OBJ_DecCount( &thread
->header
);
338 return INVALID_HANDLE_VALUE32
;
342 /***********************************************************************
343 * ExitThread [KERNEL32.215] Ends a thread
348 void WINAPI
ExitThread(
349 DWORD code
) /* [in] Exit code for this thread */
351 THDB
*thdb
= THREAD_Current();
354 /* Remove thread from process's list */
355 THREAD_RemoveQueue( &thdb
->process
->thread_list
, thdb
);
357 MODULE_InitializeDLLs( thdb
->process
, 0, DLL_THREAD_DETACH
, NULL
);
360 thdb
->exit_code
= code
;
362 /* FIXME: should free the stack somehow */
364 /* FIXME: We cannot do this; once the current thread is destroyed,
365 synchronization primitives do not work properly. */
366 K32OBJ_DecCount( &thdb
->header
);
368 /* Completely unlock the system lock just in case */
369 count
= SYSTEM_LOCK_COUNT();
370 while (count
--) SYSTEM_UNLOCK();
371 SYSDEPS_ExitThread();
375 /***********************************************************************
376 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
379 * Pseudohandle for the current thread
381 HANDLE32 WINAPI
GetCurrentThread(void)
383 return CURRENT_THREAD_PSEUDOHANDLE
;
387 /***********************************************************************
388 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
391 * Thread identifier of calling thread
393 DWORD WINAPI
GetCurrentThreadId(void)
395 return THDB_TO_THREAD_ID( THREAD_Current() );
399 /**********************************************************************
400 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
403 * Calling thread's last error code value.
405 DWORD WINAPI
GetLastError(void)
407 THDB
*thread
= THREAD_Current();
408 DWORD ret
= thread
->last_error
;
409 TRACE(thread
,"0x%lx\n",ret
);
414 /**********************************************************************
415 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
420 void WINAPI
SetLastError(
421 DWORD error
) /* [in] Per-thread error code */
423 THDB
*thread
= THREAD_Current();
424 /* This one must work before we have a thread (FIXME) */
426 TRACE(thread
,"%p error=0x%lx\n",thread
,error
);
429 thread
->last_error
= error
;
433 /**********************************************************************
434 * SetLastErrorEx [USER32.485] Sets the last-error code.
439 void WINAPI
SetLastErrorEx(
440 DWORD error
, /* [in] Per-thread error code */
441 DWORD type
) /* [in] Error type */
443 TRACE(thread
, "(0x%08lx, 0x%08lx)\n", error
,type
);
450 /* Fall through for now */
452 FIXME(thread
, "(error=%08lx, type=%08lx): Unhandled type\n", error
,type
);
455 SetLastError( error
);
459 /**********************************************************************
462 DWORD
THREAD_TlsAlloc(THDB
*thread
)
464 DWORD i
, mask
, ret
= 0;
465 DWORD
*bits
= thread
->process
->tls_bits
;
466 EnterCriticalSection( &thread
->process
->crit_section
);
467 if (*bits
== 0xffffffff)
471 if (*bits
== 0xffffffff)
473 LeaveCriticalSection( &thread
->process
->crit_section
);
474 SetLastError( ERROR_NO_MORE_ITEMS
);
478 for (i
= 0, mask
= 1; i
< 32; i
++, mask
<<= 1) if (!(*bits
& mask
)) break;
480 LeaveCriticalSection( &thread
->process
->crit_section
);
485 /**********************************************************************
486 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
488 * Allocates a thread local storage index
492 * Failure: 0xFFFFFFFF
494 DWORD WINAPI
TlsAlloc(void)
496 return THREAD_TlsAlloc(THREAD_Current());
500 /**********************************************************************
501 * TlsFree [KERNEL32.531] Releases a TLS index.
503 * Releases a thread local storage index, making it available for reuse
509 BOOL32 WINAPI
TlsFree(
510 DWORD index
) /* [in] TLS Index to free */
513 THDB
*thread
= THREAD_Current();
514 DWORD
*bits
= thread
->process
->tls_bits
;
517 SetLastError( ERROR_INVALID_PARAMETER
);
520 EnterCriticalSection( &thread
->process
->crit_section
);
521 if (index
>= 32) bits
++;
522 mask
= (1 << (index
& 31));
523 if (!(*bits
& mask
)) /* already free? */
525 LeaveCriticalSection( &thread
->process
->crit_section
);
526 SetLastError( ERROR_INVALID_PARAMETER
);
530 thread
->tls_array
[index
] = 0;
531 /* FIXME: should zero all other thread values */
532 LeaveCriticalSection( &thread
->process
->crit_section
);
537 /**********************************************************************
538 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
541 * Success: Value stored in calling thread's TLS slot for index
542 * Failure: 0 and GetLastError returns NO_ERROR
544 LPVOID WINAPI
TlsGetValue(
545 DWORD index
) /* [in] TLS index to retrieve value for */
547 THDB
*thread
= THREAD_Current();
550 SetLastError( ERROR_INVALID_PARAMETER
);
553 SetLastError( ERROR_SUCCESS
);
554 return thread
->tls_array
[index
];
558 /**********************************************************************
559 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
565 BOOL32 WINAPI
TlsSetValue(
566 DWORD index
, /* [in] TLS index to set value for */
567 LPVOID value
) /* [in] Value to be stored */
569 THDB
*thread
= THREAD_Current();
572 SetLastError( ERROR_INVALID_PARAMETER
);
575 thread
->tls_array
[index
] = value
;
580 /***********************************************************************
581 * SetThreadContext [KERNEL32.670] Sets context of thread.
587 BOOL32 WINAPI
SetThreadContext(
588 HANDLE32 handle
, /* [in] Handle to thread with context */
589 CONTEXT
*context
) /* [out] Address of context structure */
591 THDB
*thread
= THREAD_GetPtr( handle
, THREAD_GET_CONTEXT
, NULL
);
592 if (!thread
) return FALSE
;
593 *context
= thread
->context
;
594 K32OBJ_DecCount( &thread
->header
);
598 /***********************************************************************
599 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
605 BOOL32 WINAPI
GetThreadContext(
606 HANDLE32 handle
, /* [in] Handle to thread with context */
607 CONTEXT
*context
) /* [out] Address of context structure */
609 THDB
*thread
= THREAD_GetPtr( handle
, THREAD_GET_CONTEXT
, NULL
);
610 if (!thread
) return FALSE
;
611 *context
= thread
->context
;
612 K32OBJ_DecCount( &thread
->header
);
617 /**********************************************************************
618 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
621 * Success: Thread's priority level.
622 * Failure: THREAD_PRIORITY_ERROR_RETURN
624 INT32 WINAPI
GetThreadPriority(
625 HANDLE32 hthread
) /* [in] Handle to thread */
630 if (!(thread
= THREAD_GetPtr( hthread
, THREAD_QUERY_INFORMATION
, NULL
)))
631 return THREAD_PRIORITY_ERROR_RETURN
;
632 ret
= thread
->delta_priority
;
633 K32OBJ_DecCount( &thread
->header
);
638 /**********************************************************************
639 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
645 BOOL32 WINAPI
SetThreadPriority(
646 HANDLE32 hthread
, /* [in] Handle to thread */
647 INT32 priority
) /* [in] Thread priority level */
651 if (!(thread
= THREAD_GetPtr( hthread
, THREAD_SET_INFORMATION
, NULL
)))
653 thread
->delta_priority
= priority
;
654 K32OBJ_DecCount( &thread
->header
);
659 /**********************************************************************
660 * TerminateThread [KERNEL32.685] Terminates a thread
666 BOOL32 WINAPI
TerminateThread(
667 HANDLE32 handle
, /* [in] Handle to thread */
668 DWORD exitcode
) /* [in] Exit code for thread */
670 struct terminate_thread_request req
;
672 req
.handle
= HANDLE_GetServerHandle( PROCESS_Current(), handle
,
673 K32OBJ_THREAD
, THREAD_TERMINATE
);
674 req
.exit_code
= exitcode
;
675 CLIENT_SendRequest( REQ_TERMINATE_THREAD
, -1, 1, &req
, sizeof(req
) );
676 return !CLIENT_WaitReply( NULL
, NULL
, 0 );
680 /**********************************************************************
681 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
687 BOOL32 WINAPI
GetExitCodeThread(
688 HANDLE32 hthread
, /* [in] Handle to thread */
689 LPDWORD exitcode
) /* [out] Address to receive termination status */
691 struct get_thread_info_reply reply
;
692 int handle
= HANDLE_GetServerHandle( PROCESS_Current(), hthread
,
693 K32OBJ_THREAD
, THREAD_QUERY_INFORMATION
);
694 CLIENT_SendRequest( REQ_GET_THREAD_INFO
, -1, 1, &handle
, sizeof(handle
) );
695 if (CLIENT_WaitSimpleReply( &reply
, sizeof(reply
), NULL
)) return FALSE
;
696 if (exitcode
) *exitcode
= reply
.exit_code
;
701 /**********************************************************************
702 * ResumeThread [KERNEL32.587] Resumes a thread.
704 * Decrements a thread's suspend count. When count is zero, the
705 * execution of the thread is resumed.
708 * Success: Previous suspend count
709 * Failure: 0xFFFFFFFF
712 DWORD WINAPI
ResumeThread(
713 HANDLE32 hthread
) /* [in] Indentifies thread to restart */
719 if (!(thread
= THREAD_GetPtr( hthread
, THREAD_QUERY_INFORMATION
, NULL
)))
722 WARN(thread
, "Invalid thread handle\n");
725 if ((oldcount
= thread
->suspend_count
) != 0)
727 if (!--thread
->suspend_count
)
729 if (kill(thread
->unix_pid
, SIGCONT
))
731 WARN(thread
, "Unable to CONTinue pid: %04x\n",
733 oldcount
= 0xFFFFFFFF;
737 K32OBJ_DecCount(&thread
->header
);
743 /**********************************************************************
744 * SuspendThread [KERNEL32.681] Suspends a thread.
747 * Success: Previous suspend count
748 * Failure: 0xFFFFFFFF
750 DWORD WINAPI
SuspendThread(
751 HANDLE32 hthread
) /* [in] Handle to the thread */
757 if (!(thread
= THREAD_GetPtr( hthread
, THREAD_QUERY_INFORMATION
, NULL
)))
760 WARN(thread
, "Invalid thread handle\n");
764 if (!(oldcount
= thread
->suspend_count
))
766 if (thread
->unix_pid
== getpid())
767 WARN(thread
, "Attempting to suspend myself\n" );
770 if (kill(thread
->unix_pid
, SIGSTOP
))
772 WARN(thread
, "Unable to STOP pid: %04x\n",
774 oldcount
= 0xFFFFFFFF;
776 else thread
->suspend_count
++;
779 else thread
->suspend_count
++;
780 K32OBJ_DecCount( &thread
->header
);
787 /**********************************************************************
788 * GetThreadTimes [KERNEL32.???] Obtains timing information.
791 * What are the fields where these values are stored?
797 BOOL32 WINAPI
GetThreadTimes(
798 HANDLE32 thread
, /* [in] Specifies the thread of interest */
799 LPFILETIME creationtime
, /* [out] When the thread was created */
800 LPFILETIME exittime
, /* [out] When the thread was destroyed */
801 LPFILETIME kerneltime
, /* [out] Time thread spent in kernel mode */
802 LPFILETIME usertime
) /* [out] Time thread spent in user mode */
804 FIXME(thread
,"(0x%08x): stub\n",thread
);
805 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
810 /**********************************************************************
811 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
813 * Attaches the input processing mechanism of one thread to that of
820 BOOL32 WINAPI
AttachThreadInput(
821 DWORD idAttach
, /* [in] Thread to attach */
822 DWORD idAttachTo
, /* [in] Thread to attach to */
823 BOOL32 fAttach
) /* [in] Attach or detach */
827 FIXME(thread
, "(0x%08lx,0x%08lx,%d): stub\n",idAttach
,idAttachTo
,fAttach
);
828 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
840 /**********************************************************************
841 * VWin32_BoostThreadGroup [KERNEL.535]
843 VOID WINAPI
VWin32_BoostThreadGroup( DWORD threadId
, INT32 boost
)
845 FIXME(thread
, "(0x%08lx,%d): stub\n", threadId
, boost
);
848 /**********************************************************************
849 * VWin32_BoostThreadStatic [KERNEL.536]
851 VOID WINAPI
VWin32_BoostThreadStatic( DWORD threadId
, INT32 boost
)
853 FIXME(thread
, "(0x%08lx,%d): stub\n", threadId
, boost
);