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"
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_TIMES_H
28 #include <sys/times.h>
38 #include "wine/server.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(thread);
44 /***********************************************************************
45 * SetThreadContext [KERNEL32.@] Sets context of thread.
51 BOOL WINAPI SetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
52 const CONTEXT *context ) /* [in] Address of context structure */
54 NTSTATUS status = NtSetContextThread( handle, context );
55 if (status) SetLastError( RtlNtStatusToDosError(status) );
60 /***********************************************************************
61 * GetThreadContext [KERNEL32.@] Retrieves context of thread.
67 BOOL WINAPI GetThreadContext( HANDLE handle, /* [in] Handle to thread with context */
68 CONTEXT *context ) /* [out] Address of context structure */
70 NTSTATUS status = NtGetContextThread( handle, context );
71 if (status) SetLastError( RtlNtStatusToDosError(status) );
76 /**********************************************************************
77 * SuspendThread [KERNEL32.@] Suspends a thread.
80 * Success: Previous suspend count
83 DWORD WINAPI SuspendThread( HANDLE hthread ) /* [in] Handle to the thread */
86 NTSTATUS status = NtSuspendThread( hthread, &ret );
91 SetLastError( RtlNtStatusToDosError(status) );
97 /**********************************************************************
98 * ResumeThread [KERNEL32.@] Resumes a thread.
100 * Decrements a thread's suspend count. When count is zero, the
101 * execution of the thread is resumed.
104 * Success: Previous suspend count
105 * Failure: 0xFFFFFFFF
108 DWORD WINAPI ResumeThread( HANDLE hthread ) /* [in] Identifies thread to restart */
111 NTSTATUS status = NtResumeThread( hthread, &ret );
116 SetLastError( RtlNtStatusToDosError(status) );
122 /**********************************************************************
123 * GetThreadPriority [KERNEL32.@] Returns priority for thread.
126 * Success: Thread's priority level.
127 * Failure: THREAD_PRIORITY_ERROR_RETURN
129 INT WINAPI GetThreadPriority(
130 HANDLE hthread) /* [in] Handle to thread */
132 INT ret = THREAD_PRIORITY_ERROR_RETURN;
133 SERVER_START_REQ( get_thread_info )
135 req->handle = hthread;
137 if (!wine_server_call_err( req )) ret = reply->priority;
144 /**********************************************************************
145 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
151 BOOL WINAPI SetThreadPriority(
152 HANDLE hthread, /* [in] Handle to thread */
153 INT priority) /* [in] Thread priority level */
156 SERVER_START_REQ( set_thread_info )
158 req->handle = hthread;
159 req->priority = priority;
160 req->mask = SET_THREAD_INFO_PRIORITY;
161 ret = !wine_server_call_err( req );
168 /**********************************************************************
169 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
171 * Always reports that priority boost is disabled.
177 BOOL WINAPI GetThreadPriorityBoost(
178 HANDLE hthread, /* [in] Handle to thread */
179 PBOOL pstate) /* [out] pointer to var that receives the boost state */
181 if (pstate) *pstate = FALSE;
186 /**********************************************************************
187 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
189 * Priority boost is not implemented. Thsi function always returns
190 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
193 * Always returns FALSE to indicate a failure
195 BOOL WINAPI SetThreadPriorityBoost(
196 HANDLE hthread, /* [in] Handle to thread */
197 BOOL disable) /* [in] TRUE to disable priority boost */
199 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
204 /**********************************************************************
205 * SetThreadAffinityMask (KERNEL32.@)
207 DWORD WINAPI SetThreadAffinityMask( HANDLE hThread, DWORD dwThreadAffinityMask )
210 SERVER_START_REQ( set_thread_info )
212 req->handle = hThread;
213 req->affinity = dwThreadAffinityMask;
214 req->mask = SET_THREAD_INFO_AFFINITY;
215 ret = !wine_server_call_err( req );
216 /* FIXME: should return previous value */
223 /**********************************************************************
224 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
227 * Success: Value of last call to SetThreadIdealProcessor
230 DWORD WINAPI SetThreadIdealProcessor(
231 HANDLE hThread, /* [in] Specifies the thread of interest */
232 DWORD dwIdealProcessor) /* [in] Specifies the new preferred processor */
234 FIXME("(%p): stub\n",hThread);
235 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
240 /***********************************************************************
241 * SetThreadExecutionState (KERNEL32.@)
243 * Informs the system that activity is taking place for
244 * power management purposes.
246 EXECUTION_STATE WINAPI SetThreadExecutionState(EXECUTION_STATE flags)
248 static EXECUTION_STATE current =
249 ES_SYSTEM_REQUIRED|ES_DISPLAY_REQUIRED|ES_USER_PRESENT;
250 EXECUTION_STATE old = current;
252 if (!(current & ES_CONTINUOUS) || (flags & ES_CONTINUOUS))
254 FIXME("(0x%lx): stub, harmless (power management).\n", flags);
259 /* callback for QueueUserAPC */
260 static void CALLBACK call_user_apc( ULONG_PTR arg1, ULONG_PTR arg2, ULONG_PTR arg3 )
262 PAPCFUNC func = (PAPCFUNC)arg1;
266 /***********************************************************************
267 * QueueUserAPC (KERNEL32.@)
269 DWORD WINAPI QueueUserAPC( PAPCFUNC func, HANDLE hthread, ULONG_PTR data )
271 NTSTATUS status = NtQueueApcThread( hthread, call_user_apc, (ULONG_PTR)func, data, 0 );
273 if (status) SetLastError( RtlNtStatusToDosError(status) );
278 /**********************************************************************
279 * GetThreadTimes [KERNEL32.@] Obtains timing information.
285 BOOL WINAPI GetThreadTimes(
286 HANDLE thread, /* [in] Specifies the thread of interest */
287 LPFILETIME creationtime, /* [out] When the thread was created */
288 LPFILETIME exittime, /* [out] When the thread was destroyed */
289 LPFILETIME kerneltime, /* [out] Time thread spent in kernel mode */
290 LPFILETIME usertime) /* [out] Time thread spent in user mode */
294 if (creationtime || exittime)
296 /* We need to do a server call to get the creation time or exit time */
297 /* This works on any thread */
299 SERVER_START_REQ( get_thread_info )
301 req->handle = thread;
303 if ((ret = !wine_server_call_err( req )))
306 RtlSecondsSince1970ToTime( reply->creation_time, (LARGE_INTEGER*)creationtime );
308 RtlSecondsSince1970ToTime( reply->exit_time, (LARGE_INTEGER*)exittime );
313 if (ret && (kerneltime || usertime))
315 /* We call times(2) for kernel time or user time */
316 /* We can only (portably) do this for the current thread */
317 if (thread == GetCurrentThread())
321 long clocks_per_sec = sysconf(_SC_CLK_TCK);
326 time = (ULONGLONG)time_buf.tms_stime * 10000000 / clocks_per_sec;
327 kerneltime->dwHighDateTime = time >> 32;
328 kerneltime->dwLowDateTime = (DWORD)time;
332 time = (ULONGLONG)time_buf.tms_utime * 10000000 / clocks_per_sec;
333 usertime->dwHighDateTime = time >> 32;
334 usertime->dwLowDateTime = (DWORD)time;
339 if (kerneltime) kerneltime->dwHighDateTime = kerneltime->dwLowDateTime = 0;
340 if (usertime) usertime->dwHighDateTime = usertime->dwLowDateTime = 0;
341 FIXME("Cannot get kerneltime or usertime of other threads\n");
348 /**********************************************************************
349 * VWin32_BoostThreadGroup [KERNEL.535]
351 VOID WINAPI VWin32_BoostThreadGroup( DWORD threadId, INT boost )
353 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
357 /**********************************************************************
358 * VWin32_BoostThreadStatic [KERNEL.536]
360 VOID WINAPI VWin32_BoostThreadStatic( DWORD threadId, INT boost )
362 FIXME("(0x%08lx,%d): stub\n", threadId, boost);
366 /***********************************************************************
367 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
370 * Pseudohandle for the current thread
372 #undef GetCurrentThread
373 HANDLE WINAPI GetCurrentThread(void)
375 return (HANDLE)0xfffffffe;