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 THREAD_BASIC_INFORMATION info
;
133 NTSTATUS status
= NtQueryInformationThread( hthread
, ThreadBasicInformation
,
134 &info
, sizeof(info
), NULL
);
138 SetLastError( RtlNtStatusToDosError(status
) );
139 return THREAD_PRIORITY_ERROR_RETURN
;
141 return info
.Priority
;
145 /**********************************************************************
146 * SetThreadPriority [KERNEL32.@] Sets priority for thread.
152 BOOL WINAPI
SetThreadPriority(
153 HANDLE hthread
, /* [in] Handle to thread */
154 INT priority
) /* [in] Thread priority level */
157 SERVER_START_REQ( set_thread_info
)
159 req
->handle
= hthread
;
160 req
->priority
= priority
;
161 req
->mask
= SET_THREAD_INFO_PRIORITY
;
162 ret
= !wine_server_call_err( req
);
169 /**********************************************************************
170 * GetThreadPriorityBoost [KERNEL32.@] Returns priority boost for thread.
172 * Always reports that priority boost is disabled.
178 BOOL WINAPI
GetThreadPriorityBoost(
179 HANDLE hthread
, /* [in] Handle to thread */
180 PBOOL pstate
) /* [out] pointer to var that receives the boost state */
182 if (pstate
) *pstate
= FALSE
;
187 /**********************************************************************
188 * SetThreadPriorityBoost [KERNEL32.@] Sets priority boost for thread.
190 * Priority boost is not implemented. Thsi function always returns
191 * FALSE and sets last error to ERROR_CALL_NOT_IMPLEMENTED
194 * Always returns FALSE to indicate a failure
196 BOOL WINAPI
SetThreadPriorityBoost(
197 HANDLE hthread
, /* [in] Handle to thread */
198 BOOL disable
) /* [in] TRUE to disable priority boost */
200 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
205 /**********************************************************************
206 * SetThreadAffinityMask (KERNEL32.@)
208 DWORD WINAPI
SetThreadAffinityMask( HANDLE hThread
, DWORD dwThreadAffinityMask
)
211 SERVER_START_REQ( set_thread_info
)
213 req
->handle
= hThread
;
214 req
->affinity
= dwThreadAffinityMask
;
215 req
->mask
= SET_THREAD_INFO_AFFINITY
;
216 ret
= !wine_server_call_err( req
);
217 /* FIXME: should return previous value */
224 /**********************************************************************
225 * SetThreadIdealProcessor [KERNEL32.@] Obtains timing information.
228 * Success: Value of last call to SetThreadIdealProcessor
231 DWORD WINAPI
SetThreadIdealProcessor(
232 HANDLE hThread
, /* [in] Specifies the thread of interest */
233 DWORD dwIdealProcessor
) /* [in] Specifies the new preferred processor */
235 FIXME("(%p): stub\n",hThread
);
236 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
241 /***********************************************************************
242 * SetThreadExecutionState (KERNEL32.@)
244 * Informs the system that activity is taking place for
245 * power management purposes.
247 EXECUTION_STATE WINAPI
SetThreadExecutionState(EXECUTION_STATE flags
)
249 static EXECUTION_STATE current
=
250 ES_SYSTEM_REQUIRED
|ES_DISPLAY_REQUIRED
|ES_USER_PRESENT
;
251 EXECUTION_STATE old
= current
;
253 if (!(current
& ES_CONTINUOUS
) || (flags
& ES_CONTINUOUS
))
255 FIXME("(0x%lx): stub, harmless (power management).\n", flags
);
260 /* callback for QueueUserAPC */
261 static void CALLBACK
call_user_apc( ULONG_PTR arg1
, ULONG_PTR arg2
, ULONG_PTR arg3
)
263 PAPCFUNC func
= (PAPCFUNC
)arg1
;
267 /***********************************************************************
268 * QueueUserAPC (KERNEL32.@)
270 DWORD WINAPI
QueueUserAPC( PAPCFUNC func
, HANDLE hthread
, ULONG_PTR data
)
272 NTSTATUS status
= NtQueueApcThread( hthread
, call_user_apc
, (ULONG_PTR
)func
, data
, 0 );
274 if (status
) SetLastError( RtlNtStatusToDosError(status
) );
279 /**********************************************************************
280 * GetThreadTimes [KERNEL32.@] Obtains timing information.
286 BOOL WINAPI
GetThreadTimes(
287 HANDLE thread
, /* [in] Specifies the thread of interest */
288 LPFILETIME creationtime
, /* [out] When the thread was created */
289 LPFILETIME exittime
, /* [out] When the thread was destroyed */
290 LPFILETIME kerneltime
, /* [out] Time thread spent in kernel mode */
291 LPFILETIME usertime
) /* [out] Time thread spent in user mode */
295 if (creationtime
|| exittime
)
297 /* We need to do a server call to get the creation time or exit time */
298 /* This works on any thread */
300 SERVER_START_REQ( get_thread_info
)
302 req
->handle
= thread
;
304 if ((ret
= !wine_server_call_err( req
)))
307 RtlSecondsSince1970ToTime( reply
->creation_time
, (LARGE_INTEGER
*)creationtime
);
309 RtlSecondsSince1970ToTime( reply
->exit_time
, (LARGE_INTEGER
*)exittime
);
314 if (ret
&& (kerneltime
|| usertime
))
316 /* We call times(2) for kernel time or user time */
317 /* We can only (portably) do this for the current thread */
318 if (thread
== GetCurrentThread())
322 long clocks_per_sec
= sysconf(_SC_CLK_TCK
);
327 time
= (ULONGLONG
)time_buf
.tms_stime
* 10000000 / clocks_per_sec
;
328 kerneltime
->dwHighDateTime
= time
>> 32;
329 kerneltime
->dwLowDateTime
= (DWORD
)time
;
333 time
= (ULONGLONG
)time_buf
.tms_utime
* 10000000 / clocks_per_sec
;
334 usertime
->dwHighDateTime
= time
>> 32;
335 usertime
->dwLowDateTime
= (DWORD
)time
;
340 if (kerneltime
) kerneltime
->dwHighDateTime
= kerneltime
->dwLowDateTime
= 0;
341 if (usertime
) usertime
->dwHighDateTime
= usertime
->dwLowDateTime
= 0;
342 FIXME("Cannot get kerneltime or usertime of other threads\n");
349 /**********************************************************************
350 * VWin32_BoostThreadGroup [KERNEL.535]
352 VOID WINAPI
VWin32_BoostThreadGroup( DWORD threadId
, INT boost
)
354 FIXME("(0x%08lx,%d): stub\n", threadId
, boost
);
358 /**********************************************************************
359 * VWin32_BoostThreadStatic [KERNEL.536]
361 VOID WINAPI
VWin32_BoostThreadStatic( DWORD threadId
, INT boost
)
363 FIXME("(0x%08lx,%d): stub\n", threadId
, boost
);
367 /***********************************************************************
368 * GetCurrentThread [KERNEL32.@] Gets pseudohandle for current thread
371 * Pseudohandle for the current thread
373 #undef GetCurrentThread
374 HANDLE WINAPI
GetCurrentThread(void)
376 return (HANDLE
)0xfffffffe;