@@ -294,7 +294,7 @@ syncOnNonTaskThread(synchronousTask: behavior)
294
294
// CHECK: after startSynchronously, outside; cancel (wakeup) the synchronous task! [thread:[[CALLING_THREAD3]]]
295
295
296
296
print ( " \n \n ==== ------------------------------------------------------------------ " )
297
- print ( " callActorFromStartSynchronousTask() " )
297
+ print ( " callActorFromStartSynchronousTask() - not on specific queue " )
298
298
callActorFromStartSynchronousTask ( recipient: . recipient( Recipient ( ) ) )
299
299
300
300
// CHECK: callActorFromStartSynchronousTask()
@@ -308,11 +308,6 @@ callActorFromStartSynchronousTask(recipient: .recipient(Recipient()))
308
308
// CHECK-NOT: ERROR!
309
309
// CHECK: inside startSynchronously, call rec.sync() done
310
310
311
- // CHECK-NOT: ERROR!
312
- // CHECK: inside startSynchronously, call rec.async()
313
- // CHECK-NOT: ERROR!
314
- // CHECK: inside startSynchronously, call rec.async() done
315
-
316
311
// CHECK-NOT: ERROR!
317
312
// CHECK: inside startSynchronously, done
318
313
@@ -324,35 +319,20 @@ enum TargetActorToCall {
324
319
}
325
320
326
321
protocol RecipientProtocol where Self: Actor {
327
- func sync( syncTaskThreadID: ThreadID ) async
328
- func async ( syncTaskThreadID: ThreadID ) async
322
+ func callAndSuspend( syncTaskThreadID: ThreadID ) async
329
323
}
330
324
331
325
// default actor, must not declare an 'unownedExecutor'
332
- actor Recipient {
333
- func sync ( syncTaskThreadID: ThreadID ) {
326
+ actor Recipient : RecipientProtocol {
327
+ func callAndSuspend ( syncTaskThreadID: ThreadID ) async {
334
328
self . preconditionIsolated ( )
335
329
336
330
print ( " \( Recipient . self) / \( #function) Current actor thread id = \( getCurrentThreadID ( ) ) @ : \( #line) " )
337
331
if compareThreadIDs ( syncTaskThreadID, . equal, getCurrentThreadID ( ) ) {
338
332
print ( " NOTICE: Actor must not run on the synchronous task's thread : \( #line) " )
339
333
}
340
- }
341
-
342
- func async ( syncTaskThreadID: ThreadID ) async {
343
- self . preconditionIsolated ( )
344
334
345
- // Dispatch may end up reusing the thread used to service the queue so we
346
- // cannot truly assert exact thread identity in such tests.
347
- // Usually this will be on a different thread by now though.
348
- print ( " \( Recipient . self) / \( #function) Current actor thread id = \( getCurrentThreadID ( ) ) @ : \( #line) " )
349
- if compareThreadIDs ( syncTaskThreadID, . equal, getCurrentThreadID ( ) ) {
350
- print ( " NOTICE: Actor must not run on the synchronous task's thread : \( #line) " )
351
- }
352
-
353
- await Task {
354
- self . preconditionIsolated ( )
355
- } . value
335
+ try ? await Task . sleep ( for: . milliseconds( 100 ) )
356
336
}
357
337
}
358
338
@@ -379,8 +359,8 @@ func callActorFromStartSynchronousTask(recipient rec: TargetActorToCall) {
379
359
380
360
print ( " inside startSynchronously, call rec.sync() [thread: \( getCurrentThreadID ( ) ) ] @ : \( #line) " )
381
361
switch rec {
382
- case . recipient( let recipient) : await recipient. sync ( syncTaskThreadID: innerTID)
383
- case . recipientOnQueue( let recipient) : await recipient. sync ( syncTaskThreadID: innerTID)
362
+ case . recipient( let recipient) : await recipient. callAndSuspend ( syncTaskThreadID: innerTID)
363
+ case . recipientOnQueue( let recipient) : await recipient. callAndSuspend ( syncTaskThreadID: innerTID)
384
364
}
385
365
print ( " inside startSynchronously, call rec.sync() done [thread: \( getCurrentThreadID ( ) ) ] @ : \( #line) " )
386
366
@@ -395,22 +375,6 @@ func callActorFromStartSynchronousTask(recipient rec: TargetActorToCall) {
395
375
print ( " NOTICE: Task resumed on same thread as it entered the synchronous task! " )
396
376
}
397
377
398
- print ( " inside startSynchronously, call rec.async() [thread: \( getCurrentThreadID ( ) ) ] @ : \( #line) " )
399
- switch rec {
400
- case . recipient( let recipient) : await recipient. async ( syncTaskThreadID: innerTID)
401
- case . recipientOnQueue( let recipient) : await recipient. async ( syncTaskThreadID: innerTID)
402
- }
403
- print ( " inside startSynchronously, call rec.async() done [thread: \( getCurrentThreadID ( ) ) ] @ : \( #line) " )
404
-
405
- print ( " Inner thread id = \( innerTID) " )
406
- print ( " Current thread id = \( getCurrentThreadID ( ) ) " )
407
- // Dispatch may end up reusing the thread used to service the queue so we
408
- // cannot truly assert exact thread identity in such tests.
409
- // Usually this will be on a different thread by now though.
410
- if compareThreadIDs ( innerTID, . equal, getCurrentThreadID ( ) ) {
411
- print ( " NOTICE: Task resumed on same thread as it entered the synchronous task! " )
412
- }
413
-
414
378
print ( " inside startSynchronously, done [thread: \( getCurrentThreadID ( ) ) ] @ : \( #line) " )
415
379
sem1. signal ( )
416
380
}
@@ -428,6 +392,28 @@ print("callActorFromStartSynchronousTask() - actor in custom executor with its o
428
392
let actorQueue = DispatchQueue ( label: " recipient-actor-queue " )
429
393
callActorFromStartSynchronousTask ( recipient: . recipientOnQueue( RecipientOnQueue ( queue: actorQueue) ) )
430
394
395
+
396
+ // 50: callActorFromStartSynchronousTask()
397
+ // 51: before startSynchronously [thread:0x00007000054f5000] @ :366
398
+ // 52: inside startSynchronously [thread:0x00007000054f5000] @ :372
399
+ // 53: inside startSynchronously, call rec.sync() [thread:0x00007000054f5000] @ :380
400
+ // 54: Recipient/sync(syncTaskThreadID:) Current actor thread id = 0x000070000567e000 @ :336
401
+ // 55: inside startSynchronously, call rec.sync() done [thread:0x000070000567e000] @ :385
402
+ // 56: Inner thread id = 0x00007000054f5000
403
+ // 57: Current thread id = 0x000070000567e000
404
+ // 60: after startSynchronously [thread:0x00007000054f5000] @ :418
405
+ // 61: - async work on queue
406
+ // 62: - async work on queue
407
+ // 63: - async work on queue
408
+ // 64: - async work on queue
409
+ // 65: - async work on queue
410
+ // 67: - async work on queue
411
+ // 68: - async work on queue
412
+ // 69: - async work on queue
413
+ // 71: Inner thread id = 0x00007000054f5000
414
+ // 72: Current thread id = 0x000070000567e000
415
+ // 73: inside startSynchronously, done [thread:0x000070000567e000] @ :414
416
+
431
417
// CHECK-LABEL: callActorFromStartSynchronousTask() - actor in custom executor with its own queue
432
418
// No interleaving allowed between "before" and "inside":
433
419
// CHECK: before startSynchronously [thread:[[CALLING_THREAD4:.*]]]
@@ -438,21 +424,14 @@ callActorFromStartSynchronousTask(recipient: .recipientOnQueue(RecipientOnQueue(
438
424
// allowing the 'after startSynchronously' to run.
439
425
//
440
426
// CHECK-NEXT: inside startSynchronously, call rec.sync() [thread:[[CALLING_THREAD4]]]
441
- // CHECK: NaiveQueueExecutor(recipient-actor-queue) enqueue
442
427
// CHECK: after startSynchronously
443
428
// CHECK-NOT: ERROR!
444
429
// CHECK: inside startSynchronously, call rec.sync() done
445
430
446
- // CHECK-NOT: ERROR!
447
- // CHECK: inside startSynchronously, call rec.async()
448
- // CHECK: NaiveQueueExecutor(recipient-actor-queue) enqueue
449
- // CHECK-NOT: ERROR!
450
- // CHECK: inside startSynchronously, call rec.async() done
451
-
452
431
// CHECK-NOT: ERROR!
453
432
// CHECK: inside startSynchronously, done
454
433
455
- actor RecipientOnQueue {
434
+ actor RecipientOnQueue : RecipientProtocol {
456
435
let executor : NaiveQueueExecutor
457
436
nonisolated let unownedExecutor : UnownedSerialExecutor
458
437
@@ -461,30 +440,15 @@ actor RecipientOnQueue {
461
440
self . unownedExecutor = executor. asUnownedSerialExecutor ( )
462
441
}
463
442
464
- func sync( syncTaskThreadID: ThreadID ) {
465
- self . preconditionIsolated ( )
466
- dispatchPrecondition ( condition: . onQueue( self . executor. queue) )
467
-
468
- print ( " \( Recipient . self) / \( #function) Current actor thread id = \( getCurrentThreadID ( ) ) @ : \( #line) " )
469
- if compareThreadIDs ( syncTaskThreadID, . equal, getCurrentThreadID ( ) ) {
470
- print ( " NOTICE: Actor must not run on the synchronous task's thread : \( #line) " )
471
- }
472
- }
473
-
474
- func async ( syncTaskThreadID: ThreadID ) async {
443
+ func callAndSuspend( syncTaskThreadID: ThreadID ) async {
475
444
self . preconditionIsolated ( )
476
445
dispatchPrecondition ( condition: . onQueue( self . executor. queue) )
477
446
478
- // Dispatch may end up reusing the thread used to service the queue so we
479
- // cannot truly assert exact thread identity in such tests.
480
- // Usually this will be on a different thread by now though.
481
447
print ( " \( Recipient . self) / \( #function) Current actor thread id = \( getCurrentThreadID ( ) ) @ : \( #line) " )
482
448
if compareThreadIDs ( syncTaskThreadID, . equal, getCurrentThreadID ( ) ) {
483
449
print ( " NOTICE: Actor must not run on the synchronous task's thread : \( #line) " )
484
450
}
485
451
486
- await Task {
487
- self . preconditionIsolated ( )
488
- } . value
452
+ try ? await Task . sleep ( for: . milliseconds( 100 ) )
489
453
}
490
454
}
0 commit comments