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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
|
/* $Id: aiomgr.cpp $ */
/** @file
* IPRT - Async I/O manager.
*/
/*
* Copyright (C) 2013-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include <iprt/aiomgr.h>
#include <iprt/err.h>
#include <iprt/asm.h>
#include <iprt/mem.h>
#include <iprt/file.h>
#include <iprt/list.h>
#include <iprt/thread.h>
#include <iprt/assert.h>
#include <iprt/string.h>
#include <iprt/critsect.h>
#include <iprt/memcache.h>
#include <iprt/semaphore.h>
#include <iprt/queueatomic.h>
#include "internal/magics.h"
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/** Pointer to an internal async I/O file instance. */
typedef struct RTAIOMGRFILEINT *PRTAIOMGRFILEINT;
/**
* Blocking event types.
*/
typedef enum RTAIOMGREVENT
{
/** Invalid tye */
RTAIOMGREVENT_INVALID = 0,
/** No event pending. */
RTAIOMGREVENT_NO_EVENT,
/** A file is added to the manager. */
RTAIOMGREVENT_FILE_ADD,
/** A file is about to be closed. */
RTAIOMGREVENT_FILE_CLOSE,
/** The async I/O manager is shut down. */
RTAIOMGREVENT_SHUTDOWN,
/** 32bit hack */
RTAIOMGREVENT_32BIT_HACK = 0x7fffffff
} RTAIOMGREVENT;
/**
* Async I/O manager instance data.
*/
typedef struct RTAIOMGRINT
{
/** Magic value. */
uint32_t u32Magic;
/** Reference count. */
volatile uint32_t cRefs;
/** Async I/O context handle. */
RTFILEAIOCTX hAioCtx;
/** async I/O thread. */
RTTHREAD hThread;
/** List of files assigned to this manager. */
RTLISTANCHOR ListFiles;
/** Number of requests active currently. */
unsigned cReqsActive;
/** Number of maximum requests active. */
uint32_t cReqsActiveMax;
/** Memory cache for requests. */
RTMEMCACHE hMemCacheReqs;
/** Critical section protecting the blocking event handling. */
RTCRITSECT CritSectBlockingEvent;
/** Event semaphore for blocking external events.
* The caller waits on it until the async I/O manager
* finished processing the event. */
RTSEMEVENT hEventSemBlock;
/** Blocking event type */
volatile RTAIOMGREVENT enmBlockingEvent;
/** Event type data */
union
{
/** The file to be added */
volatile PRTAIOMGRFILEINT pFileAdd;
/** The file to be closed */
volatile PRTAIOMGRFILEINT pFileClose;
} BlockingEventData;
} RTAIOMGRINT;
/** Pointer to an internal async I/O manager instance. */
typedef RTAIOMGRINT *PRTAIOMGRINT;
/**
* Async I/O manager file instance data.
*/
typedef struct RTAIOMGRFILEINT
{
/** Magic value. */
uint32_t u32Magic;
/** Reference count. */
volatile uint32_t cRefs;
/** Flags. */
uint32_t fFlags;
/** Opaque user data passed on creation. */
void *pvUser;
/** File handle. */
RTFILE hFile;
/** async I/O manager this file belongs to. */
PRTAIOMGRINT pAioMgr;
/** Work queue for new requests. */
RTQUEUEATOMIC QueueReqs;
/** Completion callback for this file. */
PFNRTAIOMGRREQCOMPLETE pfnReqCompleted;
/** Data for exclusive use by the assigned async I/O manager. */
struct
{
/** List node of assigned files for a async I/O manager. */
RTLISTNODE NodeAioMgrFiles;
/** List of requests waiting for submission. */
RTLISTANCHOR ListWaitingReqs;
/** Number of requests currently being processed for this endpoint
* (excluded flush requests). */
unsigned cReqsActive;
} AioMgr;
} RTAIOMGRFILEINT;
/** Flag whether the file is closed. */
#define RTAIOMGRFILE_FLAGS_CLOSING RT_BIT_32(1)
/**
* Request type.
*/
typedef enum RTAIOMGRREQTYPE
{
/** Invalid request type. */
RTAIOMGRREQTYPE_INVALID = 0,
/** Read reques type. */
RTAIOMGRREQTYPE_READ,
/** Write request. */
RTAIOMGRREQTYPE_WRITE,
/** Flush request. */
RTAIOMGRREQTYPE_FLUSH,
/** Prefetech request. */
RTAIOMGRREQTYPE_PREFETCH,
/** 32bit hack. */
RTAIOMGRREQTYPE_32BIT_HACK = 0x7fffffff
} RTAIOMGRREQTYPE;
/** Pointer to a reques type. */
typedef RTAIOMGRREQTYPE *PRTAIOMGRREQTYPE;
/**
* Async I/O manager request.
*/
typedef struct RTAIOMGRREQ
{
/** Atomic queue work item. */
RTQUEUEATOMICITEM WorkItem;
/** Node for a waiting list. */
RTLISTNODE NodeWaitingList;
/** Request flags. */
uint32_t fFlags;
/** Transfer type. */
RTAIOMGRREQTYPE enmType;
/** Assigned file request. */
RTFILEAIOREQ hReqIo;
/** File the request belongs to. */
PRTAIOMGRFILEINT pFile;
/** Opaque user data. */
void *pvUser;
/** Start offset */
RTFOFF off;
/** Data segment. */
RTSGSEG DataSeg;
/** When non-zero the segment uses a bounce buffer because the provided buffer
* doesn't meet host requirements. */
size_t cbBounceBuffer;
/** Pointer to the used bounce buffer if any. */
void *pvBounceBuffer;
/** Start offset in the bounce buffer to copy from. */
uint32_t offBounceBuffer;
} RTAIOMGRREQ;
/** Pointer to a I/O manager request. */
typedef RTAIOMGRREQ *PRTAIOMGRREQ;
/** Flag whether the request was prepared already. */
#define RTAIOMGRREQ_FLAGS_PREPARED RT_BIT_32(0)
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
#define RTAIOMGR_VALID_RETURN_RC(a_hAioMgr, a_rc) \
do { \
AssertPtrReturn((a_hAioMgr), (a_rc)); \
AssertReturn((a_hAioMgr)->u32Magic == RTAIOMGR_MAGIC, (a_rc)); \
} while (0)
/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
#define RTAIOMGR_VALID_RETURN(a_hAioMgr) RTAIOMGR_VALID_RETURN_RC((hAioMgr), VERR_INVALID_HANDLE)
/** Validates a handle and returns (void) if not valid. */
#define RTAIOMGR_VALID_RETURN_VOID(a_hAioMgr) \
do { \
AssertPtrReturnVoid(a_hAioMgr); \
AssertReturnVoid((a_hAioMgr)->u32Magic == RTAIOMGR_MAGIC); \
} while (0)
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
static int rtAioMgrReqsEnqueue(PRTAIOMGRINT pThis, PRTAIOMGRFILEINT pFile,
PRTFILEAIOREQ pahReqs, unsigned cReqs);
/**
* Removes an endpoint from the currently assigned manager.
*
* @returns TRUE if there are still requests pending on the current manager for this endpoint.
* FALSE otherwise.
* @param pFile The endpoint to remove.
*/
static bool rtAioMgrFileRemove(PRTAIOMGRFILEINT pFile)
{
/* Make sure that there is no request pending on this manager for the endpoint. */
if (!pFile->AioMgr.cReqsActive)
{
RTListNodeRemove(&pFile->AioMgr.NodeAioMgrFiles);
return false;
}
return true;
}
/**
* Allocate a new I/O request.
*
* @returns Pointer to the allocated request or NULL if out of memory.
* @param pThis The async I/O manager instance.
*/
static PRTAIOMGRREQ rtAioMgrReqAlloc(PRTAIOMGRINT pThis)
{
return (PRTAIOMGRREQ)RTMemCacheAlloc(pThis->hMemCacheReqs);
}
/**
* Frees an I/O request.
*
* @returns nothing.
* @param pThis The async I/O manager instance.
* @param pReq The request to free.
*/
static void rtAioMgrReqFree(PRTAIOMGRINT pThis, PRTAIOMGRREQ pReq)
{
if (pReq->cbBounceBuffer)
{
AssertPtr(pReq->pvBounceBuffer);
RTMemPageFree(pReq->pvBounceBuffer, pReq->cbBounceBuffer);
pReq->pvBounceBuffer = NULL;
pReq->cbBounceBuffer = 0;
}
pReq->fFlags = 0;
RTAioMgrFileRelease(pReq->pFile);
RTMemCacheFree(pThis->hMemCacheReqs, pReq);
}
static void rtAioMgrReqCompleteRc(PRTAIOMGRINT pThis, PRTAIOMGRREQ pReq,
int rcReq, size_t cbTransfered)
{
int rc = VINF_SUCCESS;
PRTAIOMGRFILEINT pFile;
pFile = pReq->pFile;
pThis->cReqsActive--;
pFile->AioMgr.cReqsActive--;
/*
* It is possible that the request failed on Linux with kernels < 2.6.23
* if the passed buffer was allocated with remap_pfn_range or if the file
* is on an NFS endpoint which does not support async and direct I/O at the same time.
* The endpoint will be migrated to a failsafe manager in case a request fails.
*/
if (RT_FAILURE(rcReq))
{
pFile->pfnReqCompleted(pFile, rcReq, pReq->pvUser);
rtAioMgrReqFree(pThis, pReq);
}
else
{
/*
* Restart an incomplete transfer.
* This usually means that the request will return an error now
* but to get the cause of the error (disk full, file too big, I/O error, ...)
* the transfer needs to be continued.
*/
if (RT_UNLIKELY( cbTransfered < pReq->DataSeg.cbSeg
|| ( pReq->cbBounceBuffer
&& cbTransfered < pReq->cbBounceBuffer)))
{
RTFOFF offStart;
size_t cbToTransfer;
uint8_t *pbBuf = NULL;
Assert(cbTransfered % 512 == 0);
if (pReq->cbBounceBuffer)
{
AssertPtr(pReq->pvBounceBuffer);
offStart = (pReq->off & ~((RTFOFF)512-1)) + cbTransfered;
cbToTransfer = pReq->cbBounceBuffer - cbTransfered;
pbBuf = (uint8_t *)pReq->pvBounceBuffer + cbTransfered;
}
else
{
Assert(!pReq->pvBounceBuffer);
offStart = pReq->off + cbTransfered;
cbToTransfer = pReq->DataSeg.cbSeg - cbTransfered;
pbBuf = (uint8_t *)pReq->DataSeg.pvSeg + cbTransfered;
}
if ( pReq->enmType == RTAIOMGRREQTYPE_PREFETCH
|| pReq->enmType == RTAIOMGRREQTYPE_READ)
{
rc = RTFileAioReqPrepareRead(pReq->hReqIo, pFile->hFile, offStart,
pbBuf, cbToTransfer, pReq);
}
else
{
AssertMsg(pReq->enmType == RTAIOMGRREQTYPE_WRITE,
("Invalid transfer type\n"));
rc = RTFileAioReqPrepareWrite(pReq->hReqIo, pFile->hFile, offStart,
pbBuf, cbToTransfer, pReq);
}
AssertRC(rc);
rc = rtAioMgrReqsEnqueue(pThis, pFile, &pReq->hReqIo, 1);
AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
("Unexpected return code rc=%Rrc\n", rc));
}
else if (pReq->enmType == RTAIOMGRREQTYPE_PREFETCH)
{
Assert(pReq->cbBounceBuffer);
pReq->enmType = RTAIOMGRREQTYPE_WRITE;
memcpy(((uint8_t *)pReq->pvBounceBuffer) + pReq->offBounceBuffer,
pReq->DataSeg.pvSeg,
pReq->DataSeg.cbSeg);
/* Write it now. */
RTFOFF offStart = pReq->off & ~(RTFOFF)(512-1);
size_t cbToTransfer = RT_ALIGN_Z(pReq->DataSeg.cbSeg + (pReq->off - offStart), 512);
rc = RTFileAioReqPrepareWrite(pReq->hReqIo, pFile->hFile,
offStart, pReq->pvBounceBuffer, cbToTransfer, pReq);
AssertRC(rc);
rc = rtAioMgrReqsEnqueue(pThis, pFile, &pReq->hReqIo, 1);
AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
("Unexpected return code rc=%Rrc\n", rc));
}
else
{
if (RT_SUCCESS(rc) && pReq->cbBounceBuffer)
{
if (pReq->enmType == RTAIOMGRREQTYPE_READ)
memcpy(pReq->DataSeg.pvSeg,
((uint8_t *)pReq->pvBounceBuffer) + pReq->offBounceBuffer,
pReq->DataSeg.cbSeg);
}
/* Call completion callback */
pFile->pfnReqCompleted(pFile, rcReq, pReq->pvUser);
rtAioMgrReqFree(pThis, pReq);
}
} /* request completed successfully */
}
/**
* Wrapper around rtAioMgrReqCompleteRc().
*/
static void rtAioMgrReqComplete(PRTAIOMGRINT pThis, RTFILEAIOREQ hReq)
{
size_t cbTransfered = 0;
int rcReq = RTFileAioReqGetRC(hReq, &cbTransfered);
PRTAIOMGRREQ pReq = (PRTAIOMGRREQ)RTFileAioReqGetUser(hReq);
rtAioMgrReqCompleteRc(pThis, pReq, rcReq, cbTransfered);
}
/**
* Wrapper around RTFIleAioCtxSubmit() which is also doing error handling.
*/
static int rtAioMgrReqsEnqueue(PRTAIOMGRINT pThis, PRTAIOMGRFILEINT pFile,
PRTFILEAIOREQ pahReqs, unsigned cReqs)
{
pThis->cReqsActive += cReqs;
pFile->AioMgr.cReqsActive += cReqs;
int rc = RTFileAioCtxSubmit(pThis->hAioCtx, pahReqs, cReqs);
if (RT_FAILURE(rc))
{
if (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES)
{
/* Append any not submitted task to the waiting list. */
for (size_t i = 0; i < cReqs; i++)
{
int rcReq = RTFileAioReqGetRC(pahReqs[i], NULL);
if (rcReq != VERR_FILE_AIO_IN_PROGRESS)
{
PRTAIOMGRREQ pReq = (PRTAIOMGRREQ)RTFileAioReqGetUser(pahReqs[i]);
Assert(pReq->hReqIo == pahReqs[i]);
RTListAppend(&pFile->AioMgr.ListWaitingReqs, &pReq->NodeWaitingList);
pThis->cReqsActive--;
pFile->AioMgr.cReqsActive--;
}
}
pThis->cReqsActiveMax = pThis->cReqsActive;
rc = VINF_SUCCESS;
}
else /* Another kind of error happened (full disk, ...) */
{
/* An error happened. Find out which one caused the error and resubmit all other tasks. */
for (size_t i = 0; i < cReqs; i++)
{
PRTAIOMGRREQ pReq = (PRTAIOMGRREQ)RTFileAioReqGetUser(pahReqs[i]);
int rcReq = RTFileAioReqGetRC(pahReqs[i], NULL);
if (rcReq == VERR_FILE_AIO_NOT_SUBMITTED)
{
/* We call ourself again to do any error handling which might come up now. */
rc = rtAioMgrReqsEnqueue(pThis, pFile, &pahReqs[i], 1);
AssertRC(rc);
}
else if (rcReq != VERR_FILE_AIO_IN_PROGRESS)
rtAioMgrReqCompleteRc(pThis, pReq, rcReq, 0);
}
}
}
return VINF_SUCCESS;
}
/**
* Adds a list of requests to the waiting list.
*
* @returns nothing.
* @param pFile The file instance to add the requests to.
* @param pReqsHead The head of the request list to add.
*/
static void rtAioMgrFileAddReqsToWaitingList(PRTAIOMGRFILEINT pFile, PRTAIOMGRREQ pReqsHead)
{
while (pReqsHead)
{
PRTAIOMGRREQ pReqCur = pReqsHead;
pReqsHead = (PRTAIOMGRREQ)pReqsHead->WorkItem.pNext;
pReqCur->WorkItem.pNext = NULL;
RTListAppend(&pFile->AioMgr.ListWaitingReqs, &pReqCur->NodeWaitingList);
}
}
/**
* Prepare the native I/o request ensuring that all alignment prerequisites of
* the host are met.
*
* @returns IPRT statuse code.
* @param pFile The file instance data.
* @param pReq The request to prepare.
*/
static int rtAioMgrReqPrepareNonBuffered(PRTAIOMGRFILEINT pFile, PRTAIOMGRREQ pReq)
{
int rc = VINF_SUCCESS;
RTFOFF offStart = pReq->off & ~(RTFOFF)(512-1);
size_t cbToTransfer = RT_ALIGN_Z(pReq->DataSeg.cbSeg + (pReq->off - offStart), 512);
void *pvBuf = pReq->DataSeg.pvSeg;
bool fAlignedReq = cbToTransfer == pReq->DataSeg.cbSeg
&& offStart == pReq->off;
/*
* Check if the alignment requirements are met.
* Offset, transfer size and buffer address
* need to be on a 512 boundary.
*/
if ( !fAlignedReq
/** @todo || ((pEpClassFile->uBitmaskAlignment & (RTR3UINTPTR)pvBuf) != (RTR3UINTPTR)pvBuf) */)
{
/* Create bounce buffer. */
pReq->cbBounceBuffer = cbToTransfer;
AssertMsg(pReq->off >= offStart, ("Overflow in calculation off=%llu offStart=%llu\n",
pReq->off, offStart));
pReq->offBounceBuffer = pReq->off - offStart;
/** @todo I think we need something like a RTMemAllocAligned method here.
* Current assumption is that the maximum alignment is 4096byte
* (GPT disk on Windows)
* so we can use RTMemPageAlloc here.
*/
pReq->pvBounceBuffer = RTMemPageAlloc(cbToTransfer);
if (RT_LIKELY(pReq->pvBounceBuffer))
{
pvBuf = pReq->pvBounceBuffer;
if (pReq->enmType == RTAIOMGRREQTYPE_WRITE)
{
if ( RT_UNLIKELY(cbToTransfer != pReq->DataSeg.cbSeg)
|| RT_UNLIKELY(offStart != pReq->off))
{
/* We have to fill the buffer first before we can update the data. */
pReq->enmType = RTAIOMGRREQTYPE_WRITE;
}
else
memcpy(pvBuf, pReq->DataSeg.pvSeg, pReq->DataSeg.cbSeg);
}
}
else
rc = VERR_NO_MEMORY;
}
else
pReq->cbBounceBuffer = 0;
if (RT_SUCCESS(rc))
{
if (pReq->enmType == RTAIOMGRREQTYPE_WRITE)
{
rc = RTFileAioReqPrepareWrite(pReq->hReqIo, pFile->hFile,
offStart, pvBuf, cbToTransfer, pReq);
}
else /* Read or prefetch request. */
rc = RTFileAioReqPrepareRead(pReq->hReqIo, pFile->hFile,
offStart, pvBuf, cbToTransfer, pReq);
AssertRC(rc);
pReq->fFlags |= RTAIOMGRREQ_FLAGS_PREPARED;
}
return rc;
}
/**
* Prepare a new request for enqueuing.
*
* @returns IPRT status code.
* @param pReq The request to prepare.
* @param phReqIo Where to store the handle to the native I/O request on success.
*/
static int rtAioMgrPrepareReq(PRTAIOMGRREQ pReq, PRTFILEAIOREQ phReqIo)
{
int rc = VINF_SUCCESS;
PRTAIOMGRFILEINT pFile = pReq->pFile;
switch (pReq->enmType)
{
case RTAIOMGRREQTYPE_FLUSH:
{
rc = RTFileAioReqPrepareFlush(pReq->hReqIo, pFile->hFile, pReq);
break;
}
case RTAIOMGRREQTYPE_READ:
case RTAIOMGRREQTYPE_WRITE:
{
rc = rtAioMgrReqPrepareNonBuffered(pFile, pReq);
break;
}
default:
AssertMsgFailed(("Invalid transfer type %d\n", pReq->enmType));
} /* switch transfer type */
if (RT_SUCCESS(rc))
*phReqIo = pReq->hReqIo;
return rc;
}
/**
* Prepare newly submitted requests for processing.
*
* @returns IPRT status code
* @param pThis The async I/O manager instance data.
* @param pFile The file instance.
* @param pReqsNew The list of new requests to prepare.
*/
static int rtAioMgrPrepareNewReqs(PRTAIOMGRINT pThis,
PRTAIOMGRFILEINT pFile,
PRTAIOMGRREQ pReqsNew)
{
RTFILEAIOREQ apReqs[20];
unsigned cRequests = 0;
int rc = VINF_SUCCESS;
/* Go through the list and queue the requests. */
while ( pReqsNew
&& (pThis->cReqsActive + cRequests < pThis->cReqsActiveMax)
&& RT_SUCCESS(rc))
{
PRTAIOMGRREQ pCurr = pReqsNew;
pReqsNew = (PRTAIOMGRREQ)pReqsNew->WorkItem.pNext;
pCurr->WorkItem.pNext = NULL;
AssertMsg(VALID_PTR(pCurr->pFile) && (pCurr->pFile == pFile),
("Files do not match\n"));
AssertMsg(!(pCurr->fFlags & RTAIOMGRREQ_FLAGS_PREPARED),
("Request on the new list is already prepared\n"));
rc = rtAioMgrPrepareReq(pCurr, &apReqs[cRequests]);
if (RT_FAILURE(rc))
rtAioMgrReqCompleteRc(pThis, pCurr, rc, 0);
else
cRequests++;
/* Queue the requests if the array is full. */
if (cRequests == RT_ELEMENTS(apReqs))
{
rc = rtAioMgrReqsEnqueue(pThis, pFile, apReqs, cRequests);
cRequests = 0;
AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
("Unexpected return code\n"));
}
}
if (cRequests)
{
rc = rtAioMgrReqsEnqueue(pThis, pFile, apReqs, cRequests);
AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
("Unexpected return code rc=%Rrc\n", rc));
}
if (pReqsNew)
{
/* Add the rest of the tasks to the pending list */
rtAioMgrFileAddReqsToWaitingList(pFile, pReqsNew);
}
/* Insufficient resources are not fatal. */
if (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES)
rc = VINF_SUCCESS;
return rc;
}
/**
* Queues waiting requests.
*
* @returns IPRT status code.
* @param pThis The async I/O manager instance data.
* @param pFile The file to get the requests from.
*/
static int rtAioMgrQueueWaitingReqs(PRTAIOMGRINT pThis, PRTAIOMGRFILEINT pFile)
{
RTFILEAIOREQ apReqs[20];
unsigned cRequests = 0;
int rc = VINF_SUCCESS;
/* Go through the list and queue the requests. */
PRTAIOMGRREQ pReqIt;
PRTAIOMGRREQ pReqItNext;
RTListForEachSafe(&pFile->AioMgr.ListWaitingReqs, pReqIt, pReqItNext, RTAIOMGRREQ, NodeWaitingList)
{
RTListNodeRemove(&pReqIt->NodeWaitingList);
AssertMsg(VALID_PTR(pReqIt->pFile) && (pReqIt->pFile == pFile),
("Files do not match\n"));
if (!(pReqIt->fFlags & RTAIOMGRREQ_FLAGS_PREPARED))
{
rc = rtAioMgrPrepareReq(pReqIt, &apReqs[cRequests]);
if (RT_FAILURE(rc))
rtAioMgrReqCompleteRc(pThis, pReqIt, rc, 0);
else
cRequests++;
}
else
{
apReqs[cRequests] = pReqIt->hReqIo;
cRequests++;
}
/* Queue the requests if the array is full. */
if (cRequests == RT_ELEMENTS(apReqs))
{
rc = rtAioMgrReqsEnqueue(pThis, pFile, apReqs, cRequests);
cRequests = 0;
AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
("Unexpected return code\n"));
}
}
if (cRequests)
{
rc = rtAioMgrReqsEnqueue(pThis, pFile, apReqs, cRequests);
AssertMsg(RT_SUCCESS(rc) || (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES),
("Unexpected return code rc=%Rrc\n", rc));
}
/* Insufficient resources are not fatal. */
if (rc == VERR_FILE_AIO_INSUFFICIENT_RESSOURCES)
rc = VINF_SUCCESS;
return rc;
}
/**
* Adds all pending requests for the given file.
*
* @returns IPRT status code.
* @param pThis The async I/O manager instance data.
* @param pFile The file to get the requests from.
*/
static int rtAioMgrQueueReqs(PRTAIOMGRINT pThis, PRTAIOMGRFILEINT pFile)
{
int rc = VINF_SUCCESS;
/* Check the pending list first */
if (!RTListIsEmpty(&pFile->AioMgr.ListWaitingReqs))
rc = rtAioMgrQueueWaitingReqs(pThis, pFile);
if ( RT_SUCCESS(rc)
&& RTListIsEmpty(&pFile->AioMgr.ListWaitingReqs))
{
PRTAIOMGRREQ pReqsNew = (PRTAIOMGRREQ)RTQueueAtomicRemoveAll(&pFile->QueueReqs);
if (pReqsNew)
{
rc = rtAioMgrPrepareNewReqs(pThis, pFile, pReqsNew);
AssertRC(rc);
}
}
return rc;
}
/**
* Checks all files for new requests.
*
* @returns IPRT status code.
* @param pThis The I/O manager instance data.
*/
static int rtAioMgrCheckFiles(PRTAIOMGRINT pThis)
{
int rc = VINF_SUCCESS;
PRTAIOMGRFILEINT pIt;
RTListForEach(&pThis->ListFiles, pIt, RTAIOMGRFILEINT, AioMgr.NodeAioMgrFiles)
{
rc = rtAioMgrQueueReqs(pThis, pIt);
if (RT_FAILURE(rc))
return rc;
}
return rc;
}
/**
* Process a blocking event from the outside.
*
* @returns IPRT status code.
* @param pThis The async I/O manager instance data.
*/
static int rtAioMgrProcessBlockingEvent(PRTAIOMGRINT pThis)
{
int rc = VINF_SUCCESS;
bool fNotifyWaiter = false;
switch (pThis->enmBlockingEvent)
{
case RTAIOMGREVENT_NO_EVENT:
/* Nothing to do. */
break;
case RTAIOMGREVENT_FILE_ADD:
{
PRTAIOMGRFILEINT pFile = ASMAtomicReadPtrT(&pThis->BlockingEventData.pFileAdd, PRTAIOMGRFILEINT);
AssertMsg(VALID_PTR(pFile), ("Adding file event without a file to add\n"));
RTListAppend(&pThis->ListFiles, &pFile->AioMgr.NodeAioMgrFiles);
fNotifyWaiter = true;
break;
}
case RTAIOMGREVENT_FILE_CLOSE:
{
PRTAIOMGRFILEINT pFile = ASMAtomicReadPtrT(&pThis->BlockingEventData.pFileClose, PRTAIOMGRFILEINT);
AssertMsg(VALID_PTR(pFile), ("Close file event without a file to close\n"));
if (!(pFile->fFlags & RTAIOMGRFILE_FLAGS_CLOSING))
{
/* Make sure all requests finished. Process the queues a last time first. */
rc = rtAioMgrQueueReqs(pThis, pFile);
AssertRC(rc);
pFile->fFlags |= RTAIOMGRFILE_FLAGS_CLOSING;
fNotifyWaiter = !rtAioMgrFileRemove(pFile);
}
else if (!pFile->AioMgr.cReqsActive)
fNotifyWaiter = true;
break;
}
case RTAIOMGREVENT_SHUTDOWN:
{
if (!pThis->cReqsActive)
fNotifyWaiter = true;
break;
}
default:
AssertReleaseMsgFailed(("Invalid event type %d\n", pThis->enmBlockingEvent));
}
if (fNotifyWaiter)
{
/* Release the waiting thread. */
rc = RTSemEventSignal(pThis->hEventSemBlock);
AssertRC(rc);
}
return rc;
}
/**
* async I/O manager worker loop.
*
* @returns IPRT status code.
* @param hThreadSelf The thread handle this worker belongs to.
* @param pvUser Opaque user data (Pointer to async I/O manager instance).
*/
static DECLCALLBACK(int) rtAioMgrWorker(RTTHREAD hThreadSelf, void *pvUser)
{
PRTAIOMGRINT pThis = (PRTAIOMGRINT)pvUser;
/*bool fRunning = true;*/
int rc = VINF_SUCCESS;
do
{
uint32_t cReqsCompleted = 0;
RTFILEAIOREQ ahReqsCompleted[32];
rc = RTFileAioCtxWait(pThis->hAioCtx, 1, RT_INDEFINITE_WAIT, &ahReqsCompleted[0],
RT_ELEMENTS(ahReqsCompleted), &cReqsCompleted);
if (rc == VERR_INTERRUPTED)
{
/* Process external event. */
rtAioMgrProcessBlockingEvent(pThis);
rc = rtAioMgrCheckFiles(pThis);
}
else if (RT_FAILURE(rc))
{
/* Something bad happened. */
/** @todo */
}
else
{
/* Requests completed. */
for (uint32_t i = 0; i < cReqsCompleted; i++)
rtAioMgrReqComplete(pThis, ahReqsCompleted[i]);
/* Check files for new requests and queue waiting requests. */
rc = rtAioMgrCheckFiles(pThis);
}
} while ( /*fRunning - never modified
&& */ RT_SUCCESS(rc));
RT_NOREF_PV(hThreadSelf);
return rc;
}
/**
* Wakes up the async I/O manager.
*
* @returns IPRT status code.
* @param pThis The async I/O manager.
*/
static int rtAioMgrWakeup(PRTAIOMGRINT pThis)
{
return RTFileAioCtxWakeup(pThis->hAioCtx);
}
/**
* Waits until the async I/O manager handled the given event.
*
* @returns IPRT status code.
* @param pThis The async I/O manager.
* @param enmEvent The event to pass to the manager.
*/
static int rtAioMgrWaitForBlockingEvent(PRTAIOMGRINT pThis, RTAIOMGREVENT enmEvent)
{
Assert(pThis->enmBlockingEvent == RTAIOMGREVENT_NO_EVENT);
ASMAtomicWriteU32((volatile uint32_t *)&pThis->enmBlockingEvent, enmEvent);
/* Wakeup the async I/O manager */
int rc = rtAioMgrWakeup(pThis);
if (RT_FAILURE(rc))
return rc;
/* Wait for completion. */
rc = RTSemEventWait(pThis->hEventSemBlock, RT_INDEFINITE_WAIT);
AssertRC(rc);
ASMAtomicWriteU32((volatile uint32_t *)&pThis->enmBlockingEvent, RTAIOMGREVENT_NO_EVENT);
return rc;
}
/**
* Add a given file to the given I/O manager.
*
* @returns IPRT status code.
* @param pThis The async I/O manager.
* @param pFile The file to add.
*/
static int rtAioMgrAddFile(PRTAIOMGRINT pThis, PRTAIOMGRFILEINT pFile)
{
/* Update the assigned I/O manager. */
ASMAtomicWritePtr(&pFile->pAioMgr, pThis);
int rc = RTCritSectEnter(&pThis->CritSectBlockingEvent);
AssertRCReturn(rc, rc);
ASMAtomicWritePtr(&pThis->BlockingEventData.pFileAdd, pFile);
rc = rtAioMgrWaitForBlockingEvent(pThis, RTAIOMGREVENT_FILE_ADD);
ASMAtomicWriteNullPtr(&pThis->BlockingEventData.pFileAdd);
RTCritSectLeave(&pThis->CritSectBlockingEvent);
return rc;
}
/**
* Removes a given file from the given I/O manager.
*
* @returns IPRT status code.
* @param pThis The async I/O manager.
* @param pFile The file to remove.
*/
static int rtAioMgrCloseFile(PRTAIOMGRINT pThis, PRTAIOMGRFILEINT pFile)
{
int rc = RTCritSectEnter(&pThis->CritSectBlockingEvent);
AssertRCReturn(rc, rc);
ASMAtomicWritePtr(&pThis->BlockingEventData.pFileClose, pFile);
rc = rtAioMgrWaitForBlockingEvent(pThis, RTAIOMGREVENT_FILE_CLOSE);
ASMAtomicWriteNullPtr(&pThis->BlockingEventData.pFileClose);
RTCritSectLeave(&pThis->CritSectBlockingEvent);
return rc;
}
/**
* Process a shutdown event.
*
* @returns IPRT status code.
* @param pThis The async I/O manager to shut down.
*/
static int rtAioMgrShutdown(PRTAIOMGRINT pThis)
{
int rc = RTCritSectEnter(&pThis->CritSectBlockingEvent);
AssertRCReturn(rc, rc);
rc = rtAioMgrWaitForBlockingEvent(pThis, RTAIOMGREVENT_SHUTDOWN);
RTCritSectLeave(&pThis->CritSectBlockingEvent);
return rc;
}
/**
* Destroys an async I/O manager.
*
* @returns nothing.
* @param pThis The async I/O manager instance to destroy.
*/
static void rtAioMgrDestroy(PRTAIOMGRINT pThis)
{
int rc;
rc = rtAioMgrShutdown(pThis);
AssertRC(rc);
rc = RTThreadWait(pThis->hThread, RT_INDEFINITE_WAIT, NULL);
AssertRC(rc);
rc = RTFileAioCtxDestroy(pThis->hAioCtx);
AssertRC(rc);
rc = RTMemCacheDestroy(pThis->hMemCacheReqs);
AssertRC(rc);
pThis->hThread = NIL_RTTHREAD;
pThis->hAioCtx = NIL_RTFILEAIOCTX;
pThis->hMemCacheReqs = NIL_RTMEMCACHE;
pThis->u32Magic = ~RTAIOMGR_MAGIC;
RTCritSectDelete(&pThis->CritSectBlockingEvent);
RTSemEventDestroy(pThis->hEventSemBlock);
RTMemFree(pThis);
}
/**
* Queues a new request for processing.
*/
static void rtAioMgrFileQueueReq(PRTAIOMGRFILEINT pThis, PRTAIOMGRREQ pReq)
{
RTAioMgrFileRetain(pThis);
RTQueueAtomicInsert(&pThis->QueueReqs, &pReq->WorkItem);
rtAioMgrWakeup(pThis->pAioMgr);
}
/**
* Destroys an async I/O manager file.
*
* @returns nothing.
* @param pThis The async I/O manager file.
*/
static void rtAioMgrFileDestroy(PRTAIOMGRFILEINT pThis)
{
pThis->u32Magic = ~RTAIOMGRFILE_MAGIC;
rtAioMgrCloseFile(pThis->pAioMgr, pThis);
RTAioMgrRelease(pThis->pAioMgr);
RTMemFree(pThis);
}
/**
* Queues a new I/O request.
*
* @returns IPRT status code.
* @param hAioMgrFile The I/O manager file handle.
* @param off Start offset of the I/o request.
* @param pSgBuf Data S/G buffer.
* @param cbIo How much to transfer.
* @param pvUser Opaque user data.
* @param enmType I/O direction type (read/write).
*/
static int rtAioMgrFileIoReqCreate(RTAIOMGRFILE hAioMgrFile, RTFOFF off, PRTSGBUF pSgBuf,
size_t cbIo, void *pvUser, RTAIOMGRREQTYPE enmType)
{
int rc;
PRTAIOMGRFILEINT pFile = hAioMgrFile;
PRTAIOMGRINT pAioMgr;
AssertPtrReturn(pFile, VERR_INVALID_HANDLE);
pAioMgr = pFile->pAioMgr;
PRTAIOMGRREQ pReq = rtAioMgrReqAlloc(pAioMgr);
if (RT_LIKELY(pReq))
{
unsigned cSeg = 1;
size_t cbSeg = RTSgBufSegArrayCreate(pSgBuf, &pReq->DataSeg, &cSeg, cbIo);
if (cbSeg == cbIo)
{
pReq->enmType = enmType;
pReq->pFile = pFile;
pReq->pvUser = pvUser;
pReq->off = off;
rtAioMgrFileQueueReq(pFile, pReq);
rc = VERR_FILE_AIO_IN_PROGRESS;
}
else
{
/** @todo Real S/G buffer support. */
rtAioMgrReqFree(pAioMgr, pReq);
rc = VERR_NOT_SUPPORTED;
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Request constructor for the memory cache.
*
* @returns IPRT status code.
* @param hMemCache The cache handle.
* @param pvObj The memory object that should be initialized.
* @param pvUser The user argument.
*/
static DECLCALLBACK(int) rtAioMgrReqCtor(RTMEMCACHE hMemCache, void *pvObj, void *pvUser)
{
PRTAIOMGRREQ pReq = (PRTAIOMGRREQ)pvObj;
RT_NOREF_PV(hMemCache); RT_NOREF_PV(pvUser);
memset(pReq, 0, sizeof(RTAIOMGRREQ));
return RTFileAioReqCreate(&pReq->hReqIo);
}
/**
* Request destructor for the memory cache.
*
* @param hMemCache The cache handle.
* @param pvObj The memory object that should be destroyed.
* @param pvUser The user argument.
*/
static DECLCALLBACK(void) rtAioMgrReqDtor(RTMEMCACHE hMemCache, void *pvObj, void *pvUser)
{
PRTAIOMGRREQ pReq = (PRTAIOMGRREQ)pvObj;
int rc = RTFileAioReqDestroy(pReq->hReqIo);
AssertRC(rc);
RT_NOREF_PV(hMemCache); RT_NOREF_PV(pvUser);
}
RTDECL(int) RTAioMgrCreate(PRTAIOMGR phAioMgr, uint32_t cReqsMax)
{
int rc = VINF_SUCCESS;
PRTAIOMGRINT pThis;
AssertPtrReturn(phAioMgr, VERR_INVALID_POINTER);
AssertReturn(cReqsMax > 0, VERR_INVALID_PARAMETER);
pThis = (PRTAIOMGRINT)RTMemAllocZ(sizeof(RTAIOMGRINT));
if (pThis)
{
pThis->u32Magic = RTAIOMGR_MAGIC;
pThis->cRefs = 1;
pThis->enmBlockingEvent = RTAIOMGREVENT_NO_EVENT;
RTListInit(&pThis->ListFiles);
rc = RTCritSectInit(&pThis->CritSectBlockingEvent);
if (RT_SUCCESS(rc))
{
rc = RTSemEventCreate(&pThis->hEventSemBlock);
if (RT_SUCCESS(rc))
{
rc = RTMemCacheCreate(&pThis->hMemCacheReqs, sizeof(RTAIOMGRREQ),
0, UINT32_MAX, rtAioMgrReqCtor, rtAioMgrReqDtor, NULL, 0);
if (RT_SUCCESS(rc))
{
rc = RTFileAioCtxCreate(&pThis->hAioCtx, cReqsMax == UINT32_MAX
? RTFILEAIO_UNLIMITED_REQS
: cReqsMax,
RTFILEAIOCTX_FLAGS_WAIT_WITHOUT_PENDING_REQUESTS);
if (RT_SUCCESS(rc))
{
rc = RTThreadCreateF(&pThis->hThread, rtAioMgrWorker, pThis, 0, RTTHREADTYPE_IO,
RTTHREADFLAGS_WAITABLE, "AioMgr-%u", cReqsMax);
if (RT_FAILURE(rc))
{
rc = RTFileAioCtxDestroy(pThis->hAioCtx);
AssertRC(rc);
}
}
if (RT_FAILURE(rc))
RTMemCacheDestroy(pThis->hMemCacheReqs);
}
if (RT_FAILURE(rc))
RTSemEventDestroy(pThis->hEventSemBlock);
}
if (RT_FAILURE(rc))
RTCritSectDelete(&pThis->CritSectBlockingEvent);
}
if (RT_FAILURE(rc))
RTMemFree(pThis);
}
else
rc = VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
*phAioMgr = pThis;
return rc;
}
RTDECL(uint32_t) RTAioMgrRetain(RTAIOMGR hAioMgr)
{
PRTAIOMGRINT pThis = hAioMgr;
AssertReturn(hAioMgr != NIL_RTAIOMGR, UINT32_MAX);
AssertPtrReturn(pThis, UINT32_MAX);
AssertReturn(pThis->u32Magic == RTAIOMGR_MAGIC, UINT32_MAX);
uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
AssertMsg(cRefs > 1 && cRefs < _1G, ("%#x %p\n", cRefs, pThis));
return cRefs;
}
RTDECL(uint32_t) RTAioMgrRelease(RTAIOMGR hAioMgr)
{
PRTAIOMGRINT pThis = hAioMgr;
if (pThis == NIL_RTAIOMGR)
return 0;
AssertPtrReturn(pThis, UINT32_MAX);
AssertReturn(pThis->u32Magic == RTAIOMGR_MAGIC, UINT32_MAX);
uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
AssertMsg(cRefs < _1G, ("%#x %p\n", cRefs, pThis));
if (cRefs == 0)
rtAioMgrDestroy(pThis);
return cRefs;
}
RTDECL(int) RTAioMgrFileCreate(RTAIOMGR hAioMgr, RTFILE hFile, PFNRTAIOMGRREQCOMPLETE pfnReqComplete,
void *pvUser, PRTAIOMGRFILE phAioMgrFile)
{
int rc = VINF_SUCCESS;
PRTAIOMGRFILEINT pThis;
AssertReturn(hAioMgr != NIL_RTAIOMGR, VERR_INVALID_HANDLE);
AssertReturn(hFile != NIL_RTFILE, VERR_INVALID_HANDLE);
AssertPtrReturn(pfnReqComplete, VERR_INVALID_POINTER);
AssertPtrReturn(phAioMgrFile, VERR_INVALID_POINTER);
pThis = (PRTAIOMGRFILEINT)RTMemAllocZ(sizeof(RTAIOMGRFILEINT));
if (pThis)
{
pThis->u32Magic = RTAIOMGRFILE_MAGIC;
pThis->cRefs = 1;
pThis->hFile = hFile;
pThis->pAioMgr = hAioMgr;
pThis->pvUser = pvUser;
pThis->pfnReqCompleted = pfnReqComplete;
RTQueueAtomicInit(&pThis->QueueReqs);
RTListInit(&pThis->AioMgr.ListWaitingReqs);
RTAioMgrRetain(hAioMgr);
rc = RTFileAioCtxAssociateWithFile(pThis->pAioMgr->hAioCtx, hFile);
if (RT_FAILURE(rc))
rtAioMgrFileDestroy(pThis);
else
rtAioMgrAddFile(pThis->pAioMgr, pThis);
}
else
rc = VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
*phAioMgrFile = pThis;
return rc;
}
RTDECL(uint32_t) RTAioMgrFileRetain(RTAIOMGRFILE hAioMgrFile)
{
PRTAIOMGRFILEINT pThis = hAioMgrFile;
AssertReturn(hAioMgrFile != NIL_RTAIOMGRFILE, UINT32_MAX);
AssertPtrReturn(pThis, UINT32_MAX);
AssertReturn(pThis->u32Magic == RTAIOMGRFILE_MAGIC, UINT32_MAX);
uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
AssertMsg(cRefs > 1 && cRefs < _1G, ("%#x %p\n", cRefs, pThis));
return cRefs;
}
RTDECL(uint32_t) RTAioMgrFileRelease(RTAIOMGRFILE hAioMgrFile)
{
PRTAIOMGRFILEINT pThis = hAioMgrFile;
if (pThis == NIL_RTAIOMGRFILE)
return 0;
AssertPtrReturn(pThis, UINT32_MAX);
AssertReturn(pThis->u32Magic == RTAIOMGRFILE_MAGIC, UINT32_MAX);
uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
AssertMsg(cRefs < _1G, ("%#x %p\n", cRefs, pThis));
if (cRefs == 0)
rtAioMgrFileDestroy(pThis);
return cRefs;
}
RTDECL(void *) RTAioMgrFileGetUser(RTAIOMGRFILE hAioMgrFile)
{
PRTAIOMGRFILEINT pThis = hAioMgrFile;
AssertPtrReturn(pThis, NULL);
return pThis->pvUser;
}
RTDECL(int) RTAioMgrFileRead(RTAIOMGRFILE hAioMgrFile, RTFOFF off,
PRTSGBUF pSgBuf, size_t cbRead, void *pvUser)
{
return rtAioMgrFileIoReqCreate(hAioMgrFile, off, pSgBuf, cbRead, pvUser,
RTAIOMGRREQTYPE_READ);
}
RTDECL(int) RTAioMgrFileWrite(RTAIOMGRFILE hAioMgrFile, RTFOFF off,
PRTSGBUF pSgBuf, size_t cbWrite, void *pvUser)
{
return rtAioMgrFileIoReqCreate(hAioMgrFile, off, pSgBuf, cbWrite, pvUser,
RTAIOMGRREQTYPE_WRITE);
}
RTDECL(int) RTAioMgrFileFlush(RTAIOMGRFILE hAioMgrFile, void *pvUser)
{
PRTAIOMGRFILEINT pFile = hAioMgrFile;
PRTAIOMGRINT pAioMgr;
AssertPtrReturn(pFile, VERR_INVALID_HANDLE);
pAioMgr = pFile->pAioMgr;
PRTAIOMGRREQ pReq = rtAioMgrReqAlloc(pAioMgr);
if (RT_UNLIKELY(!pReq))
return VERR_NO_MEMORY;
pReq->pFile = pFile;
pReq->enmType = RTAIOMGRREQTYPE_FLUSH;
pReq->pvUser = pvUser;
rtAioMgrFileQueueReq(pFile, pReq);
return VERR_FILE_AIO_IN_PROGRESS;
}
|