2 * PASS (Power Aware System Service)
4 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <monitor/monitor.h>
22 #include <libsyscommon/resource-manager.h>
24 thread_local int empty_counter = 0;
26 static int monitor_func(void *data, void **result)
28 struct monitor *monitor = data;
29 struct monitor_command *cmd;
32 if (dequeue(monitor->q, (void *)&cmd) < 0) {
33 if (empty_counter++ > MONITOR_POLLING_DURATION) {
35 mtx_lock(&monitor->lock);
36 cnd_wait(&monitor->signal, &monitor->lock);
37 mtx_unlock(&monitor->lock);
40 return THREAD_RETURN_CONTINUE;
43 syscommon_resman_update_resource_attrs(cmd->resource_id);
48 cnd_signal(&cmd->signal);
50 return THREAD_RETURN_CONTINUE;
53 int monitor_thread_init(struct monitor *monitor)
55 struct thread *thread;
59 ret = create_queue(&queue, free);
61 _E("failed to create command queue\n");
65 /* q should be assigned before create daemon thread */
68 mtx_init(&monitor->lock, mtx_plain);
69 cnd_init(&monitor->signal);
71 ret = create_daemon_thread(&thread, monitor_func, monitor);
73 _E("failed to create monitor thread\n");
74 cnd_destroy(&monitor->signal);
75 mtx_destroy(&monitor->lock);
81 monitor->thread = thread;
86 void monitor_thread_exit(struct monitor *monitor)
88 if (monitor->thread) {
89 cnd_signal(&monitor->signal);
90 destroy_thread(monitor->thread);
91 monitor->thread = NULL;
94 cnd_destroy(&monitor->signal);
95 mtx_destroy(&monitor->lock);
98 destroy_queue(monitor->q);