now introducing the POSIX+ client DLL! (my first check-in, hope I don't f*** up somet...
[reactos.git] / posix / lib / psxdll / pthread / join.c
1 /* $Id:
2 */
3 /*
4 * COPYRIGHT: See COPYING in the top level directory
5 * PROJECT: ReactOS system libraries
6 * FILE: subsys/psx/lib/psxdll/pthread/join.c
7 * PURPOSE: Wait for thread termination
8 * PROGRAMMER: KJK::Hyperion <noog@libero.it>
9 * UPDATE HISTORY:
10 * 19/12/2001: Created
11 */
12
13 #include <ddk/ntddk.h>
14 #include <ntdll/ldr.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <pthread.h>
18 #include <psx/debug.h>
19 #include <psx/errno.h>
20
21 int pthread_join(pthread_t thread, void **value_ptr)
22 {
23 HANDLE hThread;
24 NTSTATUS nErrCode;
25 OBJECT_ATTRIBUTES oaThreadAttrs;
26 CLIENT_ID ciId;
27 THREAD_BASIC_INFORMATION tbiThreadInfo;
28
29 /* "[EDEADLK] A deadlock was detected or the value of thread specifies
30 the calling thread" */
31 if(thread == pthread_self())
32 return (EDEADLK);
33
34 /* initialize id */
35 ciId.UniqueProcess = (HANDLE)-1;
36 ciId.UniqueThread = (HANDLE)thread;
37
38 /* initialize object attributes */
39 oaThreadAttrs.Length = sizeof(OBJECT_ATTRIBUTES);
40 oaThreadAttrs.RootDirectory = NULL;
41 oaThreadAttrs.ObjectName = NULL;
42 oaThreadAttrs.Attributes = 0;
43 oaThreadAttrs.SecurityDescriptor = NULL;
44 oaThreadAttrs.SecurityQualityOfService = NULL;
45
46 /* open the thread */
47 nErrCode = NtOpenThread
48 (
49 &hThread,
50 SYNCHRONIZE | THREAD_QUERY_INFORMATION,
51 &oaThreadAttrs,
52 &ciId
53 );
54
55 /* failure */
56 if(!NT_SUCCESS(nErrCode))
57 {
58 return (__status_to_errno(nErrCode));
59 }
60
61 /* wait for thread termination */
62 nErrCode = NtWaitForSingleObject
63 (
64 hThread,
65 FALSE,
66 NULL
67 );
68
69 /* failure */
70 if(!NT_SUCCESS(nErrCode))
71 {
72 NtClose(hThread);
73 return (__status_to_errno(nErrCode));
74 }
75
76 /* get thread basic information (includes return code) */
77 nErrCode = NtQueryInformationThread
78 (
79 hThread,
80 ThreadBasicInformation,
81 &tbiThreadInfo,
82 sizeof(THREAD_BASIC_INFORMATION),
83 NULL
84 );
85
86 NtClose(hThread);
87
88 if(!value_ptr)
89 return (EFAULT);
90
91 *value_ptr = (void *)tbiThreadInfo.ExitStatus;
92
93 return (0);
94
95 }
96
97 /* EOF */
98