Changeset 95903 in vbox
- Timestamp:
- Jul 28, 2022 11:56:03 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/r3/stream.cpp
r95894 r95903 68 68 #ifdef RTSTREAM_STANDALONE 69 69 # include <iprt/file.h> 70 # include <iprt/list.h> 70 71 #endif 71 72 #include <iprt/mem.h> 73 #ifdef RTSTREAM_STANDALONE 74 # include <iprt/once.h> 75 #endif 72 76 #include <iprt/param.h> 73 77 #include <iprt/string.h> … … 175 179 PRTCRITSECT pCritSect; 176 180 #endif 181 #ifdef RTSTREAM_STANDALONE 182 /** Entry in g_StreamList (for automatic flushing and closing at 183 * exit/unload). */ 184 RTLISTNODE ListEntry; 185 #endif 177 186 } RTSTREAM; 178 187 … … 221 230 /* .fRecheckMode = */ true, 222 231 #ifndef HAVE_FWRITE_UNLOCKED 223 /* .pCritSect = */ NULL 232 /* .pCritSect = */ NULL, 233 #endif 234 #ifdef RTSTREAM_STANDALONE 235 /* .ListEntry = */ { NULL, NULL }, 224 236 #endif 225 237 }; … … 249 261 /* .fRecheckMode = */ true, 250 262 #ifndef HAVE_FWRITE_UNLOCKED 251 /* .pCritSect = */ NULL 263 /* .pCritSect = */ NULL, 264 #endif 265 #ifdef RTSTREAM_STANDALONE 266 /* .ListEntry = */ { NULL, NULL }, 252 267 #endif 253 268 }; … … 277 292 /* .fRecheckMode = */ true, 278 293 #ifndef HAVE_FWRITE_UNLOCKED 279 /* .pCritSect = */ NULL 294 /* .pCritSect = */ NULL, 295 #endif 296 #ifdef RTSTREAM_STANDALONE 297 /* .ListEntry = */ { NULL, NULL }, 280 298 #endif 281 299 }; … … 289 307 /** Pointer to the standard output stream. */ 290 308 RTDATADECL(PRTSTREAM) g_pStdOut = &g_StdOut; 309 310 #ifdef RTSTREAM_STANDALONE 311 /** Run-once initializer for the stream list (g_StreamList + g_StreamListCritSect). */ 312 static RTONCE g_StreamListOnce = RTONCE_INITIALIZER; 313 /** List of user created streams (excludes the standard streams). */ 314 static RTLISTANCHOR g_StreamList; 315 /** Critical section protecting the stream list. */ 316 static RTCRITSECT g_StreamListCritSect; 317 318 319 /** @callback_method_impl{FNRTONCE} */ 320 static DECLCALLBACK(int32_t) rtStrmListInitOnce(void *pvUser) 321 { 322 RT_NOREF(pvUser); 323 RTListInit(&g_StreamList); 324 return RTCritSectInit(&g_StreamListCritSect); 325 } 326 327 #endif 291 328 292 329 … … 442 479 return VINF_SUCCESS; 443 480 } 481 482 #ifdef RTSTREAM_STANDALONE 483 /* 484 * Make the the stream list is initialized before we allocate anything. 485 */ 486 int rc2 = RTOnce(&g_StreamListOnce, rtStrmListInitOnce, NULL); 487 AssertRCReturn(rc2, rc2); 488 #endif 444 489 445 490 /* … … 480 525 if (RT_SUCCESS(rc)) 481 526 { 527 #ifdef RTSTREAM_STANDALONE 528 /* We keep a list of these for cleanup purposes. */ 529 RTCritSectEnter(&g_StreamListCritSect); 530 RTListAppend(&g_StreamList, &pStream->ListEntry); 531 RTCritSectLeave(&g_StreamListCritSect); 532 #endif 482 533 *ppStream = pStream; 483 534 return VINF_SUCCESS; … … 559 610 * Invalidate the stream and destroy the critical section first. 560 611 */ 612 #ifdef RTSTREAM_STANDALONE 613 RTCritSectEnter(&g_StreamListCritSect); 614 RTListNodeRemove(&pStream->ListEntry); 615 RTCritSectLeave(&g_StreamListCritSect); 616 #endif 561 617 pStream->u32Magic = 0xdeaddead; 562 618 #ifndef HAVE_FWRITE_UNLOCKED … … 1295 1351 1296 1352 /** 1297 * Worker for rtStrmFlushAndCloseAll. 1298 */ 1299 static void rtStrmFlushAndClose(PRTSTREAM pStream, bool fStaticStream) 1300 { 1301 if (!fStaticStream) 1302 pStream->u32Magic = ~RTSTREAM_MAGIC; 1303 1353 * Worker for rtStrmFlushAndCloseAll and rtStrmFlushAndClose. 1354 */ 1355 static RTFILE rtStrmFlushAndCleanup(PRTSTREAM pStream) 1356 { 1304 1357 if (pStream->pchBuf) 1305 1358 { … … 1315 1368 1316 1369 PRTCRITSECT pCritSect = pStream->pCritSect; 1317 pStream->pCritSect = NULL;1318 1370 if (pCritSect) 1319 1371 { 1372 pStream->pCritSect = NULL; 1320 1373 RTCritSectDelete(pCritSect); 1321 1374 RTMemFree(pCritSect); 1322 1375 } 1323 1376 1324 if (pStream->hFile != NIL_RTFILE) 1325 { 1326 if (!fStaticStream) 1327 RTFileClose(pStream->hFile); 1328 /* else: no structure or memory behind RTFILE yet, so okay to just drop it. */ 1329 pStream->hFile = NIL_RTFILE; 1330 } 1331 1332 if (!fStaticStream) 1333 RTMemFree(pStream); 1377 RTFILE hFile = pStream->hFile; 1378 pStream->hFile = NIL_RTFILE; 1379 return hFile; 1380 } 1381 1382 1383 /** 1384 * Worker for rtStrmFlushAndCloseAll. 1385 */ 1386 static void rtStrmFlushAndClose(PRTSTREAM pStream) 1387 { 1388 pStream->u32Magic = ~RTSTREAM_MAGIC; 1389 RTFILE hFile = rtStrmFlushAndCleanup(pStream); 1390 if (hFile != NIL_RTFILE) 1391 RTFileClose(hFile); 1392 RTMemFree(pStream); 1334 1393 } 1335 1394 … … 1344 1403 * Flush the standard handles. 1345 1404 */ 1346 rtStrmFlushAndCl ose(&g_StdOut, true);1347 rtStrmFlushAndCl ose(&g_StdErr, true);1348 rtStrmFlushAndCl ose(&g_StdIn, true);1405 rtStrmFlushAndCleanup(&g_StdOut); 1406 rtStrmFlushAndCleanup(&g_StdErr); 1407 rtStrmFlushAndCleanup(&g_StdIn); 1349 1408 1350 1409 /* 1351 1410 * Make a list of the rest and flush+close those too. 1352 1411 */ 1353 /** @todo */ 1412 if (RTOnceWasInitialized(&g_StreamListOnce)) 1413 { 1414 RTCritSectDelete(&g_StreamListCritSect); 1415 1416 PRTSTREAM pStream; 1417 while ((pStream = RTListRemoveFirst(&g_StreamList, RTSTREAM, ListEntry)) != NULL) 1418 rtStrmFlushAndClose(pStream); 1419 1420 RTOnceReset(&g_StreamListOnce); 1421 } 1354 1422 } 1355 1423
Note:
See TracChangeset
for help on using the changeset viewer.