resource: Operate based on resource id
[platform/core/system/pass.git] / src / monitor / monitor-command.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 <libsyscommon/resource-manager.h>
21 #include <monitor/monitor.h>
22
23 static void _monitor_command_wait_done(struct monitor_command *cmd, int polling)
24 {
25         while (!cmd->done) {
26                 smp_rmb();
27                 if (polling-- <= 0) {
28                         mtx_lock(&cmd->lock);
29                         cnd_wait(&cmd->signal, &cmd->lock);
30                         mtx_unlock(&cmd->lock);
31                         /* done at this time */
32                         break;
33                 }
34         }
35 }
36
37 void monitor_command_wait_done(struct monitor_command *cmd)
38 {
39         _monitor_command_wait_done(cmd, 0);
40 }
41
42 void monitor_command_submit(struct monitor_command *cmd)
43 {
44         struct monitor *monitor = monitor_get();
45
46         cmd->done = false;
47         enqueue(monitor->q, (void *)cmd);
48
49         cnd_signal(&monitor->signal);
50 }
51
52 void monitor_command_submit_sync(struct monitor_command *cmd)
53 {
54         monitor_command_submit(cmd);
55         _monitor_command_wait_done(cmd, MONITOR_POLLING_DURATION);
56 }
57
58 void monitor_command_bind_resource(struct monitor_command *cmd, int resource_id)
59 {
60         if (cmd->resource_id >= 0) {
61                 _E("resource is already bound\n");
62                 return;
63         }
64
65         cmd->resource_id = resource_id;
66 }
67
68 void monitor_command_unbind_resource(struct monitor_command *cmd)
69 {
70         if (cmd->resource_id < 0) {
71                 _E("resource is not bound yet\n");
72                 return;
73         }
74
75         cmd->resource_id = -1;
76 }
77
78 int monitor_command_init(struct monitor_command **cmd)
79 {
80         struct monitor_command *new_cmd;
81
82         new_cmd = calloc(1, sizeof(struct monitor_command));
83         if (!new_cmd)
84                 return -ENOMEM;
85
86         mtx_init(&new_cmd->lock, mtx_plain);
87         cnd_init(&new_cmd->signal);
88         new_cmd->resource_id = -1;
89         new_cmd->done = false;
90
91         *cmd = new_cmd;
92
93         return 0;
94 }
95
96 void monitor_command_exit(struct monitor_command *cmd)
97 {
98         cnd_destroy(&cmd->signal);
99         mtx_destroy(&cmd->lock);
100
101         if (cmd->resource_id >= 0)
102                 _W("resource is not unbound before destroying request\n");
103
104         free(cmd);
105 }