mirrored from https://gitlab.winehq.org/wine/wine.git
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
sync.c
1765 lines (1456 loc) · 58.7 KB
/
sync.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Kernel synchronization objects
*
* Copyright 1998 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "wincon.h"
#include "winerror.h"
#include "winnls.h"
#include "winternl.h"
#include "winioctl.h"
#include "ddk/wdm.h"
#include "kernelbase.h"
#include "wine/asm.h"
#include "wine/exception.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(sync);
static const struct _KUSER_SHARED_DATA *user_shared_data = (struct _KUSER_SHARED_DATA *)0x7ffe0000;
/* check if current version is NT or Win95 */
static inline BOOL is_version_nt(void)
{
return !(GetVersion() & 0x80000000);
}
/* helper for kernel32->ntdll timeout format conversion */
static inline LARGE_INTEGER *get_nt_timeout( LARGE_INTEGER *time, DWORD timeout )
{
if (timeout == INFINITE) return NULL;
time->QuadPart = (ULONGLONG)timeout * -10000;
return time;
}
/***********************************************************************
* BaseGetNamedObjectDirectory (kernelbase.@)
*/
NTSTATUS WINAPI BaseGetNamedObjectDirectory( HANDLE *dir )
{
static HANDLE handle;
WCHAR buffer[64];
UNICODE_STRING str;
OBJECT_ATTRIBUTES attr;
NTSTATUS status = STATUS_SUCCESS;
if (!handle)
{
HANDLE dir;
swprintf( buffer, ARRAY_SIZE(buffer), L"\\Sessions\\%u\\BaseNamedObjects",
NtCurrentTeb()->Peb->SessionId );
RtlInitUnicodeString( &str, buffer );
InitializeObjectAttributes(&attr, &str, 0, 0, NULL);
status = NtOpenDirectoryObject( &dir, DIRECTORY_CREATE_OBJECT|DIRECTORY_TRAVERSE, &attr );
if (!status && InterlockedCompareExchangePointer( &handle, dir, 0 ) != 0)
{
/* someone beat us here... */
CloseHandle( dir );
}
}
*dir = handle;
return status;
}
static void get_create_object_attributes( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *nameW,
SECURITY_ATTRIBUTES *sa, const WCHAR *name )
{
attr->Length = sizeof(*attr);
attr->RootDirectory = 0;
attr->ObjectName = NULL;
attr->Attributes = OBJ_OPENIF | ((sa && sa->bInheritHandle) ? OBJ_INHERIT : 0);
attr->SecurityDescriptor = sa ? sa->lpSecurityDescriptor : NULL;
attr->SecurityQualityOfService = NULL;
if (name)
{
RtlInitUnicodeString( nameW, name );
attr->ObjectName = nameW;
BaseGetNamedObjectDirectory( &attr->RootDirectory );
}
}
static BOOL get_open_object_attributes( OBJECT_ATTRIBUTES *attr, UNICODE_STRING *nameW,
BOOL inherit, const WCHAR *name )
{
HANDLE dir;
if (!name)
{
SetLastError( ERROR_INVALID_PARAMETER );
return FALSE;
}
RtlInitUnicodeString( nameW, name );
BaseGetNamedObjectDirectory( &dir );
InitializeObjectAttributes( attr, nameW, inherit ? OBJ_INHERIT : 0, dir, NULL );
return TRUE;
}
/***********************************************************************
* Time functions
***********************************************************************/
/*********************************************************************
* GetSystemTimes (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH GetSystemTimes( FILETIME *idle, FILETIME *kernel, FILETIME *user )
{
LARGE_INTEGER idle_time, kernel_time, user_time;
SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION *info;
ULONG ret_size;
DWORD i, cpus = NtCurrentTeb()->Peb->NumberOfProcessors;
if (!(info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) * cpus )))
{
SetLastError( ERROR_OUTOFMEMORY );
return FALSE;
}
if (!set_ntstatus( NtQuerySystemInformation( SystemProcessorPerformanceInformation, info,
sizeof(*info) * cpus, &ret_size )))
{
HeapFree( GetProcessHeap(), 0, info );
return FALSE;
}
idle_time.QuadPart = 0;
kernel_time.QuadPart = 0;
user_time.QuadPart = 0;
for (i = 0; i < cpus; i++)
{
idle_time.QuadPart += info[i].IdleTime.QuadPart;
kernel_time.QuadPart += info[i].KernelTime.QuadPart;
user_time.QuadPart += info[i].UserTime.QuadPart;
}
if (idle)
{
idle->dwLowDateTime = idle_time.u.LowPart;
idle->dwHighDateTime = idle_time.u.HighPart;
}
if (kernel)
{
kernel->dwLowDateTime = kernel_time.u.LowPart;
kernel->dwHighDateTime = kernel_time.u.HighPart;
}
if (user)
{
user->dwLowDateTime = user_time.u.LowPart;
user->dwHighDateTime = user_time.u.HighPart;
}
HeapFree( GetProcessHeap(), 0, info );
return TRUE;
}
/******************************************************************************
* GetTickCount (kernelbase.@)
*/
ULONG WINAPI DECLSPEC_HOTPATCH GetTickCount(void)
{
/* note: we ignore TickCountMultiplier */
return user_shared_data->TickCount.LowPart;
}
/******************************************************************************
* GetTickCount64 (kernelbase.@)
*/
ULONGLONG WINAPI DECLSPEC_HOTPATCH GetTickCount64(void)
{
ULONG high, low;
do
{
high = user_shared_data->TickCount.High1Time;
low = user_shared_data->TickCount.LowPart;
}
while (high != user_shared_data->TickCount.High2Time);
/* note: we ignore TickCountMultiplier */
return (ULONGLONG)high << 32 | low;
}
/******************************************************************************
* QueryInterruptTime (kernelbase.@)
*/
void WINAPI DECLSPEC_HOTPATCH QueryInterruptTime( ULONGLONG *time )
{
ULONG high, low;
do
{
high = user_shared_data->InterruptTime.High1Time;
low = user_shared_data->InterruptTime.LowPart;
}
while (high != user_shared_data->InterruptTime.High2Time);
*time = (ULONGLONG)high << 32 | low;
}
/******************************************************************************
* QueryInterruptTimePrecise (kernelbase.@)
*/
void WINAPI DECLSPEC_HOTPATCH QueryInterruptTimePrecise( ULONGLONG *time )
{
static int once;
if (!once++) FIXME( "(%p) semi-stub\n", time );
QueryInterruptTime( time );
}
/***********************************************************************
* QueryUnbiasedInterruptTimePrecise (kernelbase.@)
*/
void WINAPI DECLSPEC_HOTPATCH QueryUnbiasedInterruptTimePrecise( ULONGLONG *time )
{
static int once;
if (!once++) FIXME( "(%p): semi-stub.\n", time );
RtlQueryUnbiasedInterruptTime( time );
}
/***********************************************************************
* QueryIdleProcessorCycleTime (kernelbase.@)
*/
BOOL WINAPI QueryIdleProcessorCycleTime( ULONG *size, ULONG64 *times )
{
ULONG ret_size;
NTSTATUS status = NtQuerySystemInformation( SystemProcessorIdleCycleTimeInformation, times, *size, &ret_size );
if (!*size || !status) *size = ret_size;
return TRUE;
}
/***********************************************************************
* QueryIdleProcessorCycleTimeEx (kernelbase.@)
*/
BOOL WINAPI QueryIdleProcessorCycleTimeEx( USHORT group_id, ULONG *size, ULONG64 *times )
{
ULONG ret_size;
NTSTATUS status = NtQuerySystemInformationEx( SystemProcessorIdleCycleTimeInformation, &group_id, sizeof(group_id),
times, *size, &ret_size );
if (!*size || !status) *size = ret_size;
return TRUE;
}
/***********************************************************************
* Waits
***********************************************************************/
static HANDLE normalize_std_handle( HANDLE handle )
{
if ((handle == (HANDLE)STD_INPUT_HANDLE) ||
(handle == (HANDLE)STD_OUTPUT_HANDLE) ||
(handle == (HANDLE)STD_ERROR_HANDLE))
return GetStdHandle( HandleToULong(handle) );
return handle;
}
/***********************************************************************
* RegisterWaitForSingleObjectEx (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH RegisterWaitForSingleObjectEx( HANDLE handle, WAITORTIMERCALLBACK callback,
PVOID context, ULONG timeout, ULONG flags )
{
HANDLE ret;
TRACE( "%p %p %p %ld %ld\n", handle, callback, context, timeout, flags );
handle = normalize_std_handle( handle );
if (!set_ntstatus( RtlRegisterWait( &ret, handle, callback, context, timeout, flags ))) return NULL;
return ret;
}
/***********************************************************************
* SignalObjectAndWait (kernelbase.@)
*/
DWORD WINAPI DECLSPEC_HOTPATCH SignalObjectAndWait( HANDLE signal, HANDLE wait,
DWORD timeout, BOOL alertable )
{
NTSTATUS status;
LARGE_INTEGER time;
TRACE( "%p %p %ld %d\n", signal, wait, timeout, alertable );
status = NtSignalAndWaitForSingleObject( signal, wait, alertable, get_nt_timeout( &time, timeout ) );
if (HIWORD(status))
{
SetLastError( RtlNtStatusToDosError(status) );
status = WAIT_FAILED;
}
return status;
}
/***********************************************************************
* Sleep (kernelbase.@)
*/
void WINAPI DECLSPEC_HOTPATCH Sleep( DWORD timeout )
{
LARGE_INTEGER time;
NtDelayExecution( FALSE, get_nt_timeout( &time, timeout ) );
}
/******************************************************************************
* SleepEx (kernelbase.@)
*/
DWORD WINAPI DECLSPEC_HOTPATCH SleepEx( DWORD timeout, BOOL alertable )
{
NTSTATUS status;
LARGE_INTEGER time;
status = NtDelayExecution( alertable, get_nt_timeout( &time, timeout ) );
if (status == STATUS_USER_APC) return WAIT_IO_COMPLETION;
return 0;
}
/***********************************************************************
* UnregisterWaitEx (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH UnregisterWaitEx( HANDLE handle, HANDLE event )
{
return set_ntstatus( RtlDeregisterWaitEx( handle, event ));
}
/***********************************************************************
* WaitForSingleObject (kernelbase.@)
*/
DWORD WINAPI DECLSPEC_HOTPATCH WaitForSingleObject( HANDLE handle, DWORD timeout )
{
return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, FALSE );
}
/***********************************************************************
* WaitForSingleObjectEx (kernelbase.@)
*/
DWORD WINAPI DECLSPEC_HOTPATCH WaitForSingleObjectEx( HANDLE handle, DWORD timeout, BOOL alertable )
{
return WaitForMultipleObjectsEx( 1, &handle, FALSE, timeout, alertable );
}
/***********************************************************************
* WaitForMultipleObjects (kernelbase.@)
*/
DWORD WINAPI DECLSPEC_HOTPATCH WaitForMultipleObjects( DWORD count, const HANDLE *handles,
BOOL wait_all, DWORD timeout )
{
return WaitForMultipleObjectsEx( count, handles, wait_all, timeout, FALSE );
}
/***********************************************************************
* WaitForMultipleObjectsEx (kernelbase.@)
*/
DWORD WINAPI DECLSPEC_HOTPATCH WaitForMultipleObjectsEx( DWORD count, const HANDLE *handles,
BOOL wait_all, DWORD timeout, BOOL alertable )
{
NTSTATUS status;
HANDLE hloc[MAXIMUM_WAIT_OBJECTS];
LARGE_INTEGER time;
unsigned int i;
if (count > MAXIMUM_WAIT_OBJECTS)
{
SetLastError(ERROR_INVALID_PARAMETER);
return WAIT_FAILED;
}
for (i = 0; i < count; i++) hloc[i] = normalize_std_handle( handles[i] );
status = NtWaitForMultipleObjects( count, hloc, !wait_all, alertable,
get_nt_timeout( &time, timeout ) );
if (HIWORD(status)) /* is it an error code? */
{
SetLastError( RtlNtStatusToDosError(status) );
status = WAIT_FAILED;
}
return status;
}
/******************************************************************************
* WaitForDebugEvent (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH WaitForDebugEvent( DEBUG_EVENT *event, DWORD timeout )
{
NTSTATUS status;
LARGE_INTEGER time;
DBGUI_WAIT_STATE_CHANGE state;
for (;;)
{
status = DbgUiWaitStateChange( &state, get_nt_timeout( &time, timeout ) );
switch (status)
{
case STATUS_SUCCESS:
/* continue on wide print exceptions to force resending an ANSI one. */
if (state.NewState == DbgExceptionStateChange)
{
DBGKM_EXCEPTION *info = &state.StateInfo.Exception;
DWORD code = info->ExceptionRecord.ExceptionCode;
if (code == DBG_PRINTEXCEPTION_WIDE_C && info->ExceptionRecord.NumberParameters >= 2)
{
DbgUiContinue( &state.AppClientId, DBG_EXCEPTION_NOT_HANDLED );
break;
}
}
DbgUiConvertStateChangeStructure( &state, event );
return TRUE;
case STATUS_USER_APC:
continue;
case STATUS_TIMEOUT:
SetLastError( ERROR_SEM_TIMEOUT );
return FALSE;
default:
return set_ntstatus( status );
}
}
}
/******************************************************************************
* WaitForDebugEventEx (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH WaitForDebugEventEx( DEBUG_EVENT *event, DWORD timeout )
{
NTSTATUS status;
LARGE_INTEGER time;
DBGUI_WAIT_STATE_CHANGE state;
for (;;)
{
status = DbgUiWaitStateChange( &state, get_nt_timeout( &time, timeout ) );
switch (status)
{
case STATUS_SUCCESS:
DbgUiConvertStateChangeStructure( &state, event );
return TRUE;
case STATUS_USER_APC:
continue;
case STATUS_TIMEOUT:
SetLastError( ERROR_SEM_TIMEOUT );
return FALSE;
default:
return set_ntstatus( status );
}
}
}
/***********************************************************************
* WaitOnAddress (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH WaitOnAddress( volatile void *addr, void *cmp, SIZE_T size, DWORD timeout )
{
LARGE_INTEGER to;
if (timeout != INFINITE)
{
to.QuadPart = -(LONGLONG)timeout * 10000;
return set_ntstatus( RtlWaitOnAddress( (const void *)addr, cmp, size, &to ));
}
return set_ntstatus( RtlWaitOnAddress( (const void *)addr, cmp, size, NULL ));
}
/***********************************************************************
* Events
***********************************************************************/
/***********************************************************************
* CreateEventA (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventA( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
BOOL initial_state, LPCSTR name )
{
DWORD flags = 0;
if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
return CreateEventExA( sa, name, flags, EVENT_ALL_ACCESS );
}
/***********************************************************************
* CreateEventW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW( SECURITY_ATTRIBUTES *sa, BOOL manual_reset,
BOOL initial_state, LPCWSTR name )
{
DWORD flags = 0;
if (manual_reset) flags |= CREATE_EVENT_MANUAL_RESET;
if (initial_state) flags |= CREATE_EVENT_INITIAL_SET;
return CreateEventExW( sa, name, flags, EVENT_ALL_ACCESS );
}
/***********************************************************************
* CreateEventExA (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventExA( SECURITY_ATTRIBUTES *sa, LPCSTR name,
DWORD flags, DWORD access )
{
WCHAR buffer[MAX_PATH];
if (!name) return CreateEventExW( sa, NULL, flags, access );
if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
{
SetLastError( ERROR_FILENAME_EXCED_RANGE );
return 0;
}
return CreateEventExW( sa, buffer, flags, access );
}
/***********************************************************************
* CreateEventExW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name,
DWORD flags, DWORD access )
{
HANDLE ret = 0;
UNICODE_STRING nameW;
OBJECT_ATTRIBUTES attr;
NTSTATUS status;
/* one buggy program needs this
* ("Van Dale Groot woordenboek der Nederlandse taal")
*/
__TRY
{
get_create_object_attributes( &attr, &nameW, sa, name );
}
__EXCEPT_PAGE_FAULT
{
SetLastError( ERROR_INVALID_PARAMETER);
return 0;
}
__ENDTRY
status = NtCreateEvent( &ret, access, &attr,
(flags & CREATE_EVENT_MANUAL_RESET) ? NotificationEvent : SynchronizationEvent,
(flags & CREATE_EVENT_INITIAL_SET) != 0 );
if (status == STATUS_OBJECT_NAME_EXISTS)
SetLastError( ERROR_ALREADY_EXISTS );
else
SetLastError( RtlNtStatusToDosError(status) );
return ret;
}
/***********************************************************************
* OpenEventA (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventA( DWORD access, BOOL inherit, LPCSTR name )
{
WCHAR buffer[MAX_PATH];
if (!name) return OpenEventW( access, inherit, NULL );
if (!MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, MAX_PATH ))
{
SetLastError( ERROR_FILENAME_EXCED_RANGE );
return 0;
}
return OpenEventW( access, inherit, buffer );
}
/***********************************************************************
* OpenEventW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventW( DWORD access, BOOL inherit, LPCWSTR name )
{
HANDLE ret;
UNICODE_STRING nameW;
OBJECT_ATTRIBUTES attr;
if (!is_version_nt()) access = EVENT_ALL_ACCESS;
if (!get_open_object_attributes( &attr, &nameW, inherit, name )) return 0;
if (!set_ntstatus( NtOpenEvent( &ret, access, &attr ))) return 0;
return ret;
}
/***********************************************************************
* PulseEvent (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH PulseEvent( HANDLE handle )
{
return set_ntstatus( NtPulseEvent( handle, NULL ));
}
/***********************************************************************
* SetEvent (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent( HANDLE handle )
{
return set_ntstatus( NtSetEvent( handle, NULL ));
}
/***********************************************************************
* ResetEvent (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH ResetEvent( HANDLE handle )
{
return set_ntstatus( NtResetEvent( handle, NULL ));
}
/***********************************************************************
* Mutexes
***********************************************************************/
/***********************************************************************
* CreateMutexA (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexA( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCSTR name )
{
return CreateMutexExA( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
}
/***********************************************************************
* CreateMutexW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexW( SECURITY_ATTRIBUTES *sa, BOOL owner, LPCWSTR name )
{
return CreateMutexExW( sa, name, owner ? CREATE_MUTEX_INITIAL_OWNER : 0, MUTEX_ALL_ACCESS );
}
/***********************************************************************
* CreateMutexExA (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexExA( SECURITY_ATTRIBUTES *sa, LPCSTR name,
DWORD flags, DWORD access )
{
ANSI_STRING nameA;
NTSTATUS status;
if (!name) return CreateMutexExW( sa, NULL, flags, access );
RtlInitAnsiString( &nameA, name );
status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString, &nameA, FALSE );
if (status != STATUS_SUCCESS)
{
SetLastError( ERROR_FILENAME_EXCED_RANGE );
return 0;
}
return CreateMutexExW( sa, NtCurrentTeb()->StaticUnicodeString.Buffer, flags, access );
}
/***********************************************************************
* CreateMutexExW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateMutexExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name,
DWORD flags, DWORD access )
{
HANDLE ret = 0;
UNICODE_STRING nameW;
OBJECT_ATTRIBUTES attr;
NTSTATUS status;
get_create_object_attributes( &attr, &nameW, sa, name );
status = NtCreateMutant( &ret, access, &attr, (flags & CREATE_MUTEX_INITIAL_OWNER) != 0 );
if (status == STATUS_OBJECT_NAME_EXISTS)
SetLastError( ERROR_ALREADY_EXISTS );
else
SetLastError( RtlNtStatusToDosError(status) );
return ret;
}
/***********************************************************************
* OpenMutexW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH OpenMutexW( DWORD access, BOOL inherit, LPCWSTR name )
{
HANDLE ret;
UNICODE_STRING nameW;
OBJECT_ATTRIBUTES attr;
if (!is_version_nt()) access = MUTEX_ALL_ACCESS;
if (!get_open_object_attributes( &attr, &nameW, inherit, name )) return 0;
if (!set_ntstatus( NtOpenMutant( &ret, access, &attr ))) return 0;
return ret;
}
/***********************************************************************
* ReleaseMutex (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH ReleaseMutex( HANDLE handle )
{
return set_ntstatus( NtReleaseMutant( handle, NULL ));
}
/***********************************************************************
* Semaphores
***********************************************************************/
/***********************************************************************
* CreateSemaphoreW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreW( SECURITY_ATTRIBUTES *sa, LONG initial,
LONG max, LPCWSTR name )
{
return CreateSemaphoreExW( sa, initial, max, name, 0, SEMAPHORE_ALL_ACCESS );
}
/***********************************************************************
* CreateSemaphoreExW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateSemaphoreExW( SECURITY_ATTRIBUTES *sa, LONG initial, LONG max,
LPCWSTR name, DWORD flags, DWORD access )
{
HANDLE ret = 0;
UNICODE_STRING nameW;
OBJECT_ATTRIBUTES attr;
NTSTATUS status;
get_create_object_attributes( &attr, &nameW, sa, name );
status = NtCreateSemaphore( &ret, access, &attr, initial, max );
if (status == STATUS_OBJECT_NAME_EXISTS)
SetLastError( ERROR_ALREADY_EXISTS );
else
SetLastError( RtlNtStatusToDosError(status) );
return ret;
}
/***********************************************************************
* OpenSemaphoreW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH OpenSemaphoreW( DWORD access, BOOL inherit, LPCWSTR name )
{
HANDLE ret;
UNICODE_STRING nameW;
OBJECT_ATTRIBUTES attr;
if (!is_version_nt()) access = SEMAPHORE_ALL_ACCESS;
if (!get_open_object_attributes( &attr, &nameW, inherit, name )) return 0;
if (!set_ntstatus( NtOpenSemaphore( &ret, access, &attr ))) return 0;
return ret;
}
/***********************************************************************
* ReleaseSemaphore (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH ReleaseSemaphore( HANDLE handle, LONG count, LONG *previous )
{
return set_ntstatus( NtReleaseSemaphore( handle, count, (PULONG)previous ));
}
/***********************************************************************
* Waitable timers
***********************************************************************/
/***********************************************************************
* CreateWaitableTimerW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateWaitableTimerW( SECURITY_ATTRIBUTES *sa, BOOL manual, LPCWSTR name )
{
return CreateWaitableTimerExW( sa, name, manual ? CREATE_WAITABLE_TIMER_MANUAL_RESET : 0,
TIMER_ALL_ACCESS );
}
/***********************************************************************
* CreateWaitableTimerExW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateWaitableTimerExW( SECURITY_ATTRIBUTES *sa, LPCWSTR name,
DWORD flags, DWORD access )
{
HANDLE handle;
NTSTATUS status;
UNICODE_STRING nameW;
OBJECT_ATTRIBUTES attr;
get_create_object_attributes( &attr, &nameW, sa, name );
status = NtCreateTimer( &handle, access, &attr,
(flags & CREATE_WAITABLE_TIMER_MANUAL_RESET) ? NotificationTimer : SynchronizationTimer );
if (status == STATUS_OBJECT_NAME_EXISTS)
SetLastError( ERROR_ALREADY_EXISTS );
else
SetLastError( RtlNtStatusToDosError(status) );
return handle;
}
/***********************************************************************
* OpenWaitableTimerW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH OpenWaitableTimerW( DWORD access, BOOL inherit, LPCWSTR name )
{
HANDLE handle;
UNICODE_STRING nameW;
OBJECT_ATTRIBUTES attr;
if (!is_version_nt()) access = TIMER_ALL_ACCESS;
if (!get_open_object_attributes( &attr, &nameW, inherit, name )) return 0;
if (!set_ntstatus( NtOpenTimer( &handle, access, &attr ))) return 0;
return handle;
}
/***********************************************************************
* SetWaitableTimer (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH SetWaitableTimer( HANDLE handle, const LARGE_INTEGER *when, LONG period,
PTIMERAPCROUTINE callback, LPVOID arg, BOOL resume )
{
NTSTATUS status = NtSetTimer( handle, when, (PTIMER_APC_ROUTINE)callback,
arg, resume, period, NULL );
return set_ntstatus( status ) || status == STATUS_TIMER_RESUME_IGNORED;
}
/***********************************************************************
* SetWaitableTimerEx (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH SetWaitableTimerEx( HANDLE handle, const LARGE_INTEGER *when, LONG period,
PTIMERAPCROUTINE callback, LPVOID arg,
REASON_CONTEXT *context, ULONG tolerabledelay )
{
static int once;
if (!once++) FIXME( "(%p, %p, %ld, %p, %p, %p, %ld) semi-stub\n",
handle, when, period, callback, arg, context, tolerabledelay );
return SetWaitableTimer( handle, when, period, callback, arg, FALSE );
}
/***********************************************************************
* CancelWaitableTimer (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH CancelWaitableTimer( HANDLE handle )
{
return set_ntstatus( NtCancelTimer( handle, NULL ));
}
/***********************************************************************
* Timer queues
***********************************************************************/
/***********************************************************************
* CreateTimerQueue (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateTimerQueue(void)
{
HANDLE q;
if (!set_ntstatus( RtlCreateTimerQueue( &q ))) return NULL;
return q;
}
/***********************************************************************
* CreateTimerQueueTimer (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH CreateTimerQueueTimer( PHANDLE timer, HANDLE queue,
WAITORTIMERCALLBACK callback, PVOID arg,
DWORD when, DWORD period, ULONG flags )
{
return set_ntstatus( RtlCreateTimer( queue, timer, callback, arg, when, period, flags ));
}
/***********************************************************************
* ChangeTimerQueueTimer (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH ChangeTimerQueueTimer( HANDLE queue, HANDLE timer,
ULONG when, ULONG period )
{
return set_ntstatus( RtlUpdateTimer( queue, timer, when, period ));
}
/***********************************************************************
* DeleteTimerQueueEx (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH DeleteTimerQueueEx( HANDLE queue, HANDLE event )
{
return set_ntstatus( RtlDeleteTimerQueueEx( queue, event ));
}
/***********************************************************************
* DeleteTimerQueueTimer (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH DeleteTimerQueueTimer( HANDLE queue, HANDLE timer, HANDLE event )
{
return set_ntstatus( RtlDeleteTimer( queue, timer, event ));
}
/***********************************************************************
* Critical sections
***********************************************************************/
/***********************************************************************
* InitializeCriticalSectionAndSpinCount (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH InitializeCriticalSectionAndSpinCount( CRITICAL_SECTION *crit, DWORD count )
{
return !RtlInitializeCriticalSectionAndSpinCount( crit, count );
}
/***********************************************************************
* InitializeCriticalSectionEx (kernelbase.@)
*/
BOOL WINAPI DECLSPEC_HOTPATCH InitializeCriticalSectionEx( CRITICAL_SECTION *crit, DWORD spincount,
DWORD flags )
{
NTSTATUS ret = RtlInitializeCriticalSectionEx( crit, spincount, flags );
if (ret) RtlRaiseStatus( ret );
return !ret;
}
/***********************************************************************
* File mappings
***********************************************************************/
/***********************************************************************
* CreateFileMappingW (kernelbase.@)
*/
HANDLE WINAPI DECLSPEC_HOTPATCH CreateFileMappingW( HANDLE file, LPSECURITY_ATTRIBUTES sa, DWORD protect,
DWORD size_high, DWORD size_low, LPCWSTR name )
{
static const int sec_flags = (SEC_FILE | SEC_IMAGE | SEC_RESERVE | SEC_COMMIT |
SEC_NOCACHE | SEC_WRITECOMBINE | SEC_LARGE_PAGES);
HANDLE ret;
NTSTATUS status;
DWORD access, sec_type;
LARGE_INTEGER size;
UNICODE_STRING nameW;
OBJECT_ATTRIBUTES attr;