Small fix.
[wine/multimedia.git] / scheduler / thread.c
blob3a0ff9775c0630bfea73e2b8c3949aba04e711e3
1 /*
2 * Win32 threads
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <signal.h>
9 #include <unistd.h>
10 #include "thread.h"
11 #include "process.h"
12 #include "task.h"
13 #include "module.h"
14 #include "user.h"
15 #include "winerror.h"
16 #include "heap.h"
17 #include "selectors.h"
18 #include "miscemu.h"
19 #include "winnt.h"
20 #include "server.h"
21 #include "stackframe.h"
22 #include "debug.h"
24 #ifndef __i386__
25 THDB *pCurrentThread;
26 #endif
28 static BOOL32 THREAD_Signaled( K32OBJ *obj, DWORD thread_id );
29 static BOOL32 THREAD_Satisfied( K32OBJ *obj, DWORD thread_id );
30 static void THREAD_AddWait( K32OBJ *obj, DWORD thread_id );
31 static void THREAD_RemoveWait( K32OBJ *obj, DWORD thread_id );
32 static void THREAD_Destroy( K32OBJ *obj );
34 const K32OBJ_OPS THREAD_Ops =
36 THREAD_Signaled, /* signaled */
37 THREAD_Satisfied, /* satisfied */
38 THREAD_AddWait, /* add_wait */
39 THREAD_RemoveWait, /* remove_wait */
40 NULL, /* read */
41 NULL, /* write */
42 THREAD_Destroy /* destroy */
45 /* Is threading code initialized? */
46 BOOL32 THREAD_InitDone = FALSE;
48 /**********************************************************************
49 * THREAD_GetPtr
51 * Return a pointer to a thread object. The object count must be decremented
52 * when no longer used.
54 THDB *THREAD_GetPtr( HANDLE32 handle, DWORD access, int *server_handle )
56 return (THDB *)HANDLE_GetObjPtr( PROCESS_Current(), handle,
57 K32OBJ_THREAD, access, server_handle );
61 /***********************************************************************
62 * THREAD_Current
64 * Return the current thread THDB pointer.
66 THDB *THREAD_Current(void)
68 if (!THREAD_InitDone) return NULL;
69 return (THDB *)((char *)NtCurrentTeb() - (int)&((THDB *)0)->teb);
72 /***********************************************************************
73 * THREAD_IsWin16
75 BOOL32 THREAD_IsWin16( THDB *thdb )
77 if (!thdb || !thdb->process)
78 return TRUE;
79 else
81 TDB* pTask = (TDB*)GlobalLock16( thdb->process->task );
82 return !pTask || pTask->thdb == thdb;
86 /***********************************************************************
87 * THREAD_IdToTHDB
89 * Convert a thread id to a THDB, making sure it is valid.
91 THDB *THREAD_IdToTHDB( DWORD id )
93 THDB *thdb;
95 if (!id) return THREAD_Current();
96 thdb = THREAD_ID_TO_THDB( id );
97 if (!K32OBJ_IsValid( &thdb->header, K32OBJ_THREAD ))
99 /* Allow task handles to be used; convert to main thread */
100 if ( IsTask( id ) )
102 TDB *pTask = (TDB *)GlobalLock16( id );
103 if (pTask) return pTask->thdb;
106 SetLastError( ERROR_INVALID_PARAMETER );
107 return NULL;
109 return thdb;
114 /***********************************************************************
115 * THREAD_AddQueue
117 * Add a thread to a queue.
119 void THREAD_AddQueue( THREAD_QUEUE *queue, THDB *thread )
121 THREAD_ENTRY *entry = HeapAlloc( SystemHeap, HEAP_NO_SERIALIZE,
122 sizeof(*entry) );
123 assert(entry);
124 SYSTEM_LOCK();
125 entry->thread = thread;
126 if (*queue)
128 entry->next = (*queue)->next;
129 (*queue)->next = entry;
131 else entry->next = entry;
132 *queue = entry;
133 SYSTEM_UNLOCK();
136 /***********************************************************************
137 * THREAD_RemoveQueue
139 * Remove a thread from a queue.
141 void THREAD_RemoveQueue( THREAD_QUEUE *queue, THDB *thread )
143 THREAD_ENTRY *entry = *queue;
144 SYSTEM_LOCK();
145 if (entry->next == entry) /* Only one element in the queue */
147 assert( entry->thread == thread );
148 *queue = NULL;
150 else
152 THREAD_ENTRY *next;
153 while (entry->next->thread != thread)
155 entry = entry->next;
156 assert( entry != *queue ); /* Have we come all the way around? */
158 if ((next = entry->next) == *queue) *queue = entry;
159 entry->next = entry->next->next;
160 entry = next; /* This is the one we want to free */
162 HeapFree( SystemHeap, 0, entry );
163 SYSTEM_UNLOCK();
167 /***********************************************************************
168 * THREAD_Create
170 THDB *THREAD_Create( PDB32 *pdb, DWORD stack_size, BOOL32 alloc_stack16,
171 int *server_thandle, int *server_phandle,
172 LPTHREAD_START_ROUTINE start_addr, LPVOID param )
174 DWORD old_prot;
175 WORD cs, ds;
177 THDB *thdb = HeapAlloc( SystemHeap, HEAP_ZERO_MEMORY, sizeof(THDB) );
178 if (!thdb) return NULL;
179 thdb->header.type = K32OBJ_THREAD;
180 thdb->header.refcount = 1;
181 thdb->process = pdb;
182 thdb->teb.except = (void *)-1;
183 thdb->teb.htask16 = 0; /* FIXME */
184 thdb->teb.self = &thdb->teb;
185 thdb->teb.tls_ptr = thdb->tls_array;
186 thdb->teb.process = pdb;
187 thdb->wait_list = &thdb->wait_struct;
188 thdb->exit_code = 0x103; /* STILL_ACTIVE */
189 thdb->entry_point = start_addr;
190 thdb->entry_arg = param;
191 thdb->socket = -1;
193 /* Allocate the stack */
195 /* FIXME:
196 * If stacksize smaller than 1 MB, allocate 1MB
197 * (one program wanted only 10 kB, which is recommendable, but some WINE
198 * functions, noteably in the files subdir, push HUGE structures and
199 * arrays on the stack. They probably shouldn't.)
200 * If stacksize larger than 16 MB, warn the user. (We could shrink the stack
201 * but this could give more or less unexplainable crashes.)
203 if (stack_size<1024*1024)
204 stack_size = 1024 * 1024;
205 if (stack_size >= 16*1024*1024)
206 WARN(thread,"Thread stack size is %ld MB.\n",stack_size/1024/1024);
207 thdb->stack_base = VirtualAlloc(NULL,
208 stack_size + (alloc_stack16 ? 0x10000 : 0),
209 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
210 if (!thdb->stack_base) goto error;
211 /* Set a guard page at the bottom of the stack */
212 VirtualProtect( thdb->stack_base, 1, PAGE_EXECUTE_READWRITE | PAGE_GUARD,
213 &old_prot );
214 thdb->teb.stack_top = (char *)thdb->stack_base + stack_size;
215 thdb->teb.stack_low = thdb->stack_base;
216 thdb->exit_stack = thdb->teb.stack_top;
218 /* Allocate the TEB selector (%fs register) */
220 thdb->teb_sel = SELECTOR_AllocBlock( &thdb->teb, 0x1000, SEGMENT_DATA,
221 TRUE, FALSE );
222 if (!thdb->teb_sel) goto error;
224 /* Allocate the 16-bit stack selector */
226 if (alloc_stack16)
228 thdb->teb.stack_sel = SELECTOR_AllocBlock( thdb->teb.stack_top,
229 0x10000, SEGMENT_DATA,
230 FALSE, FALSE );
231 if (!thdb->teb.stack_sel) goto error;
232 thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( thdb->teb.stack_sel,
233 0x10000 - sizeof(STACK16FRAME) );
236 /* Allocate the event */
238 if (!(thdb->event = EVENT_Create( TRUE, FALSE ))) goto error;
240 /* Create the thread socket */
242 if (CLIENT_NewThread( thdb, server_thandle, server_phandle )) goto error;
244 /* Add thread to process's list of threads */
246 THREAD_AddQueue( &pdb->thread_list, thdb );
248 /* Initialize the thread context */
250 GET_CS(cs);
251 GET_DS(ds);
252 thdb->pcontext = &thdb->context;
253 thdb->context.SegCs = cs;
254 thdb->context.SegDs = ds;
255 thdb->context.SegEs = ds;
256 thdb->context.SegGs = ds;
257 thdb->context.SegSs = ds;
258 thdb->context.SegFs = thdb->teb_sel;
259 thdb->context.Eip = (DWORD)start_addr;
260 thdb->context.Esp = (DWORD)thdb->teb.stack_top;
261 PE_InitTls( thdb );
262 return thdb;
264 error:
265 if (thdb->socket != -1) close( thdb->socket );
266 if (thdb->event) K32OBJ_DecCount( thdb->event );
267 if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
268 if (thdb->teb_sel) SELECTOR_FreeBlock( thdb->teb_sel, 1 );
269 if (thdb->stack_base) VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
270 HeapFree( SystemHeap, 0, thdb );
271 return NULL;
275 /***********************************************************************
276 * THREAD_Signaled
278 static BOOL32 THREAD_Signaled( K32OBJ *obj, DWORD thread_id )
280 THDB *thdb = (THDB *)obj;
281 assert( obj->type == K32OBJ_THREAD );
282 return K32OBJ_OPS( thdb->event )->signaled( thdb->event, thread_id );
286 /***********************************************************************
287 * THREAD_Satisfied
289 * Wait on this object has been satisfied.
291 static BOOL32 THREAD_Satisfied( K32OBJ *obj, DWORD thread_id )
293 THDB *thdb = (THDB *)obj;
294 assert( obj->type == K32OBJ_THREAD );
295 return K32OBJ_OPS( thdb->event )->satisfied( thdb->event, thread_id );
299 /***********************************************************************
300 * THREAD_AddWait
302 * Add thread to object wait queue.
304 static void THREAD_AddWait( K32OBJ *obj, DWORD thread_id )
306 THDB *thdb = (THDB *)obj;
307 assert( obj->type == K32OBJ_THREAD );
308 return K32OBJ_OPS( thdb->event )->add_wait( thdb->event, thread_id );
312 /***********************************************************************
313 * THREAD_RemoveWait
315 * Remove thread from object wait queue.
317 static void THREAD_RemoveWait( K32OBJ *obj, DWORD thread_id )
319 THDB *thdb = (THDB *)obj;
320 assert( obj->type == K32OBJ_THREAD );
321 return K32OBJ_OPS( thdb->event )->remove_wait( thdb->event, thread_id );
325 /***********************************************************************
326 * THREAD_Destroy
328 static void THREAD_Destroy( K32OBJ *ptr )
330 THDB *thdb = (THDB *)ptr;
332 assert( ptr->type == K32OBJ_THREAD );
333 ptr->type = K32OBJ_UNKNOWN;
335 /* Free the associated memory */
337 #ifdef __i386__
339 /* Check if we are deleting the current thread */
340 WORD fs;
341 GET_FS( fs );
342 if (fs == thdb->teb_sel)
344 GET_DS( fs );
345 SET_FS( fs );
348 #endif
349 close( thdb->socket );
350 K32OBJ_DecCount( thdb->event );
351 SELECTOR_FreeBlock( thdb->teb_sel, 1 );
352 if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
353 HeapFree( SystemHeap, 0, thdb );
358 /***********************************************************************
359 * THREAD_Start
361 * Start execution of a newly created thread. Does not return.
363 void THREAD_Start( THDB *thdb )
365 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)thdb->entry_point;
366 assert( THREAD_Current() == thdb );
367 CLIENT_InitThread();
368 PE_InitializeDLLs( thdb->process, DLL_THREAD_ATTACH, NULL );
369 ExitThread( func( thdb->entry_arg ) );
373 /***********************************************************************
374 * CreateThread (KERNEL32.63)
376 HANDLE32 WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
377 LPTHREAD_START_ROUTINE start, LPVOID param,
378 DWORD flags, LPDWORD id )
380 int server_handle = -1;
381 HANDLE32 handle = INVALID_HANDLE_VALUE32;
382 BOOL32 inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
384 THDB *thread = THREAD_Create( PROCESS_Current(), stack,
385 TRUE, &server_handle, NULL, start, param );
386 if (!thread) return INVALID_HANDLE_VALUE32;
387 handle = HANDLE_Alloc( PROCESS_Current(), &thread->header,
388 THREAD_ALL_ACCESS, inherit, server_handle );
389 if (handle == INVALID_HANDLE_VALUE32) goto error;
390 if (SYSDEPS_SpawnThread( thread ) == -1) goto error;
391 if (id) *id = THDB_TO_THREAD_ID( thread );
392 return handle;
394 error:
395 if (handle != INVALID_HANDLE_VALUE32) CloseHandle( handle );
396 K32OBJ_DecCount( &thread->header );
397 return INVALID_HANDLE_VALUE32;
401 /***********************************************************************
402 * ExitThread [KERNEL32.215] Ends a thread
404 * RETURNS
405 * None
407 void WINAPI ExitThread(
408 DWORD code) /* [in] Exit code for this thread */
410 THDB *thdb = THREAD_Current();
411 LONG count;
413 /* Remove thread from process's list */
414 THREAD_RemoveQueue( &thdb->process->thread_list, thdb );
416 PE_InitializeDLLs( thdb->process, DLL_THREAD_DETACH, NULL );
418 SYSTEM_LOCK();
419 thdb->exit_code = code;
420 EVENT_Set( thdb->event );
422 /* Abandon all owned mutexes */
423 while (thdb->mutex_list) MUTEX_Abandon( thdb->mutex_list );
425 /* FIXME: should free the stack somehow */
426 #if 0
427 /* FIXME: We cannot do this; once the current thread is destroyed,
428 synchronization primitives do not work properly. */
429 K32OBJ_DecCount( &thdb->header );
430 #endif
431 /* Completely unlock the system lock just in case */
432 count = SYSTEM_LOCK_COUNT();
433 while (count--) SYSTEM_UNLOCK();
434 SYSDEPS_ExitThread();
438 /***********************************************************************
439 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
441 * RETURNS
442 * Pseudohandle for the current thread
444 HANDLE32 WINAPI GetCurrentThread(void)
446 return CURRENT_THREAD_PSEUDOHANDLE;
450 /***********************************************************************
451 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
453 * RETURNS
454 * Thread identifier of calling thread
456 DWORD WINAPI GetCurrentThreadId(void)
458 return THDB_TO_THREAD_ID( THREAD_Current() );
462 /**********************************************************************
463 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
465 * RETURNS
466 * Calling thread's last error code value.
468 DWORD WINAPI GetLastError(void)
470 THDB *thread = THREAD_Current();
471 DWORD ret = thread->last_error;
472 TRACE(thread,"0x%lx\n",ret);
473 return ret;
477 /**********************************************************************
478 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
480 * RETURNS
481 * None.
483 void WINAPI SetLastError(
484 DWORD error) /* [in] Per-thread error code */
486 THDB *thread = THREAD_Current();
487 /* This one must work before we have a thread (FIXME) */
489 TRACE(thread,"%p error=0x%lx\n",thread,error);
491 if (thread)
492 thread->last_error = error;
496 /**********************************************************************
497 * SetLastErrorEx [USER32.485] Sets the last-error code.
499 * RETURNS
500 * None.
502 void WINAPI SetLastErrorEx(
503 DWORD error, /* [in] Per-thread error code */
504 DWORD type) /* [in] Error type */
506 TRACE(thread, "(0x%08lx, 0x%08lx)\n", error,type);
507 switch(type) {
508 case 0:
509 break;
510 case SLE_ERROR:
511 case SLE_MINORERROR:
512 case SLE_WARNING:
513 /* Fall through for now */
514 default:
515 FIXME(thread, "(error=%08lx, type=%08lx): Unhandled type\n", error,type);
516 break;
518 SetLastError( error );
522 /**********************************************************************
523 * THREAD_TlsAlloc
525 DWORD THREAD_TlsAlloc(THDB *thread)
527 DWORD i, mask, ret = 0;
528 DWORD *bits = thread->process->tls_bits;
529 EnterCriticalSection( &thread->process->crit_section );
530 if (*bits == 0xffffffff)
532 bits++;
533 ret = 32;
534 if (*bits == 0xffffffff)
536 LeaveCriticalSection( &thread->process->crit_section );
537 SetLastError( ERROR_NO_MORE_ITEMS );
538 return 0xffffffff;
541 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
542 *bits |= mask;
543 LeaveCriticalSection( &thread->process->crit_section );
544 return ret + i;
548 /**********************************************************************
549 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
551 * Allocates a thread local storage index
553 * RETURNS
554 * Success: TLS Index
555 * Failure: 0xFFFFFFFF
557 DWORD WINAPI TlsAlloc(void)
559 return THREAD_TlsAlloc(THREAD_Current());
563 /**********************************************************************
564 * TlsFree [KERNEL32.531] Releases a TLS index.
566 * Releases a thread local storage index, making it available for reuse
568 * RETURNS
569 * Success: TRUE
570 * Failure: FALSE
572 BOOL32 WINAPI TlsFree(
573 DWORD index) /* [in] TLS Index to free */
575 DWORD mask;
576 THDB *thread = THREAD_Current();
577 DWORD *bits = thread->process->tls_bits;
578 if (index >= 64)
580 SetLastError( ERROR_INVALID_PARAMETER );
581 return FALSE;
583 EnterCriticalSection( &thread->process->crit_section );
584 if (index >= 32) bits++;
585 mask = (1 << (index & 31));
586 if (!(*bits & mask)) /* already free? */
588 LeaveCriticalSection( &thread->process->crit_section );
589 SetLastError( ERROR_INVALID_PARAMETER );
590 return FALSE;
592 *bits &= ~mask;
593 thread->tls_array[index] = 0;
594 /* FIXME: should zero all other thread values */
595 LeaveCriticalSection( &thread->process->crit_section );
596 return TRUE;
600 /**********************************************************************
601 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
603 * RETURNS
604 * Success: Value stored in calling thread's TLS slot for index
605 * Failure: 0 and GetLastError returns NO_ERROR
607 LPVOID WINAPI TlsGetValue(
608 DWORD index) /* [in] TLS index to retrieve value for */
610 THDB *thread = THREAD_Current();
611 if (index >= 64)
613 SetLastError( ERROR_INVALID_PARAMETER );
614 return NULL;
616 SetLastError( ERROR_SUCCESS );
617 return thread->tls_array[index];
621 /**********************************************************************
622 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
624 * RETURNS
625 * Success: TRUE
626 * Failure: FALSE
628 BOOL32 WINAPI TlsSetValue(
629 DWORD index, /* [in] TLS index to set value for */
630 LPVOID value) /* [in] Value to be stored */
632 THDB *thread = THREAD_Current();
633 if (index >= 64)
635 SetLastError( ERROR_INVALID_PARAMETER );
636 return FALSE;
638 thread->tls_array[index] = value;
639 return TRUE;
643 /***********************************************************************
644 * SetThreadContext [KERNEL32.670] Sets context of thread.
646 * RETURNS
647 * Success: TRUE
648 * Failure: FALSE
650 BOOL32 WINAPI SetThreadContext(
651 HANDLE32 handle, /* [in] Handle to thread with context */
652 CONTEXT *context) /* [out] Address of context structure */
654 THDB *thread = THREAD_GetPtr( handle, THREAD_GET_CONTEXT, NULL );
655 if (!thread) return FALSE;
656 *context = thread->context;
657 K32OBJ_DecCount( &thread->header );
658 return TRUE;
661 /***********************************************************************
662 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
664 * RETURNS
665 * Success: TRUE
666 * Failure: FALSE
668 BOOL32 WINAPI GetThreadContext(
669 HANDLE32 handle, /* [in] Handle to thread with context */
670 CONTEXT *context) /* [out] Address of context structure */
672 THDB *thread = THREAD_GetPtr( handle, THREAD_GET_CONTEXT, NULL );
673 if (!thread) return FALSE;
674 *context = thread->context;
675 K32OBJ_DecCount( &thread->header );
676 return TRUE;
680 /**********************************************************************
681 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
683 * RETURNS
684 * Success: Thread's priority level.
685 * Failure: THREAD_PRIORITY_ERROR_RETURN
687 INT32 WINAPI GetThreadPriority(
688 HANDLE32 hthread) /* [in] Handle to thread */
690 THDB *thread;
691 INT32 ret;
693 if (!(thread = THREAD_GetPtr( hthread, THREAD_QUERY_INFORMATION, NULL )))
694 return THREAD_PRIORITY_ERROR_RETURN;
695 ret = thread->delta_priority;
696 K32OBJ_DecCount( &thread->header );
697 return ret;
701 /**********************************************************************
702 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
704 * RETURNS
705 * Success: TRUE
706 * Failure: FALSE
708 BOOL32 WINAPI SetThreadPriority(
709 HANDLE32 hthread, /* [in] Handle to thread */
710 INT32 priority) /* [in] Thread priority level */
712 THDB *thread;
714 if (!(thread = THREAD_GetPtr( hthread, THREAD_SET_INFORMATION, NULL )))
715 return FALSE;
716 thread->delta_priority = priority;
717 K32OBJ_DecCount( &thread->header );
718 return TRUE;
722 /**********************************************************************
723 * TerminateThread [KERNEL32.685] Terminates a thread
725 * RETURNS
726 * Success: TRUE
727 * Failure: FALSE
729 BOOL32 WINAPI TerminateThread(
730 HANDLE32 handle, /* [in] Handle to thread */
731 DWORD exitcode) /* [in] Exit code for thread */
733 int server_handle;
734 BOOL32 ret;
735 THDB *thread;
737 if (!(thread = THREAD_GetPtr( handle, THREAD_TERMINATE, &server_handle )))
738 return FALSE;
739 ret = !CLIENT_TerminateThread( server_handle, exitcode );
740 K32OBJ_DecCount( &thread->header );
741 return ret;
745 /**********************************************************************
746 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
748 * RETURNS
749 * Success: TRUE
750 * Failure: FALSE
752 BOOL32 WINAPI GetExitCodeThread(
753 HANDLE32 hthread, /* [in] Handle to thread */
754 LPDWORD exitcode) /* [out] Address to receive termination status */
756 THDB *thread;
757 int server_handle;
759 if (!(thread = THREAD_GetPtr( hthread, THREAD_QUERY_INFORMATION, &server_handle )))
760 return FALSE;
761 if (server_handle != -1)
763 struct get_thread_info_reply info;
764 CLIENT_GetThreadInfo( server_handle, &info );
765 if (exitcode) *exitcode = info.exit_code;
767 else if (exitcode) *exitcode = thread->exit_code;
768 K32OBJ_DecCount( &thread->header );
769 return TRUE;
773 /**********************************************************************
774 * ResumeThread [KERNEL32.587] Resumes a thread.
776 * Decrements a thread's suspend count. When count is zero, the
777 * execution of the thread is resumed.
779 * RETURNS
780 * Success: Previous suspend count
781 * Failure: 0xFFFFFFFF
782 * Already running: 0
784 DWORD WINAPI ResumeThread(
785 HANDLE32 hthread) /* [in] Indentifies thread to restart */
787 THDB *thread;
788 DWORD oldcount;
790 SYSTEM_LOCK();
791 if (!(thread = THREAD_GetPtr( hthread, THREAD_QUERY_INFORMATION, NULL )))
793 SYSTEM_UNLOCK();
794 WARN(thread, "Invalid thread handle\n");
795 return 0xFFFFFFFF;
797 if ((oldcount = thread->suspend_count) != 0)
799 if (!--thread->suspend_count)
801 if (kill(thread->unix_pid, SIGCONT))
803 WARN(thread, "Unable to CONTinue pid: %04x\n",
804 thread->unix_pid);
805 oldcount = 0xFFFFFFFF;
809 K32OBJ_DecCount(&thread->header);
810 SYSTEM_UNLOCK();
811 return oldcount;
815 /**********************************************************************
816 * SuspendThread [KERNEL32.681] Suspends a thread.
818 * RETURNS
819 * Success: Previous suspend count
820 * Failure: 0xFFFFFFFF
822 DWORD WINAPI SuspendThread(
823 HANDLE32 hthread) /* [in] Handle to the thread */
825 THDB *thread;
826 DWORD oldcount;
828 SYSTEM_LOCK();
829 if (!(thread = THREAD_GetPtr( hthread, THREAD_QUERY_INFORMATION, NULL )))
831 SYSTEM_UNLOCK();
832 WARN(thread, "Invalid thread handle\n");
833 return 0xFFFFFFFF;
836 if (!(oldcount = thread->suspend_count))
838 if (thread->unix_pid == getpid())
839 WARN(thread, "Attempting to suspend myself\n" );
840 else
842 if (kill(thread->unix_pid, SIGSTOP))
844 WARN(thread, "Unable to STOP pid: %04x\n",
845 thread->unix_pid);
846 oldcount = 0xFFFFFFFF;
848 else thread->suspend_count++;
851 else thread->suspend_count++;
852 K32OBJ_DecCount( &thread->header );
853 SYSTEM_UNLOCK();
854 return oldcount;
859 /**********************************************************************
860 * GetThreadTimes [KERNEL32.???] Obtains timing information.
862 * NOTES
863 * What are the fields where these values are stored?
865 * RETURNS
866 * Success: TRUE
867 * Failure: FALSE
869 BOOL32 WINAPI GetThreadTimes(
870 HANDLE32 thread, /* [in] Specifies the thread of interest */
871 LPFILETIME creationtime, /* [out] When the thread was created */
872 LPFILETIME exittime, /* [out] When the thread was destroyed */
873 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
874 LPFILETIME usertime) /* [out] Time thread spent in user mode */
876 FIXME(thread,"(0x%08x): stub\n",thread);
877 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
878 return FALSE;
882 /**********************************************************************
883 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
885 * Attaches the input processing mechanism of one thread to that of
886 * another thread.
888 * RETURNS
889 * Success: TRUE
890 * Failure: FALSE
892 BOOL32 WINAPI AttachThreadInput(
893 DWORD idAttach, /* [in] Thread to attach */
894 DWORD idAttachTo, /* [in] Thread to attach to */
895 BOOL32 fAttach) /* [in] Attach or detach */
897 BOOL32 ret;
899 FIXME(thread, "(0x%08lx,0x%08lx,%d): stub\n",idAttach,idAttachTo,fAttach);
900 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
901 if (fAttach) {
902 /* Attach threads */
903 ret = FALSE;
905 else {
906 /* Detach threads */
907 ret = FALSE;
909 return ret;
912 /**********************************************************************
913 * VWin32_BoostThreadGroup [KERNEL.535]
915 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT32 boost )
917 FIXME(thread, "(0x%08lx,%d): stub\n", threadId, boost);
920 /**********************************************************************
921 * VWin32_BoostThreadStatic [KERNEL.536]
923 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT32 boost )
925 FIXME(thread, "(0x%08lx,%d): stub\n", threadId, boost);