winex11: Consider zero-size windows mapped even when they are positioned at 0,0.
[wine/multimedia.git] / dlls / kernel32 / tests / thread.c
blob19726af54b2186201777aa9acd5e0e585209bee3
1 /*
2 * Unit test suite for thread 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* Define _WIN32_WINNT to get SetThreadIdealProcessor on Windows */
22 #define _WIN32_WINNT 0x0500
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdio.h>
28 #include "wine/test.h"
29 #include <windef.h>
30 #include <winbase.h>
31 #include <winnt.h>
32 #include <winerror.h>
34 /* Specify the number of simultaneous threads to test */
35 #define NUM_THREADS 4
36 /* Specify whether to test the extended priorities for Win2k/XP */
37 #define USE_EXTENDED_PRIORITIES 0
38 /* Specify whether to test the stack allocation in CreateThread */
39 #define CHECK_STACK 0
41 /* Set CHECK_STACK to 1 if you want to try to test the stack-limit from
42 CreateThread. So far I have been unable to make this work, and
43 I am in doubt as to how portable it is. Also, according to MSDN,
44 you shouldn't mix C-run-time-libraries (i.e. alloca) with CreateThread.
45 Anyhow, the check is currently commented out
47 #if CHECK_STACK
48 # ifdef __try
49 # define __TRY __try
50 # define __EXCEPT __except
51 # define __ENDTRY
52 # else
53 # include "wine/exception.h"
54 # endif
55 #endif
57 static BOOL (WINAPI *pGetThreadPriorityBoost)(HANDLE,PBOOL);
58 static HANDLE (WINAPI *pOpenThread)(DWORD,BOOL,DWORD);
59 static BOOL (WINAPI *pQueueUserWorkItem)(LPTHREAD_START_ROUTINE,PVOID,ULONG);
60 static DWORD (WINAPI *pSetThreadIdealProcessor)(HANDLE,DWORD);
61 static BOOL (WINAPI *pSetThreadPriorityBoost)(HANDLE,BOOL);
62 static BOOL (WINAPI *pRegisterWaitForSingleObject)(PHANDLE,HANDLE,WAITORTIMERCALLBACK,PVOID,ULONG,ULONG);
63 static BOOL (WINAPI *pUnregisterWait)(HANDLE);
64 static BOOL (WINAPI *pIsWow64Process)(HANDLE,PBOOL);
65 static BOOL (WINAPI *pSetThreadErrorMode)(DWORD,PDWORD);
66 static DWORD (WINAPI *pGetThreadErrorMode)(void);
67 static DWORD (WINAPI *pRtlGetThreadErrorMode)(void);
69 static HANDLE create_target_process(const char *arg)
71 char **argv;
72 char cmdline[MAX_PATH];
73 PROCESS_INFORMATION pi;
74 BOOL ret;
75 STARTUPINFO si = { 0 };
76 si.cb = sizeof(si);
78 winetest_get_mainargs( &argv );
79 sprintf(cmdline, "%s %s %s", argv[0], argv[1], arg);
80 ret = CreateProcess(NULL, cmdline, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
81 ok(ret, "error: %u\n", GetLastError());
82 ret = CloseHandle(pi.hThread);
83 ok(ret, "error %u\n", GetLastError());
84 return pi.hProcess;
87 /* Functions not tested yet:
88 AttachThreadInput
89 SetThreadContext
90 SwitchToThread
92 In addition there are no checks that the inheritance works properly in
93 CreateThread
96 /* Functions to ensure that from a group of threads, only one executes
97 certain chunks of code at a time, and we know which one is executing
98 it. It basically makes multithreaded execution linear, which defeats
99 the purpose of multiple threads, but makes testing easy. */
100 static HANDLE start_event, stop_event;
101 static LONG num_synced;
103 static void init_thread_sync_helpers(void)
105 start_event = CreateEvent(NULL, TRUE, FALSE, NULL);
106 ok(start_event != NULL, "CreateEvent failed\n");
107 stop_event = CreateEvent(NULL, TRUE, FALSE, NULL);
108 ok(stop_event != NULL, "CreateEvent failed\n");
109 num_synced = -1;
112 static BOOL sync_threads_and_run_one(DWORD sync_id, DWORD my_id)
114 LONG num = InterlockedIncrement(&num_synced);
115 assert(-1 <= num && num <= 1);
116 if (num == 1)
118 ResetEvent( stop_event );
119 SetEvent( start_event );
121 else
123 DWORD ret = WaitForSingleObject(start_event, 10000);
124 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed %x\n",ret);
126 return sync_id == my_id;
129 static void resync_after_run(void)
131 LONG num = InterlockedDecrement(&num_synced);
132 assert(-1 <= num && num <= 1);
133 if (num == -1)
135 ResetEvent( start_event );
136 SetEvent( stop_event );
138 else
140 DWORD ret = WaitForSingleObject(stop_event, 10000);
141 ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
145 static void cleanup_thread_sync_helpers(void)
147 CloseHandle(start_event);
148 CloseHandle(stop_event);
151 static DWORD tlsIndex;
153 typedef struct {
154 int threadnum;
155 HANDLE *event;
156 DWORD *threadmem;
157 } t1Struct;
159 /* WinME supports OpenThread but doesn't know about access restrictions so
160 we require them to be either completely ignored or always obeyed.
162 static INT obeying_ars = 0; /* -1 == no, 0 == dunno yet, 1 == yes */
163 #define obey_ar(x) \
164 (obeying_ars == 0 \
165 ? ((x) \
166 ? (obeying_ars = +1) \
167 : ((obeying_ars = -1), \
168 trace("not restricted, assuming consistent behaviour\n"))) \
169 : (obeying_ars < 0) \
170 ? ok(!(x), "access restrictions obeyed\n") \
171 : ok( (x), "access restrictions not obeyed\n"))
173 /* Basic test that simultaneous threads can access shared memory,
174 that the thread local storage routines work correctly, and that
175 threads actually run concurrently
177 static DWORD WINAPI threadFunc1(LPVOID p)
179 t1Struct *tstruct = p;
180 int i;
181 /* write our thread # into shared memory */
182 tstruct->threadmem[tstruct->threadnum]=GetCurrentThreadId();
183 ok(TlsSetValue(tlsIndex,(LPVOID)(INT_PTR)(tstruct->threadnum+1))!=0,
184 "TlsSetValue failed\n");
185 /* The threads synchronize before terminating. This is done by
186 Signaling an event, and waiting for all events to occur
188 SetEvent(tstruct->event[tstruct->threadnum]);
189 WaitForMultipleObjects(NUM_THREADS,tstruct->event,TRUE,INFINITE);
190 /* Double check that all threads really did run by validating that
191 they have all written to the shared memory. There should be no race
192 here, since all threads were synchronized after the write.*/
193 for(i=0;i<NUM_THREADS;i++) {
194 while(tstruct->threadmem[i]==0) ;
197 /* lstrlenA contains an exception handler so this makes sure exceptions work in threads */
198 ok( lstrlenA( (char *)0xdeadbeef ) == 0, "lstrlenA: unexpected success\n" );
200 /* Check that no one changed our tls memory */
201 ok((INT_PTR)TlsGetValue(tlsIndex)-1==tstruct->threadnum,
202 "TlsGetValue failed\n");
203 return NUM_THREADS+tstruct->threadnum;
206 static DWORD WINAPI threadFunc2(LPVOID p)
208 return 99;
211 static DWORD WINAPI threadFunc3(LPVOID p)
213 HANDLE thread;
214 thread=GetCurrentThread();
215 SuspendThread(thread);
216 return 99;
219 static DWORD WINAPI threadFunc4(LPVOID p)
221 HANDLE event = p;
222 if(event != NULL) {
223 SetEvent(event);
225 Sleep(99000);
226 return 0;
229 #if CHECK_STACK
230 static DWORD WINAPI threadFunc5(LPVOID p)
232 DWORD *exitCode = p;
233 SYSTEM_INFO sysInfo;
234 sysInfo.dwPageSize=0;
235 GetSystemInfo(&sysInfo);
236 *exitCode=0;
237 __TRY
239 alloca(2*sysInfo.dwPageSize);
241 __EXCEPT(1) {
242 *exitCode=1;
244 __ENDTRY
245 return 0;
247 #endif
249 static DWORD WINAPI threadFunc_SetEvent(LPVOID p)
251 SetEvent(p);
252 return 0;
255 static DWORD WINAPI threadFunc_CloseHandle(LPVOID p)
257 CloseHandle(p);
258 return 0;
261 static void create_function_addr_events(HANDLE events[2])
263 char buffer[256];
265 sprintf(buffer, "threadFunc_SetEvent %p", threadFunc_SetEvent);
266 events[0] = CreateEvent(NULL, FALSE, FALSE, buffer);
268 sprintf(buffer, "threadFunc_CloseHandle %p", threadFunc_CloseHandle);
269 events[1] = CreateEvent(NULL, FALSE, FALSE, buffer);
272 /* check CreateRemoteThread */
273 static VOID test_CreateRemoteThread(void)
275 HANDLE hProcess, hThread, hEvent, hRemoteEvent;
276 DWORD tid, ret, exitcode;
277 HANDLE hAddrEvents[2];
279 hProcess = create_target_process("sleep");
280 ok(hProcess != NULL, "Can't start process\n");
282 /* ensure threadFunc_SetEvent & threadFunc_CloseHandle are the same
283 * address as in the child process */
284 create_function_addr_events(hAddrEvents);
285 ret = WaitForMultipleObjects(2, hAddrEvents, TRUE, 5000);
286 if (ret == WAIT_TIMEOUT)
288 skip("child process wasn't mapped at same address, so can't do CreateRemoteThread tests.\n");
289 return;
291 ok(ret == WAIT_OBJECT_0 || broken(ret == WAIT_OBJECT_0+1 /* nt4,w2k */), "WaitForAllObjects 2 events %d\n", ret);
293 hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
294 ok(hEvent != NULL, "Can't create event, err=%u\n", GetLastError());
295 ret = DuplicateHandle(GetCurrentProcess(), hEvent, hProcess, &hRemoteEvent,
296 0, FALSE, DUPLICATE_SAME_ACCESS);
297 ok(ret != 0, "DuplicateHandle failed, err=%u\n", GetLastError());
299 /* create suspended remote thread with entry point SetEvent() */
300 SetLastError(0xdeadbeef);
301 hThread = CreateRemoteThread(hProcess, NULL, 0, threadFunc_SetEvent,
302 hRemoteEvent, CREATE_SUSPENDED, &tid);
303 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
305 win_skip("CreateRemoteThread is not implemented\n");
306 goto cleanup;
308 ok(hThread != NULL, "CreateRemoteThread failed, err=%u\n", GetLastError());
309 ok(tid != 0, "null tid\n");
310 ret = SuspendThread(hThread);
311 ok(ret == 1, "ret=%u, err=%u\n", ret, GetLastError());
312 ret = ResumeThread(hThread);
313 ok(ret == 2, "ret=%u, err=%u\n", ret, GetLastError());
315 /* thread still suspended, so wait times out */
316 ret = WaitForSingleObject(hEvent, 1000);
317 ok(ret == WAIT_TIMEOUT, "wait did not time out, ret=%u\n", ret);
319 ret = ResumeThread(hThread);
320 ok(ret == 1, "ret=%u, err=%u\n", ret, GetLastError());
322 /* wait that doesn't time out */
323 ret = WaitForSingleObject(hEvent, 1000);
324 ok(ret == WAIT_OBJECT_0, "object not signaled, ret=%u\n", ret);
326 /* wait for thread end */
327 ret = WaitForSingleObject(hThread, 1000);
328 ok(ret == WAIT_OBJECT_0, "waiting for thread failed, ret=%u\n", ret);
329 CloseHandle(hThread);
331 /* create and wait for remote thread with entry point CloseHandle() */
332 hThread = CreateRemoteThread(hProcess, NULL, 0,
333 threadFunc_CloseHandle,
334 hRemoteEvent, 0, &tid);
335 ok(hThread != NULL, "CreateRemoteThread failed, err=%u\n", GetLastError());
336 ret = WaitForSingleObject(hThread, 1000);
337 ok(ret == WAIT_OBJECT_0, "waiting for thread failed, ret=%u\n", ret);
338 CloseHandle(hThread);
340 /* create remote thread with entry point SetEvent() */
341 hThread = CreateRemoteThread(hProcess, NULL, 0,
342 threadFunc_SetEvent,
343 hRemoteEvent, 0, &tid);
344 ok(hThread != NULL, "CreateRemoteThread failed, err=%u\n", GetLastError());
346 /* closed handle, so wait times out */
347 ret = WaitForSingleObject(hEvent, 1000);
348 ok(ret == WAIT_TIMEOUT, "wait did not time out, ret=%u\n", ret);
350 /* check that remote SetEvent() failed */
351 ret = GetExitCodeThread(hThread, &exitcode);
352 ok(ret != 0, "GetExitCodeThread failed, err=%u\n", GetLastError());
353 if (ret) ok(exitcode == 0, "SetEvent succeeded, expected to fail\n");
354 CloseHandle(hThread);
356 cleanup:
357 TerminateProcess(hProcess, 0);
358 CloseHandle(hEvent);
359 CloseHandle(hProcess);
362 /* Check basic functionality of CreateThread and Tls* functions */
363 static VOID test_CreateThread_basic(void)
365 HANDLE thread[NUM_THREADS],event[NUM_THREADS];
366 DWORD threadid[NUM_THREADS],curthreadId;
367 DWORD threadmem[NUM_THREADS];
368 DWORD exitCode;
369 t1Struct tstruct[NUM_THREADS];
370 int error;
371 DWORD i,j;
372 DWORD GLE, ret;
373 DWORD tid;
374 BOOL bRet;
376 /* lstrlenA contains an exception handler so this makes sure exceptions work in the main thread */
377 ok( lstrlenA( (char *)0xdeadbeef ) == 0, "lstrlenA: unexpected success\n" );
379 /* Retrieve current Thread ID for later comparisons */
380 curthreadId=GetCurrentThreadId();
381 /* Allocate some local storage */
382 ok((tlsIndex=TlsAlloc())!=TLS_OUT_OF_INDEXES,"TlsAlloc failed\n");
383 /* Create events for thread synchronization */
384 for(i=0;i<NUM_THREADS;i++) {
385 threadmem[i]=0;
386 /* Note that it doesn't matter what type of event we choose here. This
387 test isn't trying to thoroughly test events
389 event[i]=CreateEventA(NULL,TRUE,FALSE,NULL);
390 tstruct[i].threadnum=i;
391 tstruct[i].threadmem=threadmem;
392 tstruct[i].event=event;
395 /* Test that passing arguments to threads works okay */
396 for(i=0;i<NUM_THREADS;i++) {
397 thread[i] = CreateThread(NULL,0,threadFunc1,
398 &tstruct[i],0,&threadid[i]);
399 ok(thread[i]!=NULL,"Create Thread failed\n");
401 /* Test that the threads actually complete */
402 for(i=0;i<NUM_THREADS;i++) {
403 error=WaitForSingleObject(thread[i],5000);
404 ok(error==WAIT_OBJECT_0, "Thread did not complete within timelimit\n");
405 if(error!=WAIT_OBJECT_0) {
406 TerminateThread(thread[i],i+NUM_THREADS);
408 ok(GetExitCodeThread(thread[i],&exitCode),"Could not retrieve ext code\n");
409 ok(exitCode==i+NUM_THREADS,"Thread returned an incorrect exit code\n");
411 /* Test that each thread executed in its parent's address space
412 (it was able to change threadmem and pass that change back to its parent)
413 and that each thread id was independent). Note that we prove that the
414 threads actually execute concurrently by having them block on each other
415 in threadFunc1
417 for(i=0;i<NUM_THREADS;i++) {
418 error=0;
419 for(j=i+1;j<NUM_THREADS;j++) {
420 if (threadmem[i]==threadmem[j]) {
421 error=1;
424 ok(!error && threadmem[i]==threadid[i] && threadmem[i]!=curthreadId,
425 "Thread did not execute successfully\n");
426 ok(CloseHandle(thread[i])!=0,"CloseHandle failed\n");
429 SetLastError(0xCAFEF00D);
430 bRet = TlsFree(tlsIndex);
431 ok(bRet, "TlsFree failed: %08x\n", GetLastError());
432 ok(GetLastError()==0xCAFEF00D,
433 "GetLastError: expected 0xCAFEF00D, got %08x\n", GetLastError());
435 /* Test freeing an already freed TLS index */
436 SetLastError(0xCAFEF00D);
437 ok(TlsFree(tlsIndex)==0,"TlsFree succeeded\n");
438 ok(GetLastError()==ERROR_INVALID_PARAMETER,
439 "GetLastError: expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
441 /* Test how passing NULL as a pointer to threadid works */
442 SetLastError(0xFACEaBAD);
443 thread[0] = CreateThread(NULL,0,threadFunc2,NULL,0,&tid);
444 GLE = GetLastError();
445 if (thread[0]) { /* NT */
446 ok(GLE==0xFACEaBAD, "CreateThread set last error to %d, expected 4207848365\n", GLE);
447 ret = WaitForSingleObject(thread[0],100);
448 ok(ret==WAIT_OBJECT_0, "threadFunc2 did not exit during 100 ms\n");
449 ret = GetExitCodeThread(thread[0],&exitCode);
450 ok(ret!=0, "GetExitCodeThread returned %d (expected nonzero)\n", ret);
451 ok(exitCode==99, "threadFunc2 exited with code: %d (expected 99)\n", exitCode);
452 ok(CloseHandle(thread[0])!=0,"Error closing thread handle\n");
454 else { /* 9x */
455 ok(GLE==ERROR_INVALID_PARAMETER, "CreateThread set last error to %d, expected 87\n", GLE);
459 /* Check that using the CREATE_SUSPENDED flag works */
460 static VOID test_CreateThread_suspended(void)
462 HANDLE thread;
463 DWORD threadId;
464 DWORD suspend_count;
465 int error;
467 thread = CreateThread(NULL,0,threadFunc2,NULL,
468 CREATE_SUSPENDED,&threadId);
469 ok(thread!=NULL,"Create Thread failed\n");
470 /* Check that the thread is suspended */
471 ok(SuspendThread(thread)==1,"Thread did not start suspended\n");
472 ok(ResumeThread(thread)==2,"Resume thread returned an invalid value\n");
473 /* Check that resume thread didn't actually start the thread. I can't think
474 of a better way of checking this than just waiting. I am not sure if this
475 will work on slow computers.
477 ok(WaitForSingleObject(thread,1000)==WAIT_TIMEOUT,
478 "ResumeThread should not have actually started the thread\n");
479 /* Now actually resume the thread and make sure that it actually completes*/
480 ok(ResumeThread(thread)==1,"Resume thread returned an invalid value\n");
481 ok((error=WaitForSingleObject(thread,1000))==WAIT_OBJECT_0,
482 "Thread did not resume\n");
483 if(error!=WAIT_OBJECT_0) {
484 TerminateThread(thread,1);
487 suspend_count = SuspendThread(thread);
488 ok(suspend_count == -1, "SuspendThread returned %d, expected -1\n", suspend_count);
490 suspend_count = ResumeThread(thread);
491 ok(suspend_count == 0 ||
492 broken(suspend_count == -1), /* win9x */
493 "ResumeThread returned %d, expected 0\n", suspend_count);
495 ok(CloseHandle(thread)!=0,"CloseHandle failed\n");
498 /* Check that SuspendThread and ResumeThread work */
499 static VOID test_SuspendThread(void)
501 HANDLE thread,access_thread;
502 DWORD threadId,exitCode,error;
503 int i;
505 thread = CreateThread(NULL,0,threadFunc3,NULL,
506 0,&threadId);
507 ok(thread!=NULL,"Create Thread failed\n");
508 /* Check that the thread is suspended */
509 /* Note that this is a polling method, and there is a race between
510 SuspendThread being called (in the child, and the loop below timing out,
511 so the test could fail on a heavily loaded or slow computer.
513 error=0;
514 for(i=0;error==0 && i<100;i++) {
515 error=SuspendThread(thread);
516 ResumeThread(thread);
517 if(error==0) {
518 Sleep(50);
519 i++;
522 ok(error==1,"SuspendThread did not work\n");
523 /* check that access restrictions are obeyed */
524 if (pOpenThread) {
525 access_thread=pOpenThread(THREAD_ALL_ACCESS & (~THREAD_SUSPEND_RESUME),
526 0,threadId);
527 ok(access_thread!=NULL,"OpenThread returned an invalid handle\n");
528 if (access_thread!=NULL) {
529 obey_ar(SuspendThread(access_thread)==~0U);
530 obey_ar(ResumeThread(access_thread)==~0U);
531 ok(CloseHandle(access_thread)!=0,"CloseHandle Failed\n");
534 /* Double check that the thread really is suspended */
535 ok((error=GetExitCodeThread(thread,&exitCode))!=0 && exitCode==STILL_ACTIVE,
536 "Thread did not really suspend\n");
537 /* Resume the thread, and make sure it actually completes */
538 ok(ResumeThread(thread)==1,"Resume thread returned an invalid value\n");
539 ok((error=WaitForSingleObject(thread,1000))==WAIT_OBJECT_0,
540 "Thread did not resume\n");
541 if(error!=WAIT_OBJECT_0) {
542 TerminateThread(thread,1);
544 /* Trying to suspend a terminated thread should fail */
545 error=SuspendThread(thread);
546 ok(error==~0U, "wrong return code: %d\n", error);
547 ok(GetLastError()==ERROR_ACCESS_DENIED || GetLastError()==ERROR_NO_MORE_ITEMS, "unexpected error code: %d\n", GetLastError());
549 ok(CloseHandle(thread)!=0,"CloseHandle Failed\n");
552 /* Check that TerminateThread works properly
554 static VOID test_TerminateThread(void)
556 HANDLE thread,access_thread,event;
557 DWORD threadId,exitCode;
558 event=CreateEventA(NULL,TRUE,FALSE,NULL);
559 thread = CreateThread(NULL,0,threadFunc4,event,0,&threadId);
560 ok(thread!=NULL,"Create Thread failed\n");
561 /* TerminateThread has a race condition in Wine. If the thread is terminated
562 before it starts, it leaves a process behind. Therefore, we wait for the
563 thread to signal that it has started. There is no easy way to force the
564 race to occur, so we don't try to find it.
566 ok(WaitForSingleObject(event,5000)==WAIT_OBJECT_0,
567 "TerminateThread didn't work\n");
568 /* check that access restrictions are obeyed */
569 if (pOpenThread) {
570 access_thread=pOpenThread(THREAD_ALL_ACCESS & (~THREAD_TERMINATE),
571 0,threadId);
572 ok(access_thread!=NULL,"OpenThread returned an invalid handle\n");
573 if (access_thread!=NULL) {
574 obey_ar(TerminateThread(access_thread,99)==0);
575 ok(CloseHandle(access_thread)!=0,"CloseHandle Failed\n");
578 /* terminate a job and make sure it terminates */
579 ok(TerminateThread(thread,99)!=0,"TerminateThread failed\n");
580 ok(WaitForSingleObject(thread,5000)==WAIT_OBJECT_0,
581 "TerminateThread didn't work\n");
582 ok(GetExitCodeThread(thread,&exitCode)!=STILL_ACTIVE,
583 "TerminateThread should not leave the thread 'STILL_ACTIVE'\n");
584 ok(exitCode==99, "TerminateThread returned invalid exit code\n");
585 ok(CloseHandle(thread)!=0,"Error Closing thread handle\n");
588 /* Check if CreateThread obeys the specified stack size. This code does
589 not work properly, and is currently disabled
591 static VOID test_CreateThread_stack(void)
593 #if CHECK_STACK
594 /* The only way I know of to test the stack size is to use alloca
595 and __try/__except. However, this is probably not portable,
596 and I couldn't get it to work under Wine anyhow. However, here
597 is the code which should allow for testing that CreateThread
598 respects the stack-size limit
600 HANDLE thread;
601 DWORD threadId,exitCode;
603 SYSTEM_INFO sysInfo;
604 sysInfo.dwPageSize=0;
605 GetSystemInfo(&sysInfo);
606 ok(sysInfo.dwPageSize>0,"GetSystemInfo should return a valid page size\n");
607 thread = CreateThread(NULL,sysInfo.dwPageSize,
608 threadFunc5,&exitCode,
609 0,&threadId);
610 ok(WaitForSingleObject(thread,5000)==WAIT_OBJECT_0,
611 "TerminateThread didn't work\n");
612 ok(exitCode==1,"CreateThread did not obey stack-size-limit\n");
613 ok(CloseHandle(thread)!=0,"CloseHandle failed\n");
614 #endif
617 /* Check whether setting/retrieving thread priorities works */
618 static VOID test_thread_priority(void)
620 HANDLE curthread,access_thread;
621 DWORD curthreadId,exitCode;
622 int min_priority=-2,max_priority=2;
623 BOOL disabled,rc;
624 int i;
626 curthread=GetCurrentThread();
627 curthreadId=GetCurrentThreadId();
628 /* Check thread priority */
629 /* NOTE: on Win2k/XP priority can be from -7 to 6. All other platforms it
630 is -2 to 2. However, even on a real Win2k system, using thread
631 priorities beyond the -2 to 2 range does not work. If you want to try
632 anyway, enable USE_EXTENDED_PRIORITIES
634 ok(GetThreadPriority(curthread)==THREAD_PRIORITY_NORMAL,
635 "GetThreadPriority Failed\n");
637 if (pOpenThread) {
638 /* check that access control is obeyed */
639 access_thread=pOpenThread(THREAD_ALL_ACCESS &
640 (~THREAD_QUERY_INFORMATION) & (~THREAD_SET_INFORMATION),
641 0,curthreadId);
642 ok(access_thread!=NULL,"OpenThread returned an invalid handle\n");
643 if (access_thread!=NULL) {
644 obey_ar(SetThreadPriority(access_thread,1)==0);
645 obey_ar(GetThreadPriority(access_thread)==THREAD_PRIORITY_ERROR_RETURN);
646 obey_ar(GetExitCodeThread(access_thread,&exitCode)==0);
647 ok(CloseHandle(access_thread),"Error Closing thread handle\n");
650 #if USE_EXTENDED_PRIORITIES
651 min_priority=-7; max_priority=6;
652 #endif
653 for(i=min_priority;i<=max_priority;i++) {
654 ok(SetThreadPriority(curthread,i)!=0,
655 "SetThreadPriority Failed for priority: %d\n",i);
656 ok(GetThreadPriority(curthread)==i,
657 "GetThreadPriority Failed for priority: %d\n",i);
659 ok(SetThreadPriority(curthread,THREAD_PRIORITY_TIME_CRITICAL)!=0,
660 "SetThreadPriority Failed\n");
661 ok(GetThreadPriority(curthread)==THREAD_PRIORITY_TIME_CRITICAL,
662 "GetThreadPriority Failed\n");
663 ok(SetThreadPriority(curthread,THREAD_PRIORITY_IDLE)!=0,
664 "SetThreadPriority Failed\n");
665 ok(GetThreadPriority(curthread)==THREAD_PRIORITY_IDLE,
666 "GetThreadPriority Failed\n");
667 ok(SetThreadPriority(curthread,0)!=0,"SetThreadPriority Failed\n");
669 /* Check that the thread priority is not changed if SetThreadPriority
670 is called with a value outside of the max/min range */
671 SetThreadPriority(curthread,min_priority);
672 SetLastError(0xdeadbeef);
673 rc = SetThreadPriority(curthread,min_priority-1);
675 ok(rc == FALSE, "SetThreadPriority passed with a bad argument\n");
676 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
677 GetLastError() == ERROR_INVALID_PRIORITY /* Win9x */,
678 "SetThreadPriority error %d, expected ERROR_INVALID_PARAMETER or ERROR_INVALID_PRIORITY\n",
679 GetLastError());
680 ok(GetThreadPriority(curthread)==min_priority,
681 "GetThreadPriority didn't return min_priority\n");
683 SetThreadPriority(curthread,max_priority);
684 SetLastError(0xdeadbeef);
685 rc = SetThreadPriority(curthread,max_priority+1);
687 ok(rc == FALSE, "SetThreadPriority passed with a bad argument\n");
688 ok(GetLastError() == ERROR_INVALID_PARAMETER ||
689 GetLastError() == ERROR_INVALID_PRIORITY /* Win9x */,
690 "SetThreadPriority error %d, expected ERROR_INVALID_PARAMETER or ERROR_INVALID_PRIORITY\n",
691 GetLastError());
692 ok(GetThreadPriority(curthread)==max_priority,
693 "GetThreadPriority didn't return max_priority\n");
695 /* Check thread priority boost */
696 if (!pGetThreadPriorityBoost || !pSetThreadPriorityBoost)
697 return; /* Win9x */
699 SetLastError(0xdeadbeef);
700 rc=pGetThreadPriorityBoost(curthread,&disabled);
701 if (rc==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
703 win_skip("GetThreadPriorityBoost is not implemented on WinME\n");
704 return;
707 ok(rc!=0,"error=%d\n",GetLastError());
709 if (pOpenThread) {
710 /* check that access control is obeyed */
711 access_thread=pOpenThread(THREAD_ALL_ACCESS &
712 (~THREAD_QUERY_INFORMATION) & (~THREAD_SET_INFORMATION),
713 0,curthreadId);
714 ok(access_thread!=NULL,"OpenThread returned an invalid handle\n");
715 if (access_thread!=NULL) {
716 obey_ar(pSetThreadPriorityBoost(access_thread,1)==0);
717 todo_wine obey_ar(pGetThreadPriorityBoost(access_thread,&disabled)==0);
718 ok(CloseHandle(access_thread),"Error Closing thread handle\n");
722 todo_wine {
723 rc = pSetThreadPriorityBoost(curthread,1);
724 ok( rc != 0, "error=%d\n",GetLastError());
725 rc=pGetThreadPriorityBoost(curthread,&disabled);
726 ok(rc!=0 && disabled==1,
727 "rc=%d error=%d disabled=%d\n",rc,GetLastError(),disabled);
729 rc = pSetThreadPriorityBoost(curthread,0);
730 ok( rc != 0, "error=%d\n",GetLastError());
732 rc=pGetThreadPriorityBoost(curthread,&disabled);
733 ok(rc!=0 && disabled==0,
734 "rc=%d error=%d disabled=%d\n",rc,GetLastError(),disabled);
737 /* check the GetThreadTimes function */
738 static VOID test_GetThreadTimes(void)
740 HANDLE thread,access_thread=NULL;
741 FILETIME creationTime,exitTime,kernelTime,userTime;
742 DWORD threadId;
743 int error;
745 thread = CreateThread(NULL,0,threadFunc2,NULL,
746 CREATE_SUSPENDED,&threadId);
748 ok(thread!=NULL,"Create Thread failed\n");
749 /* check that access control is obeyed */
750 if (pOpenThread) {
751 access_thread=pOpenThread(THREAD_ALL_ACCESS &
752 (~THREAD_QUERY_INFORMATION), 0,threadId);
753 ok(access_thread!=NULL,
754 "OpenThread returned an invalid handle\n");
756 ok(ResumeThread(thread)==1,"Resume thread returned an invalid value\n");
757 ok(WaitForSingleObject(thread,5000)==WAIT_OBJECT_0,
758 "ResumeThread didn't work\n");
759 creationTime.dwLowDateTime=99; creationTime.dwHighDateTime=99;
760 exitTime.dwLowDateTime=99; exitTime.dwHighDateTime=99;
761 kernelTime.dwLowDateTime=99; kernelTime.dwHighDateTime=99;
762 userTime.dwLowDateTime=99; userTime.dwHighDateTime=99;
763 /* GetThreadTimes should set all of the parameters passed to it */
764 error=GetThreadTimes(thread,&creationTime,&exitTime,
765 &kernelTime,&userTime);
767 if (error == 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
768 win_skip("GetThreadTimes is not implemented\n");
769 else {
770 ok(error!=0,"GetThreadTimes failed\n");
771 ok(creationTime.dwLowDateTime!=99 || creationTime.dwHighDateTime!=99,
772 "creationTime was invalid\n");
773 ok(exitTime.dwLowDateTime!=99 || exitTime.dwHighDateTime!=99,
774 "exitTime was invalid\n");
775 ok(kernelTime.dwLowDateTime!=99 || kernelTime.dwHighDateTime!=99,
776 "kernelTimewas invalid\n");
777 ok(userTime.dwLowDateTime!=99 || userTime.dwHighDateTime!=99,
778 "userTime was invalid\n");
779 ok(CloseHandle(thread)!=0,"CloseHandle failed\n");
780 if(access_thread!=NULL)
782 error=GetThreadTimes(access_thread,&creationTime,&exitTime,
783 &kernelTime,&userTime);
784 obey_ar(error==0);
787 if(access_thread!=NULL) {
788 ok(CloseHandle(access_thread)!=0,"CloseHandle Failed\n");
792 /* Check the processor affinity functions */
793 /* NOTE: These functions should also be checked that they obey access control
795 static VOID test_thread_processor(void)
797 HANDLE curthread,curproc;
798 DWORD_PTR processMask,systemMask,retMask;
799 SYSTEM_INFO sysInfo;
800 int error=0;
801 BOOL is_wow64;
803 if (!pIsWow64Process || !pIsWow64Process( GetCurrentProcess(), &is_wow64 )) is_wow64 = FALSE;
805 sysInfo.dwNumberOfProcessors=0;
806 GetSystemInfo(&sysInfo);
807 ok(sysInfo.dwNumberOfProcessors>0,
808 "GetSystemInfo failed to return a valid # of processors\n");
809 /* Use the current Thread/process for all tests */
810 curthread=GetCurrentThread();
811 ok(curthread!=NULL,"GetCurrentThread failed\n");
812 curproc=GetCurrentProcess();
813 ok(curproc!=NULL,"GetCurrentProcess failed\n");
814 /* Check the Affinity Mask functions */
815 ok(GetProcessAffinityMask(curproc,&processMask,&systemMask)!=0,
816 "GetProcessAffinityMask failed\n");
817 ok(SetThreadAffinityMask(curthread,processMask)==processMask,
818 "SetThreadAffinityMask failed\n");
819 ok(SetThreadAffinityMask(curthread,processMask+1)==0,
820 "SetThreadAffinityMask passed for an illegal processor\n");
821 /* NOTE: Pre-Vista does not recognize the "all processors" flag (all bits set) */
822 retMask = SetThreadAffinityMask(curthread,~0);
823 ok(broken(retMask==0) || retMask==processMask,
824 "SetThreadAffinityMask(thread,-1) failed to request all processors.\n");
825 if (retMask == processMask && sizeof(ULONG_PTR) > sizeof(ULONG))
827 /* only the low 32-bits matter */
828 retMask = SetThreadAffinityMask(curthread,~(ULONG_PTR)0);
829 ok(retMask == processMask, "SetThreadAffinityMask failed\n");
830 retMask = SetThreadAffinityMask(curthread,~(ULONG_PTR)0 >> 3);
831 ok(retMask == processMask, "SetThreadAffinityMask failed\n");
832 retMask = SetThreadAffinityMask(curthread,~(ULONG_PTR)1);
833 ok(retMask == 0, "SetThreadAffinityMask succeeded\n");
835 /* NOTE: This only works on WinNT/2000/XP) */
836 if (pSetThreadIdealProcessor) {
837 SetLastError(0xdeadbeef);
838 error=pSetThreadIdealProcessor(curthread,0);
839 if (GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
841 win_skip("SetThreadIdealProcessor is not implemented\n");
842 return;
844 ok(error!=-1, "SetThreadIdealProcessor failed\n");
846 if (is_wow64)
848 SetLastError(0xdeadbeef);
849 error=pSetThreadIdealProcessor(curthread,MAXIMUM_PROCESSORS+1);
850 todo_wine
851 ok(error!=-1, "SetThreadIdealProcessor failed for %u on Wow64\n", MAXIMUM_PROCESSORS+1);
853 SetLastError(0xdeadbeef);
854 error=pSetThreadIdealProcessor(curthread,65);
855 ok(error==-1, "SetThreadIdealProcessor succeeded with an illegal processor #\n");
856 ok(GetLastError()==ERROR_INVALID_PARAMETER,
857 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
859 else
861 SetLastError(0xdeadbeef);
862 error=pSetThreadIdealProcessor(curthread,MAXIMUM_PROCESSORS+1);
863 ok(error==-1, "SetThreadIdealProcessor succeeded with an illegal processor #\n");
864 ok(GetLastError()==ERROR_INVALID_PARAMETER,
865 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
868 error=pSetThreadIdealProcessor(curthread,MAXIMUM_PROCESSORS);
869 ok(error!=-1, "SetThreadIdealProcessor failed\n");
873 static VOID test_GetThreadExitCode(void)
875 DWORD exitCode, threadid;
876 DWORD GLE, ret;
877 HANDLE thread;
879 ret = GetExitCodeThread((HANDLE)0x2bad2bad,&exitCode);
880 ok(ret==0, "GetExitCodeThread returned non zero value: %d\n", ret);
881 GLE = GetLastError();
882 ok(GLE==ERROR_INVALID_HANDLE, "GetLastError returned %d (expected 6)\n", GLE);
884 thread = CreateThread(NULL,0,threadFunc2,NULL,0,&threadid);
885 ret = WaitForSingleObject(thread,100);
886 ok(ret==WAIT_OBJECT_0, "threadFunc2 did not exit during 100 ms\n");
887 ret = GetExitCodeThread(thread,&exitCode);
888 ok(ret==exitCode || ret==1,
889 "GetExitCodeThread returned %d (expected 1 or %d)\n", ret, exitCode);
890 ok(exitCode==99, "threadFunc2 exited with code %d (expected 99)\n", exitCode);
891 ok(CloseHandle(thread)!=0,"Error closing thread handle\n");
894 #ifdef __i386__
896 static int test_value = 0;
897 static HANDLE event;
899 static void WINAPI set_test_val( int val )
901 test_value += val;
902 ExitThread(0);
905 static DWORD WINAPI threadFunc6(LPVOID p)
907 SetEvent( event );
908 Sleep( 1000 );
909 test_value *= (int)p;
910 return 0;
913 static void test_SetThreadContext(void)
915 CONTEXT ctx;
916 int *stack;
917 HANDLE thread;
918 DWORD threadid;
919 DWORD prevcount;
920 BOOL ret;
922 SetLastError(0xdeadbeef);
923 event = CreateEvent( NULL, TRUE, FALSE, NULL );
924 thread = CreateThread( NULL, 0, threadFunc6, (void *)2, 0, &threadid );
925 ok( thread != NULL, "CreateThread failed : (%d)\n", GetLastError() );
926 if (!thread)
928 trace("Thread creation failed, skipping rest of test\n");
929 return;
931 WaitForSingleObject( event, INFINITE );
932 SuspendThread( thread );
933 CloseHandle( event );
935 ctx.ContextFlags = CONTEXT_FULL;
936 SetLastError(0xdeadbeef);
937 ret = GetThreadContext( thread, &ctx );
938 ok( ret, "GetThreadContext failed : (%u)\n", GetLastError() );
940 if (ret)
942 /* simulate a call to set_test_val(10) */
943 stack = (int *)ctx.Esp;
944 stack[-1] = 10;
945 stack[-2] = ctx.Eip;
946 ctx.Esp -= 2 * sizeof(int *);
947 ctx.Eip = (DWORD)set_test_val;
948 SetLastError(0xdeadbeef);
949 ret = SetThreadContext( thread, &ctx );
950 ok( ret, "SetThreadContext failed : (%d)\n", GetLastError() );
953 SetLastError(0xdeadbeef);
954 prevcount = ResumeThread( thread );
955 ok ( prevcount == 1, "Previous suspend count (%d) instead of 1, last error : (%d)\n",
956 prevcount, GetLastError() );
958 WaitForSingleObject( thread, INFINITE );
959 ok( test_value == 10, "test_value %d\n", test_value );
961 ctx.ContextFlags = CONTEXT_FULL;
962 SetLastError(0xdeadbeef);
963 ret = GetThreadContext( thread, &ctx );
964 ok( !ret, "GetThreadContext succeeded\n" );
965 ok( GetLastError() == ERROR_GEN_FAILURE || broken(GetLastError() == ERROR_INVALID_HANDLE), /* win2k */
966 "wrong error %u\n", GetLastError() );
968 SetLastError(0xdeadbeef);
969 ret = SetThreadContext( thread, &ctx );
970 ok( !ret, "SetThreadContext succeeded\n" );
971 ok( GetLastError() == ERROR_GEN_FAILURE || GetLastError() == ERROR_ACCESS_DENIED ||
972 broken(GetLastError() == ERROR_INVALID_HANDLE), /* win2k */
973 "wrong error %u\n", GetLastError() );
975 CloseHandle( thread );
978 #endif /* __i386__ */
980 static HANDLE finish_event;
981 static LONG times_executed;
983 static DWORD CALLBACK work_function(void *p)
985 LONG executed = InterlockedIncrement(&times_executed);
987 if (executed == 100)
988 SetEvent(finish_event);
989 return 0;
992 static void test_QueueUserWorkItem(void)
994 INT_PTR i;
995 DWORD wait_result;
996 DWORD before, after;
998 /* QueueUserWorkItem not present on win9x */
999 if (!pQueueUserWorkItem) return;
1001 finish_event = CreateEvent(NULL, TRUE, FALSE, NULL);
1003 before = GetTickCount();
1005 for (i = 0; i < 100; i++)
1007 BOOL ret = pQueueUserWorkItem(work_function, (void *)i, WT_EXECUTEDEFAULT);
1008 ok(ret, "QueueUserWorkItem failed with error %d\n", GetLastError());
1011 wait_result = WaitForSingleObject(finish_event, 10000);
1013 after = GetTickCount();
1014 trace("100 QueueUserWorkItem calls took %dms\n", after - before);
1015 ok(wait_result == WAIT_OBJECT_0, "wait failed with error 0x%x\n", wait_result);
1017 ok(times_executed == 100, "didn't execute all of the work items\n");
1020 static void CALLBACK signaled_function(PVOID p, BOOLEAN TimerOrWaitFired)
1022 HANDLE event = p;
1023 SetEvent(event);
1024 ok(!TimerOrWaitFired, "wait shouldn't have timed out\n");
1027 static void CALLBACK timeout_function(PVOID p, BOOLEAN TimerOrWaitFired)
1029 HANDLE event = p;
1030 SetEvent(event);
1031 ok(TimerOrWaitFired, "wait should have timed out\n");
1034 static void test_RegisterWaitForSingleObject(void)
1036 BOOL ret;
1037 HANDLE wait_handle;
1038 HANDLE handle;
1039 HANDLE complete_event;
1041 if (!pRegisterWaitForSingleObject || !pUnregisterWait)
1043 win_skip("RegisterWaitForSingleObject or UnregisterWait not implemented\n");
1044 return;
1047 /* test signaled case */
1049 handle = CreateEvent(NULL, TRUE, TRUE, NULL);
1050 complete_event = CreateEvent(NULL, FALSE, FALSE, NULL);
1052 ret = pRegisterWaitForSingleObject(&wait_handle, handle, signaled_function, complete_event, INFINITE, WT_EXECUTEONLYONCE);
1053 ok(ret, "RegisterWaitForSingleObject failed with error %d\n", GetLastError());
1055 WaitForSingleObject(complete_event, INFINITE);
1056 /* give worker thread chance to complete */
1057 Sleep(100);
1059 ret = pUnregisterWait(wait_handle);
1060 ok(ret, "UnregisterWait failed with error %d\n", GetLastError());
1062 /* test cancel case */
1064 ResetEvent(handle);
1066 ret = pRegisterWaitForSingleObject(&wait_handle, handle, signaled_function, complete_event, INFINITE, WT_EXECUTEONLYONCE);
1067 ok(ret, "RegisterWaitForSingleObject failed with error %d\n", GetLastError());
1069 ret = pUnregisterWait(wait_handle);
1070 ok(ret, "UnregisterWait failed with error %d\n", GetLastError());
1072 /* test timeout case */
1074 ret = pRegisterWaitForSingleObject(&wait_handle, handle, timeout_function, complete_event, 0, WT_EXECUTEONLYONCE);
1075 ok(ret, "RegisterWaitForSingleObject failed with error %d\n", GetLastError());
1077 WaitForSingleObject(complete_event, INFINITE);
1078 /* give worker thread chance to complete */
1079 Sleep(100);
1081 ret = pUnregisterWait(wait_handle);
1082 ok(ret, "UnregisterWait failed with error %d\n", GetLastError());
1085 static DWORD TLS_main;
1086 static DWORD TLS_index0, TLS_index1;
1088 static DWORD WINAPI TLS_InheritanceProc(LPVOID p)
1090 /* We should NOT inherit the TLS values from our parent or from the
1091 main thread. */
1092 LPVOID val;
1094 val = TlsGetValue(TLS_main);
1095 ok(val == NULL, "TLS inheritance failed\n");
1097 val = TlsGetValue(TLS_index0);
1098 ok(val == NULL, "TLS inheritance failed\n");
1100 val = TlsGetValue(TLS_index1);
1101 ok(val == NULL, "TLS inheritance failed\n");
1103 return 0;
1106 /* Basic TLS usage test. Make sure we can create slots and the values we
1107 store in them are separate among threads. Also test TLS value
1108 inheritance with TLS_InheritanceProc. */
1109 static DWORD WINAPI TLS_ThreadProc(LPVOID p)
1111 LONG_PTR id = (LONG_PTR) p;
1112 LPVOID val;
1113 BOOL ret;
1115 if (sync_threads_and_run_one(0, id))
1117 TLS_index0 = TlsAlloc();
1118 ok(TLS_index0 != TLS_OUT_OF_INDEXES, "TlsAlloc failed\n");
1120 resync_after_run();
1122 if (sync_threads_and_run_one(1, id))
1124 TLS_index1 = TlsAlloc();
1125 ok(TLS_index1 != TLS_OUT_OF_INDEXES, "TlsAlloc failed\n");
1127 /* Slot indices should be different even if created in different
1128 threads. */
1129 ok(TLS_index0 != TLS_index1, "TlsAlloc failed\n");
1131 /* Both slots should be initialized to NULL */
1132 val = TlsGetValue(TLS_index0);
1133 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1134 ok(val == NULL, "TLS slot not initialized correctly\n");
1136 val = TlsGetValue(TLS_index1);
1137 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1138 ok(val == NULL, "TLS slot not initialized correctly\n");
1140 resync_after_run();
1142 if (sync_threads_and_run_one(0, id))
1144 val = TlsGetValue(TLS_index0);
1145 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1146 ok(val == NULL, "TLS slot not initialized correctly\n");
1148 val = TlsGetValue(TLS_index1);
1149 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1150 ok(val == NULL, "TLS slot not initialized correctly\n");
1152 ret = TlsSetValue(TLS_index0, (LPVOID) 1);
1153 ok(ret, "TlsSetValue failed\n");
1155 ret = TlsSetValue(TLS_index1, (LPVOID) 2);
1156 ok(ret, "TlsSetValue failed\n");
1158 val = TlsGetValue(TLS_index0);
1159 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1160 ok(val == (LPVOID) 1, "TLS slot not initialized correctly\n");
1162 val = TlsGetValue(TLS_index1);
1163 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1164 ok(val == (LPVOID) 2, "TLS slot not initialized correctly\n");
1166 resync_after_run();
1168 if (sync_threads_and_run_one(1, id))
1170 val = TlsGetValue(TLS_index0);
1171 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1172 ok(val == NULL, "TLS slot not initialized correctly\n");
1174 val = TlsGetValue(TLS_index1);
1175 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1176 ok(val == NULL, "TLS slot not initialized correctly\n");
1178 ret = TlsSetValue(TLS_index0, (LPVOID) 3);
1179 ok(ret, "TlsSetValue failed\n");
1181 ret = TlsSetValue(TLS_index1, (LPVOID) 4);
1182 ok(ret, "TlsSetValue failed\n");
1184 val = TlsGetValue(TLS_index0);
1185 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1186 ok(val == (LPVOID) 3, "TLS slot not initialized correctly\n");
1188 val = TlsGetValue(TLS_index1);
1189 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1190 ok(val == (LPVOID) 4, "TLS slot not initialized correctly\n");
1192 resync_after_run();
1194 if (sync_threads_and_run_one(0, id))
1196 HANDLE thread;
1197 DWORD waitret, tid;
1199 val = TlsGetValue(TLS_index0);
1200 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1201 ok(val == (LPVOID) 1, "TLS slot not initialized correctly\n");
1203 val = TlsGetValue(TLS_index1);
1204 ok(GetLastError() == ERROR_SUCCESS, "TlsGetValue failed\n");
1205 ok(val == (LPVOID) 2, "TLS slot not initialized correctly\n");
1207 thread = CreateThread(NULL, 0, TLS_InheritanceProc, 0, 0, &tid);
1208 ok(thread != NULL, "CreateThread failed\n");
1209 waitret = WaitForSingleObject(thread, 60000);
1210 ok(waitret == WAIT_OBJECT_0, "WaitForSingleObject failed\n");
1211 CloseHandle(thread);
1213 ret = TlsFree(TLS_index0);
1214 ok(ret, "TlsFree failed\n");
1216 resync_after_run();
1218 if (sync_threads_and_run_one(1, id))
1220 ret = TlsFree(TLS_index1);
1221 ok(ret, "TlsFree failed\n");
1223 resync_after_run();
1225 return 0;
1228 static void test_TLS(void)
1230 HANDLE threads[2];
1231 LONG_PTR i;
1232 DWORD ret;
1233 BOOL suc;
1235 init_thread_sync_helpers();
1237 /* Allocate a TLS slot in the main thread to test for inheritance. */
1238 TLS_main = TlsAlloc();
1239 ok(TLS_main != TLS_OUT_OF_INDEXES, "TlsAlloc failed\n");
1240 suc = TlsSetValue(TLS_main, (LPVOID) 4114);
1241 ok(suc, "TlsSetValue failed\n");
1243 for (i = 0; i < 2; ++i)
1245 DWORD tid;
1247 threads[i] = CreateThread(NULL, 0, TLS_ThreadProc, (LPVOID) i, 0, &tid);
1248 ok(threads[i] != NULL, "CreateThread failed\n");
1251 ret = WaitForMultipleObjects(2, threads, TRUE, 60000);
1252 ok(ret == WAIT_OBJECT_0 || broken(ret == WAIT_OBJECT_0+1 /* nt4,w2k */), "WaitForAllObjects 2 threads %d\n",ret);
1254 for (i = 0; i < 2; ++i)
1255 CloseHandle(threads[i]);
1257 suc = TlsFree(TLS_main);
1258 ok(suc, "TlsFree failed\n");
1259 cleanup_thread_sync_helpers();
1262 static void test_ThreadErrorMode(void)
1264 DWORD oldmode;
1265 DWORD mode;
1266 DWORD rtlmode;
1267 BOOL ret;
1269 if (!pSetThreadErrorMode || !pGetThreadErrorMode)
1271 win_skip("SetThreadErrorMode and/or GetThreadErrorMode unavailable (added in Windows 7)\n");
1272 return;
1275 if (!pRtlGetThreadErrorMode) {
1276 win_skip("RtlGetThreadErrorMode not available\n");
1277 return;
1280 oldmode = pGetThreadErrorMode();
1282 ret = pSetThreadErrorMode(0, &mode);
1283 ok(ret, "SetThreadErrorMode failed\n");
1284 ok(mode == oldmode,
1285 "SetThreadErrorMode returned old mode 0x%x, expected 0x%x\n",
1286 mode, oldmode);
1287 mode = pGetThreadErrorMode();
1288 ok(mode == 0, "GetThreadErrorMode returned mode 0x%x, expected 0\n", mode);
1289 rtlmode = pRtlGetThreadErrorMode();
1290 ok(rtlmode == 0,
1291 "RtlGetThreadErrorMode returned mode 0x%x, expected 0\n", mode);
1293 ret = pSetThreadErrorMode(SEM_FAILCRITICALERRORS, &mode);
1294 ok(ret, "SetThreadErrorMode failed\n");
1295 ok(mode == 0,
1296 "SetThreadErrorMode returned old mode 0x%x, expected 0\n", mode);
1297 mode = pGetThreadErrorMode();
1298 ok(mode == SEM_FAILCRITICALERRORS,
1299 "GetThreadErrorMode returned mode 0x%x, expected SEM_FAILCRITICALERRORS\n",
1300 mode);
1301 rtlmode = pRtlGetThreadErrorMode();
1302 ok(rtlmode == 0x10,
1303 "RtlGetThreadErrorMode returned mode 0x%x, expected 0x10\n", mode);
1305 ret = pSetThreadErrorMode(SEM_NOGPFAULTERRORBOX, &mode);
1306 ok(ret, "SetThreadErrorMode failed\n");
1307 ok(mode == SEM_FAILCRITICALERRORS,
1308 "SetThreadErrorMode returned old mode 0x%x, expected SEM_FAILCRITICALERRORS\n",
1309 mode);
1310 mode = pGetThreadErrorMode();
1311 ok(mode == SEM_NOGPFAULTERRORBOX,
1312 "GetThreadErrorMode returned mode 0x%x, expected SEM_NOGPFAULTERRORBOX\n",
1313 mode);
1314 rtlmode = pRtlGetThreadErrorMode();
1315 ok(rtlmode == 0x20,
1316 "RtlGetThreadErrorMode returned mode 0x%x, expected 0x20\n", mode);
1318 ret = pSetThreadErrorMode(SEM_NOOPENFILEERRORBOX, NULL);
1319 ok(ret, "SetThreadErrorMode failed\n");
1320 mode = pGetThreadErrorMode();
1321 ok(mode == SEM_NOOPENFILEERRORBOX,
1322 "GetThreadErrorMode returned mode 0x%x, expected SEM_NOOPENFILEERRORBOX\n",
1323 mode);
1324 rtlmode = pRtlGetThreadErrorMode();
1325 ok(rtlmode == 0x40,
1326 "RtlGetThreadErrorMode returned mode 0x%x, expected 0x40\n", rtlmode);
1328 for (mode = 1; mode; mode <<= 1)
1330 ret = pSetThreadErrorMode(mode, NULL);
1331 if (mode & (SEM_FAILCRITICALERRORS |
1332 SEM_NOGPFAULTERRORBOX |
1333 SEM_NOOPENFILEERRORBOX))
1335 ok(ret,
1336 "SetThreadErrorMode(0x%x,NULL) failed with error %d\n",
1337 mode, GetLastError());
1339 else
1341 DWORD GLE = GetLastError();
1342 ok(!ret,
1343 "SetThreadErrorMode(0x%x,NULL) succeeded, expected failure\n",
1344 mode);
1345 ok(GLE == ERROR_INVALID_PARAMETER,
1346 "SetThreadErrorMode(0x%x,NULL) failed with %d, "
1347 "expected ERROR_INVALID_PARAMETER\n",
1348 mode, GLE);
1352 pSetThreadErrorMode(oldmode, NULL);
1355 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1356 static inline void set_fpu_cw(WORD cw)
1358 __asm__ volatile ("fnclex; fldcw %0" : : "m" (cw));
1361 static inline WORD get_fpu_cw(void)
1363 WORD cw = 0;
1364 __asm__ volatile ("fnstcw %0" : "=m" (cw));
1365 return cw;
1368 struct fpu_thread_ctx
1370 WORD cw;
1371 HANDLE finished;
1374 static DWORD WINAPI fpu_thread(void *param)
1376 struct fpu_thread_ctx *ctx = param;
1377 BOOL ret;
1379 ctx->cw = get_fpu_cw();
1381 ret = SetEvent(ctx->finished);
1382 ok(ret, "SetEvent failed, last error %#x.\n", GetLastError());
1384 return 0;
1387 static WORD get_thread_fpu_cw(void)
1389 struct fpu_thread_ctx ctx;
1390 DWORD tid, res;
1391 HANDLE thread;
1393 ctx.finished = CreateEvent(NULL, FALSE, FALSE, NULL);
1394 ok(!!ctx.finished, "Failed to create event, last error %#x.\n", GetLastError());
1396 thread = CreateThread(NULL, 0, fpu_thread, &ctx, 0, &tid);
1397 ok(!!thread, "Failed to create thread, last error %#x.\n", GetLastError());
1399 res = WaitForSingleObject(ctx.finished, INFINITE);
1400 ok(res == WAIT_OBJECT_0, "Wait failed (%#x), last error %#x.\n", res, GetLastError());
1402 res = CloseHandle(ctx.finished);
1403 ok(!!res, "Failed to close event handle, last error %#x.\n", GetLastError());
1405 return ctx.cw;
1408 static void test_thread_fpu_cw(void)
1410 WORD initial_cw, cw;
1412 initial_cw = get_fpu_cw();
1413 ok(initial_cw == 0x27f, "Expected FPU control word 0x27f, got %#x.\n", initial_cw);
1415 cw = get_thread_fpu_cw();
1416 ok(cw == 0x27f, "Expected FPU control word 0x27f, got %#x.\n", cw);
1418 set_fpu_cw(0xf60);
1419 cw = get_fpu_cw();
1420 ok(cw == 0xf60, "Expected FPU control word 0xf60, got %#x.\n", cw);
1422 cw = get_thread_fpu_cw();
1423 ok(cw == 0x27f, "Expected FPU control word 0x27f, got %#x.\n", cw);
1425 cw = get_fpu_cw();
1426 ok(cw == 0xf60, "Expected FPU control word 0xf60, got %#x.\n", cw);
1428 set_fpu_cw(initial_cw);
1429 cw = get_fpu_cw();
1430 ok(cw == initial_cw, "Expected FPU control word %#x, got %#x.\n", initial_cw, cw);
1432 #endif
1434 START_TEST(thread)
1436 HINSTANCE lib;
1437 HINSTANCE ntdll;
1438 int argc;
1439 char **argv;
1440 argc = winetest_get_mainargs( &argv );
1441 /* Neither Cygwin nor mingW export OpenThread, so do a dynamic check
1442 so that the compile passes
1444 lib=GetModuleHandleA("kernel32.dll");
1445 ok(lib!=NULL,"Couldn't get a handle for kernel32.dll\n");
1446 pGetThreadPriorityBoost=(void *)GetProcAddress(lib,"GetThreadPriorityBoost");
1447 pOpenThread=(void *)GetProcAddress(lib,"OpenThread");
1448 pQueueUserWorkItem=(void *)GetProcAddress(lib,"QueueUserWorkItem");
1449 pSetThreadIdealProcessor=(void *)GetProcAddress(lib,"SetThreadIdealProcessor");
1450 pSetThreadPriorityBoost=(void *)GetProcAddress(lib,"SetThreadPriorityBoost");
1451 pRegisterWaitForSingleObject=(void *)GetProcAddress(lib,"RegisterWaitForSingleObject");
1452 pUnregisterWait=(void *)GetProcAddress(lib,"UnregisterWait");
1453 pIsWow64Process=(void *)GetProcAddress(lib,"IsWow64Process");
1454 pSetThreadErrorMode=(void *)GetProcAddress(lib,"SetThreadErrorMode");
1455 pGetThreadErrorMode=(void *)GetProcAddress(lib,"GetThreadErrorMode");
1457 ntdll=GetModuleHandleA("ntdll.dll");
1458 if (ntdll)
1460 pRtlGetThreadErrorMode=(void *)GetProcAddress(ntdll,"RtlGetThreadErrorMode");
1463 if (argc >= 3)
1465 if (!strcmp(argv[2], "sleep"))
1467 HANDLE hAddrEvents[2];
1468 create_function_addr_events(hAddrEvents);
1469 SetEvent(hAddrEvents[0]);
1470 SetEvent(hAddrEvents[1]);
1471 Sleep(5000); /* spawned process runs for at most 5 seconds */
1472 return;
1474 while (1)
1476 HANDLE hThread;
1477 DWORD tid;
1478 hThread = CreateThread(NULL, 0, threadFunc2, NULL, 0, &tid);
1479 ok(hThread != NULL, "CreateThread failed, error %u\n",
1480 GetLastError());
1481 ok(WaitForSingleObject(hThread, 200) == WAIT_OBJECT_0,
1482 "Thread did not exit in time\n");
1483 if (hThread == NULL) break;
1484 CloseHandle(hThread);
1486 return;
1489 test_CreateRemoteThread();
1490 test_CreateThread_basic();
1491 test_CreateThread_suspended();
1492 test_SuspendThread();
1493 test_TerminateThread();
1494 test_CreateThread_stack();
1495 test_thread_priority();
1496 test_GetThreadTimes();
1497 test_thread_processor();
1498 test_GetThreadExitCode();
1499 #ifdef __i386__
1500 test_SetThreadContext();
1501 #endif
1502 test_QueueUserWorkItem();
1503 test_RegisterWaitForSingleObject();
1504 test_TLS();
1505 test_ThreadErrorMode();
1506 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
1507 test_thread_fpu_cw();
1508 #endif