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 : : #pragma once
16 : :
17 : : #include <hpactor/actor/event_based_actor.hpp>
18 : : #include <hpactor/sched/dispatch_policy.hpp>
19 : :
20 : : #include <atomic>
21 : : #include <thread>
22 : :
23 : : namespace hpactor {
24 : :
25 : : class DaemonActor : public EventBasedActor {
26 : : public:
27 : 8 : sched::DispatchPolicy dispatch_policy() const override {
28 : 8 : return sched::DispatchPolicy::DedicatedThread;
29 : : }
30 : :
31 : : // Override to provide the daemon's main loop body.
32 : : // Called repeatedly from the dedicated thread.
33 : : // Return false to exit the loop (actor is shutting down).
34 : : virtual bool run_once() = 0;
35 : :
36 : : // Called when the dedicated thread starts, before run_once loop.
37 : 2 : virtual void on_daemon_start() {}
38 : :
39 : : // Called when the dedicated thread stops, after run_once loop.
40 : 3 : virtual void on_daemon_stop() {}
41 : :
42 : 1 : void set_cpu_affinity(int core) {
43 : 1 : hints_.cpu_affinity = core;
44 : 1 : }
45 : :
46 : 6 : sched::DispatchHints dispatch_hints() const override {
47 : 6 : return hints_;
48 : : }
49 : :
50 : : // Access the mailbox for draining in run_once
51 : 0 : mailbox::MPSCActorMailbox<TypedMessage>* mailbox() {
52 : 0 : return get_mailbox();
53 : : }
54 : :
55 : : void on_activate() override;
56 : : void on_deactivate() override;
57 : :
58 : : protected:
59 : : DaemonActor(ActorContext* ctx, ActorSystem& sys);
60 : : ~DaemonActor() override;
61 : :
62 : : private:
63 : : void daemon_loop();
64 : :
65 : : std::thread daemon_thread_;
66 : : std::atomic<bool> running_{false};
67 : : sched::DispatchHints hints_;
68 : : };
69 : :
70 : : } // namespace hpactor
|