forked from GloriousEggroll/proton-ge-custom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproton-fsync_staging.patch
4263 lines (4077 loc) · 149 KB
/
proton-fsync_staging.patch
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
From 220ab327f6cff3c6b6f387aa4851ddeb569fa9f3 Mon Sep 17 00:00:00 2001
From: Tk-Glitch <[email protected]>
Date: Thu, 8 Aug 2019 14:44:43 +0200
Subject: Prepare for Fsync
diff --git a/server/device.c b/server/device.c
index 2b604a15a8..59ffed8d7b 100644
--- a/server/device.c
+++ b/server/device.c
@@ -974,11 +974,11 @@ DECL_HANDLER(get_next_device_request)
/* we already own the object if it's only on manager queue */
if (irp->file) grab_object( irp );
manager->current_call = irp;
-
- if (do_esync() && list_empty( &manager->requests ))
- esync_clear( manager->esync_fd );
}
else close_handle( current->process, reply->next );
+
+ if (do_esync() && list_empty( &manager->requests ))
+ esync_clear( manager->esync_fd );
}
}
else set_error( STATUS_PENDING );
diff --git a/server/process.c b/server/process.c
index 41c26e11d8..8f5b4f52e5 100644
--- a/server/process.c
+++ b/server/process.c
@@ -539,8 +539,8 @@ struct process *create_process( int fd, struct process *parent, int inherit_all,
process->trace_data = 0;
process->rawinput_mouse = NULL;
process->rawinput_kbd = NULL;
- process->esync_fd = -1;
list_init( &process->kernel_object );
+ process->esync_fd = -1;
list_init( &process->thread_list );
list_init( &process->locks );
list_init( &process->asyncs );
From 7b22ba1d06c393ea1b424fbf1e686a2d4fde570e Mon Sep 17 00:00:00 2001
From: Zebediah Figura <[email protected]>
Date: Tue, 25 Jun 2019 15:23:54 -0500
Subject: ntdll: Fastpath null esync handles
diff --git a/dlls/ntdll/esync.c b/dlls/ntdll/esync.c
index f5687d61d8..6152fa679d 100644
--- a/dlls/ntdll/esync.c
+++ b/dlls/ntdll/esync.c
@@ -263,6 +263,13 @@ static NTSTATUS get_object( HANDLE handle, struct esync **obj )
return STATUS_NOT_IMPLEMENTED;
}
+ if (!handle)
+ {
+ /* Shadow of the Tomb Raider really likes passing in NULL handles to
+ * various functions. Concerning, but let's avoid a server call. */
+ return STATUS_INVALID_HANDLE;
+ }
+
/* We need to try grabbing it from the server. */
server_enter_uninterrupted_section( &fd_cache_section, &sigset );
if (!(*obj = get_cached_object( handle )))
diff --git a/dlls/ntdll/Makefile.in b/dlls/ntdll/Makefile.in
index b75e8308ac..26d1fb537e 100644
--- a/dlls/ntdll/Makefile.in
+++ b/dlls/ntdll/Makefile.in
@@ -18,6 +18,7 @@ C_SRCS = \
esync.c \
exception.c \
file.c \
+ fsync.c \
handletable.c \
heap.c \
large_int.c \
diff --git a/dlls/ntdll/esync.c b/dlls/ntdll/esync.c
index 6152fa679d..8ec7fb342e 100644
--- a/dlls/ntdll/esync.c
+++ b/dlls/ntdll/esync.c
@@ -48,6 +48,7 @@
#include "ntdll_misc.h"
#include "esync.h"
+#include "fsync.h"
WINE_DEFAULT_DEBUG_CHANNEL(esync);
@@ -57,7 +58,7 @@ int do_esync(void)
static int do_esync_cached = -1;
if (do_esync_cached == -1)
- do_esync_cached = getenv("WINEESYNC") && atoi(getenv("WINEESYNC"));
+ do_esync_cached = getenv("WINEESYNC") && atoi(getenv("WINEESYNC")) && !do_fsync();
return do_esync_cached;
#else
diff --git a/dlls/ntdll/fsync.c b/dlls/ntdll/fsync.c
new file mode 100644
index 0000000000..970316777a
--- /dev/null
+++ b/dlls/ntdll/fsync.c
@@ -0,0 +1,1173 @@
+/*
+ * futex-based synchronization objects
+ *
+ * Copyright (C) 2018 Zebediah Figura
+ *
+ * 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 "config.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#ifdef HAVE_SYS_MMAN_H
+# include <sys/mman.h>
+#endif
+#ifdef HAVE_SYS_STAT_H
+# include <sys/stat.h>
+#endif
+#ifdef HAVE_SYS_SYSCALL_H
+# include <sys/syscall.h>
+#endif
+#include <unistd.h>
+
+#include "ntstatus.h"
+#define WIN32_NO_STATUS
+#define NONAMELESSUNION
+#include "windef.h"
+#include "winternl.h"
+#include "wine/debug.h"
+#include "wine/library.h"
+#include "wine/server.h"
+
+#include "ntdll_misc.h"
+#include "fsync.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(fsync);
+
+#include "pshpack4.h"
+struct futex_wait_block
+{
+ int *addr;
+#if __SIZEOF_POINTER__ == 4
+ int pad;
+#endif
+ int val;
+};
+#include "poppack.h"
+
+static inline int futex_wait_multiple( const struct futex_wait_block *futexes,
+ int count, const struct timespec *timeout )
+{
+ return syscall( __NR_futex, futexes, 13, count, timeout, 0, 0 );
+}
+
+static inline int futex_wake( int *addr, int val )
+{
+ return syscall( __NR_futex, addr, 1, val, NULL, 0, 0 );
+}
+
+static inline int futex_wait( int *addr, int val, struct timespec *timeout )
+{
+ return syscall( __NR_futex, addr, 0, val, timeout, 0, 0 );
+}
+
+int do_fsync(void)
+{
+#ifdef __linux__
+ static int do_fsync_cached = -1;
+
+ if (do_fsync_cached == -1)
+ {
+ static const struct timespec zero;
+ futex_wait_multiple( NULL, 0, &zero );
+ do_fsync_cached = getenv("WINEFSYNC") && atoi(getenv("WINEFSYNC")) && errno != ENOSYS;
+ }
+
+ return do_fsync_cached;
+#else
+ static int once;
+ if (!once++)
+ FIXME("futexes not supported on this platform.\n");
+ return 0;
+#endif
+}
+
+struct fsync
+{
+ enum fsync_type type;
+ void *shm; /* pointer to shm section */
+};
+
+struct semaphore
+{
+ int count;
+ int max;
+};
+C_ASSERT(sizeof(struct semaphore) == 8);
+
+struct event
+{
+ int signaled;
+ int unused;
+};
+C_ASSERT(sizeof(struct event) == 8);
+
+struct mutex
+{
+ int tid;
+ int count; /* recursion count */
+};
+C_ASSERT(sizeof(struct mutex) == 8);
+
+static char shm_name[29];
+static int shm_fd;
+static void **shm_addrs;
+static int shm_addrs_size; /* length of the allocated shm_addrs array */
+static long pagesize;
+
+static void *get_shm( unsigned int idx )
+{
+ int entry = (idx * 8) / pagesize;
+ int offset = (idx * 8) % pagesize;
+
+ if (entry >= shm_addrs_size)
+ {
+ shm_addrs_size = entry + 1;
+ if (!(shm_addrs = RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY,
+ shm_addrs, shm_addrs_size * sizeof(shm_addrs[0]) )))
+ ERR("Failed to grow shm_addrs array to size %d.\n", shm_addrs_size);
+ }
+
+ if (!shm_addrs[entry])
+ {
+ void *addr = mmap( NULL, pagesize, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, entry * pagesize );
+ if (addr == (void *)-1)
+ ERR("Failed to map page %d (offset %#lx).\n", entry, entry * pagesize);
+
+ TRACE("Mapping page %d at %p.\n", entry, addr);
+
+ if (__sync_val_compare_and_swap( &shm_addrs[entry], 0, addr ))
+ munmap( addr, pagesize ); /* someone beat us to it */
+ }
+
+ return (void *)((unsigned long)shm_addrs[entry] + offset);
+}
+
+/* We'd like lookup to be fast. To that end, we use a static list indexed by handle.
+ * This is copied and adapted from the fd cache code. */
+
+#define FSYNC_LIST_BLOCK_SIZE (65536 / sizeof(struct fsync))
+#define FSYNC_LIST_ENTRIES 256
+
+static struct fsync *fsync_list[FSYNC_LIST_ENTRIES];
+static struct fsync fsync_list_initial_block[FSYNC_LIST_BLOCK_SIZE];
+
+static inline UINT_PTR handle_to_index( HANDLE handle, UINT_PTR *entry )
+{
+ UINT_PTR idx = (((UINT_PTR)handle) >> 2) - 1;
+ *entry = idx / FSYNC_LIST_BLOCK_SIZE;
+ return idx % FSYNC_LIST_BLOCK_SIZE;
+}
+
+static struct fsync *add_to_list( HANDLE handle, enum fsync_type type, void *shm )
+{
+ UINT_PTR entry, idx = handle_to_index( handle, &entry );
+
+ if (entry >= FSYNC_LIST_ENTRIES)
+ {
+ FIXME( "too many allocated handles, not caching %p\n", handle );
+ return FALSE;
+ }
+
+ if (!fsync_list[entry]) /* do we need to allocate a new block of entries? */
+ {
+ if (!entry) fsync_list[0] = fsync_list_initial_block;
+ else
+ {
+ void *ptr = wine_anon_mmap( NULL, FSYNC_LIST_BLOCK_SIZE * sizeof(struct fsync),
+ PROT_READ | PROT_WRITE, 0 );
+ if (ptr == MAP_FAILED) return FALSE;
+ fsync_list[entry] = ptr;
+ }
+ }
+
+ if (!__sync_val_compare_and_swap((int *)&fsync_list[entry][idx].type, 0, type ))
+ fsync_list[entry][idx].shm = shm;
+
+ return &fsync_list[entry][idx];
+}
+
+static struct fsync *get_cached_object( HANDLE handle )
+{
+ UINT_PTR entry, idx = handle_to_index( handle, &entry );
+
+ if (entry >= FSYNC_LIST_ENTRIES || !fsync_list[entry]) return NULL;
+ if (!fsync_list[entry][idx].type) return NULL;
+
+ return &fsync_list[entry][idx];
+}
+
+/* Gets an object. This is either a proper fsync object (i.e. an event,
+ * semaphore, etc. created using create_fsync) or a generic synchronizable
+ * server-side object which the server will signal (e.g. a process, thread,
+ * message queue, etc.) */
+static NTSTATUS get_object( HANDLE handle, struct fsync **obj )
+{
+ NTSTATUS ret = STATUS_SUCCESS;
+ unsigned int shm_idx = 0;
+ enum fsync_type type;
+
+ if ((*obj = get_cached_object( handle ))) return STATUS_SUCCESS;
+
+ if ((INT_PTR)handle < 0)
+ {
+ /* We can deal with pseudo-handles, but it's just easier this way */
+ return STATUS_NOT_IMPLEMENTED;
+ }
+
+ /* We need to try grabbing it from the server. */
+ SERVER_START_REQ( get_fsync_idx )
+ {
+ req->handle = wine_server_obj_handle( handle );
+ if (!(ret = wine_server_call( req )))
+ {
+ shm_idx = reply->shm_idx;
+ type = reply->type;
+ }
+ }
+ SERVER_END_REQ;
+
+ if (ret)
+ {
+ WARN("Failed to retrieve shm index for handle %p, status %#x.\n", handle, ret);
+ *obj = NULL;
+ return ret;
+ }
+
+ TRACE("Got shm index %d for handle %p.\n", shm_idx, handle);
+
+ *obj = add_to_list( handle, type, get_shm( shm_idx ) );
+ return ret;
+}
+
+NTSTATUS fsync_close( HANDLE handle )
+{
+ UINT_PTR entry, idx = handle_to_index( handle, &entry );
+
+ TRACE("%p.\n", handle);
+
+ if (entry < FSYNC_LIST_ENTRIES && fsync_list[entry])
+ {
+ if (__atomic_exchange_n( &fsync_list[entry][idx].type, 0, __ATOMIC_SEQ_CST ))
+ return STATUS_SUCCESS;
+ }
+
+ return STATUS_INVALID_HANDLE;
+}
+
+static NTSTATUS create_fsync( enum fsync_type type, HANDLE *handle,
+ ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr, int low, int high )
+{
+ NTSTATUS ret;
+ data_size_t len;
+ struct object_attributes *objattr;
+ unsigned int shm_idx;
+
+ if ((ret = alloc_object_attributes( attr, &objattr, &len ))) return ret;
+
+ SERVER_START_REQ( create_fsync )
+ {
+ req->access = access;
+ req->low = low;
+ req->high = high;
+ req->type = type;
+ wine_server_add_data( req, objattr, len );
+ ret = wine_server_call( req );
+ if (!ret || ret == STATUS_OBJECT_NAME_EXISTS)
+ {
+ *handle = wine_server_ptr_handle( reply->handle );
+ shm_idx = reply->shm_idx;
+ type = reply->type;
+ }
+ }
+ SERVER_END_REQ;
+
+ if (!ret || ret == STATUS_OBJECT_NAME_EXISTS)
+ {
+ add_to_list( *handle, type, get_shm( shm_idx ));
+ TRACE("-> handle %p, shm index %d.\n", *handle, shm_idx);
+ }
+
+ RtlFreeHeap( GetProcessHeap(), 0, objattr );
+ return ret;
+}
+
+static NTSTATUS open_fsync( enum fsync_type type, HANDLE *handle,
+ ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr )
+{
+ NTSTATUS ret;
+ unsigned int shm_idx;
+
+ SERVER_START_REQ( open_fsync )
+ {
+ req->access = access;
+ req->attributes = attr->Attributes;
+ req->rootdir = wine_server_obj_handle( attr->RootDirectory );
+ req->type = type;
+ if (attr->ObjectName)
+ wine_server_add_data( req, attr->ObjectName->Buffer, attr->ObjectName->Length );
+ if (!(ret = wine_server_call( req )))
+ {
+ *handle = wine_server_ptr_handle( reply->handle );
+ type = reply->type;
+ shm_idx = reply->shm_idx;
+ }
+ }
+ SERVER_END_REQ;
+
+ if (!ret)
+ {
+ add_to_list( *handle, type, get_shm( shm_idx ) );
+
+ TRACE("-> handle %p, shm index %u.\n", *handle, shm_idx);
+ }
+ return ret;
+}
+
+void fsync_init(void)
+{
+ struct stat st;
+
+ if (!do_fsync())
+ {
+ /* make sure the server isn't running with WINEFSYNC */
+ HANDLE handle;
+ NTSTATUS ret;
+
+ ret = create_fsync( 0, &handle, 0, NULL, 0, 0 );
+ if (ret != STATUS_NOT_IMPLEMENTED)
+ {
+ ERR("Server is running with WINEFSYNC but this process is not, please enable WINEFSYNC or restart wineserver.\n");
+ exit(1);
+ }
+
+ return;
+ }
+
+ if (stat( config_dir, &st ) == -1)
+ ERR("Cannot stat %s\n", config_dir);
+
+ if (st.st_ino != (unsigned long)st.st_ino)
+ sprintf( shm_name, "/wine-%lx%08lx-fsync", (unsigned long)((unsigned long long)st.st_ino >> 32), (unsigned long)st.st_ino );
+ else
+ sprintf( shm_name, "/wine-%lx-fsync", (unsigned long)st.st_ino );
+
+ if ((shm_fd = shm_open( shm_name, O_RDWR, 0644 )) == -1)
+ {
+ /* probably the server isn't running with WINEFSYNC, tell the user and bail */
+ if (errno == ENOENT)
+ ERR("Failed to open fsync shared memory file; make sure no stale wineserver instances are running without WINEFSYNC.\n");
+ else
+ ERR("Failed to initialize shared memory: %s\n", strerror( errno ));
+ exit(1);
+ }
+
+ pagesize = sysconf( _SC_PAGESIZE );
+
+ shm_addrs = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY, 128 * sizeof(shm_addrs[0]) );
+ shm_addrs_size = 128;
+}
+
+NTSTATUS fsync_create_semaphore( HANDLE *handle, ACCESS_MASK access,
+ const OBJECT_ATTRIBUTES *attr, LONG initial, LONG max )
+{
+ TRACE("name %s, initial %d, max %d.\n",
+ attr ? debugstr_us(attr->ObjectName) : "<no name>", initial, max);
+
+ return create_fsync( FSYNC_SEMAPHORE, handle, access, attr, initial, max );
+}
+
+NTSTATUS fsync_open_semaphore( HANDLE *handle, ACCESS_MASK access,
+ const OBJECT_ATTRIBUTES *attr )
+{
+ TRACE("name %s.\n", debugstr_us(attr->ObjectName));
+
+ return open_fsync( FSYNC_SEMAPHORE, handle, access, attr );
+}
+
+NTSTATUS fsync_release_semaphore( HANDLE handle, ULONG count, ULONG *prev )
+{
+ struct fsync *obj;
+ struct semaphore *semaphore;
+ ULONG current;
+ NTSTATUS ret;
+
+ TRACE("%p, %d, %p.\n", handle, count, prev);
+
+ if ((ret = get_object( handle, &obj ))) return ret;
+ semaphore = obj->shm;
+
+ do
+ {
+ current = semaphore->count;
+ if (count + current > semaphore->max)
+ return STATUS_SEMAPHORE_LIMIT_EXCEEDED;
+ } while (__sync_val_compare_and_swap( &semaphore->count, current, count + current ) != current);
+
+ if (prev) *prev = current;
+
+ futex_wake( &semaphore->count, INT_MAX );
+
+ return STATUS_SUCCESS;
+}
+
+NTSTATUS fsync_query_semaphore( HANDLE handle, SEMAPHORE_INFORMATION_CLASS class,
+ void *info, ULONG len, ULONG *ret_len )
+{
+ struct fsync *obj;
+ struct semaphore *semaphore;
+ SEMAPHORE_BASIC_INFORMATION *out = info;
+ NTSTATUS ret;
+
+ TRACE("%p, %u, %p, %u, %p.\n", handle, class, info, len, ret_len);
+
+ if (class != SemaphoreBasicInformation)
+ {
+ FIXME("(%p,%d,%u) Unknown class\n", handle, class, len);
+ return STATUS_INVALID_INFO_CLASS;
+ }
+
+ if ((ret = get_object( handle, &obj ))) return ret;
+ semaphore = obj->shm;
+
+ out->CurrentCount = semaphore->count;
+ out->MaximumCount = semaphore->max;
+ if (ret_len) *ret_len = sizeof(*out);
+
+ return STATUS_SUCCESS;
+}
+
+NTSTATUS fsync_create_event( HANDLE *handle, ACCESS_MASK access,
+ const OBJECT_ATTRIBUTES *attr, EVENT_TYPE event_type, BOOLEAN initial )
+{
+ enum fsync_type type = (event_type == SynchronizationEvent ? FSYNC_AUTO_EVENT : FSYNC_MANUAL_EVENT);
+
+ TRACE("name %s, %s-reset, initial %d.\n",
+ attr ? debugstr_us(attr->ObjectName) : "<no name>",
+ event_type == NotificationEvent ? "manual" : "auto", initial);
+
+ return create_fsync( type, handle, access, attr, initial, 0xdeadbeef );
+}
+
+NTSTATUS fsync_open_event( HANDLE *handle, ACCESS_MASK access,
+ const OBJECT_ATTRIBUTES *attr )
+{
+ TRACE("name %s.\n", debugstr_us(attr->ObjectName));
+
+ return open_fsync( FSYNC_AUTO_EVENT, handle, access, attr );
+}
+
+NTSTATUS fsync_set_event( HANDLE handle, LONG *prev )
+{
+ struct event *event;
+ struct fsync *obj;
+ LONG current;
+ NTSTATUS ret;
+
+ TRACE("%p.\n", handle);
+
+ if ((ret = get_object( handle, &obj ))) return ret;
+ event = obj->shm;
+
+ if (!(current = __atomic_exchange_n( &event->signaled, 1, __ATOMIC_SEQ_CST )))
+ futex_wake( &event->signaled, INT_MAX );
+
+ if (prev) *prev = current;
+
+ return STATUS_SUCCESS;
+}
+
+NTSTATUS fsync_reset_event( HANDLE handle, LONG *prev )
+{
+ struct event *event;
+ struct fsync *obj;
+ LONG current;
+ NTSTATUS ret;
+
+ TRACE("%p.\n", handle);
+
+ if ((ret = get_object( handle, &obj ))) return ret;
+ event = obj->shm;
+
+ current = __atomic_exchange_n( &event->signaled, 0, __ATOMIC_SEQ_CST );
+
+ if (prev) *prev = current;
+
+ return STATUS_SUCCESS;
+}
+
+NTSTATUS fsync_query_event( HANDLE handle, EVENT_INFORMATION_CLASS class,
+ void *info, ULONG len, ULONG *ret_len )
+{
+ struct event *event;
+ struct fsync *obj;
+ EVENT_BASIC_INFORMATION *out = info;
+ NTSTATUS ret;
+
+ TRACE("%p, %u, %p, %u, %p.\n", handle, class, info, len, ret_len);
+
+ if (class != EventBasicInformation)
+ {
+ FIXME("(%p,%d,%u) Unknown class\n", handle, class, len);
+ return STATUS_INVALID_INFO_CLASS;
+ }
+
+ if ((ret = get_object( handle, &obj ))) return ret;
+ event = obj->shm;
+
+ out->EventState = event->signaled;
+ out->EventType = (obj->type == FSYNC_AUTO_EVENT ? SynchronizationEvent : NotificationEvent);
+ if (ret_len) *ret_len = sizeof(*out);
+
+ return STATUS_SUCCESS;
+}
+
+NTSTATUS fsync_create_mutex( HANDLE *handle, ACCESS_MASK access,
+ const OBJECT_ATTRIBUTES *attr, BOOLEAN initial )
+{
+ TRACE("name %s, initial %d.\n",
+ attr ? debugstr_us(attr->ObjectName) : "<no name>", initial);
+
+ return create_fsync( FSYNC_MUTEX, handle, access, attr,
+ initial ? GetCurrentThreadId() : 0, initial ? 1 : 0 );
+}
+
+NTSTATUS fsync_open_mutex( HANDLE *handle, ACCESS_MASK access,
+ const OBJECT_ATTRIBUTES *attr )
+{
+ TRACE("name %s.\n", debugstr_us(attr->ObjectName));
+
+ return open_fsync( FSYNC_MUTEX, handle, access, attr );
+}
+
+NTSTATUS fsync_release_mutex( HANDLE handle, LONG *prev )
+{
+ struct mutex *mutex;
+ struct fsync *obj;
+ NTSTATUS ret;
+
+ TRACE("%p, %p.\n", handle, prev);
+
+ if ((ret = get_object( handle, &obj ))) return ret;
+ mutex = obj->shm;
+
+ if (mutex->tid != GetCurrentThreadId()) return STATUS_MUTANT_NOT_OWNED;
+
+ if (prev) *prev = mutex->count;
+
+ if (!--mutex->count)
+ {
+ __atomic_store_n( &mutex->tid, 0, __ATOMIC_SEQ_CST );
+ futex_wake( &mutex->tid, INT_MAX );
+ }
+
+ return STATUS_SUCCESS;
+}
+
+NTSTATUS fsync_query_mutex( HANDLE handle, MUTANT_INFORMATION_CLASS class,
+ void *info, ULONG len, ULONG *ret_len )
+{
+ struct fsync *obj;
+ struct mutex *mutex;
+ MUTANT_BASIC_INFORMATION *out = info;
+ NTSTATUS ret;
+
+ TRACE("%p, %u, %p, %u, %p.\n", handle, class, info, len, ret_len);
+
+ if (class != MutantBasicInformation)
+ {
+ FIXME("(%p,%d,%u) Unknown class\n", handle, class, len);
+ return STATUS_INVALID_INFO_CLASS;
+ }
+
+ if ((ret = get_object( handle, &obj ))) return ret;
+ mutex = obj->shm;
+
+ out->CurrentCount = 1 - mutex->count;
+ out->OwnedByCaller = (mutex->tid == GetCurrentThreadId());
+ out->AbandonedState = FALSE;
+ if (ret_len) *ret_len = sizeof(*out);
+
+ return STATUS_SUCCESS;
+}
+
+#define TICKSPERSEC ((ULONGLONG)10000000)
+
+static LONGLONG update_timeout( ULONGLONG end )
+{
+ LARGE_INTEGER now;
+ LONGLONG timeleft;
+
+ NtQuerySystemTime( &now );
+ timeleft = end - now.QuadPart;
+ if (timeleft < 0) timeleft = 0;
+ return timeleft;
+}
+
+static NTSTATUS do_single_wait( int *addr, int val, ULONGLONG *end, BOOLEAN alertable )
+{
+ int ret;
+
+ if (alertable)
+ {
+ struct event *apc_event = get_shm( ntdll_get_thread_data()->fsync_apc_idx );
+ struct futex_wait_block futexes[2];
+
+ if (__atomic_load_n( &apc_event->signaled, __ATOMIC_SEQ_CST ))
+ return STATUS_USER_APC;
+
+ futexes[0].addr = addr;
+ futexes[0].val = val;
+ futexes[1].addr = &apc_event->signaled;
+ futexes[1].val = 0;
+#if __SIZEOF_POINTER__ == 4
+ futexes[0].pad = futexes[1].pad = 0;
+#endif
+
+ if (end)
+ {
+ LONGLONG timeleft = update_timeout( *end );
+ struct timespec tmo_p;
+ tmo_p.tv_sec = timeleft / (ULONGLONG)TICKSPERSEC;
+ tmo_p.tv_nsec = (timeleft % TICKSPERSEC) * 100;
+ ret = futex_wait_multiple( futexes, 2, &tmo_p );
+ }
+ else
+ ret = futex_wait_multiple( futexes, 2, NULL );
+
+ if (__atomic_load_n( &apc_event->signaled, __ATOMIC_SEQ_CST ))
+ return STATUS_USER_APC;
+ }
+ else
+ {
+ if (end)
+ {
+ LONGLONG timeleft = update_timeout( *end );
+ struct timespec tmo_p;
+ tmo_p.tv_sec = timeleft / (ULONGLONG)TICKSPERSEC;
+ tmo_p.tv_nsec = (timeleft % TICKSPERSEC) * 100;
+ ret = futex_wait( addr, val, &tmo_p );
+ }
+ else
+ ret = futex_wait( addr, val, NULL );
+ }
+
+ if (!ret)
+ return 0;
+ else if (ret < 0 && errno == ETIMEDOUT)
+ return STATUS_TIMEOUT;
+ else
+ return STATUS_PENDING;
+}
+
+static NTSTATUS __fsync_wait_objects( DWORD count, const HANDLE *handles,
+ BOOLEAN wait_any, BOOLEAN alertable, const LARGE_INTEGER *timeout )
+{
+ static const LARGE_INTEGER zero = {0};
+
+ struct futex_wait_block futexes[MAXIMUM_WAIT_OBJECTS + 1];
+ struct fsync *objs[MAXIMUM_WAIT_OBJECTS];
+ int has_fsync = 0, has_server = 0;
+ BOOL msgwait = FALSE;
+ int dummy_futex = 0;
+ LONGLONG timeleft;
+ LARGE_INTEGER now;
+ DWORD waitcount;
+ ULONGLONG end;
+ int i, ret;
+
+ /* Grab the APC futex if we don't already have it. */
+ if (alertable && !ntdll_get_thread_data()->fsync_apc_idx)
+ {
+ SERVER_START_REQ( get_fsync_apc_idx )
+ {
+ if (!(ret = wine_server_call( req )))
+ ntdll_get_thread_data()->fsync_apc_idx = reply->shm_idx;
+ }
+ SERVER_END_REQ;
+ }
+
+ NtQuerySystemTime( &now );
+ if (timeout)
+ {
+ if (timeout->QuadPart == TIMEOUT_INFINITE)
+ timeout = NULL;
+ else if (timeout->QuadPart > 0)
+ end = timeout->QuadPart;
+ else
+ end = now.QuadPart - timeout->QuadPart;
+ }
+
+ for (i = 0; i < count; i++)
+ {
+ ret = get_object( handles[i], &objs[i] );
+ if (ret == STATUS_SUCCESS)
+ has_fsync = 1;
+ else if (ret == STATUS_NOT_IMPLEMENTED)
+ has_server = 1;
+ else
+ return ret;
+ }
+
+ if (objs[count - 1] && objs[count - 1]->type == FSYNC_QUEUE)
+ msgwait = TRUE;
+
+ if (has_fsync && has_server)
+ FIXME("Can't wait on fsync and server objects at the same time!\n");
+ else if (has_server)
+ return STATUS_NOT_IMPLEMENTED;
+
+ if (TRACE_ON(fsync))
+ {
+ TRACE("Waiting for %s of %d handles:", wait_any ? "any" : "all", count);
+ for (i = 0; i < count; i++)
+ TRACE(" %p", handles[i]);
+
+ if (msgwait)
+ TRACE(" or driver events");
+ if (alertable)
+ TRACE(", alertable");
+
+ if (!timeout)
+ TRACE(", timeout = INFINITE.\n");
+ else
+ {
+ timeleft = update_timeout( end );
+ TRACE(", timeout = %ld.%07ld sec.\n",
+ (long) (timeleft / TICKSPERSEC), (long) (timeleft % TICKSPERSEC));
+ }
+ }
+
+ if (wait_any || count == 1)
+ {
+ while (1)
+ {
+ /* Try to grab anything. */
+
+ for (i = 0; i < count; i++)
+ {
+ struct fsync *obj = objs[i];
+
+ if (obj)
+ {
+ switch (obj->type)
+ {
+ case FSYNC_SEMAPHORE:
+ {
+ struct semaphore *semaphore = obj->shm;
+ int current;
+
+ do
+ {
+ if (!(current = semaphore->count)) break;
+ } while (__sync_val_compare_and_swap( &semaphore->count, current, current - 1 ) != current);
+
+ if (current)
+ {
+ TRACE("Woken up by handle %p [%d].\n", handles[i], i);
+ return i;
+ }
+
+ futexes[i].addr = &semaphore->count;
+ futexes[i].val = current;
+ break;
+ }
+ case FSYNC_MUTEX:
+ {
+ struct mutex *mutex = obj->shm;
+
+ if (mutex->tid == GetCurrentThreadId())
+ {
+ TRACE("Woken up by handle %p [%d].\n", handles[i], i);
+ mutex->count++;
+ return i;
+ }
+
+ if (!__sync_val_compare_and_swap( &mutex->tid, 0, GetCurrentThreadId() ))
+ {
+ TRACE("Woken up by handle %p [%d].\n", handles[i], i);
+ mutex->count = 1;
+ return i;
+ }
+
+ futexes[i].addr = &mutex->tid;
+ futexes[i].val = mutex->tid;
+ break;
+ }
+ case FSYNC_AUTO_EVENT:
+ case FSYNC_AUTO_SERVER:
+ {
+ struct event *event = obj->shm;
+
+ if (__sync_val_compare_and_swap( &event->signaled, 1, 0 ))
+ {
+ TRACE("Woken up by handle %p [%d].\n", handles[i], i);
+ return i;
+ }
+
+ futexes[i].addr = &event->signaled;
+ futexes[i].val = 0;
+ break;
+ }
+ case FSYNC_MANUAL_EVENT:
+ case FSYNC_MANUAL_SERVER:
+ case FSYNC_QUEUE:
+ {
+ struct event *event = obj->shm;
+
+ if (__atomic_load_n( &event->signaled, __ATOMIC_SEQ_CST ))
+ {
+ TRACE("Woken up by handle %p [%d].\n", handles[i], i);
+ return i;
+ }
+
+ futexes[i].addr = &event->signaled;
+ futexes[i].val = 0;
+ break;
+ }
+ default:
+ assert(0);
+ }
+ }
+ else
+ {
+ /* Avoid breaking things entirely. */
+ futexes[i].addr = &dummy_futex;
+ futexes[i].val = dummy_futex;
+ }
+
+#if __SIZEOF_POINTER__ == 4
+ futexes[i].pad = 0;
+#endif
+ }
+
+ if (alertable)
+ {
+ struct event *event = get_shm( ntdll_get_thread_data()->fsync_apc_idx );
+ if (__atomic_load_n( &event->signaled, __ATOMIC_SEQ_CST ))
+ goto userapc;
+
+ futexes[i].addr = &event->signaled;
+ futexes[i].val = 0;
+#if __SIZEOF_POINTER__ == 4
+ futexes[i].pad = 0;
+#endif
+ i++;
+ }
+ waitcount = i;
+
+ /* Looks like everything is contended, so wait. */
+
+ if (timeout && !timeout->QuadPart)
+ {
+ /* Unlike esync, we already know that we've timed out, so we
+ * can avoid a syscall. */
+ TRACE("Wait timed out.\n");
+ return STATUS_TIMEOUT;
+ }
+ else if (timeout)
+ {
+ LONGLONG timeleft = update_timeout( end );
+ struct timespec tmo_p;
+ tmo_p.tv_sec = timeleft / (ULONGLONG)TICKSPERSEC;
+ tmo_p.tv_nsec = (timeleft % TICKSPERSEC) * 100;
+
+ ret = futex_wait_multiple( futexes, waitcount, &tmo_p );
+ }
+ else