|
| 1 | +///===--- CooperativeGlobalExecutor.inc ---------------------*- C++ -*--===/// |
| 2 | +/// |
| 3 | +/// This source file is part of the Swift.org open source project |
| 4 | +/// |
| 5 | +/// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +/// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +/// |
| 8 | +/// See https:///swift.org/LICENSE.txt for license information |
| 9 | +/// See https:///swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +/// |
| 11 | +///===------------------------------------------------------------------===/// |
| 12 | +/// |
| 13 | +/// The implementation of the cooperative global executor. |
| 14 | +/// |
| 15 | +/// This file is included into GlobalExecutor.cpp only when |
| 16 | +/// the cooperative global executor is enabled. It is expected to |
| 17 | +/// declare the following functions: |
| 18 | +/// swift_task_enqueueGlobalImpl |
| 19 | +/// swift_task_enqueueGlobalWithDelayImpl |
| 20 | +/// swift_task_enqueueMainExecutorImpl |
| 21 | +/// as well as any cooperative-executor-specific functions in the runtime. |
| 22 | +/// |
| 23 | +///===------------------------------------------------------------------===/// |
| 24 | + |
| 25 | +#include <chrono> |
| 26 | +#include <thread> |
| 27 | + |
| 28 | +static Job *JobQueue = nullptr; |
| 29 | + |
| 30 | +class DelayedJob { |
| 31 | +public: |
| 32 | + Job *job; |
| 33 | + unsigned long long when; |
| 34 | + DelayedJob *next; |
| 35 | + |
| 36 | + DelayedJob(Job *job, unsigned long long when) : job(job), when(when), next(nullptr) {} |
| 37 | +}; |
| 38 | + |
| 39 | +static DelayedJob *DelayedJobQueue = nullptr; |
| 40 | + |
| 41 | +/// Get the next-in-queue storage slot. |
| 42 | +static Job *&nextInQueue(Job *cur) { |
| 43 | + return reinterpret_cast<Job*&>(cur->SchedulerPrivate[Job::NextWaitingTaskIndex]); |
| 44 | +} |
| 45 | + |
| 46 | +/// Insert a job into the cooperative global queue. |
| 47 | +SWIFT_CC(swift) |
| 48 | +static void swift_task_enqueueGlobalImpl(Job *job) { |
| 49 | + assert(job && "no job provided"); |
| 50 | + |
| 51 | + Job **position = &JobQueue; |
| 52 | + while (auto cur = *position) { |
| 53 | + // If we find a job with lower priority, insert here. |
| 54 | + if (cur->getPriority() < newJob->getPriority()) { |
| 55 | + nextInQueue(newJob) = cur; |
| 56 | + *position = newJob; |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Otherwise, keep advancing through the queue. |
| 61 | + position = &nextInQueue(cur); |
| 62 | + } |
| 63 | + nextInQueue(newJob) = nullptr; |
| 64 | + *position = newJob; |
| 65 | +} |
| 66 | + |
| 67 | +/// Enqueues a task on the main executor. |
| 68 | +SWIFT_CC(swift) |
| 69 | +static void swift_task_enqueueMainExecutorImpl(Job *job) { |
| 70 | + // The cooperative executor does not distinguish between the main |
| 71 | + // queue and the global queue. |
| 72 | + swift_task_enqueueGlobalImpl(job); |
| 73 | +} |
| 74 | + |
| 75 | +static unsigned long long currentNanos() { |
| 76 | + auto now = std::chrono::steady_clock::now(); |
| 77 | + auto nowNanos = std::chrono::time_point_cast<std::chrono::nanoseconds>(now); |
| 78 | + auto value = std::chrono::duration_cast<std::chrono::nanoseconds>(nowNanos.time_since_epoch()); |
| 79 | + return value.count(); |
| 80 | +} |
| 81 | + |
| 82 | +/// Insert a job into the cooperative global queue with a delay. |
| 83 | +SWIFT_CC(swift) |
| 84 | +static void swift_task_enqueueGlobalWithDelayImpl(unsigned long long delay, |
| 85 | + Job *job) { |
| 86 | + assert(job && "no job provided"); |
| 87 | + |
| 88 | + DelayedJob **position = &DelayedJobQueue; |
| 89 | + DelayedJob *newJob = new DelayedJob(job, currentNanos() + delay); |
| 90 | + |
| 91 | + while (auto cur = *position) { |
| 92 | + // If we find a job with lower priority, insert here. |
| 93 | + if (cur->when > newJob->when) { |
| 94 | + newJob->next = cur; |
| 95 | + *position = newJob; |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + // Otherwise, keep advancing through the queue. |
| 100 | + position = &cur->next; |
| 101 | + } |
| 102 | + *position = newJob; |
| 103 | +} |
| 104 | + |
| 105 | +/// Claim the next job from the cooperative global queue. |
| 106 | +static Job *claimNextFromCooperativeGlobalQueue() { |
| 107 | + // Check delayed jobs first |
| 108 | + while (true) { |
| 109 | + if (auto delayedJob = DelayedJobQueue) { |
| 110 | + if (delayedJob->when < currentNanos()) { |
| 111 | + DelayedJobQueue = delayedJob->next; |
| 112 | + auto job = delayedJob->job; |
| 113 | + |
| 114 | + delete delayedJob; |
| 115 | + |
| 116 | + return job; |
| 117 | + } |
| 118 | + } |
| 119 | + if (auto job = JobQueue) { |
| 120 | + JobQueue = nextInQueue(job); |
| 121 | + return job; |
| 122 | + } |
| 123 | + // there are only delayed jobs left, but they are not ready, |
| 124 | + // so we sleep until the first one is |
| 125 | + if (auto delayedJob = DelayedJobQueue) { |
| 126 | + std::this_thread::sleep_for(std::chrono::nanoseconds(delayedJob->when - currentNanos())); |
| 127 | + continue; |
| 128 | + } |
| 129 | + return nullptr; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +void swift:: |
| 134 | +swift_task_donateThreadToGlobalExecutorUntil(bool (*condition)(void *), |
| 135 | + void *conditionContext) { |
| 136 | + while (!condition(conditionContext)) { |
| 137 | + auto job = claimNextFromCooperativeGlobalQueue(); |
| 138 | + if (!job) return; |
| 139 | + swift_job_run(job, ExecutorRef::generic()); |
| 140 | + } |
| 141 | +} |
0 commit comments