Fix inconsistency between cache and database
[platform/core/appfw/pkgmgr-info.git] / src / server / worker_thread.hh
1 /*
2  * Copyright (c) 2021 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 SERVER_WORKER_THREAD_HH_
18 #define SERVER_WORKER_THREAD_HH_
19
20 #include <glib-2.0/glib.h>
21 #include <sched.h>
22 #include <sys/types.h>
23
24 #include <condition_variable>
25 #include <memory>
26 #include <mutex>
27 #include <queue>
28 #include <string>
29 #include <thread>
30 #include <vector>
31
32 #include "shared_object.hh"
33 #include "pkg_request.hh"
34
35 namespace pkgmgr_server {
36
37 #ifndef EXPORT_API
38 #define EXPORT_API __attribute__((visibility("default")))
39 #endif
40
41 class EXPORT_API WorkerThread {
42  public:
43   explicit WorkerThread(unsigned int num);
44   ~WorkerThread();
45   bool PushQueue(std::shared_ptr<PkgRequest> req);
46   void SetLocale(std::string locale);
47
48  private:
49   void Run();
50   void SendError(const std::shared_ptr<PkgRequest>& req);
51   void SetMemoryTrimTimer();
52   void StopDbChangeListening();
53   static gboolean TrimMemory(void* data);
54   std::shared_ptr<PkgRequest> PopQueue();
55
56  private:
57   bool stop_all_;
58   utils::SharedObject<std::string> locale_;
59   std::queue<std::shared_ptr<PkgRequest>> queue_;
60   std::vector<std::thread> threads_;
61   std::mutex lock_;
62   std::condition_variable cv_;
63   std::recursive_mutex mutex_;
64   guint timer_ = 0;
65 };
66
67 }  // namespace pkgmgr_server
68
69 #endif  // SERVER_WORKER_THREAD_HH_