• 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 #ifndef RUNTIME_INCLUDE_EVENT_CORO_CALLBACK_H
16 #define RUNTIME_INCLUDE_EVENT_CORO_CALLBACK_H
17 
18 #include "libpandabase/os/mutex.h"
19 #include "runtime/include/callback_queue.h"
20 #include "runtime/include/runtime.h"
21 
22 namespace ark {
23 
24 class CoroCallbackQueue final : public CallbackQueue {
25 public:
26     using CallbackPtr = PandaUniquePtr<Callback>;
27 
Process()28     void Process() override
29     {
30         while (!IsEmpty()) {
31             PandaList<CallbackPtr> readyCallbacks;
32             {
33                 os::memory::LockHolder lh(queueLock_);
34                 readyCallbacks = std::move(queue_);
35             }
36             while (!readyCallbacks.empty()) {
37                 auto callback = std::move(readyCallbacks.front());
38                 callback->Run();
39                 readyCallbacks.pop_front();
40             }
41         }
42     }
43 
Post(CallbackPtr callback)44     void Post(CallbackPtr callback) override
45     {
46         os::memory::LockHolder lh(queueLock_);
47         queue_.push_back(std::move(callback));
48     }
49 
PostSequence(PandaList<CallbackPtr> callbacks)50     void PostSequence(PandaList<CallbackPtr> callbacks) override
51     {
52         os::memory::LockHolder lh(queueLock_);
53         queue_.splice(std::prev(queue_.end()), std::move(callbacks));
54     }
55 
IsEmpty()56     bool IsEmpty() override
57     {
58         os::memory::LockHolder lh(queueLock_);
59         return queue_.empty();
60     }
61 
Destroy()62     void Destroy() override
63     {
64         Runtime::GetCurrent()->GetInternalAllocator()->Delete(this);
65     }
66 
67 private:
68     os::memory::Mutex queueLock_;
69     PandaList<CallbackPtr> queue_;
70 };
71 
72 }  // namespace ark
73 
74 #endif  // RUNTIME_INCLUDE_EVENT_CORO_CALLBACK_H
75