• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 
16 #ifndef PANDA_PLUGINS_ETS_RUNTIME_ETS_TASKPOOL_H
17 #define PANDA_PLUGINS_ETS_RUNTIME_ETS_TASKPOOL_H
18 
19 #include "libpandabase/os/mutex.h"
20 #include "plugins/ets/runtime/types/ets_primitives.h"
21 #include "runtime/include/mem/panda_containers.h"
22 
23 namespace ark::ets {
24 
25 /// @class Taskpool contains information about each common task passed to execution until the task will not be finished
26 class Taskpool final {
27 public:
28     NO_COPY_SEMANTIC(Taskpool);
29     NO_MOVE_SEMANTIC(Taskpool);
30 
31     Taskpool();
32     ~Taskpool() = default;
33 
34     /**
35      * @see taskpool.Task.constructor
36      * @return new unique identifier for creating task
37      */
38     EtsLong GenerateTaskId();
39 
40     /**
41      * @brief Notify taskpool about execution request for task
42      * @param taskId requsted task identifier
43      */
44     void TaskSubmitted(EtsLong taskId);
45 
46     /**
47      * @brief Notify taskpool that task is starting execution on a coroutine
48      * @param coroutineId identifier of executing coroutine for requested task
49      * @param taskId requsted task identifier
50      * @return true if task can be started, false - if task was cancled
51      *
52      * @see CancelTask
53      */
54     bool TaskStarted(uint32_t coroutineId, EtsLong taskId);
55 
56     /**
57      * @brief Notify taskpool that task is ending execution
58      * @param coroutineId identifier of executing coroutine for requested task
59      * @param taskId requsted task identifier
60      * @return true if task can be successfully finished, false - if task was cancled
61      *
62      * @see CancelTask
63      */
64     bool TaskFinished(uint32_t coroutineId, EtsLong taskId);
65 
66     /**
67      * @brief Try to mark task as cancel. Only waiting or running tasks are allowed to canceling
68      * @param taskId identifier of task for canceling
69      * @return true if task was marked as caneling, false - if task is not executed or finished
70      */
71     bool CancelTask(EtsLong taskId);
72 
73     /**
74      * @param coroutineId corotine id with potentially executing task
75      * @return true if coroutine with coroutineId is executing a task and this task is marked as canceling, false -
76      * otherwise
77      */
78     bool IsTaskCanceled(uint32_t coroutineId) const;
79 
80 private:
81     /**
82      * @brief Decrease count of tasks with requested identifier from passed collection. If count of task after
83      * decrementing == 0, then remove this task identifier from the collection
84      * @param taskId identifier of decremented task counter
85      * @param tasks map collection, key is unique task identifier, value is counter of tasks with this identifier
86      * @return count tasks with requested identifier in passed collection after decrementing
87      */
88     size_t DecrementTaskCounter(EtsLong taskId, PandaUnorderedMap<EtsLong, size_t> &tasks) REQUIRES(taskpoolLock_);
89 
90     std::atomic<EtsLong> taskId_ {1};
91     mutable os::memory::Mutex taskpoolLock_;
92     PandaUnorderedMap<EtsLong, size_t> waitingTasks_ GUARDED_BY(taskpoolLock_);
93     PandaUnorderedMap<EtsLong, size_t> runningTasks_ GUARDED_BY(taskpoolLock_);
94     PandaUnorderedSet<EtsLong> tasksToBeCanceled_ GUARDED_BY(taskpoolLock_);
95     // key is coroutine id, value is task id
96     PandaUnorderedMap<uint32_t, EtsLong> executingTasks_ GUARDED_BY(taskpoolLock_);
97 };
98 
99 }  // namespace ark::ets
100 
101 #endif  // PANDA_PLUGINS_ETS_RUNTIME_ETS_TASKPOOL_H
102