4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
27 #include <sys/types.h>
28 #ifdef HAVE_SYS_TIMES_H
29 #include <sys/times.h>
41 #include "wine/server.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(thread);
47 /***********************************************************************
48 * SetThreadContext [KERNEL32.@] Sets context of thread.
54 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
55 const CONTEXT *context ) /* [in] Address of context structure */
57 NTSTATUS status = NtSetContextThread( handle, context );
58 if (status) SetLastError( RtlNtStatusToDosError(status) );
63 /***********************************************************************
64 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
70 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
71 CONTEXT *context ) /* [out] Address of context structure */
73 NTSTATUS status = NtGetContextThread( handle, context );
74 if (status) SetLastError( RtlNtStatusToDosError(status) );
79 /**********************************************************************
80 * SuspendThread [KERNEL32.@] Suspends a thread.
83 * Success: Previous suspend count
86 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
89 NTSTATUS status = NtSuspendThread( hthread, &ret );
94 SetLastError( RtlNtStatusToDosError(status) );
100 /**********************************************************************
101 * ResumeThread [KERNEL32.@] Resumes a thread.
103 * Decrements a thread's suspend count. When count is zero, the
104 * execution of the thread is resumed.
107 * Success: Previous suspend count
108 * Failure: 0xFFFFFFFF
111 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
114 NTSTATUS status = NtResumeThread( hthread, &ret );
119 SetLastError( RtlNtStatusToDosError(status) );
125 /**********************************************************************
126 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
129 * Success: Thread's priority level.
130 * Failure: THREAD_PRIORITY_ERROR_RETURN
132 INT WINAPI GetThreadPriority(
133 HANDLE hthread) /* [in] Handle to thread */
135 THREAD_BASIC_INFORMATION info;
136 NTSTATUS status = NtQueryInformationThread( hthread, ThreadBasicInformation,
137 &info, sizeof(info), NULL );
141 SetLastError( RtlNtStatusToDosError(status) );
142 return THREAD_PRIORITY_ERROR_RETURN;
144 return info.Priority;
148 /**********************************************************************
149 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
155 BOOL WINAPI SetThreadPriority(
156 HANDLE hthread, /* [in] Handle to thread */
157 INT priority) /* [in] Thread priority level */
160 SERVER_START_REQ( set_thread_info )
162 req->handle = hthread;
163 req->priority = priority;
164 req->mask = SET_THREAD_INFO_PRIORITY;
165 ret = !wine_server_call_err( req );
172 /**********************************************************************
173 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
175 * Always reports that priority boost is disabled.
181 BOOL WINAPI GetThreadPriorityBoost(
182 HANDLE hthread, /* [in] Handle to thread */
183 PBOOL pstate) /* [out] pointer to var that receives the boost state */
185 if (pstate) *pstate = FALSE;
190 /**********************************************************************
191 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
193 * Priority boost is not implemented. Thsi function always returns
194 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
197 * Always returns FALSE to indicate a failure
199 BOOL WINAPI SetThreadPriorityBoost(
200 HANDLE hthread, /* [in] Handle to thread */
201 BOOL disable) /* [in] TRUE to disable priority boost */
203 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
208 /**********************************************************************
209 * SetThreadAffinityMask (KERNEL32.@)
211 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
214 SERVER_START_REQ( set_thread_info )
216 req->handle = hThread;
217 req->affinity = dwThreadAffinityMask;
218 req->mask = SET_THREAD_INFO_AFFINITY;
219 ret = !wine_server_call_err( req );
220 /* FIXME: should return previous value */
227 /**********************************************************************
228 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
231 * Success: Value of last call to SetThreadIdealProcessor
234 DWORD WINAPI SetThreadIdealProcessor(
235 HANDLE hThread, /* [in] Specifies the thread of interest */
236 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
238 FIXME("(%p): stub\n",hThread);
239 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
244 /* callback for QueueUserAPC */
245 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
247 PAPCFUNC func = (PAPCFUNC)arg1;
251 /***********************************************************************
252 * QueueUserAPC (KERNEL32.@)
254 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
256 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
258 if (status) SetLastError( RtlNtStatusToDosError(status) );
263 /**********************************************************************
264 * GetThreadTimes [KERNEL32.@] Obtains timing information.
270 BOOL WINAPI GetThreadTimes(
271 HANDLE thread, /* [in] Specifies the thread of interest */
272 LPFILETIME creationtime, /* [out] When the thread was created */
273 LPFILETIME exittime, /* [out] When the thread was destroyed */
274 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
275 LPFILETIME usertime) /* [out] Time thread spent in user mode */
279 if (creationtime || exittime)
281 /* We need to do a server call to get the creation time or exit time */
282 /* This works on any thread */
284 SERVER_START_REQ( get_thread_info )
286 req->handle = thread;
288 if ((ret = !wine_server_call_err( req )))
291 RtlSecondsSince1970ToTime( reply->creation_time, (LARGE_INTEGER*)creationtime );
293 RtlSecondsSince1970ToTime( reply->exit_time, (LARGE_INTEGER*)exittime );
298 if (ret && (kerneltime || usertime))
300 /* We call times(2) for kernel time or user time */
301 /* We can only (portably) do this for the current thread */
302 if (thread == GetCurrentThread())
306 long clocks_per_sec = sysconf(_SC_CLK_TCK);
311 time = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
312 kerneltime->dwHighDateTime = time >> 32;
313 kerneltime->dwLowDateTime = (DWORD)time;
317 time = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
318 usertime->dwHighDateTime = time >> 32;
319 usertime->dwLowDateTime = (DWORD)time;
324 if (kerneltime) kerneltime->dwHighDateTime = kerneltime->dwLowDateTime = 0;
325 if (usertime) usertime->dwHighDateTime = usertime->dwLowDateTime = 0;
326 FIXME("Cannot get kerneltime or usertime of other threads\n");
333 /**********************************************************************
334 * VWin32_BoostThreadGroup [KERNEL.535]
336 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
338 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
342 /**********************************************************************
343 * VWin32_BoostThreadStatic [KERNEL.536]
345 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
347 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
351 /***********************************************************************
352 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
355 * Pseudohandle for the current thread
357 #undef GetCurrentThread
358 HANDLE WINAPI GetCurrentThread(void)
360 return (HANDLE)0xfffffffe;