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 0x0600
28 /* the tests intentionally pass invalid pointers and need an exception handler */
29 #define WINE_NO_INLINE_STRING
32 #define WIN32_NO_STATUS
38 #include <wine/winternl.h>
39 #include <wine/test.h>
41 /* THREAD_ALL_ACCESS in Vista+ PSDKs is incompatible with older Windows versions */
42 #define THREAD_ALL_ACCESS_NT4 (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x3ff)
44 /* Specify the number of simultaneous threads to test */
46 /* Specify whether to test the extended priorities for Win2k/XP */
47 #define USE_EXTENDED_PRIORITIES 0
48 /* Specify whether to test the stack allocation in CreateThread */
51 /* Set CHECK_STACK to 1 if you want to try to test the stack-limit from
52 CreateThread. So far I have been unable to make this work, and
53 I am in doubt as to how portable it is. Also, according to MSDN,
54 you shouldn't mix C-run-time-libraries (i.e. alloca) with CreateThread.
55 Anyhow, the check is currently commented out
60 # define __EXCEPT __except
63 # include "wine/exception.h"
69 #elif defined __x86_64__
73 #elif defined __aarch64__
79 static BOOL (WINAPI
*pGetThreadPriorityBoost
)(HANDLE
,PBOOL
);
80 static HANDLE (WINAPI
*pOpenThread
)(DWORD
,BOOL
,DWORD
);
81 static BOOL (WINAPI
*pQueueUserWorkItem
)(LPTHREAD_START_ROUTINE
,PVOID
,ULONG
);
82 static DWORD (WINAPI
*pSetThreadIdealProcessor
)(HANDLE
,DWORD
);
83 static BOOL (WINAPI
*pSetThreadPriorityBoost
)(HANDLE
,BOOL
);
84 static BOOL (WINAPI
*pRegisterWaitForSingleObject
)(PHANDLE
,HANDLE
,WAITORTIMERCALLBACK
,PVOID
,ULONG
,ULONG
);
85 static BOOL (WINAPI
*pUnregisterWait
)(HANDLE
);
86 static BOOL (WINAPI
*pIsWow64Process
)(HANDLE
,PBOOL
);
87 static BOOL (WINAPI
*pSetThreadErrorMode
)(DWORD
,PDWORD
);
88 static DWORD (WINAPI
*pGetThreadErrorMode
)(void);
89 static DWORD (WINAPI
*pRtlGetThreadErrorMode
)(void);
90 static BOOL (WINAPI
*pActivateActCtx
)(HANDLE
,ULONG_PTR
*);
91 static HANDLE (WINAPI
*pCreateActCtxW
)(PCACTCTXW
);
92 static BOOL (WINAPI
*pDeactivateActCtx
)(DWORD
,ULONG_PTR
);
93 static BOOL (WINAPI
*pGetCurrentActCtx
)(HANDLE
*);
94 static void (WINAPI
*pReleaseActCtx
)(HANDLE
);
95 static PTP_POOL (WINAPI
*pCreateThreadpool
)(PVOID
);
96 static void (WINAPI
*pCloseThreadpool
)(PTP_POOL
);
97 static PTP_WORK (WINAPI
*pCreateThreadpoolWork
)(PTP_WORK_CALLBACK
,PVOID
,PTP_CALLBACK_ENVIRON
);
98 static void (WINAPI
*pSubmitThreadpoolWork
)(PTP_WORK
);
99 static void (WINAPI
*pWaitForThreadpoolWorkCallbacks
)(PTP_WORK
,BOOL
);
100 static void (WINAPI
*pCloseThreadpoolWork
)(PTP_WORK
);
101 static NTSTATUS (WINAPI
*pNtQueryInformationThread
)(HANDLE
,THREADINFOCLASS
,PVOID
,ULONG
,PULONG
);
102 static BOOL (WINAPI
*pGetThreadGroupAffinity
)(HANDLE
,GROUP_AFFINITY
*);
103 static BOOL (WINAPI
*pSetThreadGroupAffinity
)(HANDLE
,const GROUP_AFFINITY
*,GROUP_AFFINITY
*);
104 static NTSTATUS (WINAPI
*pNtSetInformationThread
)(HANDLE
,THREADINFOCLASS
,LPCVOID
,ULONG
);
105 static NTSTATUS (WINAPI
*pNtSetLdtEntries
)(ULONG
,ULONG
,ULONG
,ULONG
,ULONG
,ULONG
);
107 static HANDLE
create_target_process(const char *arg
)
110 char cmdline
[MAX_PATH
];
111 PROCESS_INFORMATION pi
;
113 STARTUPINFOA si
= { 0 };
116 winetest_get_mainargs( &argv
);
117 sprintf(cmdline
, "%s %s %s", argv
[0], argv
[1], arg
);
118 ret
= CreateProcessA(NULL
, cmdline
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
);
119 ok(ret
, "error: %u\n", GetLastError());
120 ret
= CloseHandle(pi
.hThread
);
121 ok(ret
, "error %u\n", GetLastError());
125 /* Functions not tested yet:
130 In addition there are no checks that the inheritance works properly in
134 /* Functions to ensure that from a group of threads, only one executes
135 certain chunks of code at a time, and we know which one is executing
136 it. It basically makes multithreaded execution linear, which defeats
137 the purpose of multiple threads, but makes testing easy. */
138 static HANDLE start_event
, stop_event
;
139 static LONG num_synced
;
141 static void init_thread_sync_helpers(void)
143 start_event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
144 ok(start_event
!= NULL
, "CreateEvent failed\n");
145 stop_event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
146 ok(stop_event
!= NULL
, "CreateEvent failed\n");
150 static BOOL
sync_threads_and_run_one(DWORD sync_id
, DWORD my_id
)
152 LONG num
= InterlockedIncrement(&num_synced
);
153 assert(-1 <= num
&& num
<= 1);
156 ResetEvent( stop_event
);
157 SetEvent( start_event
);
161 DWORD ret
= WaitForSingleObject(start_event
, 10000);
162 ok(ret
== WAIT_OBJECT_0
, "WaitForSingleObject failed %x\n",ret
);
164 return sync_id
== my_id
;
167 static void resync_after_run(void)
169 LONG num
= InterlockedDecrement(&num_synced
);
170 assert(-1 <= num
&& num
<= 1);
173 ResetEvent( start_event
);
174 SetEvent( stop_event
);
178 DWORD ret
= WaitForSingleObject(stop_event
, 10000);
179 ok(ret
== WAIT_OBJECT_0
, "WaitForSingleObject failed\n");
183 static void cleanup_thread_sync_helpers(void)
185 CloseHandle(start_event
);
186 CloseHandle(stop_event
);
189 static DWORD tlsIndex
;
197 /* WinME supports OpenThread but doesn't know about access restrictions so
198 we require them to be either completely ignored or always obeyed.
200 static INT obeying_ars
= 0; /* -1 == no, 0 == dunno yet, 1 == yes */
204 ? (obeying_ars = +1) \
205 : ((obeying_ars = -1), \
206 trace("not restricted, assuming consistent behaviour\n"))) \
207 : (obeying_ars < 0) \
208 ? ok(!(x), "access restrictions obeyed\n") \
209 : ok( (x), "access restrictions not obeyed\n"))
211 /* Basic test that simultaneous threads can access shared memory,
212 that the thread local storage routines work correctly, and that
213 threads actually run concurrently
215 static DWORD WINAPI
threadFunc1(LPVOID p
)
217 t1Struct
*tstruct
= p
;
219 /* write our thread # into shared memory */
220 tstruct
->threadmem
[tstruct
->threadnum
]=GetCurrentThreadId();
221 ok(TlsSetValue(tlsIndex
,(LPVOID
)(INT_PTR
)(tstruct
->threadnum
+1))!=0,
222 "TlsSetValue failed\n");
223 /* The threads synchronize before terminating. This is done by
224 Signaling an event, and waiting for all events to occur
226 SetEvent(tstruct
->event
[tstruct
->threadnum
]);
227 WaitForMultipleObjects(NUM_THREADS
,tstruct
->event
,TRUE
,INFINITE
);
228 /* Double check that all threads really did run by validating that
229 they have all written to the shared memory. There should be no race
230 here, since all threads were synchronized after the write.*/
231 for (i
= 0; i
< NUM_THREADS
; i
++)
232 ok(tstruct
->threadmem
[i
] != 0, "expected threadmem[%d] != 0\n", i
);
234 /* lstrlenA contains an exception handler so this makes sure exceptions work in threads */
235 ok( lstrlenA( (char *)0xdeadbeef ) == 0, "lstrlenA: unexpected success\n" );
237 /* Check that no one changed our tls memory */
238 ok((INT_PTR
)TlsGetValue(tlsIndex
)-1==tstruct
->threadnum
,
239 "TlsGetValue failed\n");
240 return NUM_THREADS
+tstruct
->threadnum
;
243 static DWORD WINAPI
threadFunc2(LPVOID p
)
248 static DWORD WINAPI
threadFunc3(LPVOID p
)
251 thread
=GetCurrentThread();
252 SuspendThread(thread
);
256 static DWORD WINAPI
threadFunc4(LPVOID p
)
267 static DWORD WINAPI
threadFunc5(LPVOID p
)
271 sysInfo
.dwPageSize
=0;
272 GetSystemInfo(&sysInfo
);
276 alloca(2*sysInfo
.dwPageSize
);
286 static DWORD WINAPI
threadFunc_SetEvent(LPVOID p
)
292 static DWORD WINAPI
threadFunc_CloseHandle(LPVOID p
)
298 struct thread_actctx_param
300 HANDLE thread_context
;
304 static DWORD WINAPI
thread_actctx_func(void *p
)
306 struct thread_actctx_param
*param
= (struct thread_actctx_param
*)p
;
310 cur
= (void*)0xdeadbeef;
311 ret
= pGetCurrentActCtx(&cur
);
312 ok(ret
, "thread GetCurrentActCtx failed, %u\n", GetLastError());
313 ok(cur
== param
->handle
, "got %p, expected %p\n", cur
, param
->handle
);
314 param
->thread_context
= cur
;
319 static void create_function_addr_events(HANDLE events
[2])
323 sprintf(buffer
, "threadFunc_SetEvent %p", threadFunc_SetEvent
);
324 events
[0] = CreateEventA(NULL
, FALSE
, FALSE
, buffer
);
326 sprintf(buffer
, "threadFunc_CloseHandle %p", threadFunc_CloseHandle
);
327 events
[1] = CreateEventA(NULL
, FALSE
, FALSE
, buffer
);
330 /* check CreateRemoteThread */
331 static VOID
test_CreateRemoteThread(void)
333 HANDLE hProcess
, hThread
, hEvent
, hRemoteEvent
;
334 DWORD tid
, ret
, exitcode
;
335 HANDLE hAddrEvents
[2];
337 hProcess
= create_target_process("sleep");
338 ok(hProcess
!= NULL
, "Can't start process\n");
340 /* ensure threadFunc_SetEvent & threadFunc_CloseHandle are the same
341 * address as in the child process */
342 create_function_addr_events(hAddrEvents
);
343 ret
= WaitForMultipleObjects(2, hAddrEvents
, TRUE
, 5000);
344 if (ret
== WAIT_TIMEOUT
)
346 skip("child process wasn't mapped at same address, so can't do CreateRemoteThread tests.\n");
349 ok(ret
== WAIT_OBJECT_0
|| broken(ret
== WAIT_OBJECT_0
+1 /* nt4,w2k */), "WaitForAllObjects 2 events %d\n", ret
);
351 hEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
352 ok(hEvent
!= NULL
, "Can't create event, err=%u\n", GetLastError());
353 ret
= DuplicateHandle(GetCurrentProcess(), hEvent
, hProcess
, &hRemoteEvent
,
354 0, FALSE
, DUPLICATE_SAME_ACCESS
);
355 ok(ret
!= 0, "DuplicateHandle failed, err=%u\n", GetLastError());
357 /* create suspended remote thread with entry point SetEvent() */
358 SetLastError(0xdeadbeef);
359 hThread
= CreateRemoteThread(hProcess
, NULL
, 0, threadFunc_SetEvent
,
360 hRemoteEvent
, CREATE_SUSPENDED
, &tid
);
361 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
363 win_skip("CreateRemoteThread is not implemented\n");
366 ok(hThread
!= NULL
, "CreateRemoteThread failed, err=%u\n", GetLastError());
367 ok(tid
!= 0, "null tid\n");
368 ret
= SuspendThread(hThread
);
369 ok(ret
== 1, "ret=%u, err=%u\n", ret
, GetLastError());
370 ret
= ResumeThread(hThread
);
371 ok(ret
== 2, "ret=%u, err=%u\n", ret
, GetLastError());
373 /* thread still suspended, so wait times out */
374 ret
= WaitForSingleObject(hEvent
, 1000);
375 ok(ret
== WAIT_TIMEOUT
, "wait did not time out, ret=%u\n", ret
);
377 ret
= ResumeThread(hThread
);
378 ok(ret
== 1, "ret=%u, err=%u\n", ret
, GetLastError());
380 /* wait that doesn't time out */
381 ret
= WaitForSingleObject(hEvent
, 1000);
382 ok(ret
== WAIT_OBJECT_0
, "object not signaled, ret=%u\n", ret
);
384 /* wait for thread end */
385 ret
= WaitForSingleObject(hThread
, 1000);
386 ok(ret
== WAIT_OBJECT_0
, "waiting for thread failed, ret=%u\n", ret
);
387 CloseHandle(hThread
);
389 /* create and wait for remote thread with entry point CloseHandle() */
390 hThread
= CreateRemoteThread(hProcess
, NULL
, 0,
391 threadFunc_CloseHandle
,
392 hRemoteEvent
, 0, &tid
);
393 ok(hThread
!= NULL
, "CreateRemoteThread failed, err=%u\n", GetLastError());
394 ret
= WaitForSingleObject(hThread
, 1000);
395 ok(ret
== WAIT_OBJECT_0
, "waiting for thread failed, ret=%u\n", ret
);
396 CloseHandle(hThread
);
398 /* create remote thread with entry point SetEvent() */
399 hThread
= CreateRemoteThread(hProcess
, NULL
, 0,
401 hRemoteEvent
, 0, &tid
);
402 ok(hThread
!= NULL
, "CreateRemoteThread failed, err=%u\n", GetLastError());
404 /* closed handle, so wait times out */
405 ret
= WaitForSingleObject(hEvent
, 1000);
406 ok(ret
== WAIT_TIMEOUT
, "wait did not time out, ret=%u\n", ret
);
408 /* check that remote SetEvent() failed */
409 ret
= GetExitCodeThread(hThread
, &exitcode
);
410 ok(ret
!= 0, "GetExitCodeThread failed, err=%u\n", GetLastError());
411 if (ret
) ok(exitcode
== 0, "SetEvent succeeded, expected to fail\n");
412 CloseHandle(hThread
);
415 TerminateProcess(hProcess
, 0);
417 CloseHandle(hProcess
);
420 /* Check basic functionality of CreateThread and Tls* functions */
421 static VOID
test_CreateThread_basic(void)
423 HANDLE thread
[NUM_THREADS
],event
[NUM_THREADS
];
424 DWORD threadid
[NUM_THREADS
],curthreadId
;
425 DWORD threadmem
[NUM_THREADS
];
427 t1Struct tstruct
[NUM_THREADS
];
434 /* lstrlenA contains an exception handler so this makes sure exceptions work in the main thread */
435 ok( lstrlenA( (char *)0xdeadbeef ) == 0, "lstrlenA: unexpected success\n" );
437 /* Retrieve current Thread ID for later comparisons */
438 curthreadId
=GetCurrentThreadId();
439 /* Allocate some local storage */
440 ok((tlsIndex
=TlsAlloc())!=TLS_OUT_OF_INDEXES
,"TlsAlloc failed\n");
441 /* Create events for thread synchronization */
442 for(i
=0;i
<NUM_THREADS
;i
++) {
444 /* Note that it doesn't matter what type of event we choose here. This
445 test isn't trying to thoroughly test events
447 event
[i
]=CreateEventA(NULL
,TRUE
,FALSE
,NULL
);
448 tstruct
[i
].threadnum
=i
;
449 tstruct
[i
].threadmem
=threadmem
;
450 tstruct
[i
].event
=event
;
453 /* Test that passing arguments to threads works okay */
454 for(i
=0;i
<NUM_THREADS
;i
++) {
455 thread
[i
] = CreateThread(NULL
,0,threadFunc1
,
456 &tstruct
[i
],0,&threadid
[i
]);
457 ok(thread
[i
]!=NULL
,"Create Thread failed\n");
459 /* Test that the threads actually complete */
460 for(i
=0;i
<NUM_THREADS
;i
++) {
461 error
=WaitForSingleObject(thread
[i
],5000);
462 ok(error
==WAIT_OBJECT_0
, "Thread did not complete within timelimit\n");
463 if(error
!=WAIT_OBJECT_0
) {
464 TerminateThread(thread
[i
],i
+NUM_THREADS
);
466 ok(GetExitCodeThread(thread
[i
],&exitCode
),"Could not retrieve ext code\n");
467 ok(exitCode
==i
+NUM_THREADS
,"Thread returned an incorrect exit code\n");
469 /* Test that each thread executed in its parent's address space
470 (it was able to change threadmem and pass that change back to its parent)
471 and that each thread id was independent). Note that we prove that the
472 threads actually execute concurrently by having them block on each other
475 for(i
=0;i
<NUM_THREADS
;i
++) {
477 for(j
=i
+1;j
<NUM_THREADS
;j
++) {
478 if (threadmem
[i
]==threadmem
[j
]) {
482 ok(!error
&& threadmem
[i
]==threadid
[i
] && threadmem
[i
]!=curthreadId
,
483 "Thread did not execute successfully\n");
484 ok(CloseHandle(thread
[i
])!=0,"CloseHandle failed\n");
487 SetLastError(0xCAFEF00D);
488 bRet
= TlsFree(tlsIndex
);
489 ok(bRet
, "TlsFree failed: %08x\n", GetLastError());
490 ok(GetLastError()==0xCAFEF00D,
491 "GetLastError: expected 0xCAFEF00D, got %08x\n", GetLastError());
493 /* Test freeing an already freed TLS index */
494 SetLastError(0xCAFEF00D);
495 ok(TlsFree(tlsIndex
)==0,"TlsFree succeeded\n");
496 ok(GetLastError()==ERROR_INVALID_PARAMETER
,
497 "GetLastError: expected ERROR_INVALID_PARAMETER, got %08x\n", GetLastError());
499 /* Test how passing NULL as a pointer to threadid works */
500 SetLastError(0xFACEaBAD);
501 thread
[0] = CreateThread(NULL
,0,threadFunc2
,NULL
,0,&tid
);
502 GLE
= GetLastError();
503 if (thread
[0]) { /* NT */
504 ok(GLE
==0xFACEaBAD, "CreateThread set last error to %d, expected 4207848365\n", GLE
);
505 ret
= WaitForSingleObject(thread
[0],100);
506 ok(ret
==WAIT_OBJECT_0
, "threadFunc2 did not exit during 100 ms\n");
507 ret
= GetExitCodeThread(thread
[0],&exitCode
);
508 ok(ret
!=0, "GetExitCodeThread returned %d (expected nonzero)\n", ret
);
509 ok(exitCode
==99, "threadFunc2 exited with code: %d (expected 99)\n", exitCode
);
510 ok(CloseHandle(thread
[0])!=0,"Error closing thread handle\n");
513 ok(GLE
==ERROR_INVALID_PARAMETER
, "CreateThread set last error to %d, expected 87\n", GLE
);
517 /* Check that using the CREATE_SUSPENDED flag works */
518 static VOID
test_CreateThread_suspended(void)
525 thread
= CreateThread(NULL
,0,threadFunc2
,NULL
,
526 CREATE_SUSPENDED
,&threadId
);
527 ok(thread
!=NULL
,"Create Thread failed\n");
528 /* Check that the thread is suspended */
529 ok(SuspendThread(thread
)==1,"Thread did not start suspended\n");
530 ok(ResumeThread(thread
)==2,"Resume thread returned an invalid value\n");
531 /* Check that resume thread didn't actually start the thread. I can't think
532 of a better way of checking this than just waiting. I am not sure if this
533 will work on slow computers.
535 ok(WaitForSingleObject(thread
,1000)==WAIT_TIMEOUT
,
536 "ResumeThread should not have actually started the thread\n");
537 /* Now actually resume the thread and make sure that 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);
545 suspend_count
= SuspendThread(thread
);
546 ok(suspend_count
== -1, "SuspendThread returned %d, expected -1\n", suspend_count
);
548 suspend_count
= ResumeThread(thread
);
549 ok(suspend_count
== 0 ||
550 broken(suspend_count
== -1), /* win9x */
551 "ResumeThread returned %d, expected 0\n", suspend_count
);
553 ok(CloseHandle(thread
)!=0,"CloseHandle failed\n");
556 /* Check that SuspendThread and ResumeThread work */
557 static VOID
test_SuspendThread(void)
559 HANDLE thread
,access_thread
;
560 DWORD threadId
,exitCode
,error
;
563 thread
= CreateThread(NULL
,0,threadFunc3
,NULL
,
565 ok(thread
!=NULL
,"Create Thread failed\n");
566 /* Check that the thread is suspended */
567 /* Note that this is a polling method, and there is a race between
568 SuspendThread being called (in the child, and the loop below timing out,
569 so the test could fail on a heavily loaded or slow computer.
572 for(i
=0;error
==0 && i
<100;i
++) {
573 error
=SuspendThread(thread
);
574 ResumeThread(thread
);
580 ok(error
==1,"SuspendThread did not work\n");
581 /* check that access restrictions are obeyed */
583 access_thread
=pOpenThread(THREAD_ALL_ACCESS_NT4
& (~THREAD_SUSPEND_RESUME
),
585 ok(access_thread
!=NULL
,"OpenThread returned an invalid handle\n");
586 if (access_thread
!=NULL
) {
587 obey_ar(SuspendThread(access_thread
)==~0U);
588 obey_ar(ResumeThread(access_thread
)==~0U);
589 ok(CloseHandle(access_thread
)!=0,"CloseHandle Failed\n");
592 /* Double check that the thread really is suspended */
593 ok((error
=GetExitCodeThread(thread
,&exitCode
))!=0 && exitCode
==STILL_ACTIVE
,
594 "Thread did not really suspend\n");
595 /* Resume the thread, and make sure it actually completes */
596 ok(ResumeThread(thread
)==1,"Resume thread returned an invalid value\n");
597 ok((error
=WaitForSingleObject(thread
,1000))==WAIT_OBJECT_0
,
598 "Thread did not resume\n");
599 if(error
!=WAIT_OBJECT_0
) {
600 TerminateThread(thread
,1);
602 /* Trying to suspend a terminated thread should fail */
603 error
=SuspendThread(thread
);
604 ok(error
==~0U, "wrong return code: %d\n", error
);
605 ok(GetLastError()==ERROR_ACCESS_DENIED
|| GetLastError()==ERROR_NO_MORE_ITEMS
, "unexpected error code: %d\n", GetLastError());
607 ok(CloseHandle(thread
)!=0,"CloseHandle Failed\n");
610 /* Check that TerminateThread works properly
612 static VOID
test_TerminateThread(void)
614 HANDLE thread
,access_thread
,event
;
615 DWORD threadId
,exitCode
;
616 event
=CreateEventA(NULL
,TRUE
,FALSE
,NULL
);
617 thread
= CreateThread(NULL
,0,threadFunc4
,event
,0,&threadId
);
618 ok(thread
!=NULL
,"Create Thread failed\n");
619 /* TerminateThread has a race condition in Wine. If the thread is terminated
620 before it starts, it leaves a process behind. Therefore, we wait for the
621 thread to signal that it has started. There is no easy way to force the
622 race to occur, so we don't try to find it.
624 ok(WaitForSingleObject(event
,5000)==WAIT_OBJECT_0
,
625 "TerminateThread didn't work\n");
626 /* check that access restrictions are obeyed */
628 access_thread
=pOpenThread(THREAD_ALL_ACCESS_NT4
& (~THREAD_TERMINATE
),
630 ok(access_thread
!=NULL
,"OpenThread returned an invalid handle\n");
631 if (access_thread
!=NULL
) {
632 obey_ar(TerminateThread(access_thread
,99)==0);
633 ok(CloseHandle(access_thread
)!=0,"CloseHandle Failed\n");
636 /* terminate a job and make sure it terminates */
637 ok(TerminateThread(thread
,99)!=0,"TerminateThread failed\n");
638 ok(WaitForSingleObject(thread
,5000)==WAIT_OBJECT_0
,
639 "TerminateThread didn't work\n");
640 ok(GetExitCodeThread(thread
,&exitCode
)!=STILL_ACTIVE
,
641 "TerminateThread should not leave the thread 'STILL_ACTIVE'\n");
642 ok(exitCode
==99, "TerminateThread returned invalid exit code\n");
643 ok(CloseHandle(thread
)!=0,"Error Closing thread handle\n");
646 /* Check if CreateThread obeys the specified stack size. This code does
647 not work properly, and is currently disabled
649 static VOID
test_CreateThread_stack(void)
652 /* The only way I know of to test the stack size is to use alloca
653 and __try/__except. However, this is probably not portable,
654 and I couldn't get it to work under Wine anyhow. However, here
655 is the code which should allow for testing that CreateThread
656 respects the stack-size limit
659 DWORD threadId
,exitCode
;
662 sysInfo
.dwPageSize
=0;
663 GetSystemInfo(&sysInfo
);
664 ok(sysInfo
.dwPageSize
>0,"GetSystemInfo should return a valid page size\n");
665 thread
= CreateThread(NULL
,sysInfo
.dwPageSize
,
666 threadFunc5
,&exitCode
,
668 ok(WaitForSingleObject(thread
,5000)==WAIT_OBJECT_0
,
669 "TerminateThread didn't work\n");
670 ok(exitCode
==1,"CreateThread did not obey stack-size-limit\n");
671 ok(CloseHandle(thread
)!=0,"CloseHandle failed\n");
675 /* Check whether setting/retrieving thread priorities works */
676 static VOID
test_thread_priority(void)
678 HANDLE curthread
,access_thread
;
679 DWORD curthreadId
,exitCode
;
680 int min_priority
=-2,max_priority
=2;
684 curthread
=GetCurrentThread();
685 curthreadId
=GetCurrentThreadId();
686 /* Check thread priority */
687 /* NOTE: on Win2k/XP priority can be from -7 to 6. All other platforms it
688 is -2 to 2. However, even on a real Win2k system, using thread
689 priorities beyond the -2 to 2 range does not work. If you want to try
690 anyway, enable USE_EXTENDED_PRIORITIES
692 ok(GetThreadPriority(curthread
)==THREAD_PRIORITY_NORMAL
,
693 "GetThreadPriority Failed\n");
696 /* check that access control is obeyed */
697 access_thread
=pOpenThread(THREAD_ALL_ACCESS_NT4
&
698 (~THREAD_QUERY_INFORMATION
) & (~THREAD_SET_INFORMATION
),
700 ok(access_thread
!=NULL
,"OpenThread returned an invalid handle\n");
701 if (access_thread
!=NULL
) {
702 obey_ar(SetThreadPriority(access_thread
,1)==0);
703 obey_ar(GetThreadPriority(access_thread
)==THREAD_PRIORITY_ERROR_RETURN
);
704 obey_ar(GetExitCodeThread(access_thread
,&exitCode
)==0);
705 ok(CloseHandle(access_thread
),"Error Closing thread handle\n");
708 #if USE_EXTENDED_PRIORITIES
709 min_priority
=-7; max_priority
=6;
711 for(i
=min_priority
;i
<=max_priority
;i
++) {
712 ok(SetThreadPriority(curthread
,i
)!=0,
713 "SetThreadPriority Failed for priority: %d\n",i
);
714 ok(GetThreadPriority(curthread
)==i
,
715 "GetThreadPriority Failed for priority: %d\n",i
);
717 ok(SetThreadPriority(curthread
,THREAD_PRIORITY_TIME_CRITICAL
)!=0,
718 "SetThreadPriority Failed\n");
719 ok(GetThreadPriority(curthread
)==THREAD_PRIORITY_TIME_CRITICAL
,
720 "GetThreadPriority Failed\n");
721 ok(SetThreadPriority(curthread
,THREAD_PRIORITY_IDLE
)!=0,
722 "SetThreadPriority Failed\n");
723 ok(GetThreadPriority(curthread
)==THREAD_PRIORITY_IDLE
,
724 "GetThreadPriority Failed\n");
725 ok(SetThreadPriority(curthread
,0)!=0,"SetThreadPriority Failed\n");
727 /* Check that the thread priority is not changed if SetThreadPriority
728 is called with a value outside of the max/min range */
729 SetThreadPriority(curthread
,min_priority
);
730 SetLastError(0xdeadbeef);
731 rc
= SetThreadPriority(curthread
,min_priority
-1);
733 ok(rc
== FALSE
, "SetThreadPriority passed with a bad argument\n");
734 ok(GetLastError() == ERROR_INVALID_PARAMETER
||
735 GetLastError() == ERROR_INVALID_PRIORITY
/* Win9x */,
736 "SetThreadPriority error %d, expected ERROR_INVALID_PARAMETER or ERROR_INVALID_PRIORITY\n",
738 ok(GetThreadPriority(curthread
)==min_priority
,
739 "GetThreadPriority didn't return min_priority\n");
741 SetThreadPriority(curthread
,max_priority
);
742 SetLastError(0xdeadbeef);
743 rc
= SetThreadPriority(curthread
,max_priority
+1);
745 ok(rc
== FALSE
, "SetThreadPriority passed with a bad argument\n");
746 ok(GetLastError() == ERROR_INVALID_PARAMETER
||
747 GetLastError() == ERROR_INVALID_PRIORITY
/* Win9x */,
748 "SetThreadPriority error %d, expected ERROR_INVALID_PARAMETER or ERROR_INVALID_PRIORITY\n",
750 ok(GetThreadPriority(curthread
)==max_priority
,
751 "GetThreadPriority didn't return max_priority\n");
753 /* Check thread priority boost */
754 if (!pGetThreadPriorityBoost
|| !pSetThreadPriorityBoost
)
757 SetLastError(0xdeadbeef);
758 rc
=pGetThreadPriorityBoost(curthread
,&disabled
);
759 if (rc
==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED
)
761 win_skip("GetThreadPriorityBoost is not implemented on WinME\n");
765 ok(rc
!=0,"error=%d\n",GetLastError());
768 /* check that access control is obeyed */
769 access_thread
=pOpenThread(THREAD_ALL_ACCESS_NT4
&
770 (~THREAD_QUERY_INFORMATION
) & (~THREAD_SET_INFORMATION
),
772 ok(access_thread
!=NULL
,"OpenThread returned an invalid handle\n");
773 if (access_thread
!=NULL
) {
774 todo_wine
obey_ar(pSetThreadPriorityBoost(access_thread
,1)==0);
775 todo_wine
obey_ar(pGetThreadPriorityBoost(access_thread
,&disabled
)==0);
776 ok(CloseHandle(access_thread
),"Error Closing thread handle\n");
780 rc
= pSetThreadPriorityBoost(curthread
,1);
781 ok( rc
!= 0, "error=%d\n",GetLastError());
783 rc
=pGetThreadPriorityBoost(curthread
,&disabled
);
784 ok(rc
!=0 && disabled
==1,
785 "rc=%d error=%d disabled=%d\n",rc
,GetLastError(),disabled
);
788 rc
= pSetThreadPriorityBoost(curthread
,0);
789 ok( rc
!= 0, "error=%d\n",GetLastError());
790 rc
=pGetThreadPriorityBoost(curthread
,&disabled
);
791 ok(rc
!=0 && disabled
==0,
792 "rc=%d error=%d disabled=%d\n",rc
,GetLastError(),disabled
);
795 /* check the GetThreadTimes function */
796 static VOID
test_GetThreadTimes(void)
798 HANDLE thread
,access_thread
=NULL
;
799 FILETIME creationTime
,exitTime
,kernelTime
,userTime
;
803 thread
= CreateThread(NULL
,0,threadFunc2
,NULL
,
804 CREATE_SUSPENDED
,&threadId
);
806 ok(thread
!=NULL
,"Create Thread failed\n");
807 /* check that access control is obeyed */
809 access_thread
=pOpenThread(THREAD_ALL_ACCESS_NT4
&
810 (~THREAD_QUERY_INFORMATION
), 0,threadId
);
811 ok(access_thread
!=NULL
,
812 "OpenThread returned an invalid handle\n");
814 ok(ResumeThread(thread
)==1,"Resume thread returned an invalid value\n");
815 ok(WaitForSingleObject(thread
,5000)==WAIT_OBJECT_0
,
816 "ResumeThread didn't work\n");
817 creationTime
.dwLowDateTime
=99; creationTime
.dwHighDateTime
=99;
818 exitTime
.dwLowDateTime
=99; exitTime
.dwHighDateTime
=99;
819 kernelTime
.dwLowDateTime
=99; kernelTime
.dwHighDateTime
=99;
820 userTime
.dwLowDateTime
=99; userTime
.dwHighDateTime
=99;
821 /* GetThreadTimes should set all of the parameters passed to it */
822 error
=GetThreadTimes(thread
,&creationTime
,&exitTime
,
823 &kernelTime
,&userTime
);
825 if (error
== 0 && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
826 win_skip("GetThreadTimes is not implemented\n");
828 ok(error
!=0,"GetThreadTimes failed\n");
829 ok(creationTime
.dwLowDateTime
!=99 || creationTime
.dwHighDateTime
!=99,
830 "creationTime was invalid\n");
831 ok(exitTime
.dwLowDateTime
!=99 || exitTime
.dwHighDateTime
!=99,
832 "exitTime was invalid\n");
833 ok(kernelTime
.dwLowDateTime
!=99 || kernelTime
.dwHighDateTime
!=99,
834 "kernelTimewas invalid\n");
835 ok(userTime
.dwLowDateTime
!=99 || userTime
.dwHighDateTime
!=99,
836 "userTime was invalid\n");
837 ok(CloseHandle(thread
)!=0,"CloseHandle failed\n");
838 if(access_thread
!=NULL
)
840 error
=GetThreadTimes(access_thread
,&creationTime
,&exitTime
,
841 &kernelTime
,&userTime
);
845 if(access_thread
!=NULL
) {
846 ok(CloseHandle(access_thread
)!=0,"CloseHandle Failed\n");
850 /* Check the processor affinity functions */
851 /* NOTE: These functions should also be checked that they obey access control
853 static VOID
test_thread_processor(void)
855 HANDLE curthread
,curproc
;
856 DWORD_PTR processMask
,systemMask
,retMask
;
861 if (!pIsWow64Process
|| !pIsWow64Process( GetCurrentProcess(), &is_wow64
)) is_wow64
= FALSE
;
863 sysInfo
.dwNumberOfProcessors
=0;
864 GetSystemInfo(&sysInfo
);
865 ok(sysInfo
.dwNumberOfProcessors
>0,
866 "GetSystemInfo failed to return a valid # of processors\n");
867 /* Use the current Thread/process for all tests */
868 curthread
=GetCurrentThread();
869 ok(curthread
!=NULL
,"GetCurrentThread failed\n");
870 curproc
=GetCurrentProcess();
871 ok(curproc
!=NULL
,"GetCurrentProcess failed\n");
872 /* Check the Affinity Mask functions */
873 ok(GetProcessAffinityMask(curproc
,&processMask
,&systemMask
)!=0,
874 "GetProcessAffinityMask failed\n");
875 ok(SetThreadAffinityMask(curthread
,processMask
)==processMask
,
876 "SetThreadAffinityMask failed\n");
877 ok(SetThreadAffinityMask(curthread
,processMask
+1)==0,
878 "SetThreadAffinityMask passed for an illegal processor\n");
879 /* NOTE: Pre-Vista does not recognize the "all processors" flag (all bits set) */
880 retMask
= SetThreadAffinityMask(curthread
,~0);
881 ok(broken(retMask
==0) || retMask
==processMask
,
882 "SetThreadAffinityMask(thread,-1) failed to request all processors.\n");
884 if (retMask
== processMask
)
886 /* Show that the "all processors" flag is handled in ntdll */
887 DWORD_PTR mask
= ~0u;
888 NTSTATUS status
= pNtSetInformationThread(curthread
, ThreadAffinityMask
, &mask
, sizeof(mask
));
889 ok(status
== STATUS_SUCCESS
, "Expected STATUS_SUCCESS in NtSetInformationThread, got %x\n", status
);
892 if (retMask
== processMask
&& sizeof(ULONG_PTR
) > sizeof(ULONG
))
894 /* only the low 32-bits matter */
895 retMask
= SetThreadAffinityMask(curthread
,~(ULONG_PTR
)0);
896 ok(retMask
== processMask
, "SetThreadAffinityMask failed\n");
897 retMask
= SetThreadAffinityMask(curthread
,~(ULONG_PTR
)0 >> 3);
898 ok(retMask
== processMask
, "SetThreadAffinityMask failed\n");
900 /* NOTE: This only works on WinNT/2000/XP) */
901 if (pSetThreadIdealProcessor
)
903 SetLastError(0xdeadbeef);
904 error
=pSetThreadIdealProcessor(curthread
,0);
905 if (GetLastError() != ERROR_CALL_NOT_IMPLEMENTED
)
907 ok(error
!=-1, "SetThreadIdealProcessor failed\n");
911 SetLastError(0xdeadbeef);
912 error
=pSetThreadIdealProcessor(curthread
,MAXIMUM_PROCESSORS
+1);
914 ok(error
!=-1, "SetThreadIdealProcessor failed for %u on Wow64\n", MAXIMUM_PROCESSORS
+1);
916 SetLastError(0xdeadbeef);
917 error
=pSetThreadIdealProcessor(curthread
,65);
918 ok(error
==-1, "SetThreadIdealProcessor succeeded with an illegal processor #\n");
919 ok(GetLastError()==ERROR_INVALID_PARAMETER
,
920 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
924 SetLastError(0xdeadbeef);
925 error
=pSetThreadIdealProcessor(curthread
,MAXIMUM_PROCESSORS
+1);
926 ok(error
==-1, "SetThreadIdealProcessor succeeded with an illegal processor #\n");
927 ok(GetLastError()==ERROR_INVALID_PARAMETER
,
928 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
931 error
=pSetThreadIdealProcessor(curthread
,MAXIMUM_PROCESSORS
);
932 ok(error
!=-1, "SetThreadIdealProcessor failed\n");
935 win_skip("SetThreadIdealProcessor is not implemented\n");
938 if (pGetThreadGroupAffinity
&& pSetThreadGroupAffinity
)
940 GROUP_AFFINITY affinity
, affinity_new
;
943 memset(&affinity
, 0, sizeof(affinity
));
944 ok(pGetThreadGroupAffinity(curthread
, &affinity
), "GetThreadGroupAffinity failed\n");
946 SetLastError(0xdeadbeef);
947 ok(!pGetThreadGroupAffinity(curthread
, NULL
), "GetThreadGroupAffinity succeeded\n");
948 ok(GetLastError() == ERROR_INVALID_PARAMETER
|| broken(GetLastError() == ERROR_NOACCESS
), /* Win 7 and 8 */
949 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
950 ok(affinity
.Group
== 0, "Expected group 0 got %u\n", affinity
.Group
);
952 memset(&affinity_new
, 0, sizeof(affinity_new
));
953 affinity_new
.Group
= 0;
954 affinity_new
.Mask
= affinity
.Mask
;
955 ok(pSetThreadGroupAffinity(curthread
, &affinity_new
, &affinity
), "SetThreadGroupAffinity failed\n");
956 ok(affinity_new
.Mask
== affinity
.Mask
, "Expected old affinity mask %lx, got %lx\n",
957 affinity_new
.Mask
, affinity
.Mask
);
959 /* show that the "all processors" flag is not supported for SetThreadGroupAffinity */
960 affinity_new
.Group
= 0;
961 affinity_new
.Mask
= ~0u;
962 SetLastError(0xdeadbeef);
963 ok(!pSetThreadGroupAffinity(curthread
, &affinity_new
, NULL
), "SetThreadGroupAffinity succeeded\n");
964 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
965 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
967 affinity_new
.Group
= 1; /* assumes that you have less than 64 logical processors */
968 affinity_new
.Mask
= 0x1;
969 SetLastError(0xdeadbeef);
970 ok(!pSetThreadGroupAffinity(curthread
, &affinity_new
, NULL
), "SetThreadGroupAffinity succeeded\n");
971 ok(GetLastError() == ERROR_INVALID_PARAMETER
,
972 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
974 SetLastError(0xdeadbeef);
975 ok(!pSetThreadGroupAffinity(curthread
, NULL
, NULL
), "SetThreadGroupAffinity succeeded\n");
976 ok(GetLastError() == ERROR_NOACCESS
,
977 "Expected ERROR_NOACCESS, got %d\n", GetLastError());
979 /* show that the access violation was detected in ntdll */
980 status
= pNtSetInformationThread(curthread
, ThreadGroupInformation
, NULL
, sizeof(affinity_new
));
981 ok(status
== STATUS_ACCESS_VIOLATION
,
982 "Expected STATUS_ACCESS_VIOLATION, got %08x\n", status
);
984 /* restore original mask */
985 affinity_new
.Group
= 0;
986 affinity_new
.Mask
= affinity
.Mask
;
987 SetLastError(0xdeadbeef);
988 ok(pSetThreadGroupAffinity(curthread
, &affinity_new
, &affinity
), "SetThreadGroupAffinity failed\n");
989 ok(affinity_new
.Mask
== affinity
.Mask
, "Expected old affinity mask %lx, got %lx\n",
990 affinity_new
.Mask
, affinity
.Mask
);
993 win_skip("Get/SetThreadGroupAffinity not available\n");
996 static VOID
test_GetThreadExitCode(void)
998 DWORD exitCode
, threadid
;
1002 ret
= GetExitCodeThread((HANDLE
)0x2bad2bad,&exitCode
);
1003 ok(ret
==0, "GetExitCodeThread returned non zero value: %d\n", ret
);
1004 GLE
= GetLastError();
1005 ok(GLE
==ERROR_INVALID_HANDLE
, "GetLastError returned %d (expected 6)\n", GLE
);
1007 thread
= CreateThread(NULL
,0,threadFunc2
,NULL
,0,&threadid
);
1008 ret
= WaitForSingleObject(thread
,100);
1009 ok(ret
==WAIT_OBJECT_0
, "threadFunc2 did not exit during 100 ms\n");
1010 ret
= GetExitCodeThread(thread
,&exitCode
);
1011 ok(ret
==exitCode
|| ret
==1,
1012 "GetExitCodeThread returned %d (expected 1 or %d)\n", ret
, exitCode
);
1013 ok(exitCode
==99, "threadFunc2 exited with code %d (expected 99)\n", exitCode
);
1014 ok(CloseHandle(thread
)!=0,"Error closing thread handle\n");
1019 static int test_value
= 0;
1020 static HANDLE event
;
1022 static void WINAPI
set_test_val( int val
)
1028 static DWORD WINAPI
threadFunc6(LPVOID p
)
1032 test_value
*= (int)p
;
1036 static void test_SetThreadContext(void)
1045 SetLastError(0xdeadbeef);
1046 event
= CreateEventW( NULL
, TRUE
, FALSE
, NULL
);
1047 thread
= CreateThread( NULL
, 0, threadFunc6
, (void *)2, 0, &threadid
);
1048 ok( thread
!= NULL
, "CreateThread failed : (%d)\n", GetLastError() );
1051 trace("Thread creation failed, skipping rest of test\n");
1054 WaitForSingleObject( event
, INFINITE
);
1055 SuspendThread( thread
);
1056 CloseHandle( event
);
1058 ctx
.ContextFlags
= CONTEXT_FULL
;
1059 SetLastError(0xdeadbeef);
1060 ret
= GetThreadContext( thread
, &ctx
);
1061 ok( ret
, "GetThreadContext failed : (%u)\n", GetLastError() );
1065 /* simulate a call to set_test_val(10) */
1066 stack
= (int *)ctx
.Esp
;
1068 stack
[-2] = ctx
.Eip
;
1069 ctx
.Esp
-= 2 * sizeof(int *);
1070 ctx
.Eip
= (DWORD
)set_test_val
;
1071 SetLastError(0xdeadbeef);
1072 ret
= SetThreadContext( thread
, &ctx
);
1073 ok( ret
, "SetThreadContext failed : (%d)\n", GetLastError() );
1076 SetLastError(0xdeadbeef);
1077 prevcount
= ResumeThread( thread
);
1078 ok ( prevcount
== 1, "Previous suspend count (%d) instead of 1, last error : (%d)\n",
1079 prevcount
, GetLastError() );
1081 WaitForSingleObject( thread
, INFINITE
);
1082 ok( test_value
== 10, "test_value %d\n", test_value
);
1084 ctx
.ContextFlags
= CONTEXT_FULL
;
1085 SetLastError(0xdeadbeef);
1086 ret
= GetThreadContext( thread
, &ctx
);
1087 ok( (!ret
&& (GetLastError() == ERROR_GEN_FAILURE
)) ||
1088 (!ret
&& broken(GetLastError() == ERROR_INVALID_HANDLE
)) || /* win2k */
1089 broken(ret
), /* 32bit application on NT 5.x 64bit */
1090 "got %d with %u (expected FALSE with ERROR_GEN_FAILURE)\n",
1091 ret
, GetLastError() );
1093 SetLastError(0xdeadbeef);
1094 ret
= SetThreadContext( thread
, &ctx
);
1095 ok( (!ret
&& ((GetLastError() == ERROR_GEN_FAILURE
) || (GetLastError() == ERROR_ACCESS_DENIED
))) ||
1096 (!ret
&& broken(GetLastError() == ERROR_INVALID_HANDLE
)) || /* win2k */
1097 broken(ret
), /* 32bit application on NT 5.x 64bit */
1098 "got %d with %u (expected FALSE with ERROR_GEN_FAILURE or ERROR_ACCESS_DENIED)\n",
1099 ret
, GetLastError() );
1101 CloseHandle( thread
);
1104 static void test_GetThreadSelectorEntry(void)
1112 memset(&ctx
, 0x11, sizeof(ctx
));
1113 ctx
.ContextFlags
= CONTEXT_SEGMENTS
| CONTEXT_CONTROL
;
1114 ret
= GetThreadContext(GetCurrentThread(), &ctx
);
1115 ok(ret
, "GetThreadContext error %u\n", GetLastError());
1116 ok(!HIWORD(ctx
.SegCs
), "expected HIWORD(SegCs) == 0, got %u\n", ctx
.SegCs
);
1117 ok(!HIWORD(ctx
.SegDs
), "expected HIWORD(SegDs) == 0, got %u\n", ctx
.SegDs
);
1118 ok(!HIWORD(ctx
.SegFs
), "expected HIWORD(SegFs) == 0, got %u\n", ctx
.SegFs
);
1120 ret
= GetThreadSelectorEntry(GetCurrentThread(), ctx
.SegCs
, &entry
);
1121 ok(ret
, "GetThreadSelectorEntry(SegCs) error %u\n", GetLastError());
1122 ret
= GetThreadSelectorEntry(GetCurrentThread(), ctx
.SegDs
, &entry
);
1123 ok(ret
, "GetThreadSelectorEntry(SegDs) error %u\n", GetLastError());
1125 memset(&entry
, 0x11, sizeof(entry
));
1126 ret
= GetThreadSelectorEntry(GetCurrentThread(), ctx
.SegFs
, &entry
);
1127 ok(ret
, "GetThreadSelectorEntry(SegFs) error %u\n", GetLastError());
1128 entry
.HighWord
.Bits
.Type
&= ~1; /* ignore accessed bit */
1130 base
= (void *)((entry
.HighWord
.Bits
.BaseHi
<< 24) | (entry
.HighWord
.Bits
.BaseMid
<< 16) | entry
.BaseLow
);
1131 limit
= (entry
.HighWord
.Bits
.LimitHi
<< 16) | entry
.LimitLow
;
1133 ok(base
== NtCurrentTeb(), "expected %p, got %p\n", NtCurrentTeb(), base
);
1134 ok(limit
== 0x0fff || limit
== 0x4000, "expected 0x0fff or 0x4000, got %#x\n", limit
);
1135 ok(entry
.HighWord
.Bits
.Type
== 0x12, "expected 0x12, got %#x\n", entry
.HighWord
.Bits
.Type
);
1136 ok(entry
.HighWord
.Bits
.Dpl
== 3, "expected 3, got %u\n", entry
.HighWord
.Bits
.Dpl
);
1137 ok(entry
.HighWord
.Bits
.Pres
== 1, "expected 1, got %u\n", entry
.HighWord
.Bits
.Pres
);
1138 ok(entry
.HighWord
.Bits
.Sys
== 0, "expected 0, got %u\n", entry
.HighWord
.Bits
.Sys
);
1139 ok(entry
.HighWord
.Bits
.Reserved_0
== 0, "expected 0, got %u\n", entry
.HighWord
.Bits
.Reserved_0
);
1140 ok(entry
.HighWord
.Bits
.Default_Big
== 1, "expected 1, got %u\n", entry
.HighWord
.Bits
.Default_Big
);
1141 ok(entry
.HighWord
.Bits
.Granularity
== 0, "expected 0, got %u\n", entry
.HighWord
.Bits
.Granularity
);
1144 static void test_NtSetLdtEntries(void)
1146 THREAD_DESCRIPTOR_INFORMATION tdi
;
1156 if (!pNtSetLdtEntries
)
1158 win_skip("NtSetLdtEntries is not available on this platform\n");
1162 if (pNtSetLdtEntries(0, 0, 0, 0, 0, 0) == STATUS_NOT_IMPLEMENTED
) /* WoW64 */
1164 win_skip("NtSetLdtEntries is not implemented on this platform\n");
1168 ret
= pNtSetLdtEntries(0, 0, 0, 0, 0, 0);
1169 ok(!ret
, "NtSetLdtEntries failed: %08x\n", ret
);
1171 ctx
.ContextFlags
= CONTEXT_SEGMENTS
;
1172 ret
= GetThreadContext(GetCurrentThread(), &ctx
);
1173 ok(ret
, "GetThreadContext failed\n");
1175 tdi
.Selector
= ctx
.SegDs
;
1176 ret
= pNtQueryInformationThread(GetCurrentThread(), ThreadDescriptorTableEntry
, &tdi
, sizeof(tdi
), &ret
);
1177 ok(!ret
, "NtQueryInformationThread failed: %08x\n", ret
);
1178 ds_entry
= tdi
.Entry
;
1180 tdi
.Selector
= 0x000f;
1181 ret
= pNtQueryInformationThread(GetCurrentThread(), ThreadDescriptorTableEntry
, &tdi
, sizeof(tdi
), &ret
);
1182 ok(ret
== STATUS_ACCESS_VIOLATION
, "got %08x\n", ret
);
1184 tdi
.Selector
= 0x001f;
1185 ret
= pNtQueryInformationThread(GetCurrentThread(), ThreadDescriptorTableEntry
, &tdi
, sizeof(tdi
), &ret
);
1186 ok(ret
== STATUS_ACCESS_VIOLATION
, "NtQueryInformationThread returned %08x\n", ret
);
1188 ret
= GetThreadSelectorEntry(GetCurrentThread(), 0x000f, &sel
.entry
);
1189 ok(!ret
, "GetThreadSelectorEntry should fail\n");
1191 ret
= GetThreadSelectorEntry(GetCurrentThread(), 0x001f, &sel
.entry
);
1192 ok(!ret
, "GetThreadSelectorEntry should fail\n");
1194 memset(&sel
.entry
, 0x9a, sizeof(sel
.entry
));
1195 ret
= GetThreadSelectorEntry(GetCurrentThread(), ctx
.SegDs
, &sel
.entry
);
1196 ok(ret
, "GetThreadSelectorEntry failed\n");
1197 ok(!memcmp(&ds_entry
, &sel
.entry
, sizeof(ds_entry
)), "entries do not match\n");
1199 ret
= pNtSetLdtEntries(0x000f, sel
.dw
[0], sel
.dw
[1], 0x001f, sel
.dw
[0], sel
.dw
[1]);
1200 ok(!ret
|| broken(ret
== STATUS_INVALID_LDT_DESCRIPTOR
) /*XP*/, "NtSetLdtEntries failed: %08x\n", ret
);
1204 memset(&sel
.entry
, 0x9a, sizeof(sel
.entry
));
1205 ret
= GetThreadSelectorEntry(GetCurrentThread(), 0x000f, &sel
.entry
);
1206 ok(ret
, "GetThreadSelectorEntry failed\n");
1207 ok(!memcmp(&ds_entry
, &sel
.entry
, sizeof(ds_entry
)), "entries do not match\n");
1209 memset(&sel
.entry
, 0x9a, sizeof(sel
.entry
));
1210 ret
= GetThreadSelectorEntry(GetCurrentThread(), 0x001f, &sel
.entry
);
1211 ok(ret
, "GetThreadSelectorEntry failed\n");
1212 ok(!memcmp(&ds_entry
, &sel
.entry
, sizeof(ds_entry
)), "entries do not match\n");
1216 #endif /* __i386__ */
1218 static HANDLE finish_event
;
1219 static LONG times_executed
;
1221 static DWORD CALLBACK
work_function(void *p
)
1223 LONG executed
= InterlockedIncrement(×_executed
);
1225 if (executed
== 100)
1226 SetEvent(finish_event
);
1230 static void test_QueueUserWorkItem(void)
1234 DWORD before
, after
;
1236 /* QueueUserWorkItem not present on win9x */
1237 if (!pQueueUserWorkItem
) return;
1239 finish_event
= CreateEventW(NULL
, TRUE
, FALSE
, NULL
);
1241 before
= GetTickCount();
1243 for (i
= 0; i
< 100; i
++)
1245 BOOL ret
= pQueueUserWorkItem(work_function
, (void *)i
, WT_EXECUTEDEFAULT
);
1246 ok(ret
, "QueueUserWorkItem failed with error %d\n", GetLastError());
1249 wait_result
= WaitForSingleObject(finish_event
, 10000);
1251 after
= GetTickCount();
1252 trace("100 QueueUserWorkItem calls took %dms\n", after
- before
);
1253 ok(wait_result
== WAIT_OBJECT_0
, "wait failed with error 0x%x\n", wait_result
);
1255 ok(times_executed
== 100, "didn't execute all of the work items\n");
1258 static void CALLBACK
signaled_function(PVOID p
, BOOLEAN TimerOrWaitFired
)
1262 ok(!TimerOrWaitFired
, "wait shouldn't have timed out\n");
1265 static void CALLBACK
timeout_function(PVOID p
, BOOLEAN TimerOrWaitFired
)
1269 ok(TimerOrWaitFired
, "wait should have timed out\n");
1272 static void test_RegisterWaitForSingleObject(void)
1277 HANDLE complete_event
;
1279 if (!pRegisterWaitForSingleObject
|| !pUnregisterWait
)
1281 win_skip("RegisterWaitForSingleObject or UnregisterWait not implemented\n");
1285 /* test signaled case */
1287 handle
= CreateEventW(NULL
, TRUE
, TRUE
, NULL
);
1288 complete_event
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1290 ret
= pRegisterWaitForSingleObject(&wait_handle
, handle
, signaled_function
, complete_event
, INFINITE
, WT_EXECUTEONLYONCE
);
1291 ok(ret
, "RegisterWaitForSingleObject failed with error %d\n", GetLastError());
1293 WaitForSingleObject(complete_event
, INFINITE
);
1294 /* give worker thread chance to complete */
1297 ret
= pUnregisterWait(wait_handle
);
1298 ok(ret
, "UnregisterWait failed with error %d\n", GetLastError());
1300 /* test cancel case */
1304 ret
= pRegisterWaitForSingleObject(&wait_handle
, handle
, signaled_function
, complete_event
, INFINITE
, WT_EXECUTEONLYONCE
);
1305 ok(ret
, "RegisterWaitForSingleObject failed with error %d\n", GetLastError());
1307 ret
= pUnregisterWait(wait_handle
);
1308 ok(ret
, "UnregisterWait failed with error %d\n", GetLastError());
1310 /* test timeout case */
1312 ret
= pRegisterWaitForSingleObject(&wait_handle
, handle
, timeout_function
, complete_event
, 0, WT_EXECUTEONLYONCE
);
1313 ok(ret
, "RegisterWaitForSingleObject failed with error %d\n", GetLastError());
1315 WaitForSingleObject(complete_event
, INFINITE
);
1316 /* give worker thread chance to complete */
1319 ret
= pUnregisterWait(wait_handle
);
1320 ok(ret
, "UnregisterWait failed with error %d\n", GetLastError());
1322 SetLastError(0xdeadbeef);
1323 ret
= pUnregisterWait(NULL
);
1324 ok(!ret
, "Expected UnregisterWait to fail\n");
1325 ok(GetLastError() == ERROR_INVALID_HANDLE
,
1326 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
1329 static DWORD LS_main
;
1330 static DWORD LS_index0
, LS_index1
;
1331 static DWORD LS_OutOfIndexesValue
;
1333 /* Function pointers to the FLS/TLS functions to test in LS_ThreadProc() */
1334 static DWORD (WINAPI
*LS_AllocFunc
)(void);
1335 static PVOID (WINAPI
*LS_GetValueFunc
)(DWORD
);
1336 static BOOL (WINAPI
*LS_SetValueFunc
)(DWORD
, PVOID
);
1337 static BOOL (WINAPI
*LS_FreeFunc
)(DWORD
);
1339 /* Names of the functions tested in LS_ThreadProc(), for error messages */
1340 static const char* LS_AllocFuncName
= "";
1341 static const char* LS_GetValueFuncName
= "";
1342 static const char* LS_SetValueFuncName
= "";
1343 static const char* LS_FreeFuncName
= "";
1345 /* FLS entry points, dynamically loaded in platforms that support them */
1346 static DWORD (WINAPI
*pFlsAlloc
)(PFLS_CALLBACK_FUNCTION
);
1347 static BOOL (WINAPI
*pFlsFree
)(DWORD
);
1348 static PVOID (WINAPI
*pFlsGetValue
)(DWORD
);
1349 static BOOL (WINAPI
*pFlsSetValue
)(DWORD
,PVOID
);
1351 /* A thunk function to make FlsAlloc compatible with the signature of TlsAlloc */
1352 static DWORD WINAPI
FLS_AllocFuncThunk(void)
1354 return pFlsAlloc(NULL
);
1357 static DWORD WINAPI
LS_InheritanceProc(LPVOID p
)
1359 /* We should NOT inherit the FLS/TLS values from our parent or from the
1363 val
= LS_GetValueFunc(LS_main
);
1364 ok(val
== NULL
, "%s inheritance failed\n", LS_GetValueFuncName
);
1366 val
= LS_GetValueFunc(LS_index0
);
1367 ok(val
== NULL
, "%s inheritance failed\n", LS_GetValueFuncName
);
1369 val
= LS_GetValueFunc(LS_index1
);
1370 ok(val
== NULL
, "%s inheritance failed\n", LS_GetValueFuncName
);
1375 /* Basic FLS/TLS usage test. Make sure we can create slots and the values we
1376 store in them are separate among threads. Also test FLS/TLS value
1377 inheritance with LS_InheritanceProc. */
1378 static DWORD WINAPI
LS_ThreadProc(LPVOID p
)
1380 LONG_PTR id
= (LONG_PTR
) p
;
1384 if (sync_threads_and_run_one(0, id
))
1386 LS_index0
= LS_AllocFunc();
1387 ok(LS_index0
!= LS_OutOfIndexesValue
, "%s failed\n", LS_AllocFuncName
);
1391 if (sync_threads_and_run_one(1, id
))
1393 LS_index1
= LS_AllocFunc();
1394 ok(LS_index1
!= LS_OutOfIndexesValue
, "%s failed\n", LS_AllocFuncName
);
1396 /* Slot indices should be different even if created in different
1398 ok(LS_index0
!= LS_index1
, "%s failed\n", LS_AllocFuncName
);
1400 /* Both slots should be initialized to NULL */
1401 val
= LS_GetValueFunc(LS_index0
);
1402 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1403 ok(val
== NULL
, "Slot not initialized correctly\n");
1405 val
= LS_GetValueFunc(LS_index1
);
1406 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1407 ok(val
== NULL
, "Slot not initialized correctly\n");
1411 if (sync_threads_and_run_one(0, id
))
1413 val
= LS_GetValueFunc(LS_index0
);
1414 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1415 ok(val
== NULL
, "Slot not initialized correctly\n");
1417 val
= LS_GetValueFunc(LS_index1
);
1418 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1419 ok(val
== NULL
, "Slot not initialized correctly\n");
1421 ret
= LS_SetValueFunc(LS_index0
, (LPVOID
) 1);
1422 ok(ret
, "%s failed\n", LS_SetValueFuncName
);
1424 ret
= LS_SetValueFunc(LS_index1
, (LPVOID
) 2);
1425 ok(ret
, "%s failed\n", LS_SetValueFuncName
);
1427 val
= LS_GetValueFunc(LS_index0
);
1428 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1429 ok(val
== (LPVOID
) 1, "Slot not initialized correctly\n");
1431 val
= LS_GetValueFunc(LS_index1
);
1432 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1433 ok(val
== (LPVOID
) 2, "Slot not initialized correctly\n");
1437 if (sync_threads_and_run_one(1, id
))
1439 val
= LS_GetValueFunc(LS_index0
);
1440 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1441 ok(val
== NULL
, "Slot not initialized correctly\n");
1443 val
= LS_GetValueFunc(LS_index1
);
1444 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1445 ok(val
== NULL
, "Slot not initialized correctly\n");
1447 ret
= LS_SetValueFunc(LS_index0
, (LPVOID
) 3);
1448 ok(ret
, "%s failed\n", LS_SetValueFuncName
);
1450 ret
= LS_SetValueFunc(LS_index1
, (LPVOID
) 4);
1451 ok(ret
, "%s failed\n", LS_SetValueFuncName
);
1453 val
= LS_GetValueFunc(LS_index0
);
1454 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1455 ok(val
== (LPVOID
) 3, "Slot not initialized correctly\n");
1457 val
= LS_GetValueFunc(LS_index1
);
1458 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1459 ok(val
== (LPVOID
) 4, "Slot not initialized correctly\n");
1463 if (sync_threads_and_run_one(0, id
))
1468 val
= LS_GetValueFunc(LS_index0
);
1469 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1470 ok(val
== (LPVOID
) 1, "Slot not initialized correctly\n");
1472 val
= LS_GetValueFunc(LS_index1
);
1473 ok(GetLastError() == ERROR_SUCCESS
, "%s failed\n", LS_GetValueFuncName
);
1474 ok(val
== (LPVOID
) 2, "Slot not initialized correctly\n");
1476 thread
= CreateThread(NULL
, 0, LS_InheritanceProc
, 0, 0, &tid
);
1477 ok(thread
!= NULL
, "CreateThread failed\n");
1478 waitret
= WaitForSingleObject(thread
, 60000);
1479 ok(waitret
== WAIT_OBJECT_0
, "WaitForSingleObject failed\n");
1480 CloseHandle(thread
);
1482 ret
= LS_FreeFunc(LS_index0
);
1483 ok(ret
, "%s failed\n", LS_FreeFuncName
);
1487 if (sync_threads_and_run_one(1, id
))
1489 ret
= LS_FreeFunc(LS_index1
);
1490 ok(ret
, "%s failed\n", LS_FreeFuncName
);
1497 static void run_LS_tests(void)
1504 init_thread_sync_helpers();
1506 /* Allocate a slot in the main thread to test for inheritance. */
1507 LS_main
= LS_AllocFunc();
1508 ok(LS_main
!= LS_OutOfIndexesValue
, "%s failed\n", LS_AllocFuncName
);
1509 suc
= LS_SetValueFunc(LS_main
, (LPVOID
) 4114);
1510 ok(suc
, "%s failed\n", LS_SetValueFuncName
);
1512 for (i
= 0; i
< 2; ++i
)
1516 threads
[i
] = CreateThread(NULL
, 0, LS_ThreadProc
, (LPVOID
) i
, 0, &tid
);
1517 ok(threads
[i
] != NULL
, "CreateThread failed\n");
1520 ret
= WaitForMultipleObjects(2, threads
, TRUE
, 60000);
1521 ok(ret
== WAIT_OBJECT_0
|| broken(ret
== WAIT_OBJECT_0
+1 /* nt4,w2k */), "WaitForAllObjects 2 threads %d\n",ret
);
1523 for (i
= 0; i
< 2; ++i
)
1524 CloseHandle(threads
[i
]);
1526 suc
= LS_FreeFunc(LS_main
);
1527 ok(suc
, "%s failed\n", LS_FreeFuncName
);
1528 cleanup_thread_sync_helpers();
1531 static void test_TLS(void)
1533 LS_OutOfIndexesValue
= TLS_OUT_OF_INDEXES
;
1535 LS_AllocFunc
= &TlsAlloc
;
1536 LS_GetValueFunc
= &TlsGetValue
;
1537 LS_SetValueFunc
= &TlsSetValue
;
1538 LS_FreeFunc
= &TlsFree
;
1540 LS_AllocFuncName
= "TlsAlloc";
1541 LS_GetValueFuncName
= "TlsGetValue";
1542 LS_SetValueFuncName
= "TlsSetValue";
1543 LS_FreeFuncName
= "TlsFree";
1548 static void test_FLS(void)
1550 if (!pFlsAlloc
|| !pFlsFree
|| !pFlsGetValue
|| !pFlsSetValue
)
1552 win_skip("Fiber Local Storage not supported\n");
1556 LS_OutOfIndexesValue
= FLS_OUT_OF_INDEXES
;
1558 LS_AllocFunc
= &FLS_AllocFuncThunk
;
1559 LS_GetValueFunc
= pFlsGetValue
;
1560 LS_SetValueFunc
= pFlsSetValue
;
1561 LS_FreeFunc
= pFlsFree
;
1563 LS_AllocFuncName
= "FlsAlloc";
1564 LS_GetValueFuncName
= "FlsGetValue";
1565 LS_SetValueFuncName
= "FlsSetValue";
1566 LS_FreeFuncName
= "FlsFree";
1571 static void test_ThreadErrorMode(void)
1578 if (!pSetThreadErrorMode
|| !pGetThreadErrorMode
)
1580 win_skip("SetThreadErrorMode and/or GetThreadErrorMode unavailable (added in Windows 7)\n");
1584 if (!pRtlGetThreadErrorMode
) {
1585 win_skip("RtlGetThreadErrorMode not available\n");
1589 oldmode
= pGetThreadErrorMode();
1591 ret
= pSetThreadErrorMode(0, &mode
);
1592 ok(ret
, "SetThreadErrorMode failed\n");
1594 "SetThreadErrorMode returned old mode 0x%x, expected 0x%x\n",
1596 mode
= pGetThreadErrorMode();
1597 ok(mode
== 0, "GetThreadErrorMode returned mode 0x%x, expected 0\n", mode
);
1598 rtlmode
= pRtlGetThreadErrorMode();
1600 "RtlGetThreadErrorMode returned mode 0x%x, expected 0\n", mode
);
1602 ret
= pSetThreadErrorMode(SEM_FAILCRITICALERRORS
, &mode
);
1603 ok(ret
, "SetThreadErrorMode failed\n");
1605 "SetThreadErrorMode returned old mode 0x%x, expected 0\n", mode
);
1606 mode
= pGetThreadErrorMode();
1607 ok(mode
== SEM_FAILCRITICALERRORS
,
1608 "GetThreadErrorMode returned mode 0x%x, expected SEM_FAILCRITICALERRORS\n",
1610 rtlmode
= pRtlGetThreadErrorMode();
1612 "RtlGetThreadErrorMode returned mode 0x%x, expected 0x10\n", mode
);
1614 ret
= pSetThreadErrorMode(SEM_NOGPFAULTERRORBOX
, &mode
);
1615 ok(ret
, "SetThreadErrorMode failed\n");
1616 ok(mode
== SEM_FAILCRITICALERRORS
,
1617 "SetThreadErrorMode returned old mode 0x%x, expected SEM_FAILCRITICALERRORS\n",
1619 mode
= pGetThreadErrorMode();
1620 ok(mode
== SEM_NOGPFAULTERRORBOX
,
1621 "GetThreadErrorMode returned mode 0x%x, expected SEM_NOGPFAULTERRORBOX\n",
1623 rtlmode
= pRtlGetThreadErrorMode();
1625 "RtlGetThreadErrorMode returned mode 0x%x, expected 0x20\n", mode
);
1627 ret
= pSetThreadErrorMode(SEM_NOOPENFILEERRORBOX
, NULL
);
1628 ok(ret
, "SetThreadErrorMode failed\n");
1629 mode
= pGetThreadErrorMode();
1630 ok(mode
== SEM_NOOPENFILEERRORBOX
,
1631 "GetThreadErrorMode returned mode 0x%x, expected SEM_NOOPENFILEERRORBOX\n",
1633 rtlmode
= pRtlGetThreadErrorMode();
1635 "RtlGetThreadErrorMode returned mode 0x%x, expected 0x40\n", rtlmode
);
1637 for (mode
= 1; mode
; mode
<<= 1)
1639 ret
= pSetThreadErrorMode(mode
, NULL
);
1640 if (mode
& (SEM_FAILCRITICALERRORS
|
1641 SEM_NOGPFAULTERRORBOX
|
1642 SEM_NOOPENFILEERRORBOX
))
1645 "SetThreadErrorMode(0x%x,NULL) failed with error %d\n",
1646 mode
, GetLastError());
1650 DWORD GLE
= GetLastError();
1652 "SetThreadErrorMode(0x%x,NULL) succeeded, expected failure\n",
1654 ok(GLE
== ERROR_INVALID_PARAMETER
,
1655 "SetThreadErrorMode(0x%x,NULL) failed with %d, "
1656 "expected ERROR_INVALID_PARAMETER\n",
1661 pSetThreadErrorMode(oldmode
, NULL
);
1664 #if (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))) || (defined(_MSC_VER) && defined(__i386__))
1665 static inline void set_fpu_cw(WORD cw
)
1669 __asm
{ fldcw
[cw
] }
1671 __asm__
volatile ("fnclex; fldcw %0" : : "m" (cw
));
1675 static inline WORD
get_fpu_cw(void)
1679 __asm
{ fnstcw
[cw
] }
1681 __asm__
volatile ("fnstcw %0" : "=m" (cw
));
1686 struct fpu_thread_ctx
1692 static DWORD WINAPI
fpu_thread(void *param
)
1694 struct fpu_thread_ctx
*ctx
= param
;
1697 ctx
->cw
= get_fpu_cw();
1699 ret
= SetEvent(ctx
->finished
);
1700 ok(ret
, "SetEvent failed, last error %#x.\n", GetLastError());
1705 static WORD
get_thread_fpu_cw(void)
1707 struct fpu_thread_ctx ctx
;
1711 ctx
.finished
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1712 ok(!!ctx
.finished
, "Failed to create event, last error %#x.\n", GetLastError());
1714 thread
= CreateThread(NULL
, 0, fpu_thread
, &ctx
, 0, &tid
);
1715 ok(!!thread
, "Failed to create thread, last error %#x.\n", GetLastError());
1717 res
= WaitForSingleObject(ctx
.finished
, INFINITE
);
1718 ok(res
== WAIT_OBJECT_0
, "Wait failed (%#x), last error %#x.\n", res
, GetLastError());
1720 res
= CloseHandle(ctx
.finished
);
1721 ok(!!res
, "Failed to close event handle, last error %#x.\n", GetLastError());
1726 static void test_thread_fpu_cw(void)
1728 WORD initial_cw
, cw
;
1730 initial_cw
= get_fpu_cw();
1731 ok(initial_cw
== 0x27f, "Expected FPU control word 0x27f, got %#x.\n", initial_cw
);
1733 cw
= get_thread_fpu_cw();
1734 ok(cw
== 0x27f, "Expected FPU control word 0x27f, got %#x.\n", cw
);
1738 ok(cw
== 0xf60, "Expected FPU control word 0xf60, got %#x.\n", cw
);
1740 cw
= get_thread_fpu_cw();
1741 ok(cw
== 0x27f, "Expected FPU control word 0x27f, got %#x.\n", cw
);
1744 ok(cw
== 0xf60, "Expected FPU control word 0xf60, got %#x.\n", cw
);
1746 set_fpu_cw(initial_cw
);
1748 ok(cw
== initial_cw
, "Expected FPU control word %#x, got %#x.\n", initial_cw
, cw
);
1752 static const char manifest_dep
[] =
1753 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
1754 "<assemblyIdentity version=\"1.2.3.4\" name=\"testdep1\" type=\"win32\" processorArchitecture=\"" ARCH
"\"/>"
1755 " <file name=\"testdep.dll\" />"
1758 static const char manifest_main
[] =
1759 "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\" manifestVersion=\"1.0\">"
1760 "<assemblyIdentity version=\"1.2.3.4\" name=\"Wine.Test\" type=\"win32\" />"
1762 " <dependentAssembly>"
1763 " <assemblyIdentity type=\"win32\" name=\"testdep1\" version=\"1.2.3.4\" processorArchitecture=\"" ARCH
"\" />"
1764 " </dependentAssembly>"
1768 static void create_manifest_file(const char *filename
, const char *manifest
)
1770 WCHAR path
[MAX_PATH
];
1774 MultiByteToWideChar( CP_ACP
, 0, filename
, -1, path
, MAX_PATH
);
1775 file
= CreateFileW(path
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
1776 ok(file
!= INVALID_HANDLE_VALUE
, "CreateFile failed: %u\n", GetLastError());
1777 WriteFile(file
, manifest
, strlen(manifest
), &size
, NULL
);
1781 static HANDLE
test_create(const char *file
)
1783 WCHAR path
[MAX_PATH
];
1787 MultiByteToWideChar(CP_ACP
, 0, file
, -1, path
, MAX_PATH
);
1788 memset(&actctx
, 0, sizeof(ACTCTXW
));
1789 actctx
.cbSize
= sizeof(ACTCTXW
);
1790 actctx
.lpSource
= path
;
1792 handle
= pCreateActCtxW(&actctx
);
1793 ok(handle
!= INVALID_HANDLE_VALUE
, "failed to create context, error %u\n", GetLastError());
1795 ok(actctx
.cbSize
== sizeof(actctx
), "cbSize=%d\n", actctx
.cbSize
);
1796 ok(actctx
.dwFlags
== 0, "dwFlags=%d\n", actctx
.dwFlags
);
1797 ok(actctx
.lpSource
== path
, "lpSource=%p\n", actctx
.lpSource
);
1798 ok(actctx
.wProcessorArchitecture
== 0, "wProcessorArchitecture=%d\n", actctx
.wProcessorArchitecture
);
1799 ok(actctx
.wLangId
== 0, "wLangId=%d\n", actctx
.wLangId
);
1800 ok(actctx
.lpAssemblyDirectory
== NULL
, "lpAssemblyDirectory=%p\n", actctx
.lpAssemblyDirectory
);
1801 ok(actctx
.lpResourceName
== NULL
, "lpResourceName=%p\n", actctx
.lpResourceName
);
1802 ok(actctx
.lpApplicationName
== NULL
, "lpApplicationName=%p\n", actctx
.lpApplicationName
);
1803 ok(actctx
.hModule
== NULL
, "hModule=%p\n", actctx
.hModule
);
1808 static void test_thread_actctx(void)
1810 struct thread_actctx_param param
;
1811 HANDLE thread
, handle
, context
;
1816 if (!pActivateActCtx
)
1818 win_skip("skipping activation context tests\n");
1822 create_manifest_file("testdep1.manifest", manifest_dep
);
1823 create_manifest_file("main.manifest", manifest_main
);
1825 context
= test_create("main.manifest");
1826 DeleteFileA("testdep1.manifest");
1827 DeleteFileA("main.manifest");
1829 handle
= (void*)0xdeadbeef;
1830 b
= pGetCurrentActCtx(&handle
);
1831 ok(b
, "GetCurrentActCtx failed: %u\n", GetLastError());
1832 ok(handle
== 0, "active context %p\n", handle
);
1834 /* without active context */
1835 param
.thread_context
= (void*)0xdeadbeef;
1836 param
.handle
= NULL
;
1837 thread
= CreateThread(NULL
, 0, thread_actctx_func
, ¶m
, 0, &tid
);
1838 ok(thread
!= NULL
, "failed, got %u\n", GetLastError());
1840 ret
= WaitForSingleObject(thread
, 1000);
1841 ok(ret
== WAIT_OBJECT_0
, "wait timeout\n");
1842 ok(param
.thread_context
== NULL
, "got wrong thread context %p\n", param
.thread_context
);
1843 CloseHandle(thread
);
1845 b
= pActivateActCtx(context
, &cookie
);
1846 ok(b
, "activation failed: %u\n", GetLastError());
1849 b
= pGetCurrentActCtx(&handle
);
1850 ok(b
, "GetCurrentActCtx failed: %u\n", GetLastError());
1851 ok(handle
!= 0, "no active context\n");
1852 pReleaseActCtx(handle
);
1854 param
.handle
= NULL
;
1855 b
= pGetCurrentActCtx(¶m
.handle
);
1856 ok(b
&& param
.handle
!= NULL
, "failed to get context, %u\n", GetLastError());
1858 param
.thread_context
= (void*)0xdeadbeef;
1859 thread
= CreateThread(NULL
, 0, thread_actctx_func
, ¶m
, 0, &tid
);
1860 ok(thread
!= NULL
, "failed, got %u\n", GetLastError());
1862 ret
= WaitForSingleObject(thread
, 1000);
1863 ok(ret
== WAIT_OBJECT_0
, "wait timeout\n");
1864 ok(param
.thread_context
== context
, "got wrong thread context %p, %p\n", param
.thread_context
, context
);
1865 pReleaseActCtx(param
.thread_context
);
1866 CloseHandle(thread
);
1868 /* similar test for CreateRemoteThread() */
1869 param
.thread_context
= (void*)0xdeadbeef;
1870 thread
= CreateRemoteThread(GetCurrentProcess(), NULL
, 0, thread_actctx_func
, ¶m
, 0, &tid
);
1871 ok(thread
!= NULL
, "failed, got %u\n", GetLastError());
1873 ret
= WaitForSingleObject(thread
, 1000);
1874 ok(ret
== WAIT_OBJECT_0
, "wait timeout\n");
1875 ok(param
.thread_context
== context
, "got wrong thread context %p, %p\n", param
.thread_context
, context
);
1876 pReleaseActCtx(param
.thread_context
);
1877 CloseHandle(thread
);
1879 pReleaseActCtx(param
.handle
);
1881 b
= pDeactivateActCtx(0, cookie
);
1882 ok(b
, "DeactivateActCtx failed: %u\n", GetLastError());
1883 pReleaseActCtx(context
);
1887 static void WINAPI
threadpool_workcallback(PTP_CALLBACK_INSTANCE instance
, void *context
, PTP_WORK work
) {
1888 int *foo
= (int*)context
;
1894 static void test_threadpool(void)
1900 if (!pCreateThreadpool
) {
1901 win_skip("thread pool apis not supported.\n");
1905 work
= pCreateThreadpoolWork(threadpool_workcallback
, &workcalled
, NULL
);
1906 ok (work
!= NULL
, "Error %d in CreateThreadpoolWork\n", GetLastError());
1907 pSubmitThreadpoolWork(work
);
1908 pWaitForThreadpoolWorkCallbacks(work
, FALSE
);
1909 pCloseThreadpoolWork(work
);
1911 ok (workcalled
== 1, "expected work to be called once, got %d\n", workcalled
);
1913 pool
= pCreateThreadpool(NULL
);
1914 ok (pool
!= NULL
, "CreateThreadpool failed\n");
1915 pCloseThreadpool(pool
);
1918 static void test_reserved_tls(void)
1924 /* This seems to be a WinXP SP2+ feature. */
1925 if(!pIsWow64Process
) {
1926 win_skip("Skipping reserved TLS slot on too old Windows.\n");
1930 val
= TlsGetValue(0);
1931 ok(!val
, "TlsGetValue(0) = %p\n", val
);
1933 /* Also make sure that there is a TLS allocated. */
1935 ok(tls
&& tls
!= TLS_OUT_OF_INDEXES
, "tls = %x\n", tls
);
1936 TlsSetValue(tls
, (void*)1);
1938 val
= TlsGetValue(0);
1939 ok(!val
, "TlsGetValue(0) = %p\n", val
);
1943 /* The following is too ugly to be run by default */
1945 /* Set TLS index 0 value and see that this works and doesn't cause problems
1946 * for remaining tests. */
1947 ret
= TlsSetValue(0, (void*)1);
1948 ok(ret
, "TlsSetValue(0, 1) failed: %u\n", GetLastError());
1950 val
= TlsGetValue(0);
1951 ok(val
== (void*)1, "TlsGetValue(0) = %p\n", val
);
1955 static void test_thread_info(void)
1958 static const ULONG info_size
[] =
1960 sizeof(THREAD_BASIC_INFORMATION
), /* ThreadBasicInformation */
1961 sizeof(KERNEL_USER_TIMES
), /* ThreadTimes */
1962 sizeof(ULONG
), /* ThreadPriority */
1963 sizeof(ULONG
), /* ThreadBasePriority */
1964 sizeof(ULONG_PTR
), /* ThreadAffinityMask */
1965 sizeof(HANDLE
), /* ThreadImpersonationToken */
1966 sizeof(THREAD_DESCRIPTOR_INFORMATION
), /* ThreadDescriptorTableEntry */
1967 sizeof(BOOLEAN
), /* ThreadEnableAlignmentFaultFixup */
1968 0, /* ThreadEventPair_Reusable */
1969 sizeof(ULONG_PTR
), /* ThreadQuerySetWin32StartAddress */
1970 sizeof(ULONG
), /* ThreadZeroTlsCell */
1971 sizeof(LARGE_INTEGER
), /* ThreadPerformanceCount */
1972 sizeof(ULONG
), /* ThreadAmILastThread */
1973 sizeof(ULONG
), /* ThreadIdealProcessor */
1974 sizeof(ULONG
), /* ThreadPriorityBoost */
1975 sizeof(ULONG_PTR
), /* ThreadSetTlsArrayAddress */
1976 sizeof(ULONG
), /* ThreadIsIoPending */
1977 sizeof(BOOLEAN
), /* ThreadHideFromDebugger */
1978 /* FIXME: Add remaining classes */
1981 ULONG i
, status
, ret_len
;
1985 win_skip("OpenThread is not available on this platform\n");
1989 if (!pNtQueryInformationThread
)
1991 win_skip("NtQueryInformationThread is not available on this platform\n");
1995 thread
= pOpenThread(THREAD_QUERY_LIMITED_INFORMATION
, FALSE
, GetCurrentThreadId());
1998 win_skip("THREAD_QUERY_LIMITED_INFORMATION is not supported on this platform\n");
2002 for (i
= 0; i
< sizeof(info_size
)/sizeof(info_size
[0]); i
++)
2004 memset(buf
, 0, sizeof(buf
));
2007 if (i
== ThreadDescriptorTableEntry
)
2010 THREAD_DESCRIPTOR_INFORMATION
*tdi
= (void *)buf
;
2012 ctx
.ContextFlags
= CONTEXT_SEGMENTS
;
2013 GetThreadContext(GetCurrentThread(), &ctx
);
2014 tdi
->Selector
= ctx
.SegDs
;
2018 status
= pNtQueryInformationThread(thread
, i
, buf
, info_size
[i
], &ret_len
);
2019 if (status
== STATUS_NOT_IMPLEMENTED
) continue;
2020 if (status
== STATUS_INVALID_INFO_CLASS
) continue;
2021 if (status
== STATUS_UNSUCCESSFUL
) continue;
2025 case ThreadBasicInformation
:
2026 case ThreadAmILastThread
:
2027 case ThreadPriorityBoost
:
2028 ok(status
== STATUS_SUCCESS
, "for info %u expected STATUS_SUCCESS, got %08x (ret_len %u)\n", i
, status
, ret_len
);
2032 case ThreadDescriptorTableEntry
:
2033 ok(status
== STATUS_SUCCESS
|| broken(status
== STATUS_ACCESS_DENIED
) /* testbot VM is broken */,
2034 "for info %u expected STATUS_SUCCESS, got %08x (ret_len %u)\n", i
, status
, ret_len
);
2040 ok(status
== STATUS_SUCCESS
, "for info %u expected STATUS_SUCCESS, got %08x (ret_len %u)\n", i
, status
, ret_len
);
2043 case ThreadAffinityMask
:
2044 case ThreadQuerySetWin32StartAddress
:
2046 ok(status
== STATUS_ACCESS_DENIED
, "for info %u expected STATUS_ACCESS_DENIED, got %08x (ret_len %u)\n", i
, status
, ret_len
);
2050 ok(status
== STATUS_ACCESS_DENIED
, "for info %u expected STATUS_ACCESS_DENIED, got %08x (ret_len %u)\n", i
, status
, ret_len
);
2055 CloseHandle(thread
);
2058 static void init_funcs(void)
2060 HMODULE hKernel32
= GetModuleHandleA("kernel32.dll");
2061 HMODULE ntdll
= GetModuleHandleA("ntdll.dll");
2063 /* Neither Cygwin nor mingW export OpenThread, so do a dynamic check
2064 so that the compile passes */
2066 #define X(f) p##f = (void*)GetProcAddress(hKernel32, #f)
2067 X(GetThreadPriorityBoost
);
2069 X(QueueUserWorkItem
);
2070 X(SetThreadIdealProcessor
);
2071 X(SetThreadPriorityBoost
);
2072 X(RegisterWaitForSingleObject
);
2075 X(SetThreadErrorMode
);
2076 X(GetThreadErrorMode
);
2079 X(DeactivateActCtx
);
2080 X(GetCurrentActCtx
);
2083 X(CreateThreadpool
);
2085 X(CreateThreadpoolWork
);
2086 X(SubmitThreadpoolWork
);
2087 X(WaitForThreadpoolWorkCallbacks
);
2088 X(CloseThreadpoolWork
);
2090 X(GetThreadGroupAffinity
);
2091 X(SetThreadGroupAffinity
);
2099 #define X(f) p##f = (void*)GetProcAddress(ntdll, #f)
2102 X(NtQueryInformationThread
);
2103 X(RtlGetThreadErrorMode
);
2104 X(NtSetInformationThread
);
2114 argc
= winetest_get_mainargs( &argv
);
2120 if (!strcmp(argv
[2], "sleep"))
2122 HANDLE hAddrEvents
[2];
2123 create_function_addr_events(hAddrEvents
);
2124 SetEvent(hAddrEvents
[0]);
2125 SetEvent(hAddrEvents
[1]);
2126 Sleep(5000); /* spawned process runs for at most 5 seconds */
2133 hThread
= CreateThread(NULL
, 0, threadFunc2
, NULL
, 0, &tid
);
2134 ok(hThread
!= NULL
, "CreateThread failed, error %u\n",
2136 ok(WaitForSingleObject(hThread
, 200) == WAIT_OBJECT_0
,
2137 "Thread did not exit in time\n");
2138 if (hThread
== NULL
) break;
2139 CloseHandle(hThread
);
2145 test_reserved_tls();
2146 test_CreateRemoteThread();
2147 test_CreateThread_basic();
2148 test_CreateThread_suspended();
2149 test_SuspendThread();
2150 test_TerminateThread();
2151 test_CreateThread_stack();
2152 test_thread_priority();
2153 test_GetThreadTimes();
2154 test_thread_processor();
2155 test_GetThreadExitCode();
2157 test_SetThreadContext();
2158 test_GetThreadSelectorEntry();
2159 test_NtSetLdtEntries();
2161 test_QueueUserWorkItem();
2162 test_RegisterWaitForSingleObject();
2165 test_ThreadErrorMode();
2166 #if (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))) || (defined(_MSC_VER) && defined(__i386__))
2167 test_thread_fpu_cw();
2169 test_thread_actctx();