2 * Process synchronisation
4 * Copyright 1996, 1997, 1998 Marcus Meissner
5 * Copyright 1997, 1999 Alexandre Julliard
6 * Copyright 1999, 2000 Juergen Schmied
7 * Copyright 2003 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
35 #ifdef HAVE_SYS_POLL_H
36 # include <sys/poll.h>
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
54 #define WIN32_NO_STATUS
57 #include "wine/server.h"
58 #include "wine/debug.h"
59 #include "ntdll_misc.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(ntdll
);
68 /******************************************************************************
69 * NtCreateSemaphore (NTDLL.@)
71 NTSTATUS WINAPI
NtCreateSemaphore( OUT PHANDLE SemaphoreHandle
,
72 IN ACCESS_MASK access
,
73 IN
const OBJECT_ATTRIBUTES
*attr OPTIONAL
,
75 IN LONG MaximumCount
)
77 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
80 if (MaximumCount
<= 0 || InitialCount
< 0 || InitialCount
> MaximumCount
)
81 return STATUS_INVALID_PARAMETER
;
82 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
84 SERVER_START_REQ( create_semaphore
)
87 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
88 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
89 req
->initial
= InitialCount
;
90 req
->max
= MaximumCount
;
91 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
92 ret
= wine_server_call( req
);
93 *SemaphoreHandle
= reply
->handle
;
99 /******************************************************************************
100 * NtOpenSemaphore (NTDLL.@)
102 NTSTATUS WINAPI
NtOpenSemaphore( OUT PHANDLE SemaphoreHandle
,
103 IN ACCESS_MASK access
,
104 IN
const OBJECT_ATTRIBUTES
*attr
)
106 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
109 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
111 SERVER_START_REQ( open_semaphore
)
113 req
->access
= access
;
114 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
115 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
116 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
117 ret
= wine_server_call( req
);
118 *SemaphoreHandle
= reply
->handle
;
124 /******************************************************************************
125 * NtQuerySemaphore (NTDLL.@)
127 NTSTATUS WINAPI
NtQuerySemaphore(
128 HANDLE SemaphoreHandle
,
129 SEMAPHORE_INFORMATION_CLASS SemaphoreInformationClass
,
130 PVOID SemaphoreInformation
,
134 FIXME("(%p,%d,%p,0x%08x,%p) stub!\n",
135 SemaphoreHandle
, SemaphoreInformationClass
, SemaphoreInformation
, Length
, ReturnLength
);
136 return STATUS_SUCCESS
;
139 /******************************************************************************
140 * NtReleaseSemaphore (NTDLL.@)
142 NTSTATUS WINAPI
NtReleaseSemaphore( HANDLE handle
, ULONG count
, PULONG previous
)
145 SERVER_START_REQ( release_semaphore
)
147 req
->handle
= handle
;
149 if (!(ret
= wine_server_call( req
)))
151 if (previous
) *previous
= reply
->prev_count
;
162 /**************************************************************************
163 * NtCreateEvent (NTDLL.@)
164 * ZwCreateEvent (NTDLL.@)
166 NTSTATUS WINAPI
NtCreateEvent(
167 OUT PHANDLE EventHandle
,
168 IN ACCESS_MASK DesiredAccess
,
169 IN
const OBJECT_ATTRIBUTES
*attr
,
170 IN BOOLEAN ManualReset
,
171 IN BOOLEAN InitialState
)
173 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
176 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
178 SERVER_START_REQ( create_event
)
180 req
->access
= DesiredAccess
;
181 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
182 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
183 req
->manual_reset
= ManualReset
;
184 req
->initial_state
= InitialState
;
185 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
186 ret
= wine_server_call( req
);
187 *EventHandle
= reply
->handle
;
193 /******************************************************************************
194 * NtOpenEvent (NTDLL.@)
195 * ZwOpenEvent (NTDLL.@)
197 NTSTATUS WINAPI
NtOpenEvent(
198 OUT PHANDLE EventHandle
,
199 IN ACCESS_MASK DesiredAccess
,
200 IN
const OBJECT_ATTRIBUTES
*attr
)
202 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
205 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
207 SERVER_START_REQ( open_event
)
209 req
->access
= DesiredAccess
;
210 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
211 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
212 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
213 ret
= wine_server_call( req
);
214 *EventHandle
= reply
->handle
;
221 /******************************************************************************
222 * NtSetEvent (NTDLL.@)
223 * ZwSetEvent (NTDLL.@)
225 NTSTATUS WINAPI
NtSetEvent( HANDLE handle
, PULONG NumberOfThreadsReleased
)
229 /* FIXME: set NumberOfThreadsReleased */
231 SERVER_START_REQ( event_op
)
233 req
->handle
= handle
;
235 ret
= wine_server_call( req
);
241 /******************************************************************************
242 * NtResetEvent (NTDLL.@)
244 NTSTATUS WINAPI
NtResetEvent( HANDLE handle
, PULONG NumberOfThreadsReleased
)
248 /* resetting an event can't release any thread... */
249 if (NumberOfThreadsReleased
) *NumberOfThreadsReleased
= 0;
251 SERVER_START_REQ( event_op
)
253 req
->handle
= handle
;
254 req
->op
= RESET_EVENT
;
255 ret
= wine_server_call( req
);
261 /******************************************************************************
262 * NtClearEvent (NTDLL.@)
265 * same as NtResetEvent ???
267 NTSTATUS WINAPI
NtClearEvent ( HANDLE handle
)
269 return NtResetEvent( handle
, NULL
);
272 /******************************************************************************
273 * NtPulseEvent (NTDLL.@)
278 NTSTATUS WINAPI
NtPulseEvent( HANDLE handle
, PULONG PulseCount
)
283 FIXME("(%p,%d)\n", handle
, *PulseCount
);
285 SERVER_START_REQ( event_op
)
287 req
->handle
= handle
;
288 req
->op
= PULSE_EVENT
;
289 ret
= wine_server_call( req
);
295 /******************************************************************************
296 * NtQueryEvent (NTDLL.@)
298 NTSTATUS WINAPI
NtQueryEvent (
299 IN HANDLE EventHandle
,
300 IN EVENT_INFORMATION_CLASS EventInformationClass
,
301 OUT PVOID EventInformation
,
302 IN ULONG EventInformationLength
,
303 OUT PULONG ReturnLength
)
305 FIXME("(%p)\n", EventHandle
);
306 return STATUS_SUCCESS
;
310 * Mutants (known as Mutexes in Kernel32)
313 /******************************************************************************
314 * NtCreateMutant [NTDLL.@]
315 * ZwCreateMutant [NTDLL.@]
317 NTSTATUS WINAPI
NtCreateMutant(OUT HANDLE
* MutantHandle
,
318 IN ACCESS_MASK access
,
319 IN
const OBJECT_ATTRIBUTES
* attr OPTIONAL
,
320 IN BOOLEAN InitialOwner
)
323 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
325 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
327 SERVER_START_REQ( create_mutex
)
329 req
->access
= access
;
330 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
331 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
332 req
->owned
= InitialOwner
;
333 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
334 status
= wine_server_call( req
);
335 *MutantHandle
= reply
->handle
;
341 /**************************************************************************
342 * NtOpenMutant [NTDLL.@]
343 * ZwOpenMutant [NTDLL.@]
345 NTSTATUS WINAPI
NtOpenMutant(OUT HANDLE
* MutantHandle
,
346 IN ACCESS_MASK access
,
347 IN
const OBJECT_ATTRIBUTES
* attr
)
350 DWORD len
= attr
&& attr
->ObjectName
? attr
->ObjectName
->Length
: 0;
352 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
354 SERVER_START_REQ( open_mutex
)
356 req
->access
= access
;
357 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
358 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
359 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
360 status
= wine_server_call( req
);
361 *MutantHandle
= reply
->handle
;
367 /**************************************************************************
368 * NtReleaseMutant [NTDLL.@]
369 * ZwReleaseMutant [NTDLL.@]
371 NTSTATUS WINAPI
NtReleaseMutant( IN HANDLE handle
, OUT PLONG prev_count OPTIONAL
)
375 SERVER_START_REQ( release_mutex
)
377 req
->handle
= handle
;
378 status
= wine_server_call( req
);
379 if (prev_count
) *prev_count
= reply
->prev_count
;
385 /******************************************************************
386 * NtQueryMutant [NTDLL.@]
387 * ZwQueryMutant [NTDLL.@]
389 NTSTATUS WINAPI
NtQueryMutant(IN HANDLE handle
,
390 IN MUTANT_INFORMATION_CLASS MutantInformationClass
,
391 OUT PVOID MutantInformation
,
392 IN ULONG MutantInformationLength
,
393 OUT PULONG ResultLength OPTIONAL
)
395 FIXME("(%p %u %p %u %p): stub!\n",
396 handle
, MutantInformationClass
, MutantInformation
, MutantInformationLength
, ResultLength
);
397 return STATUS_NOT_IMPLEMENTED
;
404 /**************************************************************************
405 * NtCreateTimer [NTDLL.@]
406 * ZwCreateTimer [NTDLL.@]
408 NTSTATUS WINAPI
NtCreateTimer(OUT HANDLE
*handle
,
409 IN ACCESS_MASK access
,
410 IN
const OBJECT_ATTRIBUTES
*attr OPTIONAL
,
411 IN TIMER_TYPE timer_type
)
413 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
416 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
418 if (timer_type
!= NotificationTimer
&& timer_type
!= SynchronizationTimer
)
419 return STATUS_INVALID_PARAMETER
;
421 SERVER_START_REQ( create_timer
)
423 req
->access
= access
;
424 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
425 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
426 req
->manual
= (timer_type
== NotificationTimer
) ? TRUE
: FALSE
;
427 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
428 status
= wine_server_call( req
);
429 *handle
= reply
->handle
;
436 /**************************************************************************
437 * NtOpenTimer [NTDLL.@]
438 * ZwOpenTimer [NTDLL.@]
440 NTSTATUS WINAPI
NtOpenTimer(OUT PHANDLE handle
,
441 IN ACCESS_MASK access
,
442 IN
const OBJECT_ATTRIBUTES
* attr
)
444 DWORD len
= (attr
&& attr
->ObjectName
) ? attr
->ObjectName
->Length
: 0;
447 if (len
>= MAX_PATH
* sizeof(WCHAR
)) return STATUS_NAME_TOO_LONG
;
449 SERVER_START_REQ( open_timer
)
451 req
->access
= access
;
452 req
->attributes
= (attr
) ? attr
->Attributes
: 0;
453 req
->rootdir
= attr
? attr
->RootDirectory
: 0;
454 if (len
) wine_server_add_data( req
, attr
->ObjectName
->Buffer
, len
);
455 status
= wine_server_call( req
);
456 *handle
= reply
->handle
;
462 /**************************************************************************
463 * NtSetTimer [NTDLL.@]
464 * ZwSetTimer [NTDLL.@]
466 NTSTATUS WINAPI
NtSetTimer(IN HANDLE handle
,
467 IN
const LARGE_INTEGER
* when
,
468 IN PTIMER_APC_ROUTINE callback
,
469 IN PVOID callback_arg
,
471 IN ULONG period OPTIONAL
,
472 OUT PBOOLEAN state OPTIONAL
)
474 NTSTATUS status
= STATUS_SUCCESS
;
476 TRACE("(%p,%p,%p,%p,%08x,0x%08x,%p) stub\n",
477 handle
, when
, callback
, callback_arg
, resume
, period
, state
);
479 SERVER_START_REQ( set_timer
)
481 req
->handle
= handle
;
482 req
->period
= period
;
483 req
->expire
= when
->QuadPart
;
484 req
->callback
= callback
;
485 req
->arg
= callback_arg
;
486 status
= wine_server_call( req
);
487 if (state
) *state
= reply
->signaled
;
491 /* set error but can still succeed */
492 if (resume
&& status
== STATUS_SUCCESS
) return STATUS_TIMER_RESUME_IGNORED
;
496 /**************************************************************************
497 * NtCancelTimer [NTDLL.@]
498 * ZwCancelTimer [NTDLL.@]
500 NTSTATUS WINAPI
NtCancelTimer(IN HANDLE handle
, OUT BOOLEAN
* state
)
504 SERVER_START_REQ( cancel_timer
)
506 req
->handle
= handle
;
507 status
= wine_server_call( req
);
508 if (state
) *state
= reply
->signaled
;
514 /******************************************************************************
515 * NtQueryTimer (NTDLL.@)
517 * Retrieves information about a timer.
520 * TimerHandle [I] The timer to retrieve information about.
521 * TimerInformationClass [I] The type of information to retrieve.
522 * TimerInformation [O] Pointer to buffer to store information in.
523 * Length [I] The length of the buffer pointed to by TimerInformation.
524 * ReturnLength [O] Optional. The size of buffer actually used.
527 * Success: STATUS_SUCCESS
528 * Failure: STATUS_INFO_LENGTH_MISMATCH, if Length doesn't match the required data
529 * size for the class specified.
530 * STATUS_INVALID_INFO_CLASS, if an invalid TimerInformationClass was specified.
531 * STATUS_ACCESS_DENIED, if TimerHandle does not have TIMER_QUERY_STATE access
534 NTSTATUS WINAPI
NtQueryTimer(
536 TIMER_INFORMATION_CLASS TimerInformationClass
,
537 PVOID TimerInformation
,
541 TIMER_BASIC_INFORMATION
* basic_info
= (TIMER_BASIC_INFORMATION
*)TimerInformation
;
545 TRACE("(%p,%d,%p,0x%08x,%p)\n", TimerHandle
, TimerInformationClass
,
546 TimerInformation
, Length
, ReturnLength
);
548 switch (TimerInformationClass
)
550 case TimerBasicInformation
:
551 if (Length
< sizeof(TIMER_BASIC_INFORMATION
))
552 return STATUS_INFO_LENGTH_MISMATCH
;
554 SERVER_START_REQ(get_timer_info
)
556 req
->handle
= TimerHandle
;
557 status
= wine_server_call(req
);
559 /* convert server time to absolute NTDLL time */
560 basic_info
->RemainingTime
.QuadPart
= reply
->when
;
561 basic_info
->TimerState
= reply
->signaled
;
565 /* convert from absolute into relative time */
566 NtQuerySystemTime(&now
);
567 if (now
.QuadPart
> basic_info
->RemainingTime
.QuadPart
)
568 basic_info
->RemainingTime
.QuadPart
= 0;
570 basic_info
->RemainingTime
.QuadPart
-= now
.QuadPart
;
572 if (ReturnLength
) *ReturnLength
= sizeof(TIMER_BASIC_INFORMATION
);
577 FIXME("Unhandled class %d\n", TimerInformationClass
);
578 return STATUS_INVALID_INFO_CLASS
;
582 /******************************************************************************
583 * NtQueryTimerResolution [NTDLL.@]
585 NTSTATUS WINAPI
NtQueryTimerResolution(OUT ULONG
* min_resolution
,
586 OUT ULONG
* max_resolution
,
587 OUT ULONG
* current_resolution
)
589 FIXME("(%p,%p,%p), stub!\n",
590 min_resolution
, max_resolution
, current_resolution
);
592 return STATUS_NOT_IMPLEMENTED
;
595 /******************************************************************************
596 * NtSetTimerResolution [NTDLL.@]
598 NTSTATUS WINAPI
NtSetTimerResolution(IN ULONG resolution
,
599 IN BOOLEAN set_resolution
,
600 OUT ULONG
* current_resolution
)
602 FIXME("(%u,%u,%p), stub!\n",
603 resolution
, set_resolution
, current_resolution
);
605 return STATUS_NOT_IMPLEMENTED
;
609 /***********************************************************************
612 * Wait for a reply on the waiting pipe of the current thread.
614 static int wait_reply( void *cookie
)
617 struct wake_up_reply reply
;
621 ret
= read( ntdll_get_thread_data()->wait_fd
[0], &reply
, sizeof(reply
) );
622 if (ret
== sizeof(reply
))
624 if (!reply
.cookie
) break; /* thread got killed */
625 if (reply
.cookie
== cookie
) return reply
.signaled
;
626 /* we stole another reply, wait for the real one */
627 signaled
= wait_reply( cookie
);
628 /* and now put the wrong one back in the pipe */
631 ret
= write( ntdll_get_thread_data()->wait_fd
[1], &reply
, sizeof(reply
) );
632 if (ret
== sizeof(reply
)) break;
633 if (ret
>= 0) server_protocol_error( "partial wakeup write %d\n", ret
);
634 if (errno
== EINTR
) continue;
635 server_protocol_perror("wakeup write");
639 if (ret
>= 0) server_protocol_error( "partial wakeup read %d\n", ret
);
640 if (errno
== EINTR
) continue;
641 server_protocol_perror("wakeup read");
643 /* the server closed the connection; time to die... */
644 server_abort_thread(0);
648 /***********************************************************************
651 * Invoke a single APC. Return TRUE if a user APC has been run.
653 static BOOL
invoke_apc( const apc_call_t
*call
, apc_result_t
*result
)
655 BOOL user_apc
= FALSE
;
657 memset( result
, 0, sizeof(*result
) );
664 call
->user
.func( call
->user
.args
[0], call
->user
.args
[1], call
->user
.args
[2] );
668 call
->timer
.func( call
->timer
.arg
, (DWORD
)call
->timer
.time
, (DWORD
)(call
->timer
.time
>> 32) );
672 result
->type
= call
->type
;
673 result
->async_io
.status
= call
->async_io
.func( call
->async_io
.user
,
675 call
->async_io
.status
);
677 case APC_VIRTUAL_ALLOC
:
678 result
->type
= call
->type
;
679 result
->virtual_alloc
.addr
= call
->virtual_alloc
.addr
;
680 result
->virtual_alloc
.size
= call
->virtual_alloc
.size
;
681 result
->virtual_alloc
.status
= NtAllocateVirtualMemory( NtCurrentProcess(),
682 &result
->virtual_alloc
.addr
,
683 call
->virtual_alloc
.zero_bits
,
684 &result
->virtual_alloc
.size
,
685 call
->virtual_alloc
.op_type
,
686 call
->virtual_alloc
.prot
);
688 case APC_VIRTUAL_FREE
:
689 result
->type
= call
->type
;
690 result
->virtual_free
.addr
= call
->virtual_free
.addr
;
691 result
->virtual_free
.size
= call
->virtual_free
.size
;
692 result
->virtual_free
.status
= NtFreeVirtualMemory( NtCurrentProcess(),
693 &result
->virtual_free
.addr
,
694 &result
->virtual_free
.size
,
695 call
->virtual_free
.op_type
);
697 case APC_VIRTUAL_QUERY
:
699 MEMORY_BASIC_INFORMATION info
;
700 result
->type
= call
->type
;
701 result
->virtual_query
.status
= NtQueryVirtualMemory( NtCurrentProcess(),
702 call
->virtual_query
.addr
,
703 MemoryBasicInformation
, &info
,
704 sizeof(info
), NULL
);
705 if (result
->virtual_query
.status
== STATUS_SUCCESS
)
707 result
->virtual_query
.base
= info
.BaseAddress
;
708 result
->virtual_query
.alloc_base
= info
.AllocationBase
;
709 result
->virtual_query
.size
= info
.RegionSize
;
710 result
->virtual_query
.state
= info
.State
;
711 result
->virtual_query
.prot
= info
.Protect
;
712 result
->virtual_query
.alloc_prot
= info
.AllocationProtect
;
713 result
->virtual_query
.alloc_type
= info
.Type
;
717 case APC_VIRTUAL_PROTECT
:
718 result
->type
= call
->type
;
719 result
->virtual_protect
.addr
= call
->virtual_protect
.addr
;
720 result
->virtual_protect
.size
= call
->virtual_protect
.size
;
721 result
->virtual_protect
.status
= NtProtectVirtualMemory( NtCurrentProcess(),
722 &result
->virtual_protect
.addr
,
723 &result
->virtual_protect
.size
,
724 call
->virtual_protect
.prot
,
725 &result
->virtual_protect
.prot
);
727 case APC_VIRTUAL_FLUSH
:
728 result
->type
= call
->type
;
729 result
->virtual_flush
.addr
= call
->virtual_flush
.addr
;
730 result
->virtual_flush
.size
= call
->virtual_flush
.size
;
731 result
->virtual_flush
.status
= NtFlushVirtualMemory( NtCurrentProcess(),
732 &result
->virtual_flush
.addr
,
733 &result
->virtual_flush
.size
, 0 );
735 case APC_VIRTUAL_LOCK
:
736 result
->type
= call
->type
;
737 result
->virtual_lock
.addr
= call
->virtual_lock
.addr
;
738 result
->virtual_lock
.size
= call
->virtual_lock
.size
;
739 result
->virtual_lock
.status
= NtLockVirtualMemory( NtCurrentProcess(),
740 &result
->virtual_lock
.addr
,
741 &result
->virtual_lock
.size
, 0 );
743 case APC_VIRTUAL_UNLOCK
:
744 result
->type
= call
->type
;
745 result
->virtual_unlock
.addr
= call
->virtual_unlock
.addr
;
746 result
->virtual_unlock
.size
= call
->virtual_unlock
.size
;
747 result
->virtual_unlock
.status
= NtUnlockVirtualMemory( NtCurrentProcess(),
748 &result
->virtual_unlock
.addr
,
749 &result
->virtual_unlock
.size
, 0 );
753 LARGE_INTEGER offset
;
754 result
->type
= call
->type
;
755 result
->map_view
.addr
= call
->map_view
.addr
;
756 result
->map_view
.size
= call
->map_view
.size
;
757 offset
.u
.LowPart
= call
->map_view
.offset_low
;
758 offset
.u
.HighPart
= call
->map_view
.offset_high
;
759 result
->map_view
.status
= NtMapViewOfSection( call
->map_view
.handle
, NtCurrentProcess(),
760 &result
->map_view
.addr
, call
->map_view
.zero_bits
,
761 0, &offset
, &result
->map_view
.size
, ViewShare
,
762 call
->map_view
.alloc_type
, call
->map_view
.prot
);
763 NtClose( call
->map_view
.handle
);
767 result
->type
= call
->type
;
768 result
->unmap_view
.status
= NtUnmapViewOfSection( NtCurrentProcess(), call
->unmap_view
.addr
);
770 case APC_CREATE_THREAD
:
773 result
->type
= call
->type
;
774 result
->create_thread
.status
= RtlCreateUserThread( NtCurrentProcess(), NULL
,
775 call
->create_thread
.suspend
, NULL
,
776 call
->create_thread
.reserve
,
777 call
->create_thread
.commit
,
778 call
->create_thread
.func
,
779 call
->create_thread
.arg
,
780 &result
->create_thread
.handle
, &id
);
781 result
->create_thread
.tid
= HandleToULong(id
.UniqueThread
);
785 server_protocol_error( "get_apc_request: bad type %d\n", call
->type
);
791 /***********************************************************************
792 * NTDLL_queue_process_apc
794 NTSTATUS
NTDLL_queue_process_apc( HANDLE process
, const apc_call_t
*call
, apc_result_t
*result
)
802 SERVER_START_REQ( queue_apc
)
804 req
->process
= process
;
806 if (!(ret
= wine_server_call( req
)))
808 handle
= reply
->handle
;
813 if (ret
!= STATUS_SUCCESS
) return ret
;
817 invoke_apc( call
, result
);
821 NtWaitForSingleObject( handle
, FALSE
, NULL
);
823 SERVER_START_REQ( get_apc_result
)
825 req
->handle
= handle
;
826 if (!(ret
= wine_server_call( req
))) *result
= reply
->result
;
830 if (!ret
&& result
->type
== APC_NONE
) continue; /* APC didn't run, try again */
831 if (ret
) NtClose( handle
);
838 /***********************************************************************
839 * NTDLL_wait_for_multiple_objects
841 * Implementation of NtWaitForMultipleObjects
843 NTSTATUS
NTDLL_wait_for_multiple_objects( UINT count
, const HANDLE
*handles
, UINT flags
,
844 const LARGE_INTEGER
*timeout
, HANDLE signal_object
)
848 BOOL user_apc
= FALSE
;
849 obj_handle_t apc_handle
= 0;
852 timeout_t abs_timeout
= timeout
? timeout
->QuadPart
: TIMEOUT_INFINITE
;
854 memset( &result
, 0, sizeof(result
) );
858 SERVER_START_REQ( select
)
861 req
->cookie
= &cookie
;
862 req
->signal
= signal_object
;
863 req
->prev_apc
= apc_handle
;
864 req
->timeout
= abs_timeout
;
865 wine_server_add_data( req
, &result
, sizeof(result
) );
866 wine_server_add_data( req
, handles
, count
* sizeof(HANDLE
) );
867 ret
= wine_server_call( req
);
868 abs_timeout
= reply
->timeout
;
869 apc_handle
= reply
->apc_handle
;
873 if (ret
== STATUS_PENDING
) ret
= wait_reply( &cookie
);
874 if (ret
!= STATUS_USER_APC
) break;
875 if (invoke_apc( &call
, &result
))
877 /* if we ran a user apc we have to check once more if an object got signaled,
878 * but we don't want to wait */
882 signal_object
= 0; /* don't signal it multiple times */
885 if (ret
== STATUS_TIMEOUT
&& user_apc
) ret
= STATUS_USER_APC
;
887 /* A test on Windows 2000 shows that Windows always yields during
888 a wait, but a wait that is hit by an event gets a priority
889 boost as well. This seems to model that behavior the closest. */
890 if (ret
== STATUS_TIMEOUT
) NtYieldExecution();
896 /* wait operations */
898 /******************************************************************
899 * NtWaitForMultipleObjects (NTDLL.@)
901 NTSTATUS WINAPI
NtWaitForMultipleObjects( DWORD count
, const HANDLE
*handles
,
902 BOOLEAN wait_all
, BOOLEAN alertable
,
903 const LARGE_INTEGER
*timeout
)
905 UINT flags
= SELECT_INTERRUPTIBLE
;
907 if (!count
|| count
> MAXIMUM_WAIT_OBJECTS
) return STATUS_INVALID_PARAMETER_1
;
909 if (wait_all
) flags
|= SELECT_ALL
;
910 if (alertable
) flags
|= SELECT_ALERTABLE
;
911 return NTDLL_wait_for_multiple_objects( count
, handles
, flags
, timeout
, 0 );
915 /******************************************************************
916 * NtWaitForSingleObject (NTDLL.@)
918 NTSTATUS WINAPI
NtWaitForSingleObject(HANDLE handle
, BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
920 return NtWaitForMultipleObjects( 1, &handle
, FALSE
, alertable
, timeout
);
924 /******************************************************************
925 * NtSignalAndWaitForSingleObject (NTDLL.@)
927 NTSTATUS WINAPI
NtSignalAndWaitForSingleObject( HANDLE hSignalObject
, HANDLE hWaitObject
,
928 BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
930 UINT flags
= SELECT_INTERRUPTIBLE
;
932 if (!hSignalObject
) return STATUS_INVALID_HANDLE
;
933 if (alertable
) flags
|= SELECT_ALERTABLE
;
934 return NTDLL_wait_for_multiple_objects( 1, &hWaitObject
, flags
, timeout
, hSignalObject
);
938 /******************************************************************
939 * NtYieldExecution (NTDLL.@)
941 NTSTATUS WINAPI
NtYieldExecution(void)
943 #ifdef HAVE_SCHED_YIELD
945 return STATUS_SUCCESS
;
947 return STATUS_NO_YIELD_PERFORMED
;
952 /******************************************************************
953 * NtDelayExecution (NTDLL.@)
955 NTSTATUS WINAPI
NtDelayExecution( BOOLEAN alertable
, const LARGE_INTEGER
*timeout
)
957 /* if alertable, we need to query the server */
959 return NTDLL_wait_for_multiple_objects( 0, NULL
, SELECT_INTERRUPTIBLE
| SELECT_ALERTABLE
,
962 if (!timeout
|| timeout
->QuadPart
== TIMEOUT_INFINITE
) /* sleep forever */
964 for (;;) select( 0, NULL
, NULL
, NULL
, NULL
);
969 timeout_t when
, diff
;
971 if ((when
= timeout
->QuadPart
) < 0)
973 NtQuerySystemTime( &now
);
974 when
= now
.QuadPart
- when
;
977 /* Note that we yield after establishing the desired timeout */
979 if (!when
) return STATUS_SUCCESS
;
984 NtQuerySystemTime( &now
);
985 diff
= (when
- now
.QuadPart
+ 9) / 10;
986 if (diff
<= 0) break;
987 tv
.tv_sec
= diff
/ 1000000;
988 tv
.tv_usec
= diff
% 1000000;
989 if (select( 0, NULL
, NULL
, NULL
, &tv
) != -1) break;
992 return STATUS_SUCCESS
;
995 /******************************************************************
996 * NtCreateIoCompletion (NTDLL.@)
997 * ZwCreateIoCompletion (NTDLL.@)
999 * Creates I/O completion object.
1002 * CompletionPort [O] created completion object handle will be placed there
1003 * DesiredAccess [I] desired access to a handle (combination of IO_COMPLETION_*)
1004 * ObjectAttributes [I] completion object attributes
1005 * NumberOfConcurrentThreads [I] desired number of concurrent active worker threads
1008 NTSTATUS WINAPI
NtCreateIoCompletion( PHANDLE CompletionPort
, ACCESS_MASK DesiredAccess
,
1009 POBJECT_ATTRIBUTES ObjectAttributes
, ULONG NumberOfConcurrentThreads
)
1011 FIXME("(%p, %x, %p, %d)\n", CompletionPort
, DesiredAccess
,
1012 ObjectAttributes
, NumberOfConcurrentThreads
);
1013 return STATUS_NOT_IMPLEMENTED
;
1016 /******************************************************************
1017 * NtSetIoCompletion (NTDLL.@)
1018 * ZwSetIoCompletion (NTDLL.@)
1020 * Inserts completion message into queue
1023 * CompletionPort [I] HANDLE to completion object
1024 * CompletionKey [I] completion key
1025 * CompletionValue [I] completion value (usually pointer to OVERLAPPED)
1026 * Status [I] operation status
1027 * NumberOfBytesTransferred [I] number of bytes transferred
1029 NTSTATUS WINAPI
NtSetIoCompletion( HANDLE CompletionPort
, ULONG_PTR CompletionKey
,
1030 ULONG_PTR CompletionValue
, NTSTATUS Status
,
1031 ULONG NumberOfBytesToTransfer
)
1033 FIXME("(%p, %lx, %lx, %x, %d)\n", CompletionPort
, CompletionKey
,
1034 CompletionValue
, Status
, NumberOfBytesToTransfer
);
1035 return STATUS_NOT_IMPLEMENTED
;
1038 /******************************************************************
1039 * NtRemoveIoCompletion (NTDLL.@)
1040 * ZwRemoveIoCompletion (NTDLL.@)
1042 * (Wait for and) retrieve first completion message from completion object's queue
1045 * CompletionPort [I] HANDLE to I/O completion object
1046 * CompletionKey [O] completion key
1047 * CompletionValue [O] Completion value given in NtSetIoCompletion or in async operation
1048 * iosb [O] IO_STATUS_BLOCK of completed asynchronous operation
1049 * WaitTime [I] optional wait time in NTDLL format
1052 NTSTATUS WINAPI
NtRemoveIoCompletion( HANDLE CompletionPort
, PULONG_PTR CompletionKey
,
1053 PULONG_PTR CompletionValue
, PIO_STATUS_BLOCK iosb
,
1054 PLARGE_INTEGER WaitTime
)
1056 FIXME("(%p, %p, %p, %p, %p)\n", CompletionPort
, CompletionKey
,
1057 CompletionValue
, iosb
, WaitTime
);
1058 return STATUS_NOT_IMPLEMENTED
;
1061 /******************************************************************
1062 * NtOpenIoCompletion (NTDLL.@)
1063 * ZwOpenIoCompletion (NTDLL.@)
1065 * Opens I/O completion object
1068 * CompletionPort [O] completion object handle will be placed there
1069 * DesiredAccess [I] desired access to a handle (combination of IO_COMPLETION_*)
1070 * ObjectAttributes [I] completion object name
1073 NTSTATUS WINAPI
NtOpenIoCompletion( PHANDLE CompletionPort
, ACCESS_MASK DesiredAccess
,
1074 POBJECT_ATTRIBUTES ObjectAttributes
)
1076 FIXME("(%p, 0x%x, %p)\n", CompletionPort
, DesiredAccess
, ObjectAttributes
);
1077 return STATUS_NOT_IMPLEMENTED
;
1080 /******************************************************************
1081 * NtQueryIoCompletion (NTDLL.@)
1082 * ZwQueryIoCompletion (NTDLL.@)
1084 * Requests information about given I/O completion object
1087 * CompletionPort [I] HANDLE to completion port to request
1088 * InformationClass [I] information class
1089 * CompletionInformation [O] user-provided buffer for data
1090 * BufferLength [I] buffer length
1091 * RequiredLength [O] required buffer length
1094 NTSTATUS WINAPI
NtQueryIoCompletion( HANDLE CompletionPort
, IO_COMPLETION_INFORMATION_CLASS InformationClass
,
1095 PVOID CompletionInformation
, ULONG BufferLength
, PULONG RequiredLength
)
1097 FIXME("(%p, %d, %p, 0x%x, %p)\n", CompletionPort
, InformationClass
, CompletionInformation
,
1098 BufferLength
, RequiredLength
);
1099 return STATUS_NOT_IMPLEMENTED
;