d38e327271b2a693d70e31c8ac9f574cb2f56534
[platform/core/appfw/launchpad.git] / src / launchpad-process-pool / process_pool.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 LAUNCHPAD_PROCESS_POOL_PROCESS_POOL_HH_
18 #define LAUNCHPAD_PROCESS_POOL_PROCESS_POOL_HH_
19
20 #include <glib.h>
21 #include <sys/types.h>
22
23 #include <queue>
24 #include <memory>
25 #include <string>
26 #include <vector>
27
28 #include <parcel.hh>
29 #include <socket.hh>
30
31 #include "launchpad-process-pool/executor.hh"
32
33 namespace launchpad {
34
35 class ProcessPool : public Executor::Delegator,
36                     public Executor {
37  public:
38   class IEvent {
39    public:
40     virtual ~IEvent() = default;
41     virtual void OnRequestReceived(tizen_base::Parcel* parcel) = 0;
42   };
43
44   explicit ProcessPool(std::string name, int num_processes,
45       IEvent* event_listener);
46   virtual ~ProcessPool();
47
48   bool IsPrepared() const;
49   pid_t Execute(const tizen_base::Parcel& parcel);
50   void Dispose();
51   void SetTimer();
52
53  private:
54   class Process {
55    public:
56     Process(pid_t pid, int fd);
57
58     pid_t GetPid() const;
59     int Send(const tizen_base::Parcel& parcel);
60     void Kill();
61
62    private:
63     pid_t pid_;
64     std::unique_ptr<Socket> socket_;
65   };
66
67   void OnExecution() override;
68   void UnsetTimer();
69   void PrepareProcess();
70   int WaitForRequest(std::unique_ptr<Socket> socket);
71   static gboolean OnTimeout(gpointer user_data);
72
73  private:
74   std::string name_;
75   int num_processes_;
76   IEvent* event_listener_;
77   int pipe_fd_[2] = { -1, -1 };
78   std::queue<std::shared_ptr<Process>> queue_;
79   guint timer_ = 0;
80 };
81
82 }  // namespace launchpad
83
84 #endif  // LAUNCHPAD_PROCESS_POOL_PROCESS_POOL_HH_