-
Notifications
You must be signed in to change notification settings - Fork 468
/
Copy pathio.c
2860 lines (2744 loc) · 86.3 KB
/
io.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
/*
* Copyright (c) 2009-2013 Apple Inc. All rights reserved.
*
* @APPLE_APACHE_LICENSE_HEADER_START@
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @APPLE_APACHE_LICENSE_HEADER_END@
*/
#include "internal.h"
#if defined(__FreeBSD__)
#include <fcntl.h>
#define F_RDADVISE F_RDAHEAD
#endif
#ifndef DISPATCH_IO_DEBUG
#define DISPATCH_IO_DEBUG DISPATCH_DEBUG
#endif
#ifndef PAGE_SIZE
#if defined(_WIN32)
static DWORD
getpagesize(void)
{
SYSTEM_INFO siInfo;
GetSystemInfo(&siInfo);
return siInfo.dwPageSize;
}
#endif
#define PAGE_SIZE ((size_t)getpagesize())
#endif
#if DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
#define _dispatch_io_data_retain(x) _dispatch_objc_retain(x)
#define _dispatch_io_data_release(x) _dispatch_objc_release(x)
#else
#define _dispatch_io_data_retain(x) dispatch_retain(x)
#define _dispatch_io_data_release(x) dispatch_release(x)
#endif
typedef void (^dispatch_fd_entry_init_callback_t)(dispatch_fd_entry_t fd_entry);
DISPATCH_EXPORT DISPATCH_NOTHROW
void _dispatch_iocntl(uint32_t param, uint64_t value);
static dispatch_operation_t _dispatch_operation_create(
dispatch_op_direction_t direction, dispatch_io_t channel, off_t offset,
size_t length, dispatch_data_t data, dispatch_queue_t queue,
dispatch_io_handler_t handler);
static void _dispatch_operation_enqueue(dispatch_operation_t op,
dispatch_op_direction_t direction, dispatch_data_t data);
static dispatch_source_t _dispatch_operation_timer(dispatch_queue_t tq,
dispatch_operation_t op);
static inline void _dispatch_fd_entry_retain(dispatch_fd_entry_t fd_entry);
static inline void _dispatch_fd_entry_release(dispatch_fd_entry_t fd_entry);
static void _dispatch_fd_entry_init_async(dispatch_fd_t fd,
dispatch_fd_entry_init_callback_t completion_callback);
static dispatch_fd_entry_t _dispatch_fd_entry_create_with_fd(dispatch_fd_t fd,
uintptr_t hash);
static dispatch_fd_entry_t _dispatch_fd_entry_create_with_path(
dispatch_io_path_data_t path_data, dev_t dev, mode_t mode);
static int _dispatch_fd_entry_open(dispatch_fd_entry_t fd_entry,
dispatch_io_t channel);
static void _dispatch_fd_entry_cleanup_operations(dispatch_fd_entry_t fd_entry,
dispatch_io_t channel);
static void _dispatch_stream_init(dispatch_fd_entry_t fd_entry,
dispatch_queue_t tq);
static void _dispatch_stream_dispose(dispatch_fd_entry_t fd_entry,
dispatch_op_direction_t direction);
static void _dispatch_disk_init(dispatch_fd_entry_t fd_entry, dev_t dev);
static void _dispatch_stream_enqueue_operation(dispatch_stream_t stream,
dispatch_operation_t operation, dispatch_data_t data);
static void _dispatch_disk_enqueue_operation(dispatch_disk_t dsk,
dispatch_operation_t operation, dispatch_data_t data);
static void _dispatch_stream_cleanup_operations(dispatch_stream_t stream,
dispatch_io_t channel);
static void _dispatch_disk_cleanup_inactive_operations(dispatch_disk_t disk,
dispatch_io_t channel);
static void _dispatch_stream_source_handler(void *ctx);
static void _dispatch_stream_queue_handler(void *ctx);
static void _dispatch_stream_handler(void *ctx);
static void _dispatch_disk_handler(void *ctx);
static void _dispatch_disk_perform(void *ctxt);
static void _dispatch_operation_advise(dispatch_operation_t op,
size_t chunk_size);
static int _dispatch_operation_perform(dispatch_operation_t op);
static void _dispatch_operation_deliver_data(dispatch_operation_t op,
dispatch_op_flags_t flags);
// Macros to wrap syscalls which return -1 on error, and retry on EINTR
#define _dispatch_io_syscall_switch_noerr(_err, _syscall, ...) do { \
switch (((_err) = (((_syscall) == -1) ? errno : 0))) { \
case EINTR: continue; \
__VA_ARGS__ \
} \
break; \
} while (1)
#define _dispatch_io_syscall_switch(__err, __syscall, ...) do { \
_dispatch_io_syscall_switch_noerr(__err, __syscall, \
case 0: break; \
__VA_ARGS__ \
); \
} while (0)
#define _dispatch_io_syscall(__syscall) do { int __err; \
_dispatch_io_syscall_switch(__err, __syscall); \
} while (0)
enum {
DISPATCH_OP_COMPLETE = 1,
DISPATCH_OP_DELIVER,
DISPATCH_OP_DELIVER_AND_COMPLETE,
DISPATCH_OP_COMPLETE_RESUME,
DISPATCH_OP_RESUME,
DISPATCH_OP_ERR,
DISPATCH_OP_FD_ERR,
};
#define _dispatch_io_Block_copy(x) \
((__typeof__(x))_dispatch_Block_copy((dispatch_block_t)(x)))
#pragma mark -
#pragma mark dispatch_io_debug
#if DISPATCH_IO_DEBUG
#if !DISPATCH_DEBUG
#define _dispatch_io_log(x, ...) do { \
_dispatch_log("%llu\t%p\t" x, _dispatch_uptime(), \
(void *)_dispatch_thread_self(), ##__VA_ARGS__); \
} while (0)
#ifdef _dispatch_object_debug
#undef _dispatch_object_debug
#define _dispatch_object_debug dispatch_debug
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
#else
#define _dispatch_io_log(x, ...) _dispatch_debug(x, ##__VA_ARGS__)
#endif // DISPATCH_DEBUG
#else
#define _dispatch_io_log(x, ...)
#endif // DISPATCH_IO_DEBUG
#define _dispatch_fd_debug(msg, fd, ...) \
_dispatch_io_log("fd[0x%" PRIx64 "]: " msg, fd, ##__VA_ARGS__)
#define _dispatch_op_debug(msg, op, ...) \
_dispatch_io_log("op[%p]: " msg, op, ##__VA_ARGS__)
#define _dispatch_channel_debug(msg, channel, ...) \
_dispatch_io_log("channel[%p]: " msg, channel, ##__VA_ARGS__)
#define _dispatch_fd_entry_debug(msg, fd_entry, ...) \
_dispatch_io_log("fd_entry[%p]: " msg, fd_entry, ##__VA_ARGS__)
#define _dispatch_disk_debug(msg, disk, ...) \
_dispatch_io_log("disk[%p]: " msg, disk, ##__VA_ARGS__)
#pragma mark -
#pragma mark dispatch_io_hashtables
LIST_HEAD(dispatch_disk_head_s, dispatch_disk_s);
LIST_HEAD(dispatch_fd_entry_head_s, dispatch_fd_entry_s);
// Global hashtable of dev_t -> disk_s mappings
DISPATCH_STATIC_GLOBAL(struct dispatch_disk_head_s _dispatch_io_devs[DIO_HASH_SIZE]);
DISPATCH_STATIC_GLOBAL(dispatch_queue_t _dispatch_io_devs_lockq);
// Global hashtable of fd -> fd_entry_s mappings
DISPATCH_STATIC_GLOBAL(struct dispatch_fd_entry_head_s _dispatch_io_fds[DIO_HASH_SIZE]);
DISPATCH_STATIC_GLOBAL(dispatch_queue_t _dispatch_io_fds_lockq);
DISPATCH_STATIC_GLOBAL(dispatch_once_t _dispatch_io_init_pred);
static char const * const _dispatch_io_key = "io";
static void
_dispatch_io_queues_init(void *context DISPATCH_UNUSED)
{
_dispatch_io_fds_lockq = dispatch_queue_create(
"com.apple.libdispatch-io.fd_lockq", NULL);
_dispatch_io_devs_lockq = dispatch_queue_create(
"com.apple.libdispatch-io.dev_lockq", NULL);
}
#pragma mark -
#pragma mark dispatch_io_defaults
enum {
DISPATCH_IOCNTL_CHUNK_PAGES = 1,
DISPATCH_IOCNTL_LOW_WATER_CHUNKS,
DISPATCH_IOCNTL_INITIAL_DELIVERY,
DISPATCH_IOCNTL_MAX_PENDING_IO_REQS,
};
extern struct dispatch_io_defaults_s {
size_t chunk_size, low_water_chunks, max_pending_io_reqs;
bool initial_delivery;
} dispatch_io_defaults;
DISPATCH_GLOBAL_INIT(struct dispatch_io_defaults_s dispatch_io_defaults, {
.chunk_size = DIO_MAX_CHUNK_SIZE,
.low_water_chunks = DIO_DEFAULT_LOW_WATER_CHUNKS,
.max_pending_io_reqs = DIO_MAX_PENDING_IO_REQS,
});
#define _dispatch_iocntl_set_default(p, v) do { \
dispatch_io_defaults.p = (__typeof__(dispatch_io_defaults.p))(v); \
} while (0)
void
_dispatch_iocntl(uint32_t param, uint64_t value)
{
switch (param) {
case DISPATCH_IOCNTL_CHUNK_PAGES:
_dispatch_iocntl_set_default(chunk_size, value * PAGE_SIZE);
break;
case DISPATCH_IOCNTL_LOW_WATER_CHUNKS:
_dispatch_iocntl_set_default(low_water_chunks, value);
break;
case DISPATCH_IOCNTL_INITIAL_DELIVERY:
_dispatch_iocntl_set_default(initial_delivery, value);
break;
case DISPATCH_IOCNTL_MAX_PENDING_IO_REQS:
_dispatch_iocntl_set_default(max_pending_io_reqs, value);
break;
}
}
#pragma mark -
#pragma mark dispatch_io_t
static dispatch_io_t
_dispatch_io_create(dispatch_io_type_t type)
{
dispatch_io_t channel = _dispatch_object_alloc(DISPATCH_VTABLE(io),
sizeof(struct dispatch_io_s));
channel->do_next = DISPATCH_OBJECT_LISTLESS;
channel->do_targetq = _dispatch_get_default_queue(true);
channel->params.type = type;
channel->params.high = SIZE_MAX;
channel->params.low = dispatch_io_defaults.low_water_chunks *
dispatch_io_defaults.chunk_size;
channel->queue = dispatch_queue_create("com.apple.libdispatch-io.channelq",
NULL);
return channel;
}
static void
_dispatch_io_init(dispatch_io_t channel, dispatch_fd_entry_t fd_entry,
dispatch_queue_t queue, int err, void (^cleanup_handler)(int))
{
// Enqueue the cleanup handler on the suspended close queue
if (cleanup_handler) {
_dispatch_retain(queue);
dispatch_async(!err ? fd_entry->close_queue : channel->queue, ^{
dispatch_async(queue, ^{
_dispatch_channel_debug("cleanup handler invoke: err %d",
channel, err);
cleanup_handler(err);
});
_dispatch_release(queue);
});
}
if (fd_entry) {
channel->fd_entry = fd_entry;
dispatch_retain(fd_entry->barrier_queue);
dispatch_retain(fd_entry->barrier_group);
channel->barrier_queue = fd_entry->barrier_queue;
channel->barrier_group = fd_entry->barrier_group;
} else {
// Still need to create a barrier queue, since all operations go
// through it
channel->barrier_queue = dispatch_queue_create(
"com.apple.libdispatch-io.barrierq", NULL);
channel->barrier_group = dispatch_group_create();
}
}
void
_dispatch_io_dispose(dispatch_io_t channel, DISPATCH_UNUSED bool *allow_free)
{
_dispatch_object_debug(channel, "%s", __func__);
if (channel->fd_entry &&
!(channel->atomic_flags & (DIO_CLOSED|DIO_STOPPED))) {
if (channel->fd_entry->path_data) {
// This modification is safe since path_data->channel is checked
// only on close_queue (which is still suspended at this point)
channel->fd_entry->path_data->channel = NULL;
}
// Cleanup handlers will only run when all channels related to this
// fd are complete
_dispatch_fd_entry_release(channel->fd_entry);
}
if (channel->queue) {
dispatch_release(channel->queue);
}
if (channel->barrier_queue) {
dispatch_release(channel->barrier_queue);
}
if (channel->barrier_group) {
dispatch_release(channel->barrier_group);
}
}
static int
_dispatch_io_validate_type(dispatch_io_t channel, mode_t mode)
{
int err = 0;
if (S_ISDIR(mode)) {
err = EISDIR;
} else if (channel->params.type == DISPATCH_IO_RANDOM &&
(S_ISFIFO(mode) || S_ISSOCK(mode))) {
err = ESPIPE;
}
return err;
}
static int
_dispatch_io_get_error(dispatch_operation_t op, dispatch_io_t channel,
bool ignore_closed)
{
// On _any_ queue
int err;
if (op) {
channel = op->channel;
}
if (channel->atomic_flags & (DIO_CLOSED|DIO_STOPPED)) {
if (!ignore_closed || channel->atomic_flags & DIO_STOPPED) {
err = ECANCELED;
} else {
err = 0;
}
} else {
err = op ? op->fd_entry->err : channel->err;
}
return err;
}
#pragma mark -
#pragma mark dispatch_io_channels
dispatch_io_t
dispatch_io_create(dispatch_io_type_t type, dispatch_fd_t fd,
dispatch_queue_t queue, void (^cleanup_handler)(int))
{
if (type != DISPATCH_IO_STREAM && type != DISPATCH_IO_RANDOM) {
return DISPATCH_BAD_INPUT;
}
dispatch_io_t channel = _dispatch_io_create(type);
channel->fd = fd;
_dispatch_channel_debug("create", channel);
channel->fd_actual = fd;
dispatch_suspend(channel->queue);
_dispatch_retain(queue);
_dispatch_retain(channel);
_dispatch_fd_entry_init_async(fd, ^(dispatch_fd_entry_t fd_entry) {
// On barrier queue
int err = fd_entry->err;
if (!err) {
err = _dispatch_io_validate_type(channel, fd_entry->stat.mode);
}
if (!err && type == DISPATCH_IO_RANDOM) {
#if defined(_WIN32)
LARGE_INTEGER liPosition;
LARGE_INTEGER liDistance = {};
if (!SetFilePointerEx((HANDLE)fd_entry->fd, liDistance, &liPosition, FILE_CURRENT)) {
err = (int)GetLastError();
} else {
err = 0;
channel->f_ptr = liPosition.QuadPart;
}
#else
off_t f_ptr;
_dispatch_io_syscall_switch_noerr(err,
f_ptr = lseek(fd_entry->fd, 0, SEEK_CUR),
case 0: channel->f_ptr = f_ptr; break;
default: (void)dispatch_assume_zero(err); break;
);
#endif
}
channel->err = err;
_dispatch_fd_entry_retain(fd_entry);
_dispatch_io_init(channel, fd_entry, queue, err, cleanup_handler);
dispatch_resume(channel->queue);
_dispatch_object_debug(channel, "%s", __func__);
_dispatch_release(channel);
_dispatch_release(queue);
});
_dispatch_object_debug(channel, "%s", __func__);
return channel;
}
dispatch_io_t
dispatch_io_create_f(dispatch_io_type_t type, dispatch_fd_t fd,
dispatch_queue_t queue, void *context,
void (*cleanup_handler)(void *context, int error))
{
return dispatch_io_create(type, fd, queue, !cleanup_handler ? NULL :
^(int error){ cleanup_handler(context, error); });
}
#if defined(_WIN32)
#define _is_separator(ch) ((ch) == L'/' || (ch) == L'\\')
#define _dispatch_stat _wstati64
typedef struct _stati64 _dispatch_stat_t;
#else
#define _is_separator(ch) ((ch) == '/')
#define _dispatch_stat stat
typedef struct stat _dispatch_stat_t;
#endif
dispatch_io_t
dispatch_io_create_with_path(dispatch_io_type_t type, const char *path,
int oflag, mode_t mode, dispatch_queue_t queue,
void (^cleanup_handler)(int error))
{
if (type != DISPATCH_IO_STREAM && type != DISPATCH_IO_RANDOM) {
return DISPATCH_BAD_INPUT;
}
#if defined(_WIN32)
if (PathIsRelativeA(path)) {
return DISPATCH_BAD_INPUT;
}
int cchLength = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, NULL, 0);
if (!cchLength) {
dispatch_assert(GetLastError() == ERROR_NO_UNICODE_TRANSLATION);
return DISPATCH_BAD_INPUT;
}
dispatch_io_path_data_t path_data = malloc(sizeof(*path_data) + sizeof(WCHAR) * cchLength);
if (!path_data) {
return DISPATCH_OUT_OF_MEMORY;
}
path_data->pathlen = cchLength - 1; // Don't include terminating null character
cchLength = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, path, -1, path_data->path, cchLength);
if (!cchLength) {
free(path_data);
// We already checked the input when measuring buffer length.
// Any error at this point seems fatal.
DISPATCH_INTERNAL_CRASH(GetLastError(), "MultiByteToWideChar");
return DISPATCH_BAD_INPUT;
}
#else
if (!_is_separator(*path)) {
return DISPATCH_BAD_INPUT;
}
size_t pathlen = strlen(path);
dispatch_io_path_data_t path_data = malloc(sizeof(*path_data) + pathlen+1);
if (!path_data) {
return DISPATCH_OUT_OF_MEMORY;
}
path_data->pathlen = pathlen;
memcpy(path_data->path, path, pathlen + 1);
#endif
dispatch_io_t channel = _dispatch_io_create(type);
channel->fd = -1;
_dispatch_channel_debug("create with path %s", channel, path);
channel->fd_actual = -1;
path_data->channel = channel;
path_data->oflag = oflag;
path_data->mode = mode;
_dispatch_retain(queue);
_dispatch_retain(channel);
dispatch_async(channel->queue, ^{
int err = 0;
_dispatch_stat_t st;
_dispatch_io_syscall_switch_noerr(err,
#if defined(_WIN32)
_dispatch_stat(path_data->path, &st),
#else
(path_data->oflag & O_NOFOLLOW) == O_NOFOLLOW
#if __APPLE__
|| (path_data->oflag & O_SYMLINK) == O_SYMLINK
#endif
? lstat(path_data->path, &st) : stat(path_data->path, &st),
#endif
case 0:
err = _dispatch_io_validate_type(channel, st.st_mode);
break;
default:
if ((path_data->oflag & O_CREAT) &&
!_is_separator(*(path_data->path + path_data->pathlen - 1))) {
// Check parent directory
dispatch_io_path_char_t *c = NULL;
for (ssize_t i = (ssize_t)path_data->pathlen - 1; i >= 0; i--) {
if (_is_separator(path_data->path[i])) {
c = &path_data->path[i];
break;
}
}
dispatch_assert(c);
*c = 0;
int perr;
_dispatch_io_syscall_switch_noerr(perr,
_dispatch_stat(path_data->path, &st),
case 0:
// Since the parent directory exists, open() will
// create a regular file after the fd_entry has
// been filled in
st.st_mode = S_IFREG;
err = 0;
break;
);
#if defined(_WIN32)
*c = L'\\';
#else
*c = '/';
#endif
}
break;
);
channel->err = err;
if (err) {
free(path_data);
_dispatch_io_init(channel, NULL, queue, err, cleanup_handler);
_dispatch_release(channel);
_dispatch_release(queue);
return;
}
dispatch_suspend(channel->queue);
dispatch_once_f(&_dispatch_io_init_pred, NULL,
_dispatch_io_queues_init);
dispatch_async(_dispatch_io_devs_lockq, ^{
dispatch_fd_entry_t fd_entry = _dispatch_fd_entry_create_with_path(
path_data, st.st_dev, st.st_mode);
_dispatch_io_init(channel, fd_entry, queue, 0, cleanup_handler);
dispatch_resume(channel->queue);
_dispatch_object_debug(channel, "%s", __func__);
_dispatch_release(channel);
_dispatch_release(queue);
});
});
_dispatch_object_debug(channel, "%s", __func__);
return channel;
}
dispatch_io_t
dispatch_io_create_with_path_f(dispatch_io_type_t type, const char *path,
int oflag, mode_t mode, dispatch_queue_t queue, void *context,
void (*cleanup_handler)(void *context, int error))
{
return dispatch_io_create_with_path(type, path, oflag, mode, queue,
!cleanup_handler ? NULL :
^(int error){ cleanup_handler(context, error); });
}
dispatch_io_t
dispatch_io_create_with_io(dispatch_io_type_t type, dispatch_io_t in_channel,
dispatch_queue_t queue, void (^cleanup_handler)(int error))
{
if (type != DISPATCH_IO_STREAM && type != DISPATCH_IO_RANDOM) {
return DISPATCH_BAD_INPUT;
}
dispatch_io_t channel = _dispatch_io_create(type);
_dispatch_channel_debug("create with channel %p", channel, in_channel);
dispatch_suspend(channel->queue);
_dispatch_retain(queue);
_dispatch_retain(channel);
_dispatch_retain(in_channel);
dispatch_async(in_channel->queue, ^{
int err0 = _dispatch_io_get_error(NULL, in_channel, false);
if (err0) {
channel->err = err0;
_dispatch_io_init(channel, NULL, queue, err0, cleanup_handler);
dispatch_resume(channel->queue);
_dispatch_release(channel);
_dispatch_release(in_channel);
_dispatch_release(queue);
return;
}
dispatch_async(in_channel->barrier_queue, ^{
int err = _dispatch_io_get_error(NULL, in_channel, false);
// If there is no error, the fd_entry for the in_channel is valid.
// Since we are running on in_channel's queue, the fd_entry has been
// fully resolved and will stay valid for the duration of this block
if (!err) {
err = in_channel->err;
if (!err) {
err = in_channel->fd_entry->err;
}
}
if (!err) {
err = _dispatch_io_validate_type(channel,
in_channel->fd_entry->stat.mode);
}
if (!err && type == DISPATCH_IO_RANDOM && in_channel->fd != -1) {
#if defined(_WIN32)
LARGE_INTEGER liPosition;
LARGE_INTEGER liDistance = {};
if (!SetFilePointerEx((HANDLE)in_channel->fd_entry->fd, liDistance, &liPosition, FILE_CURRENT)) {
err = (int)GetLastError();
} else {
err = 0;
channel->f_ptr = liPosition.QuadPart;
}
#else
off_t f_ptr;
_dispatch_io_syscall_switch_noerr(err,
f_ptr = lseek(in_channel->fd_entry->fd, 0, SEEK_CUR),
case 0: channel->f_ptr = f_ptr; break;
default: (void)dispatch_assume_zero(err); break;
);
#endif
}
channel->err = err;
if (err) {
_dispatch_io_init(channel, NULL, queue, err, cleanup_handler);
dispatch_resume(channel->queue);
_dispatch_release(channel);
_dispatch_release(in_channel);
_dispatch_release(queue);
return;
}
if (in_channel->fd == -1) {
// in_channel was created from path
channel->fd = -1;
channel->fd_actual = -1;
mode_t mode = in_channel->fd_entry->stat.mode;
dev_t dev = in_channel->fd_entry->stat.dev;
size_t path_data_len = sizeof(struct dispatch_io_path_data_s) +
#if defined(_WIN32)
sizeof(WCHAR) * (in_channel->fd_entry->path_data->pathlen + 1);
#else
in_channel->fd_entry->path_data->pathlen + 1;
#endif
dispatch_io_path_data_t path_data = malloc(path_data_len);
memcpy(path_data, in_channel->fd_entry->path_data,
path_data_len);
path_data->channel = channel;
// lockq_io_devs is known to already exist
dispatch_async(_dispatch_io_devs_lockq, ^{
dispatch_fd_entry_t fd_entry;
fd_entry = _dispatch_fd_entry_create_with_path(path_data,
dev, mode);
_dispatch_io_init(channel, fd_entry, queue, 0,
cleanup_handler);
dispatch_resume(channel->queue);
_dispatch_release(channel);
_dispatch_release(queue);
});
} else {
dispatch_fd_entry_t fd_entry = in_channel->fd_entry;
channel->fd = in_channel->fd;
channel->fd_actual = in_channel->fd_actual;
_dispatch_fd_entry_retain(fd_entry);
_dispatch_io_init(channel, fd_entry, queue, 0, cleanup_handler);
dispatch_resume(channel->queue);
_dispatch_release(channel);
_dispatch_release(queue);
}
_dispatch_release(in_channel);
_dispatch_object_debug(channel, "%s", __func__);
});
});
_dispatch_object_debug(channel, "%s", __func__);
return channel;
}
dispatch_io_t
dispatch_io_create_with_io_f(dispatch_io_type_t type, dispatch_io_t in_channel,
dispatch_queue_t queue, void *context,
void (*cleanup_handler)(void *context, int error))
{
return dispatch_io_create_with_io(type, in_channel, queue,
!cleanup_handler ? NULL :
^(int error){ cleanup_handler(context, error); });
}
#pragma mark -
#pragma mark dispatch_io_accessors
void
dispatch_io_set_high_water(dispatch_io_t channel, size_t high_water)
{
_dispatch_retain(channel);
dispatch_async(channel->queue, ^{
_dispatch_channel_debug("set high water: %zu", channel, high_water);
if (channel->params.low > high_water) {
channel->params.low = high_water;
}
channel->params.high = high_water ? high_water : 1;
_dispatch_release(channel);
});
}
void
dispatch_io_set_low_water(dispatch_io_t channel, size_t low_water)
{
_dispatch_retain(channel);
dispatch_async(channel->queue, ^{
_dispatch_channel_debug("set low water: %zu", channel, low_water);
if (channel->params.high < low_water) {
channel->params.high = low_water ? low_water : 1;
}
channel->params.low = low_water;
_dispatch_release(channel);
});
}
void
dispatch_io_set_interval(dispatch_io_t channel, uint64_t interval,
unsigned long flags)
{
_dispatch_retain(channel);
dispatch_async(channel->queue, ^{
_dispatch_channel_debug("set interval: %llu", channel,
(unsigned long long)interval);
channel->params.interval = interval < INT64_MAX ? interval : INT64_MAX;
channel->params.interval_flags = flags;
_dispatch_release(channel);
});
}
void
_dispatch_io_set_target_queue(dispatch_io_t channel, dispatch_queue_t dq)
{
_dispatch_retain(dq);
_dispatch_retain(channel);
dispatch_async(channel->queue, ^{
dispatch_queue_t prev_dq = channel->do_targetq;
channel->do_targetq = dq;
_dispatch_release(prev_dq);
_dispatch_object_debug(channel, "%s", __func__);
_dispatch_release(channel);
});
}
dispatch_fd_t
dispatch_io_get_descriptor(dispatch_io_t channel)
{
if (channel->atomic_flags & (DIO_CLOSED|DIO_STOPPED)) {
return -1;
}
dispatch_fd_t fd = channel->fd_actual;
if (fd == -1 && !_dispatch_io_get_error(NULL, channel, false)) {
dispatch_thread_context_t ctxt =
_dispatch_thread_context_find(_dispatch_io_key);
if (ctxt && ctxt->dtc_io_in_barrier == channel) {
(void)_dispatch_fd_entry_open(channel->fd_entry, channel);
}
}
return channel->fd_actual;
}
#pragma mark -
#pragma mark dispatch_io_operations
static void
_dispatch_io_stop(dispatch_io_t channel)
{
_dispatch_channel_debug("stop", channel);
(void)os_atomic_or2o(channel, atomic_flags, DIO_STOPPED, relaxed);
_dispatch_retain(channel);
dispatch_async(channel->queue, ^{
dispatch_async(channel->barrier_queue, ^{
_dispatch_object_debug(channel, "%s", __func__);
dispatch_fd_entry_t fd_entry = channel->fd_entry;
if (fd_entry) {
_dispatch_channel_debug("stop cleanup", channel);
_dispatch_fd_entry_cleanup_operations(fd_entry, channel);
if (!(channel->atomic_flags & DIO_CLOSED)) {
if (fd_entry->path_data) {
fd_entry->path_data->channel = NULL;
}
channel->fd_entry = NULL;
_dispatch_fd_entry_release(fd_entry);
}
} else if (channel->fd != -1) {
// Stop after close, need to check if fd_entry still exists
_dispatch_retain(channel);
dispatch_async(_dispatch_io_fds_lockq, ^{
_dispatch_object_debug(channel, "%s", __func__);
_dispatch_channel_debug("stop cleanup after close",
channel);
dispatch_fd_entry_t fdi;
uintptr_t hash = DIO_HASH(channel->fd);
LIST_FOREACH(fdi, &_dispatch_io_fds[hash], fd_list) {
if (fdi->fd == channel->fd) {
_dispatch_fd_entry_cleanup_operations(fdi, channel);
break;
}
}
_dispatch_release(channel);
});
}
_dispatch_release(channel);
});
});
}
void
dispatch_io_close(dispatch_io_t channel, unsigned long flags)
{
if (flags & DISPATCH_IO_STOP) {
// Don't stop an already stopped channel
if (channel->atomic_flags & DIO_STOPPED) {
return;
}
return _dispatch_io_stop(channel);
}
// Don't close an already closed or stopped channel
if (channel->atomic_flags & (DIO_CLOSED|DIO_STOPPED)) {
return;
}
_dispatch_retain(channel);
dispatch_async(channel->queue, ^{
dispatch_async(channel->barrier_queue, ^{
_dispatch_object_debug(channel, "%s", __func__);
_dispatch_channel_debug("close", channel);
if (!(channel->atomic_flags & (DIO_CLOSED|DIO_STOPPED))) {
(void)os_atomic_or2o(channel, atomic_flags, DIO_CLOSED,
relaxed);
dispatch_fd_entry_t fd_entry = channel->fd_entry;
if (fd_entry) {
if (fd_entry->path_data) {
fd_entry->path_data->channel = NULL;
}
channel->fd_entry = NULL;
_dispatch_fd_entry_release(fd_entry);
}
}
_dispatch_release(channel);
});
});
}
void
dispatch_io_barrier(dispatch_io_t channel, dispatch_block_t barrier)
{
_dispatch_retain(channel);
dispatch_async(channel->queue, ^{
dispatch_queue_t io_q = channel->do_targetq;
dispatch_queue_t barrier_queue = channel->barrier_queue;
dispatch_group_t barrier_group = channel->barrier_group;
dispatch_async(barrier_queue, ^{
dispatch_suspend(barrier_queue);
dispatch_group_notify(barrier_group, io_q, ^{
dispatch_thread_context_s io_ctxt = {
.dtc_key = _dispatch_io_key,
.dtc_io_in_barrier = channel,
};
_dispatch_object_debug(channel, "%s", __func__);
_dispatch_thread_context_push(&io_ctxt);
barrier();
_dispatch_thread_context_pop(&io_ctxt);
dispatch_resume(barrier_queue);
_dispatch_release(channel);
});
});
});
}
void
dispatch_io_barrier_f(dispatch_io_t channel, void *context,
dispatch_function_t barrier)
{
return dispatch_io_barrier(channel, ^{ barrier(context); });
}
void
dispatch_io_read(dispatch_io_t channel, off_t offset, size_t length,
dispatch_queue_t queue, dispatch_io_handler_t handler)
{
_dispatch_retain(channel);
_dispatch_retain(queue);
dispatch_async(channel->queue, ^{
dispatch_operation_t op;
op = _dispatch_operation_create(DOP_DIR_READ, channel, offset,
length, dispatch_data_empty, queue, handler);
if (op) {
dispatch_queue_t barrier_q = channel->barrier_queue;
dispatch_async(barrier_q, ^{
_dispatch_operation_enqueue(op, DOP_DIR_READ,
dispatch_data_empty);
});
}
_dispatch_release(channel);
_dispatch_release(queue);
});
}
void
dispatch_io_read_f(dispatch_io_t channel, off_t offset, size_t length,
dispatch_queue_t queue, void *context,
dispatch_io_handler_function_t handler)
{
return dispatch_io_read(channel, offset, length, queue,
^(bool done, dispatch_data_t d, int error){
handler(context, done, d, error);
});
}
void
dispatch_io_write(dispatch_io_t channel, off_t offset, dispatch_data_t data,
dispatch_queue_t queue, dispatch_io_handler_t handler)
{
_dispatch_io_data_retain(data);
_dispatch_retain(channel);
_dispatch_retain(queue);
dispatch_async(channel->queue, ^{
dispatch_operation_t op;
op = _dispatch_operation_create(DOP_DIR_WRITE, channel, offset,
dispatch_data_get_size(data), data, queue, handler);
if (op) {
dispatch_queue_t barrier_q = channel->barrier_queue;
dispatch_async(barrier_q, ^{
_dispatch_operation_enqueue(op, DOP_DIR_WRITE, data);
_dispatch_io_data_release(data);
});
} else {
_dispatch_io_data_release(data);
}
_dispatch_release(channel);
_dispatch_release(queue);
});
}
void
dispatch_io_write_f(dispatch_io_t channel, off_t offset, dispatch_data_t data,
dispatch_queue_t queue, void *context,
dispatch_io_handler_function_t handler)
{
return dispatch_io_write(channel, offset, data, queue,
^(bool done, dispatch_data_t d, int error){
handler(context, done, d, error);
});
}
void
dispatch_read(dispatch_fd_t fd, size_t length, dispatch_queue_t queue,
void (^handler)(dispatch_data_t, int))
{
_dispatch_retain(queue);
_dispatch_fd_entry_init_async(fd, ^(dispatch_fd_entry_t fd_entry) {
// On barrier queue
if (fd_entry->err) {
int err = fd_entry->err;
dispatch_async(queue, ^{
_dispatch_fd_debug("convenience handler invoke", fd);
handler(dispatch_data_empty, err);
});
_dispatch_release(queue);
return;
}
// Safe to access fd_entry on barrier queue
dispatch_io_t channel = fd_entry->convenience_channel;
if (!channel) {
channel = _dispatch_io_create(DISPATCH_IO_STREAM);
channel->fd = fd;
channel->fd_actual = fd;
channel->fd_entry = fd_entry;
dispatch_retain(fd_entry->barrier_queue);
dispatch_retain(fd_entry->barrier_group);
channel->barrier_queue = fd_entry->barrier_queue;
channel->barrier_group = fd_entry->barrier_group;
fd_entry->convenience_channel = channel;
}
__block dispatch_data_t deliver_data = dispatch_data_empty;
__block int err = 0;
dispatch_async(fd_entry->close_queue, ^{
dispatch_async(queue, ^{
_dispatch_fd_debug("convenience handler invoke", fd);
handler(deliver_data, err);
_dispatch_io_data_release(deliver_data);
});
_dispatch_release(queue);
});
dispatch_operation_t op =
_dispatch_operation_create(DOP_DIR_READ, channel, 0,
length, dispatch_data_empty,
_dispatch_get_default_queue(false),
^(bool done, dispatch_data_t data, int error) {
if (data) {
data = dispatch_data_create_concat(deliver_data, data);
_dispatch_io_data_release(deliver_data);
deliver_data = data;
}
if (done) {
err = error;
}
});
if (op) {
_dispatch_operation_enqueue(op, DOP_DIR_READ, dispatch_data_empty);
}
});
}
void
dispatch_read_f(dispatch_fd_t fd, size_t length, dispatch_queue_t queue,
void *context, void (*handler)(void *, dispatch_data_t, int))
{
return dispatch_read(fd, length, queue, ^(dispatch_data_t d, int error){
handler(context, d, error);