Implemented NtQueueApcThread, and changed the server APC interface to
[wine] / dlls / ntdll / thread.c
1 /*
2  * NT threads support
3  *
4  * Copyright 1996, 2003 Alexandre Julliard
5  *
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.
10  *
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.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "winternl.h"
22 #include "wine/server.h"
23
24
25 /***********************************************************************
26  *              NtOpenThread   (NTDLL.@)
27  *              ZwOpenThread   (NTDLL.@)
28  */
29 NTSTATUS WINAPI NtOpenThread( HANDLE *handle, ACCESS_MASK access,
30                               const OBJECT_ATTRIBUTES *attr, const CLIENT_ID *id )
31 {
32     NTSTATUS ret;
33
34     SERVER_START_REQ( open_thread )
35     {
36         req->tid     = (thread_id_t)id->UniqueThread;
37         req->access  = access;
38         req->inherit = attr && (attr->Attributes & OBJ_INHERIT);
39         ret = wine_server_call( req );
40         *handle = reply->handle;
41     }
42     SERVER_END_REQ;
43     return ret;
44 }
45
46
47 /******************************************************************************
48  *              NtSuspendThread   (NTDLL.@)
49  *              ZwSuspendThread   (NTDLL.@)
50  */
51 NTSTATUS WINAPI NtSuspendThread( HANDLE handle, PULONG count )
52 {
53     NTSTATUS ret;
54
55     SERVER_START_REQ( suspend_thread )
56     {
57         req->handle = handle;
58         if (!(ret = wine_server_call( req ))) *count = reply->count;
59     }
60     SERVER_END_REQ;
61     return ret;
62 }
63
64
65 /******************************************************************************
66  *              NtResumeThread   (NTDLL.@)
67  *              ZwResumeThread   (NTDLL.@)
68  */
69 NTSTATUS WINAPI NtResumeThread( HANDLE handle, PULONG count )
70 {
71     NTSTATUS ret;
72
73     SERVER_START_REQ( resume_thread )
74     {
75         req->handle = handle;
76         if (!(ret = wine_server_call( req ))) *count = reply->count;
77     }
78     SERVER_END_REQ;
79     return ret;
80 }
81
82
83 /******************************************************************************
84  *              NtTerminateThread  (NTDLL.@)
85  *              ZwTerminateThread  (NTDLL.@)
86  */
87 NTSTATUS WINAPI NtTerminateThread( HANDLE handle, LONG exit_code )
88 {
89     NTSTATUS ret;
90     BOOL self, last;
91
92     SERVER_START_REQ( terminate_thread )
93     {
94         req->handle    = handle;
95         req->exit_code = exit_code;
96         ret = wine_server_call( req );
97         self = !ret && reply->self;
98         last = reply->last;
99     }
100     SERVER_END_REQ;
101
102     if (self)
103     {
104         if (last) exit( exit_code );
105         else SYSDEPS_ExitThread( exit_code );
106     }
107     return ret;
108 }
109
110
111 /******************************************************************************
112  *              NtQueueApcThread  (NTDLL.@)
113  */
114 NTSTATUS WINAPI NtQueueApcThread( HANDLE handle, PNTAPCFUNC func, ULONG_PTR arg1,
115                                   ULONG_PTR arg2, ULONG_PTR arg3 )
116 {
117     NTSTATUS ret;
118     SERVER_START_REQ( queue_apc )
119     {
120         req->handle = handle;
121         req->user   = 1;
122         req->func   = func;
123         req->arg1   = (void *)arg1;
124         req->arg2   = (void *)arg2;
125         req->arg3   = (void *)arg3;
126         ret = wine_server_call( req );
127     }
128     SERVER_END_REQ;
129     return ret;
130 }
131
132
133 /***********************************************************************
134  *              NtSetContextThread  (NTDLL.@)
135  *              ZwSetContextThread  (NTDLL.@)
136  */
137 NTSTATUS WINAPI NtSetContextThread( HANDLE handle, const CONTEXT *context )
138 {
139     NTSTATUS ret;
140
141     SERVER_START_REQ( set_thread_context )
142     {
143         req->handle = handle;
144         req->flags  = context->ContextFlags;
145         wine_server_add_data( req, context, sizeof(*context) );
146         ret = wine_server_call( req );
147     }
148     SERVER_END_REQ;
149     return ret;
150 }
151
152
153 /***********************************************************************
154  *              NtGetContextThread  (NTDLL.@)
155  *              ZwGetContextThread  (NTDLL.@)
156  */
157 NTSTATUS WINAPI NtGetContextThread( HANDLE handle, CONTEXT *context )
158 {
159     NTSTATUS ret;
160
161     SERVER_START_REQ( get_thread_context )
162     {
163         req->handle = handle;
164         req->flags = context->ContextFlags;
165         wine_server_add_data( req, context, sizeof(*context) );
166         wine_server_set_reply( req, context, sizeof(*context) );
167         ret = wine_server_call( req );
168     }
169     SERVER_END_REQ;
170     return ret;
171 }