Skip to content

Commit 4326321

Browse files
committed
DP: application: switch back to events
Events provide a more natural API when the listener have to wait for several kinds of alarms at once. As the userspace code stabilises initial difficulties with the API got fixed. Signed-off-by: Guennadi Liakhovetski <[email protected]>
1 parent 778a895 commit 4326321

File tree

3 files changed

+15
-52
lines changed

3 files changed

+15
-52
lines changed

src/schedule/zephyr_dp_schedule.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,8 @@ static int scheduler_dp_task_stop(void *data, struct task *task)
266266
schedule_task_cancel(&dp_sch->ll_tick_src);
267267

268268
/* if the task is waiting - let it run and self-terminate */
269-
#if CONFIG_SOF_USERSPACE_APPLICATION
270-
k_sem_give(pdata->sem);
271-
#else
272269
k_event_set(pdata->event, DP_TASK_EVENT_CANCEL);
273-
#endif
270+
274271
scheduler_dp_unlock(lock_key);
275272

276273
/* wait till the task has finished, if there was any task created */
@@ -296,12 +293,8 @@ static int scheduler_dp_task_free(void *data, struct task *task)
296293
}
297294

298295
#ifdef CONFIG_USERSPACE
299-
#if CONFIG_SOF_USERSPACE_PROXY
300296
if (pdata->event != &pdata->event_struct)
301297
k_object_free(pdata->event);
302-
#else
303-
k_object_free(pdata->sem);
304-
#endif
305298
if (pdata->thread != &pdata->thread_struct)
306299
k_object_free(pdata->thread);
307300
#endif

src/schedule/zephyr_dp_schedule.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,11 @@ struct task_dp_pdata {
4141
struct processing_module *mod; /* the module to be scheduled */
4242
uint32_t ll_cycles_to_start; /* current number of LL cycles till delayed start */
4343
#if CONFIG_SOF_USERSPACE_APPLICATION
44-
struct k_sem *sem; /* pointer to semaphore for task scheduling */
4544
struct ipc4_flat *flat;
46-
unsigned char pend_ipc;
47-
unsigned char pend_proc;
4845
struct k_mem_partition mpart[SOF_DP_PART_TYPE_COUNT];
49-
#else
46+
#endif
5047
struct k_event *event; /* pointer to event for task scheduling */
5148
struct k_event event_struct; /* event for task scheduling for kernel threads */
52-
#endif
5349
};
5450

5551
void scheduler_dp_recalculate(struct scheduler_dp_data *dp_sch, bool is_ll_post_run);

src/schedule/zephyr_dp_schedule_application.c

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,8 @@ int scheduler_dp_thread_ipc(struct processing_module *pmod, unsigned int cmd,
175175
pdata->flat->ret = -ENOSYS;
176176

177177
ret = ipc_thread_flatten(cmd, param, pdata->flat);
178-
if (!ret) {
179-
pdata->pend_ipc++;
180-
k_sem_give(pdata->sem);
181-
}
178+
if (!ret)
179+
k_event_post(pdata->event, DP_TASK_EVENT_IPC);
182180

183181
scheduler_dp_unlock(lock_key);
184182

@@ -224,8 +222,7 @@ void scheduler_dp_recalculate(struct scheduler_dp_data *dp_sch, bool is_ll_post_
224222
/* trigger the task */
225223
curr_task->state = SOF_TASK_STATE_RUNNING;
226224
trigger_task = true;
227-
pdata->pend_proc++;
228-
k_sem_give(pdata->sem);
225+
k_event_post(pdata->event, DP_TASK_EVENT_PROCESS);
229226
}
230227

231228
if (curr_task->state == SOF_TASK_STATE_RUNNING) {
@@ -277,41 +274,18 @@ void dp_thread_fn(void *p1, void *p2, void *p3)
277274
comp_info(pmod->dev, "userspace thread started");
278275

279276
do {
280-
/*
281-
* The thread is started immediately after creation, it stops here and waits
282-
* for the semaphore to be signalled to handle IPC or process audio data.
283-
*/
284-
k_sem_take(task_pdata->sem, K_FOREVER);
285-
286-
lock_key = scheduler_dp_lock(task->core);
287-
288-
unsigned char pend_ipc = task_pdata->pend_ipc,
289-
pend_proc = task_pdata->pend_proc;
290-
291-
task_pdata->pend_proc = 0;
292-
task_pdata->pend_ipc = 0;
293-
294-
scheduler_dp_unlock(lock_key);
295-
296-
/*
297-
* Only 0:1, 1:0 and 1:1 are valid. 0:0 is also possible if IPC and audio
298-
* were signalled in a quick succession before we took the lock above. Any
299-
* value > 1 would mean that we've missed IPCs or LL ticks while in queued /
300-
* idle state, which shouldn't happen.
301-
*/
302-
if (pend_ipc > 1 || pend_proc > 1) {
303-
tr_err(&dp_tr, "Invalid wake up %u:%u", pend_proc, pend_ipc);
304-
continue;
305-
}
277+
uint32_t mask = k_event_wait_safe(task_pdata->event,
278+
DP_TASK_EVENT_PROCESS | DP_TASK_EVENT_CANCEL |
279+
DP_TASK_EVENT_IPC, false, K_FOREVER);
306280

307-
if (pend_ipc) {
281+
if (mask & DP_TASK_EVENT_IPC) {
308282
/* handle IPC */
309283
tr_dbg(&dp_tr, "got IPC wake up for %p state %d", pmod, task->state);
310284
ipc_thread_unflatten_run(pmod, task_pdata->flat);
311285
k_sem_give(&dp_sync[task->core]);
312286
}
313287

314-
if (pend_proc) {
288+
if (mask & DP_TASK_EVENT_PROCESS) {
315289
bool ready;
316290

317291
if (task->state == SOF_TASK_STATE_RUNNING) {
@@ -455,8 +429,8 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid,
455429

456430
pdata->flat = &task_memory->flat;
457431

458-
pdata->sem = k_object_alloc(K_OBJ_SEM);
459-
if (!pdata->sem) {
432+
pdata->event = k_object_alloc(K_OBJ_EVENT);
433+
if (!pdata->event) {
460434
tr_err(&dp_tr, "Event object allocation failed");
461435
ret = -ENOMEM;
462436
goto e_stack;
@@ -493,7 +467,7 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid,
493467
goto e_thread;
494468
}
495469

496-
k_thread_access_grant(pdata->thread_id, pdata->sem, &dp_sync[core]);
470+
k_thread_access_grant(pdata->thread_id, pdata->event, &dp_sync[core]);
497471
scheduler_dp_grant(pdata->thread_id, core);
498472

499473
unsigned int pidx;
@@ -547,7 +521,7 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid,
547521
}
548522

549523
/* start the thread, it should immediately stop at the semaphore */
550-
k_sem_init(pdata->sem, 0, 1);
524+
k_event_init(pdata->event);
551525
k_thread_start(pdata->thread_id);
552526

553527
return 0;
@@ -559,7 +533,7 @@ int scheduler_dp_task_init(struct task **task, const struct sof_uuid_entry *uid,
559533
e_kobj:
560534
/* k_object_free looks for a pointer in the list, any invalid value can be passed */
561535
k_object_free(pdata->thread);
562-
k_object_free(pdata->sem);
536+
k_object_free(pdata->event);
563537
e_stack:
564538
user_stack_free(p_stack);
565539
e_tmem:

0 commit comments

Comments
 (0)