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 thread_local int empty_counter = 0;
24 static int monitor_func(void *data, void **result)
26 struct monitor *monitor = data;
27 struct monitor_command *cmd;
30 if (dequeue(monitor->q, (void *)&cmd) < 0) {
31 if (empty_counter++ > MONITOR_POLLING_DURATION) {
33 mtx_lock(&monitor->lock);
34 cnd_wait(&monitor->signal, &monitor->lock);
35 mtx_unlock(&monitor->lock);
38 return THREAD_RETURN_CONTINUE;
41 syscommon_resman_update_resource_attrs(cmd->resource);
46 cnd_signal(&cmd->signal);
48 return THREAD_RETURN_CONTINUE;
51 int monitor_thread_init(struct monitor *monitor)
53 struct thread *thread;
57 ret = create_queue(&queue, free);
59 _E("failed to create command queue\n");
63 /* q should be assigned before create daemon thread */
66 mtx_init(&monitor->lock, mtx_plain);
67 cnd_init(&monitor->signal);
69 ret = create_daemon_thread(&thread, monitor_func, monitor);
71 _E("failed to create monitor thread\n");
72 cnd_destroy(&monitor->signal);
73 mtx_destroy(&monitor->lock);
79 monitor->thread = thread;
84 void monitor_thread_exit(struct monitor *monitor)
86 if (monitor->thread) {
87 cnd_signal(&monitor->signal);
88 destroy_thread(monitor->thread);
89 monitor->thread = NULL;
92 cnd_destroy(&monitor->signal);
93 mtx_destroy(&monitor->lock);
96 destroy_queue(monitor->q);