Add CynaraThread
[platform/core/appfw/rpc-port.git] / src / cynara_thread.hh
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef CYNARA_THREAD_HH_
18 #define CYNARA_THREAD_HH_
19
20 #include <functional>
21 #include <thread>
22
23 #include "shared-queue-internal.hh"
24
25 namespace rpc_port {
26 namespace internal {
27
28 class Job {
29  public:
30   enum class Type {
31     Continue = 0,
32     Finish = 1,
33   };
34
35   using JobHandlerCallback = std::function<Type(void)>;
36
37   explicit Job(JobHandlerCallback cb);
38   Job();
39
40   ~Job() = default;
41
42   Type Do();
43
44  private:
45   JobHandlerCallback cb_;
46 };
47
48 class CynaraThread {
49  public:
50   static CynaraThread& GetInst();
51   CynaraThread(CynaraThread&) = delete;
52   CynaraThread& operator=(CynaraThread&) = delete;
53   ~CynaraThread();
54
55   void ThreadRun();
56   void Push(Job job);
57
58  private:
59   CynaraThread();
60   Job Pop();
61
62   std::thread thread_;
63   mutable SharedQueue<Job> queue_;
64 };
65
66 }  // namespace internal
67 }  // namespace rpc_port
68
69 #endif  // CYNARA_THREAD_HH_