Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
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 : : #include <hpactor/actor/daemon_actor.hpp>
16 : : #include <hpactor/core/actor_system.hpp>
17 : :
18 : : namespace hpactor {
19 : :
20 : 5 : DaemonActor::DaemonActor(ActorContext* ctx, ActorSystem& sys)
21 : 5 : : EventBasedActor(ctx, sys) {}
22 : :
23 : 5 : DaemonActor::~DaemonActor() {
24 : 5 : running_.store(false, std::memory_order_release);
25 : 5 : if (daemon_thread_.joinable()) {
26 : 4 : daemon_thread_.join();
27 : : }
28 : 5 : }
29 : :
30 : 5 : void DaemonActor::on_activate() {
31 : 5 : EventBasedActor::on_activate();
32 : :
33 : 5 : running_.store(true, std::memory_order_release);
34 : 5 : daemon_thread_ = std::thread(&DaemonActor::daemon_loop, this);
35 : 5 : }
36 : :
37 : 1 : void DaemonActor::on_deactivate() {
38 : 1 : running_.store(false, std::memory_order_release);
39 : 1 : if (daemon_thread_.joinable()) {
40 : 1 : daemon_thread_.join();
41 : : }
42 : 1 : EventBasedActor::on_deactivate();
43 : 1 : }
44 : :
45 : 5 : void DaemonActor::daemon_loop() {
46 : 5 : on_daemon_start();
47 : 21 : while (running_.load(std::memory_order_acquire)) {
48 : 19 : if (!run_once()) break;
49 : : }
50 : 5 : on_daemon_stop();
51 : 5 : running_.store(false, std::memory_order_release);
52 : 5 : }
53 : :
54 : : } // namespace hpactor
|