MCPcopy Create free account
hub / github.com/apache/arrow / WorkerTask

Method WorkerTask

cpp/src/arrow/util/async_generator.h:1872–1930  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1870 };
1871
1872 static void WorkerTask(std::shared_ptr<State> state) {
1873 state->worker_thread_id.store(::arrow::internal::GetThreadId());
1874 // We need to capture the state to read while outside the mutex
1875 bool reading = true;
1876 while (reading) {
1877 auto next = state->it.Next();
1878 // Need to capture state->waiting_future inside the mutex to mark finished outside
1879 Future<T> waiting_future;
1880 {
1881 auto guard = state->mutex.Lock();
1882
1883 if (state->should_shutdown) {
1884 state->finished = true;
1885 break;
1886 }
1887
1888 if (!next.ok() || IsIterationEnd<T>(*next)) {
1889 // Terminal item. Mark finished to true, send this last item, and quit
1890 state->finished = true;
1891 if (!next.ok()) {
1892 state->ClearQueue();
1893 }
1894 }
1895 // At this point we are going to send an item. Either we will add it to the
1896 // queue or deliver it to a waiting future.
1897 if (state->waiting_future.has_value()) {
1898 waiting_future = std::move(state->waiting_future.value());
1899 state->waiting_future.reset();
1900 } else {
1901 state->queue.push(std::move(next));
1902 // We just filled up the queue so it is time to quit. We may need to notify
1903 // a cleanup task so we transition to Quitting
1904 if (static_cast<int>(state->queue.size()) >= state->max_q) {
1905 state->reading = false;
1906 }
1907 }
1908 reading = state->reading && !state->finished;
1909 }
1910 // This should happen outside the mutex. Presumably there is a
1911 // transferring generator on the other end that will quickly transfer any
1912 // callbacks off of this thread so we can continue looping. Still, best not to
1913 // rely on that
1914 if (waiting_future.is_valid()) {
1915 waiting_future.MarkFinished(next);
1916 }
1917 }
1918 // Once we've sent our last item we can notify any waiters that we are done and so
1919 // either state can be cleaned up or a new background task can be started
1920 Future<> task_finished;
1921 {
1922 auto guard = state->mutex.Lock();
1923 // After we give up the mutex state can be safely deleted. We will no longer
1924 // reference it. We can safely transition to idle now.
1925 task_finished = state->task_finished;
1926 state->task_finished = Future<>();
1927 state->worker_thread_id.store(kUnlikelyThreadId);
1928 }
1929 task_finished.MarkFinished();

Callers

nothing calls this directly

Calls 12

GetThreadIdFunction · 0.85
storeMethod · 0.80
ClearQueueMethod · 0.80
NextMethod · 0.45
LockMethod · 0.45
okMethod · 0.45
valueMethod · 0.45
resetMethod · 0.45
pushMethod · 0.45
sizeMethod · 0.45
is_validMethod · 0.45
MarkFinishedMethod · 0.45

Tested by

no test coverage detected