• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DAWNNATIVE_ASYC_TASK_H_
16 #define DAWNNATIVE_ASYC_TASK_H_
17 
18 #include <functional>
19 #include <memory>
20 #include <mutex>
21 #include <unordered_map>
22 
23 #include "common/RefCounted.h"
24 
25 namespace dawn_platform {
26     class WaitableEvent;
27     class WorkerTaskPool;
28 }  // namespace dawn_platform
29 
30 namespace dawn_native {
31 
32     // TODO(crbug.com/dawn/826): we'll add additional things to AsyncTask in the future, like
33     // Cancel() and RunNow(). Cancelling helps avoid running the task's body when we are just
34     // shutting down the device. RunNow() could be used for more advanced scenarios, for example
35     // always doing ShaderModule initial compilation asynchronously, but being able to steal the
36     // task if we need it for synchronous pipeline compilation.
37     using AsyncTask = std::function<void()>;
38 
39     class AsyncTaskManager {
40       public:
41         explicit AsyncTaskManager(dawn_platform::WorkerTaskPool* workerTaskPool);
42 
43         void PostTask(AsyncTask asyncTask);
44         void WaitAllPendingTasks();
45         bool HasPendingTasks();
46 
47       private:
48         class WaitableTask : public RefCounted {
49           public:
50             AsyncTask asyncTask;
51             AsyncTaskManager* taskManager;
52             std::unique_ptr<dawn_platform::WaitableEvent> waitableEvent;
53         };
54 
55         static void DoWaitableTask(void* task);
56         void HandleTaskCompletion(WaitableTask* task);
57 
58         std::mutex mPendingTasksMutex;
59         std::unordered_map<WaitableTask*, Ref<WaitableTask>> mPendingTasks;
60         dawn_platform::WorkerTaskPool* mWorkerTaskPool;
61     };
62 
63 }  // namespace dawn_native
64 
65 #endif
66