Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / kernel / tests / thread.c
blob86cf182c12f5062d6f7e09f71768fbfbf630a6f6
1 /*
2 * Unit test suite for directory functions.
4 * Copyright 2002 Geoffrey Hausheer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 /* Define _WIN32_WINNT to get SetThreadIdealProcessor on Windows */
22 #define _WIN32_WINNT 0x0500
24 #include <stdarg.h>
26 #include "wine/test.h"
27 #include <ntstatus.h>
28 #include <windef.h>
29 #include <winbase.h>
30 #include <winnt.h>
31 #include <winerror.h>
33 /* Specify the number of simultaneous threads to test */
34 #define NUM_THREADS 4
35 /* Specify whether to test the extended priorities for Win2k/XP */
36 #define USE_EXTENDED_PRIORITIES 0
37 /* Specify whether to test the stack allocation in CreateThread */
38 #define CHECK_STACK 0
40 /* Set CHECK_STACK to 1 if you want to try to test the stack-limit from
41 CreateThread. So far I have been unable to make this work, and
42 I am in doubt as to how portable it is. Also, according to MSDN,
43 you shouldn't mix C-run-time-libraries (i.e. alloca) with CreateThread.
44 Anyhow, the check is currently commented out
46 #if CHECK_STACK
47 #ifdef __try
48 #define __TRY __try
49 #define __EXCEPT __except
50 #define __ENDTRY
51 #else
52 #include "wine/exception.h"
53 #endif
54 #endif
56 typedef BOOL (WINAPI *GetThreadPriorityBoost_t)(HANDLE,PBOOL);
57 static GetThreadPriorityBoost_t pGetThreadPriorityBoost=NULL;
59 typedef HANDLE (WINAPI *OpenThread_t)(DWORD,BOOL,DWORD);
60 static OpenThread_t pOpenThread=NULL;
62 typedef DWORD (WINAPI *SetThreadIdealProcessor_t)(HANDLE,DWORD);
63 static SetThreadIdealProcessor_t pSetThreadIdealProcessor=NULL;
65 typedef BOOL (WINAPI *SetThreadPriorityBoost_t)(HANDLE,BOOL);
66 static SetThreadPriorityBoost_t pSetThreadPriorityBoost=NULL;
68 /* Functions not tested yet:
69 AttachThreadInput
70 CreateRemoteThread
71 SetThreadContext
72 SwitchToThread
74 In addition there are no checks that the inheritance works properly in
75 CreateThread
78 DWORD tlsIndex;
80 typedef struct {
81 int threadnum;
82 HANDLE *event;
83 DWORD *threadmem;
84 } t1Struct;
86 /* Basic test that simulatneous threads can access shared memory,
87 that the thread local storage routines work correctly, and that
88 threads actually run concurrently
90 VOID WINAPI threadFunc1(t1Struct *tstruct)
92 int i;
93 /* write our thread # into shared memory */
94 tstruct->threadmem[tstruct->threadnum]=GetCurrentThreadId();
95 ok(TlsSetValue(tlsIndex,(LPVOID)(tstruct->threadnum+1))!=0,
96 "TlsSetValue failed");
97 /* The threads synchronize before terminating. This is done by
98 Signaling an event, and waiting for all events to occur
100 SetEvent(tstruct->event[tstruct->threadnum]);
101 WaitForMultipleObjects(NUM_THREADS,tstruct->event,TRUE,INFINITE);
102 /* Double check that all threads really did run by validating that
103 they have all written to the shared memory. There should be no race
104 here, since all threads were synchronized after the write.*/
105 for(i=0;i<NUM_THREADS;i++) {
106 while(tstruct->threadmem[i]==0) ;
108 /* Check that noone cahnged our tls memory */
109 ok((int)TlsGetValue(tlsIndex)-1==tstruct->threadnum,
110 "TlsGetValue failed");
111 ExitThread(NUM_THREADS+tstruct->threadnum);
114 VOID WINAPI threadFunc2()
116 ExitThread(99);
119 VOID WINAPI threadFunc3()
121 HANDLE thread;
122 thread=GetCurrentThread();
123 SuspendThread(thread);
124 ExitThread(99);
127 VOID WINAPI threadFunc4(HANDLE event)
129 if(event != NULL) {
130 SetEvent(event);
132 Sleep(99000);
133 ExitThread(0);
136 #if CHECK_STACK
137 VOID WINAPI threadFunc5(DWORD *exitCode)
139 SYSTEM_INFO sysInfo;
140 sysInfo.dwPageSize=0;
141 GetSystemInfo(&sysInfo);
142 *exitCode=0;
143 __TRY
145 alloca(2*sysInfo.dwPageSize);
147 __EXCEPT(1) {
148 *exitCode=1;
150 __ENDTRY
151 ExitThread(0);
153 #endif
155 /* Check basic funcationality of CreateThread and Tls* functions */
156 VOID test_CreateThread_basic()
158 HANDLE thread[NUM_THREADS],event[NUM_THREADS];
159 DWORD threadid[NUM_THREADS],curthreadId;
160 DWORD threadmem[NUM_THREADS];
161 DWORD exitCode;
162 t1Struct tstruct[NUM_THREADS];
163 int error;
164 DWORD i,j;
165 /* Retrieve current Thread ID for later comparisons */
166 curthreadId=GetCurrentThreadId();
167 /* Allocate some local storage */
168 ok((tlsIndex=TlsAlloc())!=TLS_OUT_OF_INDEXES,"TlsAlloc failed");
169 /* Create events for thread synchronization */
170 for(i=0;i<NUM_THREADS;i++) {
171 threadmem[i]=0;
172 /* Note that it doesn't matter what type of event we chose here. This
173 test isn't trying to thoroughly test events
175 event[i]=CreateEventA(NULL,TRUE,FALSE,NULL);
176 tstruct[i].threadnum=i;
177 tstruct[i].threadmem=threadmem;
178 tstruct[i].event=event;
181 /* Test that passing arguments to threads works okay */
182 for(i=0;i<NUM_THREADS;i++) {
183 thread[i] = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadFunc1,
184 &tstruct[i],0,&threadid[i]);
185 ok(thread[i]!=NULL,"Create Thread failed.");
187 /* Test that the threads actually complete */
188 for(i=0;i<NUM_THREADS;i++) {
189 error=WaitForSingleObject(thread[i],5000);
190 ok(error==WAIT_OBJECT_0, "Thread did not complete within timelimit");
191 if(error!=WAIT_OBJECT_0) {
192 TerminateThread(thread[i],i+NUM_THREADS);
194 ok(GetExitCodeThread(thread[i],&exitCode),"Could not retrieve ext code");
195 ok(exitCode==i+NUM_THREADS,"Thread returned an incorrect exit code");
197 /* Test that each thread executed in its parent's address space
198 (it was able to change threadmem and pass that change back to its parent)
199 and that each thread id was independant). Note that we prove that the
200 threads actually execute concurrently by having them block on each other
201 in threadFunc1
203 for(i=0;i<NUM_THREADS;i++) {
204 error=0;
205 for(j=i+1;j<NUM_THREADS;j++) {
206 if (threadmem[i]==threadmem[j]) {
207 error=1;
210 ok(!error && threadmem[i]==threadid[i] && threadmem[i]!=curthreadId,
211 "Thread did not execute successfully");
212 ok(CloseHandle(thread[i])!=0,"CloseHandle failed");
214 ok(TlsFree(tlsIndex)!=0,"TlsFree failed");
217 /* Check that using the CREATE_SUSPENDED flag works */
218 VOID test_CreateThread_suspended()
220 HANDLE thread;
221 DWORD threadId;
222 int error;
224 thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadFunc2,NULL,
225 CREATE_SUSPENDED,&threadId);
226 ok(thread!=NULL,"Create Thread failed.");
227 /* Check that the thread is suspended */
228 ok(SuspendThread(thread)==1,"Thread did not start suspended");
229 ok(ResumeThread(thread)==2,"Resume thread returned an invalid value");
230 /* Check that resume thread didn't actually start the thread. I can't think
231 of a better way of checking this than just waiting. I am not sure if this
232 will work on slow computers.
234 ok(WaitForSingleObject(thread,1000)==WAIT_TIMEOUT,
235 "ResumeThread should not have actually started the thread");
236 /* Now actually resume the thread and make sure that it actually completes*/
237 ok(ResumeThread(thread)==1,"Resume thread returned an invalid value");
238 ok((error=WaitForSingleObject(thread,1000))==WAIT_OBJECT_0,
239 "Thread did not resume");
240 if(error!=WAIT_OBJECT_0) {
241 TerminateThread(thread,1);
243 ok(CloseHandle(thread)!=0,"CloseHandle failed");
246 /* Check that SuspendThread and ResumeThread work */
247 VOID test_SuspendThread()
249 HANDLE thread,access_thread;
250 DWORD threadId,exitCode;
251 int i,error;
253 thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadFunc3,NULL,
254 0,&threadId);
255 ok(thread!=NULL,"Create Thread failed.");
256 /* Check that the thread is suspended */
257 /* Note that this is a polling method, and there is a race between
258 SuspendThread being called (in the child, and the loop below timing out,
259 so the test could fail on a heavily loaded or slow computer.
261 error=0;
262 for(i=0;error==0 && i<100;i++) {
263 error=SuspendThread(thread);
264 ResumeThread(thread);
265 if(error==0) {
266 Sleep(50);
267 i++;
270 ok(error==1,"SuspendThread did not work");
271 /* check that access restrictions are obeyed */
272 if (pOpenThread) {
273 access_thread=pOpenThread(THREAD_ALL_ACCESS & (~THREAD_SUSPEND_RESUME),
274 0,threadId);
275 ok(access_thread!=NULL,"OpenThread returned an invalid handle");
276 if (access_thread!=NULL) {
277 ok(SuspendThread(access_thread)==-1,
278 "SuspendThread did not obey access restrictions");
279 ok(ResumeThread(access_thread)==-1,
280 "ResumeThread did not obey access restrictions");
281 ok(CloseHandle(access_thread)!=0,"CloseHandle Failed");
284 /* Double check that the thread really is suspended */
285 ok((error=GetExitCodeThread(thread,&exitCode))!=0 && exitCode==STILL_ACTIVE,
286 "Thread did not really suspend");
287 /* Resume the thread, and make sure it actually completes */
288 ok(ResumeThread(thread)==1,"Resume thread returned an invalid value");
289 ok((error=WaitForSingleObject(thread,1000))==WAIT_OBJECT_0,
290 "Thread did not resume");
291 if(error!=WAIT_OBJECT_0) {
292 TerminateThread(thread,1);
294 /* Trying to suspend a terminated thread should fail */
295 error=SuspendThread(thread);
296 ok(error==0xffffffff, "wrong return code: %d", error);
297 ok(GetLastError()==ERROR_ACCESS_DENIED || GetLastError()==ERROR_NO_MORE_ITEMS, "unexpected error code: %ld", GetLastError());
299 ok(CloseHandle(thread)!=0,"CloseHandle Failed");
302 /* Check that TerminateThread works properly
304 VOID test_TerminateThread()
306 HANDLE thread,access_thread,event;
307 DWORD threadId,exitCode;
308 int i,error;
309 i=0; error=0;
310 event=CreateEventA(NULL,TRUE,FALSE,NULL);
311 thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadFunc4,
312 (LPVOID)event, 0,&threadId);
313 ok(thread!=NULL,"Create Thread failed.");
314 /* Terminate thread has a race condition in Wine. If the thread is terminated
315 before it starts, it leaves a process behind. Therefore, we wait for the
316 thread to signal that it has started. There is no easy way to force the
317 race to occur, so we don't try to find it.
319 ok(WaitForSingleObject(event,5000)==WAIT_OBJECT_0,
320 "TerminateThread didn't work");
321 /* check that access restrictions are obeyed */
322 if (pOpenThread) {
323 access_thread=pOpenThread(THREAD_ALL_ACCESS & (~THREAD_TERMINATE),
324 0,threadId);
325 ok(access_thread!=NULL,"OpenThread returned an invalid handle");
326 if (access_thread!=NULL) {
327 ok(TerminateThread(access_thread,99)==0,
328 "TerminateThread did not obey access restrictions");
329 ok(CloseHandle(access_thread)!=0,"CloseHandle Failed");
332 /* terminate a job and make sure it terminates */
333 ok(TerminateThread(thread,99)!=0,"TerminateThread failed");
334 ok(WaitForSingleObject(thread,5000)==WAIT_OBJECT_0,
335 "TerminateThread didn't work");
336 ok(GetExitCodeThread(thread,&exitCode)!=STILL_ACTIVE,
337 "TerminateThread should not leave the thread 'STILL_ACTIVE'");
338 ok(exitCode==99, "TerminateThread returned invalid exit code");
339 ok(CloseHandle(thread)!=0,"Error Closing thread handle");
342 /* Check if CreateThread obeys the specified stack size. This code does
343 not work properly, and is currently disabled
345 VOID test_CreateThread_stack()
347 #if CHECK_STACK
348 /* The only way I know of to test the stack size is to use alloca
349 and __try/__except. However, this is probably not portable,
350 and I couldn't get it to work under Wine anyhow. However, here
351 is the code which should allow for testing that CreateThread
352 respects the stack-size limit
354 HANDLE thread;
355 DWORD threadId,exitCode;
357 SYSTEM_INFO sysInfo;
358 sysInfo.dwPageSize=0;
359 GetSystemInfo(&sysInfo);
360 ok(sysInfo.dwPageSize>0,"GetSystemInfo should return a valid page size");
361 thread = CreateThread(NULL,sysInfo.dwPageSize,
362 (LPTHREAD_START_ROUTINE)threadFunc5,&exitCode,
363 0,&threadId);
364 ok(WaitForSingleObject(thread,5000)==WAIT_OBJECT_0,
365 "TerminateThread didn't work");
366 ok(exitCode==1,"CreateThread did not obey stack-size-limit");
367 ok(CloseHandle(thread)!=0,"CloseHandle failed");
368 #endif
371 /* Check whether setting/retreiving thread priorities works */
372 VOID test_thread_priority()
374 HANDLE curthread,access_thread;
375 DWORD curthreadId,exitCode;
376 int min_priority=-2,max_priority=2;
377 BOOL disabled;
378 int i;
380 curthread=GetCurrentThread();
381 curthreadId=GetCurrentThreadId();
382 /* Check thread priority */
383 /* NOTE: on Win2k/XP priority can be from -7 to 6. All other platforms it
384 is -2 to 2. However, even on a real Win2k system, using thread
385 priorities beyond the -2 to 2 range does not work. If you want to try
386 anyway, enable USE_EXTENDED_PRIORITIES
388 ok(GetThreadPriority(curthread)==THREAD_PRIORITY_NORMAL,
389 "GetThreadPriority Failed");
391 if (pOpenThread) {
392 /* check that access control is obeyed */
393 access_thread=pOpenThread(THREAD_ALL_ACCESS &
394 (~THREAD_QUERY_INFORMATION) & (~THREAD_SET_INFORMATION),
395 0,curthreadId);
396 ok(access_thread!=NULL,"OpenThread returned an invalid handle");
397 if (access_thread!=NULL) {
398 ok(SetThreadPriority(access_thread,1)==0,
399 "SetThreadPriority did not obey access restrictions");
400 ok(GetThreadPriority(access_thread)==THREAD_PRIORITY_ERROR_RETURN,
401 "GetThreadPriority did not obey access restrictions");
402 if (pSetThreadPriorityBoost)
403 ok(pSetThreadPriorityBoost(access_thread,1)==0,
404 "SetThreadPriorityBoost did not obey access restrictions");
405 if (pGetThreadPriorityBoost)
406 ok(pGetThreadPriorityBoost(access_thread,&disabled)==0,
407 "GetThreadPriorityBoost did not obey access restrictions");
408 ok(GetExitCodeThread(access_thread,&exitCode)==0,
409 "GetExitCodeThread did not obey access restrictions");
410 ok(CloseHandle(access_thread),"Error Closing thread handle");
412 #if USE_EXTENDED_PRIORITIES
413 min_priority=-7; max_priority=6;
414 #endif
416 for(i=min_priority;i<=max_priority;i++) {
417 ok(SetThreadPriority(curthread,i)!=0,
418 "SetThreadPriority Failed for priority: %d",i);
419 ok(GetThreadPriority(curthread)==i,
420 "GetThreadPriority Failed for priority: %d",i);
422 ok(SetThreadPriority(curthread,THREAD_PRIORITY_TIME_CRITICAL)!=0,
423 "SetThreadPriority Failed");
424 ok(GetThreadPriority(curthread)==THREAD_PRIORITY_TIME_CRITICAL,
425 "GetThreadPriority Failed");
426 ok(SetThreadPriority(curthread,THREAD_PRIORITY_IDLE)!=0,
427 "SetThreadPriority Failed");
428 ok(GetThreadPriority(curthread)==THREAD_PRIORITY_IDLE,
429 "GetThreadPriority Failed");
430 ok(SetThreadPriority(curthread,0)!=0,"SetThreadPriority Failed");
432 /* Check thread priority boost */
433 if (pGetThreadPriorityBoost && pSetThreadPriorityBoost) {
434 BOOL rc;
435 todo_wine {
436 SetLastError(0);
437 rc=pGetThreadPriorityBoost(curthread,&disabled);
438 if (rc!=0 || GetLastError()!=ERROR_CALL_NOT_IMPLEMENTED) {
439 ok(rc!=0,"error=%ld",GetLastError());
441 ok(pSetThreadPriorityBoost(curthread,1)!=0,
442 "error=%ld",GetLastError());
443 rc=pGetThreadPriorityBoost(curthread,&disabled);
444 ok(rc!=0 && disabled==1,
445 "rc=%d error=%ld disabled=%d",rc,GetLastError(),disabled);
447 ok(pSetThreadPriorityBoost(curthread,0)!=0,
448 "error=%ld",GetLastError());
449 rc=pGetThreadPriorityBoost(curthread,&disabled);
450 ok(rc!=0 && disabled==0,
451 "rc=%d error=%ld disabled=%d",rc,GetLastError(),disabled);
457 /* check the GetThreadTimes function */
458 VOID test_GetThreadTimes()
460 HANDLE thread,access_thread=NULL;
461 FILETIME creationTime,exitTime,kernelTime,userTime;
462 DWORD threadId;
463 int error;
465 thread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadFunc2,NULL,
466 CREATE_SUSPENDED,&threadId);
468 ok(thread!=NULL,"Create Thread failed.");
469 /* check that access control is obeyed */
470 if (pOpenThread) {
471 access_thread=pOpenThread(THREAD_ALL_ACCESS &
472 (~THREAD_QUERY_INFORMATION), 0,threadId);
473 ok(access_thread!=NULL,
474 "OpenThread returned an invalid handle");
476 ok(ResumeThread(thread)==1,"Resume thread returned an invalid value");
477 ok(WaitForSingleObject(thread,5000)==WAIT_OBJECT_0,
478 "ResumeThread didn't work");
479 if(access_thread!=NULL) {
480 error=GetThreadTimes(access_thread,&creationTime,&exitTime,
481 &kernelTime,&userTime);
482 ok(error==0, "GetThreadTimes did not obey access restrictions");
483 ok(CloseHandle(access_thread)!=0,"CloseHandle Failed");
485 creationTime.dwLowDateTime=99; creationTime.dwHighDateTime=99;
486 exitTime.dwLowDateTime=99; exitTime.dwHighDateTime=99;
487 kernelTime.dwLowDateTime=99; kernelTime.dwHighDateTime=99;
488 userTime.dwLowDateTime=99; userTime.dwHighDateTime=99;
489 /* GetThreadTimes should set all of the parameters passed to it */
490 error=GetThreadTimes(thread,&creationTime,&exitTime,
491 &kernelTime,&userTime);
492 if (error!=0 || GetLastError()!=ERROR_CALL_NOT_IMPLEMENTED) {
493 ok(error!=0,"GetThreadTimes failed");
494 ok(creationTime.dwLowDateTime!=99 || creationTime.dwHighDateTime!=99,
495 "creationTime was invalid");
496 ok(exitTime.dwLowDateTime!=99 || exitTime.dwHighDateTime!=99,
497 "exitTime was invalid");
498 ok(kernelTime.dwLowDateTime!=99 || kernelTime.dwHighDateTime!=99,
499 "kernelTimewas invalid");
500 ok(userTime.dwLowDateTime!=99 || userTime.dwHighDateTime!=99,
501 "userTime was invalid");
502 ok(CloseHandle(thread)!=0,"CloseHandle failed");
506 /* Check the processor affinity functions */
507 /* NOTE: These functions should also be checked that they obey access control
509 VOID test_thread_processor()
511 HANDLE curthread,curproc;
512 DWORD processMask,systemMask;
513 SYSTEM_INFO sysInfo;
514 int error=0;
516 sysInfo.dwNumberOfProcessors=0;
517 GetSystemInfo(&sysInfo);
518 ok(sysInfo.dwNumberOfProcessors>0,
519 "GetSystemInfo failed to return a valid # of processors");
520 /* Use the current Thread/process for all tests */
521 curthread=GetCurrentThread();
522 ok(curthread!=NULL,"GetCurrentThread failed");
523 curproc=GetCurrentProcess();
524 ok(curproc!=NULL,"GetCurrentProcess failed");
525 /* Check the Affinity Mask functions */
526 ok(GetProcessAffinityMask(curproc,&processMask,&systemMask)!=0,
527 "GetProcessAffinityMask failed");
528 ok(SetThreadAffinityMask(curthread,processMask)==1,
529 "SetThreadAffinityMask failed");
530 ok(SetThreadAffinityMask(curthread,processMask+1)==0,
531 "SetThreadAffinityMask passed for an illegal processor");
532 /* NOTE: This only works on WinNT/2000/XP) */
533 if (pSetThreadIdealProcessor) {
534 todo_wine {
535 SetLastError(0);
536 error=pSetThreadIdealProcessor(curthread,0);
537 if (GetLastError()!=ERROR_CALL_NOT_IMPLEMENTED) {
538 ok(error!=-1, "SetThreadIdealProcessor failed");
541 if (GetLastError()!=ERROR_CALL_NOT_IMPLEMENTED) {
542 error=pSetThreadIdealProcessor(curthread,MAXIMUM_PROCESSORS+1);
543 ok(error==-1,
544 "SetThreadIdealProcessor succeeded with an illegal processor #");
545 todo_wine {
546 error=pSetThreadIdealProcessor(curthread,MAXIMUM_PROCESSORS);
547 ok(error==0, "SetThreadIdealProcessor returned an incorrect value");
553 START_TEST(thread)
555 HINSTANCE lib;
556 /* Neither Cygwin nor mingW export OpenThread, so do a dynamic check
557 so that the compile passes
559 lib=LoadLibraryA("kernel32");
560 ok(lib!=NULL,"Couldn't load kernel32.dll");
561 pGetThreadPriorityBoost=(GetThreadPriorityBoost_t)GetProcAddress(lib,"GetThreadPriorityBoost");
562 pOpenThread=(OpenThread_t)GetProcAddress(lib,"OpenThread");
563 pSetThreadIdealProcessor=(SetThreadIdealProcessor_t)GetProcAddress(lib,"SetThreadIdealProcessor");
564 pSetThreadPriorityBoost=(SetThreadPriorityBoost_t)GetProcAddress(lib,"SetThreadPriorityBoost");
565 test_CreateThread_basic();
566 test_CreateThread_suspended();
567 test_SuspendThread();
568 test_TerminateThread();
569 test_CreateThread_stack();
570 test_thread_priority();
571 test_GetThreadTimes();
572 test_thread_processor();