resource: Operate based on resource id
[platform/core/system/pass.git] / src / monitor / monitor-thread.c
1 /*
2  * PASS (Power Aware System Service)
3  *
4  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <util/log.h>
20 #include <monitor/monitor.h>
21
22 #include <libsyscommon/resource-manager.h>
23
24 thread_local int empty_counter = 0;
25
26 static int monitor_func(void *data, void **result)
27 {
28         struct monitor *monitor = data;
29         struct monitor_command *cmd;
30
31 new_command:
32         if (dequeue(monitor->q, (void *)&cmd) < 0) {
33                 if (empty_counter++ > MONITOR_POLLING_DURATION) {
34                         empty_counter = 0;
35                         mtx_lock(&monitor->lock);
36                         cnd_wait(&monitor->signal, &monitor->lock);
37                         mtx_unlock(&monitor->lock);
38                         goto new_command;
39                 }
40                 return THREAD_RETURN_CONTINUE;
41         }
42
43         syscommon_resman_update_resource_attrs(cmd->resource_id);
44
45         cmd->done = true;
46         smp_wmb();
47
48         cnd_signal(&cmd->signal);
49
50         return THREAD_RETURN_CONTINUE;
51 }
52
53 int monitor_thread_init(struct monitor *monitor)
54 {
55         struct thread *thread;
56         struct queue *queue;
57         int ret;
58
59         ret = create_queue(&queue, free);
60         if (ret < 0) {
61                 _E("failed to create command queue\n");
62                 return ret;
63         }
64
65         /* q should be assigned before create daemon thread */
66         monitor->q = queue;
67         monitor->priv = NULL;
68         mtx_init(&monitor->lock, mtx_plain);
69         cnd_init(&monitor->signal);
70
71         ret = create_daemon_thread(&thread, monitor_func, monitor);
72         if (ret < 0) {
73                 _E("failed to create monitor thread\n");
74                 cnd_destroy(&monitor->signal);
75                 mtx_destroy(&monitor->lock);
76                 destroy_queue(queue);
77                 monitor->q = NULL;
78                 return ret;
79         }
80
81         monitor->thread = thread;
82
83         return 0;
84 }
85
86 void monitor_thread_exit(struct monitor *monitor)
87 {
88         if (monitor->thread) {
89                 cnd_signal(&monitor->signal);
90                 destroy_thread(monitor->thread);
91                 monitor->thread = NULL;
92         }
93
94         cnd_destroy(&monitor->signal);
95         mtx_destroy(&monitor->lock);
96
97         if (monitor->q) {
98                 destroy_queue(monitor->q);
99                 monitor->q = NULL;
100         }
101 }