-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrategroup.c
More file actions
270 lines (219 loc) · 6.09 KB
/
rategroup.c
File metadata and controls
270 lines (219 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <stdio.h>
#include <errno.h>
#include <aul/exception.h>
#include <aul/iterator.h>
#include <array.h>
#include <buffer.h>
#include <kernel.h>
#include <kernel-priv.h>
extern list_t rategroups;
static ssize_t rategroup_desc(const kobject_t * object, char * buffer, size_t length)
{
const rategroup_t * rg = (const rategroup_t *)object;
string_t ids = string_blank();
list_t * pos = NULL;
list_foreach(pos, &rg->blockinsts)
{
rategroup_blockinst_t * rg_blockinst = list_entry(pos, rategroup_blockinst_t, rategroup_list);
string_append(&ids, "%s'%#x'", (ids.length == 0)? "" : ", ", kobj_id(kobj_cast(rg_blockinst->blockinst)));
}
return snprintf(buffer, length, "{ 'name': '%s', 'priority': %d, 'trigger_id': '%#x', 'blockinstance_ids': [ %s ] }", rg->name, rg->priority, kobj_id(kobj_cast(trigger_cast(rg->trigger))), ids.string);
}
static void rategroup_destroy(kobject_t * object)
{
rategroup_t * rg = (rategroup_t *)object;
// Destroy the rategroup block instances
{
list_t * pos = NULL, * n = NULL;
list_foreach_safe(pos, n, &rg->blockinsts)
{
rategroup_blockinst_t * rg_blockinst = list_entry(pos, rategroup_blockinst_t, rategroup_list);
list_remove(pos);
port_destroy(&rg_blockinst->ports);
free(rg_blockinst);
}
}
free(rg->name);
}
static inline rategroup_t * rategroup_getrunning()
{
kthread_t * self = kthread_self();
if unlikely(self == NULL)
{
return NULL;
}
return (rategroup_t *)kthread_object(self);
}
static bool rategroup_run(kthread_t * thread, kobject_t * object)
{
unused(thread);
rategroup_t * rg = (rategroup_t *)object;
list_t * pos = NULL;
list_foreach(pos, &rg->blockinsts)
{
rategroup_blockinst_t * rg_blockinst = list_entry(pos, rategroup_blockinst_t, rategroup_list);
// Handle all the input links
link_doinputs(&rg_blockinst->ports, &rg_blockinst->blockinst->links);
// Set up the active cache
rg->active = rg_blockinst;
// Call the onupdate function
{
blockact_f onupdate = block_cbupdate(blockinst_block(rg_blockinst->blockinst));
if (onupdate != NULL)
{
blockinst_act(rg_blockinst->blockinst, onupdate);
}
}
// Clear the active cache
rg->active = NULL;
// Handle all the output links
link_dooutputs(&rg_blockinst->ports, &rg_blockinst->blockinst->links);
}
return true;
}
rategroup_t * rategroup_new(const model_linkable_t * linkable, exception_t ** err)
{
// Sanity check
{
if unlikely(exception_check(err))
{
return NULL;
}
if unlikely(model_type(model_object(linkable)) != model_rategroup)
{
exception_set(err, EINVAL, "Bad arguments (not a rategroup linkable)");
return NULL;
}
}
const char * name = NULL;
int priority = 0;
double hertz = 0;
model_getrategroup(linkable, &name, &priority, &hertz);
LOGK(LOG_DEBUG, "Creating rategroup %s with priority %d and update rate of %f Hz", name, priority, hertz);
string_t trigger_name = string_new("%s trigger", name);
trigger_varclock_t * trigger = trigger_newvarclock(trigger_name.string, hertz, err);
if (trigger == NULL || exception_check(err))
{
return NULL;
}
rategroup_t * rg = kobj_new("Rategroup", name, rategroup_desc, rategroup_destroy, sizeof(rategroup_t));
rg->name = strdup(name);
rg->priority = priority;
rg->trigger = trigger;
list_init(&rg->blockinsts);
list_add(&rategroups, &rg->global_list);
return rg;
}
bool rategroup_addblockinst(rategroup_t * rategroup, blockinst_t * blockinst, exception_t ** err)
{
// Sanity check
{
if unlikely(exception_check(err))
{
return false;
}
if unlikely(rategroup == NULL || blockinst == NULL)
{
exception_set(err, EINVAL, "Invalid arguments!");
return false;
}
}
rategroup_blockinst_t * backing = malloc(sizeof(rategroup_blockinst_t));
memset(backing, 0, sizeof(rategroup_blockinst_t));
backing->blockinst = blockinst;
portlist_init(&backing->ports);
// Create the ports
if (!port_makeblockports(blockinst_block(blockinst), &backing->ports, err))
{
return false;
}
// Add the backing to the rategroup
list_add(&rategroup->blockinsts, &backing->rategroup_list);
return true;
}
bool rategroup_schedule(rategroup_t * rategroup, exception_t ** err)
{
// Sanity check
{
if unlikely(exception_check(err))
{
return false;
}
if unlikely(rategroup == NULL)
{
exception_set(err, EINVAL, "Bad arguments!");
return false;
}
}
string_t name = string_new("%s thread", rategroup->name);
kthread_t * thread = kthread_new(name.string, rategroup->priority, trigger_cast(rategroup->trigger), kobj_cast(rategroup), rategroup_run, NULL, err);
if (thread == NULL || exception_check(err))
{
return false;
}
kthread_schedule(thread);
return true;
}
const void * rategroup_input(const char * name)
{
// Sanity check
{
if unlikely(name == NULL)
{
return NULL;
}
}
rategroup_t * rg = rategroup_getrunning();
if unlikely(rg == NULL)
{
LOGK(LOG_WARN, "Could not get executing rategroup. Invalid operating context!");
return NULL;
}
rategroup_blockinst_t * rg_blockinst = rg->active;
if unlikely(rg_blockinst == NULL)
{
LOGK(LOG_WARN, "Rategroup is not currently executing a block instance!");
return NULL;
}
port_t * port = port_lookup(&rg_blockinst->ports, meta_input, name);
if unlikely(port == NULL)
{
LOGK(LOG_WARN, "Could not find input '%s' in block instance!", name);
return NULL;
}
iobacking_t * backing = port_iobacking(port);
if (iobacking_isnull(backing))
{
return NULL;
}
return iobacking_data(backing);
}
void rategroup_output(const char * name, const void * output)
{
// Sanity check
{
if unlikely(name == NULL)
{
return;
}
}
rategroup_t * rg = rategroup_getrunning();
if unlikely(rg == NULL)
{
LOGK(LOG_WARN, "Could not get executing rategroup. Invalid operating context!");
return;
}
rategroup_blockinst_t * rg_blockinst = rg->active;
if unlikely(rg_blockinst == NULL)
{
LOGK(LOG_WARN, "Rategroup is not currently executing a block instance!");
return;
}
port_t * port = port_lookup(&rg_blockinst->ports, meta_output, name);
if unlikely(port == NULL)
{
LOGK(LOG_WARN, "Could not find output '%s' in block instance!", name);
return;
}
iobacking_copy(port_iobacking(port), output);
}