Merge "Refactor WorkerThread" into tizen
[platform/core/appfw/pkgmgr-info.git] / src / server / request_handler / create_cache_request_handler.cc
1 // Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache-2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "create_cache_request_handler.hh"
6
7 #include <string>
8
9 #include "cache_db_handler.hh"
10 #include "utils/logging.hh"
11
12 namespace psd = pkgmgr_server::database;
13
14 namespace pkgmgr_server {
15 namespace request_handler {
16
17 CreateCacheRequestHandler::Scheduler::Scheduler(pid_t tid) : tid_(tid) {
18   Get(&policy_, &param_);
19 }
20
21 void CreateCacheRequestHandler::Scheduler::ChangePolicy() {
22   struct sched_param param {
23     .sched_priority = 1,
24   };
25   if (Set(SCHED_RR, &param) != 0)
26     return;
27
28   int policy = -1;
29   Get(&policy, &param);
30   LOG(WARNING) << "Current policy: " << policy
31                << ", priority: " << param.sched_priority;
32 }
33
34 void CreateCacheRequestHandler::Scheduler::ResetPolicy() {
35   if (Set(policy_, &param_) != 0)
36     return;
37
38   struct sched_param param = {
39     .sched_priority = 0,
40   };
41   int policy = -1;
42   Get(&policy, &param);
43   LOG(WARNING) << "Current policy: " << policy
44                << ", priority: " << param.sched_priority;
45 }
46
47 void CreateCacheRequestHandler::Scheduler::Get(int* policy,
48     struct sched_param* param) {
49   if (sched_getparam(tid_, param) != 0)
50     LOG(ERROR) << "sched_getparam() is failed. errno: " << errno;
51
52   *policy = sched_getscheduler(tid_);
53   if (*policy < 0)
54     LOG(ERROR) << "sched_getscheduler() is failed. errno: " << errno;
55 }
56
57 int CreateCacheRequestHandler::Scheduler::Set(int policy,
58     struct sched_param* param) {
59   if (sched_setscheduler(tid_, policy, param) != 0) {
60     LOG(ERROR) << "sched_setscheduler() is failed. policy: " << policy
61                << ", errno: " << errno;
62     return -1;
63   }
64
65   LOG(WARNING) << "policy: " << policy
66                << ", sched_priority: " << param->sched_priority;
67   return 0;
68 }
69
70 CreateCacheRequestHandler::CreateCacheRequestHandler(): scheduler_(gettid()) {
71 }
72
73 bool CreateCacheRequestHandler::HandleRequest(unsigned char* data, int size,
74                                             const std::string& locale) {
75   // TODO(ilho159.kim) need to get logined user id
76   psd::CacheDBHandler db(5001, GetPID());
77   db.SetLocale(locale);
78
79   int ret = db.Execute();
80
81   return ret == PMINFO_R_OK;
82 }
83
84 std::vector<uint8_t> CreateCacheRequestHandler::ExtractResult() {
85   return {};
86 }
87
88 void CreateCacheRequestHandler::PreExec() {
89   scheduler_.ChangePolicy();
90 }
91
92 void CreateCacheRequestHandler::PostExec() {
93   scheduler_.ResetPolicy();
94 }
95
96 }  // namespace request_handler
97 }  // namespace pkgmgr_server