Add support for selection of console mode drivers to use using the
[wine/multimedia.git] / scheduler / thread.c
blob91eb1837eee1da60af7e12de747e059965acadbf
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.flags = (pdb->flags & PDB32_WIN16_PROC)? 0 : TEBF_WIN32;
186 thdb->teb.tls_ptr = thdb->tls_array;
187 thdb->teb.process = pdb;
188 thdb->wait_list = &thdb->wait_struct;
189 thdb->exit_code = 0x103; /* STILL_ACTIVE */
190 thdb->entry_point = start_addr;
191 thdb->entry_arg = param;
192 thdb->socket = -1;
194 /* Allocate the stack */
196 /* FIXME:
197 * If stacksize smaller than 1 MB, allocate 1MB
198 * (one program wanted only 10 kB, which is recommendable, but some WINE
199 * functions, noteably in the files subdir, push HUGE structures and
200 * arrays on the stack. They probably shouldn't.)
201 * If stacksize larger than 16 MB, warn the user. (We could shrink the stack
202 * but this could give more or less unexplainable crashes.)
204 if (stack_size<1024*1024)
205 stack_size = 1024 * 1024;
206 if (stack_size >= 16*1024*1024)
207 WARN(thread,"Thread stack size is %ld MB.\n",stack_size/1024/1024);
208 thdb->stack_base = VirtualAlloc(NULL,
209 stack_size + (alloc_stack16 ? 0x10000 : 0),
210 MEM_COMMIT, PAGE_EXECUTE_READWRITE );
211 if (!thdb->stack_base) goto error;
212 /* Set a guard page at the bottom of the stack */
213 VirtualProtect( thdb->stack_base, 1, PAGE_EXECUTE_READWRITE | PAGE_GUARD,
214 &old_prot );
215 thdb->teb.stack_top = (char *)thdb->stack_base + stack_size;
216 thdb->teb.stack_low = thdb->stack_base;
217 thdb->exit_stack = thdb->teb.stack_top;
219 /* Allocate the TEB selector (%fs register) */
221 thdb->teb_sel = SELECTOR_AllocBlock( &thdb->teb, 0x1000, SEGMENT_DATA,
222 TRUE, FALSE );
223 if (!thdb->teb_sel) goto error;
225 /* Allocate the 16-bit stack selector */
227 if (alloc_stack16)
229 thdb->teb.stack_sel = SELECTOR_AllocBlock( thdb->teb.stack_top,
230 0x10000, SEGMENT_DATA,
231 FALSE, FALSE );
232 if (!thdb->teb.stack_sel) goto error;
233 thdb->cur_stack = PTR_SEG_OFF_TO_SEGPTR( thdb->teb.stack_sel,
234 0x10000 - sizeof(STACK16FRAME) );
237 /* Allocate the event */
239 if (!(thdb->event = EVENT_Create( TRUE, FALSE ))) goto error;
241 /* Create the thread socket */
243 if (CLIENT_NewThread( thdb, server_thandle, server_phandle )) goto error;
245 /* Add thread to process's list of threads */
247 THREAD_AddQueue( &pdb->thread_list, thdb );
249 /* Initialize the thread context */
251 GET_CS(cs);
252 GET_DS(ds);
253 thdb->pcontext = &thdb->context;
254 thdb->context.SegCs = cs;
255 thdb->context.SegDs = ds;
256 thdb->context.SegEs = ds;
257 thdb->context.SegGs = ds;
258 thdb->context.SegSs = ds;
259 thdb->context.SegFs = thdb->teb_sel;
260 thdb->context.Eip = (DWORD)start_addr;
261 thdb->context.Esp = (DWORD)thdb->teb.stack_top;
262 PE_InitTls( thdb );
263 return thdb;
265 error:
266 if (thdb->socket != -1) close( thdb->socket );
267 if (thdb->event) K32OBJ_DecCount( thdb->event );
268 if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
269 if (thdb->teb_sel) SELECTOR_FreeBlock( thdb->teb_sel, 1 );
270 if (thdb->stack_base) VirtualFree( thdb->stack_base, 0, MEM_RELEASE );
271 HeapFree( SystemHeap, 0, thdb );
272 return NULL;
276 /***********************************************************************
277 * THREAD_Signaled
279 static BOOL32 THREAD_Signaled( K32OBJ *obj, DWORD thread_id )
281 THDB *thdb = (THDB *)obj;
282 assert( obj->type == K32OBJ_THREAD );
283 return K32OBJ_OPS( thdb->event )->signaled( thdb->event, thread_id );
287 /***********************************************************************
288 * THREAD_Satisfied
290 * Wait on this object has been satisfied.
292 static BOOL32 THREAD_Satisfied( K32OBJ *obj, DWORD thread_id )
294 THDB *thdb = (THDB *)obj;
295 assert( obj->type == K32OBJ_THREAD );
296 return K32OBJ_OPS( thdb->event )->satisfied( thdb->event, thread_id );
300 /***********************************************************************
301 * THREAD_AddWait
303 * Add thread to object wait queue.
305 static void THREAD_AddWait( K32OBJ *obj, DWORD thread_id )
307 THDB *thdb = (THDB *)obj;
308 assert( obj->type == K32OBJ_THREAD );
309 return K32OBJ_OPS( thdb->event )->add_wait( thdb->event, thread_id );
313 /***********************************************************************
314 * THREAD_RemoveWait
316 * Remove thread from object wait queue.
318 static void THREAD_RemoveWait( K32OBJ *obj, DWORD thread_id )
320 THDB *thdb = (THDB *)obj;
321 assert( obj->type == K32OBJ_THREAD );
322 return K32OBJ_OPS( thdb->event )->remove_wait( thdb->event, thread_id );
326 /***********************************************************************
327 * THREAD_Destroy
329 static void THREAD_Destroy( K32OBJ *ptr )
331 THDB *thdb = (THDB *)ptr;
333 assert( ptr->type == K32OBJ_THREAD );
334 ptr->type = K32OBJ_UNKNOWN;
336 /* Free the associated memory */
338 #ifdef __i386__
340 /* Check if we are deleting the current thread */
341 WORD fs;
342 GET_FS( fs );
343 if (fs == thdb->teb_sel)
345 GET_DS( fs );
346 SET_FS( fs );
349 #endif
350 close( thdb->socket );
351 K32OBJ_DecCount( thdb->event );
352 SELECTOR_FreeBlock( thdb->teb_sel, 1 );
353 if (thdb->teb.stack_sel) SELECTOR_FreeBlock( thdb->teb.stack_sel, 1 );
354 HeapFree( SystemHeap, 0, thdb );
359 /***********************************************************************
360 * THREAD_Start
362 * Start execution of a newly created thread. Does not return.
364 void THREAD_Start( THDB *thdb )
366 LPTHREAD_START_ROUTINE func = (LPTHREAD_START_ROUTINE)thdb->entry_point;
367 assert( THREAD_Current() == thdb );
368 CLIENT_InitThread();
369 MODULE_InitializeDLLs( thdb->process, 0, DLL_THREAD_ATTACH, NULL );
370 ExitThread( func( thdb->entry_arg ) );
374 /***********************************************************************
375 * CreateThread (KERNEL32.63)
377 HANDLE32 WINAPI CreateThread( SECURITY_ATTRIBUTES *sa, DWORD stack,
378 LPTHREAD_START_ROUTINE start, LPVOID param,
379 DWORD flags, LPDWORD id )
381 int server_handle = -1;
382 HANDLE32 handle = INVALID_HANDLE_VALUE32;
383 BOOL32 inherit = (sa && (sa->nLength>=sizeof(*sa)) && sa->bInheritHandle);
385 THDB *thread = THREAD_Create( PROCESS_Current(), stack,
386 TRUE, &server_handle, NULL, start, param );
387 if (!thread) return INVALID_HANDLE_VALUE32;
388 handle = HANDLE_Alloc( PROCESS_Current(), &thread->header,
389 THREAD_ALL_ACCESS, inherit, server_handle );
390 if (handle == INVALID_HANDLE_VALUE32) goto error;
391 if (SYSDEPS_SpawnThread( thread ) == -1) goto error;
392 if (id) *id = THDB_TO_THREAD_ID( thread );
393 return handle;
395 error:
396 if (handle != INVALID_HANDLE_VALUE32) CloseHandle( handle );
397 K32OBJ_DecCount( &thread->header );
398 return INVALID_HANDLE_VALUE32;
402 /***********************************************************************
403 * ExitThread [KERNEL32.215] Ends a thread
405 * RETURNS
406 * None
408 void WINAPI ExitThread(
409 DWORD code) /* [in] Exit code for this thread */
411 THDB *thdb = THREAD_Current();
412 LONG count;
414 /* Remove thread from process's list */
415 THREAD_RemoveQueue( &thdb->process->thread_list, thdb );
417 MODULE_InitializeDLLs( thdb->process, 0, DLL_THREAD_DETACH, NULL );
419 SYSTEM_LOCK();
420 thdb->exit_code = code;
421 EVENT_Set( thdb->event );
423 /* Abandon all owned mutexes */
424 while (thdb->mutex_list) MUTEX_Abandon( thdb->mutex_list );
426 /* FIXME: should free the stack somehow */
427 #if 0
428 /* FIXME: We cannot do this; once the current thread is destroyed,
429 synchronization primitives do not work properly. */
430 K32OBJ_DecCount( &thdb->header );
431 #endif
432 /* Completely unlock the system lock just in case */
433 count = SYSTEM_LOCK_COUNT();
434 while (count--) SYSTEM_UNLOCK();
435 SYSDEPS_ExitThread();
439 /***********************************************************************
440 * GetCurrentThread [KERNEL32.200] Gets pseudohandle for current thread
442 * RETURNS
443 * Pseudohandle for the current thread
445 HANDLE32 WINAPI GetCurrentThread(void)
447 return CURRENT_THREAD_PSEUDOHANDLE;
451 /***********************************************************************
452 * GetCurrentThreadId [KERNEL32.201] Returns thread identifier.
454 * RETURNS
455 * Thread identifier of calling thread
457 DWORD WINAPI GetCurrentThreadId(void)
459 return THDB_TO_THREAD_ID( THREAD_Current() );
463 /**********************************************************************
464 * GetLastError [KERNEL.148] [KERNEL32.227] Returns last-error code.
466 * RETURNS
467 * Calling thread's last error code value.
469 DWORD WINAPI GetLastError(void)
471 THDB *thread = THREAD_Current();
472 DWORD ret = thread->last_error;
473 TRACE(thread,"0x%lx\n",ret);
474 return ret;
478 /**********************************************************************
479 * SetLastError [KERNEL.147] [KERNEL32.497] Sets the last-error code.
481 * RETURNS
482 * None.
484 void WINAPI SetLastError(
485 DWORD error) /* [in] Per-thread error code */
487 THDB *thread = THREAD_Current();
488 /* This one must work before we have a thread (FIXME) */
490 TRACE(thread,"%p error=0x%lx\n",thread,error);
492 if (thread)
493 thread->last_error = error;
497 /**********************************************************************
498 * SetLastErrorEx [USER32.485] Sets the last-error code.
500 * RETURNS
501 * None.
503 void WINAPI SetLastErrorEx(
504 DWORD error, /* [in] Per-thread error code */
505 DWORD type) /* [in] Error type */
507 TRACE(thread, "(0x%08lx, 0x%08lx)\n", error,type);
508 switch(type) {
509 case 0:
510 break;
511 case SLE_ERROR:
512 case SLE_MINORERROR:
513 case SLE_WARNING:
514 /* Fall through for now */
515 default:
516 FIXME(thread, "(error=%08lx, type=%08lx): Unhandled type\n", error,type);
517 break;
519 SetLastError( error );
523 /**********************************************************************
524 * THREAD_TlsAlloc
526 DWORD THREAD_TlsAlloc(THDB *thread)
528 DWORD i, mask, ret = 0;
529 DWORD *bits = thread->process->tls_bits;
530 EnterCriticalSection( &thread->process->crit_section );
531 if (*bits == 0xffffffff)
533 bits++;
534 ret = 32;
535 if (*bits == 0xffffffff)
537 LeaveCriticalSection( &thread->process->crit_section );
538 SetLastError( ERROR_NO_MORE_ITEMS );
539 return 0xffffffff;
542 for (i = 0, mask = 1; i < 32; i++, mask <<= 1) if (!(*bits & mask)) break;
543 *bits |= mask;
544 LeaveCriticalSection( &thread->process->crit_section );
545 return ret + i;
549 /**********************************************************************
550 * TlsAlloc [KERNEL32.530] Allocates a TLS index.
552 * Allocates a thread local storage index
554 * RETURNS
555 * Success: TLS Index
556 * Failure: 0xFFFFFFFF
558 DWORD WINAPI TlsAlloc(void)
560 return THREAD_TlsAlloc(THREAD_Current());
564 /**********************************************************************
565 * TlsFree [KERNEL32.531] Releases a TLS index.
567 * Releases a thread local storage index, making it available for reuse
569 * RETURNS
570 * Success: TRUE
571 * Failure: FALSE
573 BOOL32 WINAPI TlsFree(
574 DWORD index) /* [in] TLS Index to free */
576 DWORD mask;
577 THDB *thread = THREAD_Current();
578 DWORD *bits = thread->process->tls_bits;
579 if (index >= 64)
581 SetLastError( ERROR_INVALID_PARAMETER );
582 return FALSE;
584 EnterCriticalSection( &thread->process->crit_section );
585 if (index >= 32) bits++;
586 mask = (1 << (index & 31));
587 if (!(*bits & mask)) /* already free? */
589 LeaveCriticalSection( &thread->process->crit_section );
590 SetLastError( ERROR_INVALID_PARAMETER );
591 return FALSE;
593 *bits &= ~mask;
594 thread->tls_array[index] = 0;
595 /* FIXME: should zero all other thread values */
596 LeaveCriticalSection( &thread->process->crit_section );
597 return TRUE;
601 /**********************************************************************
602 * TlsGetValue [KERNEL32.532] Gets value in a thread's TLS slot
604 * RETURNS
605 * Success: Value stored in calling thread's TLS slot for index
606 * Failure: 0 and GetLastError returns NO_ERROR
608 LPVOID WINAPI TlsGetValue(
609 DWORD index) /* [in] TLS index to retrieve value for */
611 THDB *thread = THREAD_Current();
612 if (index >= 64)
614 SetLastError( ERROR_INVALID_PARAMETER );
615 return NULL;
617 SetLastError( ERROR_SUCCESS );
618 return thread->tls_array[index];
622 /**********************************************************************
623 * TlsSetValue [KERNEL32.533] Stores a value in the thread's TLS slot.
625 * RETURNS
626 * Success: TRUE
627 * Failure: FALSE
629 BOOL32 WINAPI TlsSetValue(
630 DWORD index, /* [in] TLS index to set value for */
631 LPVOID value) /* [in] Value to be stored */
633 THDB *thread = THREAD_Current();
634 if (index >= 64)
636 SetLastError( ERROR_INVALID_PARAMETER );
637 return FALSE;
639 thread->tls_array[index] = value;
640 return TRUE;
644 /***********************************************************************
645 * SetThreadContext [KERNEL32.670] Sets context of thread.
647 * RETURNS
648 * Success: TRUE
649 * Failure: FALSE
651 BOOL32 WINAPI SetThreadContext(
652 HANDLE32 handle, /* [in] Handle to thread with context */
653 CONTEXT *context) /* [out] Address of context structure */
655 THDB *thread = THREAD_GetPtr( handle, THREAD_GET_CONTEXT, NULL );
656 if (!thread) return FALSE;
657 *context = thread->context;
658 K32OBJ_DecCount( &thread->header );
659 return TRUE;
662 /***********************************************************************
663 * GetThreadContext [KERNEL32.294] Retrieves context of thread.
665 * RETURNS
666 * Success: TRUE
667 * Failure: FALSE
669 BOOL32 WINAPI GetThreadContext(
670 HANDLE32 handle, /* [in] Handle to thread with context */
671 CONTEXT *context) /* [out] Address of context structure */
673 THDB *thread = THREAD_GetPtr( handle, THREAD_GET_CONTEXT, NULL );
674 if (!thread) return FALSE;
675 *context = thread->context;
676 K32OBJ_DecCount( &thread->header );
677 return TRUE;
681 /**********************************************************************
682 * GetThreadPriority [KERNEL32.296] Returns priority for thread.
684 * RETURNS
685 * Success: Thread's priority level.
686 * Failure: THREAD_PRIORITY_ERROR_RETURN
688 INT32 WINAPI GetThreadPriority(
689 HANDLE32 hthread) /* [in] Handle to thread */
691 THDB *thread;
692 INT32 ret;
694 if (!(thread = THREAD_GetPtr( hthread, THREAD_QUERY_INFORMATION, NULL )))
695 return THREAD_PRIORITY_ERROR_RETURN;
696 ret = thread->delta_priority;
697 K32OBJ_DecCount( &thread->header );
698 return ret;
702 /**********************************************************************
703 * SetThreadPriority [KERNEL32.514] Sets priority for thread.
705 * RETURNS
706 * Success: TRUE
707 * Failure: FALSE
709 BOOL32 WINAPI SetThreadPriority(
710 HANDLE32 hthread, /* [in] Handle to thread */
711 INT32 priority) /* [in] Thread priority level */
713 THDB *thread;
715 if (!(thread = THREAD_GetPtr( hthread, THREAD_SET_INFORMATION, NULL )))
716 return FALSE;
717 thread->delta_priority = priority;
718 K32OBJ_DecCount( &thread->header );
719 return TRUE;
723 /**********************************************************************
724 * TerminateThread [KERNEL32.685] Terminates a thread
726 * RETURNS
727 * Success: TRUE
728 * Failure: FALSE
730 BOOL32 WINAPI TerminateThread(
731 HANDLE32 handle, /* [in] Handle to thread */
732 DWORD exitcode) /* [in] Exit code for thread */
734 int server_handle;
735 BOOL32 ret;
736 THDB *thread;
738 if (!(thread = THREAD_GetPtr( handle, THREAD_TERMINATE, &server_handle )))
739 return FALSE;
740 ret = !CLIENT_TerminateThread( server_handle, exitcode );
741 K32OBJ_DecCount( &thread->header );
742 return ret;
746 /**********************************************************************
747 * GetExitCodeThread [KERNEL32.???] Gets termination status of thread.
749 * RETURNS
750 * Success: TRUE
751 * Failure: FALSE
753 BOOL32 WINAPI GetExitCodeThread(
754 HANDLE32 hthread, /* [in] Handle to thread */
755 LPDWORD exitcode) /* [out] Address to receive termination status */
757 THDB *thread;
758 int server_handle;
760 if (!(thread = THREAD_GetPtr( hthread, THREAD_QUERY_INFORMATION, &server_handle )))
761 return FALSE;
762 if (server_handle != -1)
764 struct get_thread_info_reply info;
765 CLIENT_GetThreadInfo( server_handle, &info );
766 if (exitcode) *exitcode = info.exit_code;
768 else if (exitcode) *exitcode = thread->exit_code;
769 K32OBJ_DecCount( &thread->header );
770 return TRUE;
774 /**********************************************************************
775 * ResumeThread [KERNEL32.587] Resumes a thread.
777 * Decrements a thread's suspend count. When count is zero, the
778 * execution of the thread is resumed.
780 * RETURNS
781 * Success: Previous suspend count
782 * Failure: 0xFFFFFFFF
783 * Already running: 0
785 DWORD WINAPI ResumeThread(
786 HANDLE32 hthread) /* [in] Indentifies thread to restart */
788 THDB *thread;
789 DWORD oldcount;
791 SYSTEM_LOCK();
792 if (!(thread = THREAD_GetPtr( hthread, THREAD_QUERY_INFORMATION, NULL )))
794 SYSTEM_UNLOCK();
795 WARN(thread, "Invalid thread handle\n");
796 return 0xFFFFFFFF;
798 if ((oldcount = thread->suspend_count) != 0)
800 if (!--thread->suspend_count)
802 if (kill(thread->unix_pid, SIGCONT))
804 WARN(thread, "Unable to CONTinue pid: %04x\n",
805 thread->unix_pid);
806 oldcount = 0xFFFFFFFF;
810 K32OBJ_DecCount(&thread->header);
811 SYSTEM_UNLOCK();
812 return oldcount;
816 /**********************************************************************
817 * SuspendThread [KERNEL32.681] Suspends a thread.
819 * RETURNS
820 * Success: Previous suspend count
821 * Failure: 0xFFFFFFFF
823 DWORD WINAPI SuspendThread(
824 HANDLE32 hthread) /* [in] Handle to the thread */
826 THDB *thread;
827 DWORD oldcount;
829 SYSTEM_LOCK();
830 if (!(thread = THREAD_GetPtr( hthread, THREAD_QUERY_INFORMATION, NULL )))
832 SYSTEM_UNLOCK();
833 WARN(thread, "Invalid thread handle\n");
834 return 0xFFFFFFFF;
837 if (!(oldcount = thread->suspend_count))
839 if (thread->unix_pid == getpid())
840 WARN(thread, "Attempting to suspend myself\n" );
841 else
843 if (kill(thread->unix_pid, SIGSTOP))
845 WARN(thread, "Unable to STOP pid: %04x\n",
846 thread->unix_pid);
847 oldcount = 0xFFFFFFFF;
849 else thread->suspend_count++;
852 else thread->suspend_count++;
853 K32OBJ_DecCount( &thread->header );
854 SYSTEM_UNLOCK();
855 return oldcount;
860 /**********************************************************************
861 * GetThreadTimes [KERNEL32.???] Obtains timing information.
863 * NOTES
864 * What are the fields where these values are stored?
866 * RETURNS
867 * Success: TRUE
868 * Failure: FALSE
870 BOOL32 WINAPI GetThreadTimes(
871 HANDLE32 thread, /* [in] Specifies the thread of interest */
872 LPFILETIME creationtime, /* [out] When the thread was created */
873 LPFILETIME exittime, /* [out] When the thread was destroyed */
874 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
875 LPFILETIME usertime) /* [out] Time thread spent in user mode */
877 FIXME(thread,"(0x%08x): stub\n",thread);
878 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
879 return FALSE;
883 /**********************************************************************
884 * AttachThreadInput [KERNEL32.8] Attaches input of 1 thread to other
886 * Attaches the input processing mechanism of one thread to that of
887 * another thread.
889 * RETURNS
890 * Success: TRUE
891 * Failure: FALSE
893 BOOL32 WINAPI AttachThreadInput(
894 DWORD idAttach, /* [in] Thread to attach */
895 DWORD idAttachTo, /* [in] Thread to attach to */
896 BOOL32 fAttach) /* [in] Attach or detach */
898 BOOL32 ret;
900 FIXME(thread, "(0x%08lx,0x%08lx,%d): stub\n",idAttach,idAttachTo,fAttach);
901 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
902 if (fAttach) {
903 /* Attach threads */
904 ret = FALSE;
906 else {
907 /* Detach threads */
908 ret = FALSE;
910 return ret;
913 /**********************************************************************
914 * VWin32_BoostThreadGroup [KERNEL.535]
916 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT32 boost )
918 FIXME(thread, "(0x%08lx,%d): stub\n", threadId, boost);
921 /**********************************************************************
922 * VWin32_BoostThreadStatic [KERNEL.536]
924 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT32 boost )
926 FIXME(thread, "(0x%08lx,%d): stub\n", threadId, boost);