@@ -54,11 +54,13 @@ using namespace swift;
54
54
/* ************************** TASK GROUP ***************************************/
55
55
/* *****************************************************************************/
56
56
57
- #if SWIFT_TASK_DEBUG_LOG_ENABLED
58
- #define SWIFT_TASK_GROUP_DEBUG_LOG (group, fmt, ...) \
59
- SWIFT_TASK_DEBUG_LOG (" group(%p%s) " fmt, \
60
- group, group->isDiscardingResults () ? ",discardResults" : "", \
61
- __VA_ARGS__)
57
+ #if 1
58
+ #define SWIFT_TASK_GROUP_DEBUG_LOG (group, fmt, ...) \
59
+ fprintf (stderr, " [%#lx] [%s:%d](%s) group(%p%s) " fmt " \n " , \
60
+ (unsigned long )Thread::current().platformThreadId(), \
61
+ __FILE__, __LINE__, __FUNCTION__, \
62
+ group, group->isDiscardingResults() ? ",discardResults" : "", \
63
+ __VA_ARGS__)
62
64
#else
63
65
#define SWIFT_TASK_GROUP_DEBUG_LOG(group, fmt, ...) (void)0
64
66
#endif
@@ -212,13 +214,13 @@ class TaskGroupImpl: public TaskGroupTaskStatusRecord {
212
214
}
213
215
214
216
unsigned int readyTasks (const TaskGroupImpl* _Nonnull group) {
215
- assert (group && group ->isAccumulatingResults ()
217
+ assert (group->isAccumulatingResults ()
216
218
&& " attempted to check ready tasks on group that does not accumulate results!" );
217
219
return (status & maskReady) >> 31 ;
218
220
}
219
221
220
- uint64_t pendingTasks (const TaskGroupImpl* _Nullable group) {
221
- if (group && group ->isAccumulatingResults ()) {
222
+ uint64_t pendingTasks (const TaskGroupImpl* _Nonnull group) {
223
+ if (group->isAccumulatingResults ()) {
222
224
return (status & maskAccumulatingPending);
223
225
} else {
224
226
return (status & maskDiscardingPending);
@@ -260,17 +262,16 @@ class TaskGroupImpl: public TaskGroupTaskStatusRecord {
260
262
// / GroupStatus{ C:{cancelled} W:{waiting task} R:{ready tasks} P:{pending tasks} {binary repr} }
261
263
// / If discarding results:
262
264
// / GroupStatus{ C:{cancelled} W:{waiting task} P:{pending tasks} {binary repr} }
263
- std::string to_string (const TaskGroupImpl* _Nullable group) {
265
+ std::string to_string (const TaskGroupImpl* _Nonnull group) {
264
266
std::string str;
265
267
str.append (" GroupStatus{ " );
266
268
str.append (" C:" ); // cancelled
267
- str.append (isCancelled () ? " y " : " n " );
268
- str.append (" W:" ); // has waiting task
269
+ str.append (isCancelled () ? " y" : " n" );
270
+ str.append (" W:" ); // has waiting task
269
271
str.append (hasWaitingTask () ? " y" : " n" );
270
272
if (group && group->isAccumulatingResults ()) {
271
273
str.append (" R:" ); // ready
272
274
str.append (std::to_string (readyTasks (group)));
273
- str.append (" " );
274
275
}
275
276
str.append (" P:" ); // pending
276
277
str.append (std::to_string (pendingTasks (group)));
@@ -411,7 +412,7 @@ class TaskGroupImpl: public TaskGroupTaskStatusRecord {
411
412
GroupStatus statusMarkWaitingAssumeAcquire () {
412
413
fprintf (stderr, " [%s:%d](%s) statusMarkWaitingAssumeAcquire, load....\n " , __FILE_NAME__, __LINE__, __FUNCTION__);
413
414
fprintf (stderr, " [%s:%d](%s) statusMarkWaitingAssumeAcquire = %s\n " , __FILE_NAME__, __LINE__, __FUNCTION__,
414
- statusLoadRelaxed ().to_string (nullptr ).c_str ());
415
+ statusLoadRelaxed ().to_string (this ).c_str ());
415
416
auto old = status.fetch_or (GroupStatus::waiting, std::memory_order_acquire);
416
417
return GroupStatus{old | GroupStatus::waiting};
417
418
}
@@ -693,12 +694,16 @@ static void fillGroupNextResult(TaskFutureWaitAsyncContext *context,
693
694
// Initialize the result as an Optional<Success>.
694
695
const Metadata *successType = result.successType ;
695
696
OpaqueValue *destPtr = context->successResultPointer ;
696
- successType->vw_storeEnumTagSinglePayload (destPtr, 1 , 1 );
697
+ // TODO: figure out a way to try to optimistically take the
698
+ // value out of the finished task's future, if there are no
699
+ // remaining references to it.
700
+ successType->vw_initializeWithCopy (destPtr, result.storage );
701
+ successType->vw_storeEnumTagSinglePayload (destPtr, 0 , 1 );
697
702
return ;
698
703
}
699
704
700
705
case PollStatus::Empty: {
701
- // Initialize the result as an Optional<Success>.
706
+ // Initialize the result as a .none Optional<Success>.
702
707
const Metadata *successType = result.successType ;
703
708
OpaqueValue *destPtr = context->successResultPointer ;
704
709
successType->vw_storeEnumTagSinglePayload (destPtr, 1 , 1 );
@@ -709,37 +714,10 @@ static void fillGroupNextResult(TaskFutureWaitAsyncContext *context,
709
714
710
715
static void fillGroupNextNilResult (TaskFutureWaitAsyncContext *context,
711
716
PollResult result) {
712
- // /// Fill in the result value
713
- // switch (result.status) {
714
- // case PollStatus::MustWait:
715
- // assert(false && "filling a waiting status?");
716
- // return;
717
- //
718
- // case PollStatus::Error: {
719
- // assert(false && "cannot have errors");
720
- // return;
721
- // }
722
- //
723
- // case PollStatus::Success: {
724
- // // Initialize the result as an Optional<Void>.
725
- // const Metadata *successType = result.successType;
726
- // OpaqueValue *destPtr = context->successResultPointer;
727
- // // TODO: figure out a way to try to optimistically take the
728
- // // value out of the finished task's future, if there are no
729
- // // remaining references to it.
730
- // successType->vw_initializeWithCopy(destPtr, result.storage);
731
- // successType->vw_storeEnumTagSinglePayload(destPtr, 0, 1);
732
- // return;
733
- // }
734
- //
735
- // case PollStatus::Empty: {
736
- // Initialize the result as a nil Optional<Success>.
737
- const Metadata *successType = result.successType ;
738
- OpaqueValue *destPtr = context->successResultPointer ;
739
- successType->vw_storeEnumTagSinglePayload (destPtr, 1 , 1 );
740
- return ;
741
- // }
742
- // }
717
+ // Initialize the result as a .none Optional<Success>.
718
+ const Metadata *successType = result.successType ;
719
+ OpaqueValue *destPtr = context->successResultPointer ;
720
+ successType->vw_storeEnumTagSinglePayload (destPtr, 1 , 1 );
743
721
}
744
722
745
723
// TaskGroup is locked upon entry and exit
@@ -774,7 +752,7 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
774
752
assert (completedTask->hasChildFragment ());
775
753
assert (completedTask->hasGroupChildFragment ());
776
754
assert (completedTask->groupChildFragment ()->getGroup () == asAbstract (this ));
777
- SWIFT_TASK_GROUP_DEBUG_LOG (this , " offer, completedTask:%p , status:%s" , completedTask, statusLoadRelaxed ().to_string (this ).c_str ());
755
+ SWIFT_TASK_GROUP_DEBUG_LOG (this , " ENTER offer, completedTask:%p , status:%s" , completedTask, statusLoadRelaxed ().to_string (this ).c_str ());
778
756
779
757
// The current ownership convention is that we are *not* given ownership
780
758
// of a retain on completedTask; we're called from the task completion
@@ -802,19 +780,19 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
802
780
// Immediately decrement the pending count.
803
781
// We can do this, since in this mode there is no ready count to keep track of,
804
782
// and we immediately discard the result.
805
- SWIFT_TASK_DEBUG_LOG ( " group(%p) discard result, was pending:%llu" ,
806
- this , assumed.pendingTasks (this ));
783
+ SWIFT_TASK_GROUP_DEBUG_LOG ( this , " discard result, was pending:%llu" ,
784
+ assumed.pendingTasks (this ));
807
785
808
786
// If this was the last pending task, and there is a waiting task (from waitAll),
809
787
// we must resume the task; but not otherwise. There cannot be any waiters on next()
810
788
// while we're discarding results.
811
789
if (assumed.pendingTasks (this ) == 1 && assumed.hasWaitingTask ()) {
812
- SWIFT_TASK_DEBUG_LOG ( " group(%p) offer, discardResults, offered last pending task, resume waiting task:%p" ,
813
- this , waitQueue.load (std::memory_order_relaxed));
790
+ SWIFT_TASK_GROUP_DEBUG_LOG ( this , " offer, discardResults, offered last pending task, resume waiting task:%p" ,
791
+ waitQueue.load (std::memory_order_relaxed));
814
792
resumeWaitingTask (completedTask, assumed, /* hadErrorResult=*/ false );
815
793
} else {
816
794
auto afterComplete = statusCompletePendingAssumeRelease (this );
817
- SWIFT_TASK_DEBUG_LOG ( " group(%p) offer, discardResults, either more pending tasks, or no waiting task, status:%s" , this ,
795
+ SWIFT_TASK_GROUP_DEBUG_LOG ( this , " offer, either more pending tasks, or no waiting task, status:%s" ,
818
796
afterComplete.to_string (this ).c_str ());
819
797
_swift_taskGroup_detachChild (asAbstract (this ), completedTask);
820
798
}
@@ -823,8 +801,8 @@ void TaskGroupImpl::offer(AsyncTask *completedTask, AsyncContext *context) {
823
801
return ;
824
802
}
825
803
826
- SWIFT_TASK_DEBUG_LOG ( " group(%p), ready: %d, pending: %llu" ,
827
- this , assumed.readyTasks (this ), assumed.pendingTasks (this ));
804
+ SWIFT_TASK_GROUP_DEBUG_LOG ( this , " ready: %d, pending: %llu" ,
805
+ assumed.readyTasks (this ), assumed.pendingTasks (this ));
828
806
829
807
auto asyncContextPrefix = reinterpret_cast <FutureAsyncContextPrefix *>(
830
808
reinterpret_cast <char *>(context) - sizeof (FutureAsyncContextPrefix));
@@ -859,8 +837,8 @@ void TaskGroupImpl::resumeWaitingTask(
859
837
TaskGroupImpl::GroupStatus &assumed,
860
838
bool hadErrorResult) {
861
839
auto waitingTask = waitQueue.load (std::memory_order_acquire);
862
- SWIFT_TASK_DEBUG_LOG ( " group(%p) resume waiting task = %p, complete with = %p" ,
863
- this , waitingTask, completedTask);
840
+ SWIFT_TASK_GROUP_DEBUG_LOG ( this , " resume waiting task = %p, complete with = %p" ,
841
+ waitingTask, completedTask);
864
842
while (true ) {
865
843
// ==== a) run waiting task directly -------------------------------------
866
844
assert (assumed.hasWaitingTask ());
@@ -1186,7 +1164,6 @@ static void swift_taskGroup_waitAllImpl(
1186
1164
TaskGroup *_group,
1187
1165
ThrowingTaskFutureWaitContinuationFunction *resumeFunction,
1188
1166
AsyncContext *rawContext) {
1189
- fprintf (stderr, " [%s:%d](%s) group(%p) WAIT ALL IMPL\n " , __FILE_NAME__, __LINE__, __FUNCTION__, _group);
1190
1167
auto waitingTask = swift_task_getCurrent ();
1191
1168
waitingTask->ResumeTask = task_group_wait_resume_adapter;
1192
1169
waitingTask->ResumeContext = rawContext;
@@ -1199,8 +1176,8 @@ static void swift_taskGroup_waitAllImpl(
1199
1176
context->successResultPointer = resultPointer;
1200
1177
1201
1178
auto group = asImpl (_group);
1202
- SWIFT_TASK_DEBUG_LOG ( " group(%p) waitAll, waiting task = %p, status:%s" ,
1203
- group, waitingTask, group->statusLoadRelaxed ().to_string (group).c_str ());
1179
+ SWIFT_TASK_GROUP_DEBUG_LOG ( group, " waitAll, waiting task = %p, status:%s" ,
1180
+ waitingTask, group->statusLoadRelaxed ().to_string (group).c_str ());
1204
1181
1205
1182
PollResult polled = group->waitAll (waitingTask);
1206
1183
switch (polled.status ) {
0 commit comments