|
24 | 24 | #include "swift/Runtime/HeapObject.h" |
25 | 25 | #include "TaskGroupPrivate.h" |
26 | 26 | #include "TaskPrivate.h" |
27 | | -#include "AsyncCall.h" |
28 | 27 | #include "Debug.h" |
29 | 28 | #include "Error.h" |
30 | 29 |
|
@@ -857,126 +856,6 @@ void swift_task_future_wait_throwingImpl( |
857 | 856 | } |
858 | 857 | } |
859 | 858 |
|
860 | | -namespace { |
861 | | - |
862 | | -#if SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR |
863 | | - |
864 | | -class RunAndBlockSemaphore { |
865 | | - bool Finished = false; |
866 | | -public: |
867 | | - void wait() { |
868 | | - donateThreadToGlobalExecutorUntil([](void *context) { |
869 | | - return *reinterpret_cast<bool*>(context); |
870 | | - }, &Finished); |
871 | | - |
872 | | - assert(Finished && "ran out of tasks before we were signalled"); |
873 | | - } |
874 | | - |
875 | | - void signal() { |
876 | | - Finished = true; |
877 | | - } |
878 | | -}; |
879 | | - |
880 | | -#else |
881 | | - |
882 | | -class RunAndBlockSemaphore { |
883 | | - ConditionVariable Queue; |
884 | | - ConditionVariable::Mutex Lock; |
885 | | - bool Finished = false; |
886 | | -public: |
887 | | - /// Wait for a signal. |
888 | | - void wait() { |
889 | | - Lock.withLockOrWait(Queue, [&] { |
890 | | - return Finished; |
891 | | - }); |
892 | | - } |
893 | | - |
894 | | - void signal() { |
895 | | - Lock.withLockThenNotifyAll(Queue, [&]{ |
896 | | - Finished = true; |
897 | | - }); |
898 | | - } |
899 | | -}; |
900 | | - |
901 | | -#endif |
902 | | - |
903 | | -using RunAndBlockSignature = |
904 | | - AsyncSignature<void(HeapObject*), /*throws*/ false>; |
905 | | -struct RunAndBlockContext: AsyncContext { |
906 | | - const void *Function; |
907 | | - HeapObject *FunctionContext; |
908 | | - RunAndBlockSemaphore *Semaphore; |
909 | | -}; |
910 | | -using RunAndBlockCalleeContext = |
911 | | - AsyncCalleeContext<RunAndBlockContext, RunAndBlockSignature>; |
912 | | - |
913 | | -} // end anonymous namespace |
914 | | - |
915 | | -/// Second half of the runAndBlock async function. |
916 | | -SWIFT_CC(swiftasync) |
917 | | -static void runAndBlock_finish(SWIFT_ASYNC_CONTEXT AsyncContext *_context) { |
918 | | - auto calleeContext = static_cast<RunAndBlockCalleeContext*>(_context); |
919 | | - |
920 | | - auto context = popAsyncContext(calleeContext); |
921 | | - |
922 | | - context->Semaphore->signal(); |
923 | | - |
924 | | - return context->ResumeParent(context); |
925 | | -} |
926 | | - |
927 | | -/// First half of the runAndBlock async function. |
928 | | -SWIFT_CC(swiftasync) |
929 | | -static void runAndBlock_start(SWIFT_ASYNC_CONTEXT AsyncContext *_context, |
930 | | - SWIFT_CONTEXT HeapObject *closureContext) { |
931 | | - auto callerContext = static_cast<RunAndBlockContext*>(_context); |
932 | | - |
933 | | - RunAndBlockSignature::FunctionType *function; |
934 | | - size_t calleeContextSize; |
935 | | - auto functionContext = callerContext->FunctionContext; |
936 | | - assert(closureContext == functionContext); |
937 | | - std::tie(function, calleeContextSize) |
938 | | - = getAsyncClosureEntryPointAndContextSize< |
939 | | - RunAndBlockSignature, |
940 | | - SpecialPointerAuthDiscriminators::AsyncRunAndBlockFunction |
941 | | - >(const_cast<void*>(callerContext->Function), functionContext); |
942 | | - |
943 | | - auto calleeContext = |
944 | | - pushAsyncContext<RunAndBlockSignature>(callerContext, |
945 | | - calleeContextSize, |
946 | | - &runAndBlock_finish, |
947 | | - functionContext); |
948 | | - return reinterpret_cast<AsyncVoidClosureEntryPoint *>(function)( |
949 | | - calleeContext, functionContext); |
950 | | -} |
951 | | - |
952 | | -// TODO: Remove this hack. |
953 | | -void swift::swift_task_runAndBlockThread(const void *function, |
954 | | - HeapObject *functionContext) { |
955 | | - RunAndBlockSemaphore semaphore; |
956 | | - |
957 | | - // Set up a task that runs the runAndBlock async function above. |
958 | | - auto flags = TaskCreateFlags(); |
959 | | - flags.setPriority(JobPriority::Default); |
960 | | - auto pair = swift_task_create_common( |
961 | | - flags.getOpaqueValue(), |
962 | | - /*options=*/nullptr, |
963 | | - /*futureResultType=*/nullptr, |
964 | | - reinterpret_cast<ThinNullaryAsyncSignature::FunctionType *>( |
965 | | - &runAndBlock_start), |
966 | | - nullptr, |
967 | | - sizeof(RunAndBlockContext)); |
968 | | - auto context = static_cast<RunAndBlockContext*>(pair.InitialContext); |
969 | | - context->Function = function; |
970 | | - context->FunctionContext = functionContext; |
971 | | - context->Semaphore = &semaphore; |
972 | | - |
973 | | - // Enqueue the task. |
974 | | - swift_task_enqueueGlobal(pair.Task); |
975 | | - |
976 | | - // Wait until the task completes. |
977 | | - semaphore.wait(); |
978 | | -} |
979 | | - |
980 | 859 | size_t swift::swift_task_getJobFlags(AsyncTask *task) { |
981 | 860 | return task->Flags.getOpaqueValue(); |
982 | 861 | } |
|
0 commit comments