Skip to content

Commit 06be8c3

Browse files
committed
dp: Switch DP thread trigger from semaphore to event
Replace the DP thread trigger mechanism from semaphore to event to prepare for handling multiple types of signals within the DP thread. The previous semaphore-based approach could only wake the thread without providing context about what needs to be processed. Using an event allows signaling different conditions so the thread can identify which tasks to handle. Currently, no new event types are introduced, but this change lays the groundwork for future extensions without altering existing functionality. Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent 3a4dfc9 commit 06be8c3

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

src/include/sof/schedule/dp_schedule.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,11 @@ int scheduler_dp_task_init(struct task **task,
8686
void scheduler_get_task_info_dp(struct scheduler_props *scheduler_props,
8787
uint32_t *data_off_size);
8888

89+
enum {
90+
DP_TASK_EVENT_PROCESS = BIT(0), /* Need to process data */
91+
DP_TASK_EVENT_CANCEL = BIT(1), /* Thread cancellation */
92+
};
93+
94+
95+
8996
#endif /* __SOF_SCHEDULE_DP_SCHEDULE_H__ */

src/schedule/zephyr_dp_schedule.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ static int scheduler_dp_task_cancel(void *data, struct task *task)
258258
schedule_task_cancel(&dp_sch->ll_tick_src);
259259

260260
/* if the task is waiting on a semaphore - let it run and self-terminate */
261-
k_sem_give(pdata->sem);
261+
k_event_set(pdata->event, DP_TASK_EVENT_CANCEL);
262262
scheduler_dp_unlock(lock_key);
263263

264264
/* wait till the task has finished, if there was any task created */
@@ -284,8 +284,8 @@ static int scheduler_dp_task_free(void *data, struct task *task)
284284
}
285285

286286
#ifdef CONFIG_USERSPACE
287-
if (pdata->sem != &pdata->sem_struct)
288-
k_object_free(pdata->sem);
287+
if (pdata->event != &pdata->event_struct)
288+
k_object_free(pdata->event);
289289
if (pdata->thread != &pdata->thread_struct)
290290
k_object_free(pdata->thread);
291291
#endif
@@ -420,14 +420,14 @@ int scheduler_dp_task_init(struct task **task,
420420

421421
/* Point to ksem semaphore for kernel threads synchronization */
422422
/* It will be overwritten for K_USER threads to dynamic ones. */
423-
pdata->sem = &pdata->sem_struct;
423+
pdata->event = &pdata->event_struct;
424424
pdata->thread = &pdata->thread_struct;
425425

426426
#ifdef CONFIG_USERSPACE
427427
if (options & K_USER) {
428-
pdata->sem = k_object_alloc(K_OBJ_SEM);
429-
if (!pdata->sem) {
430-
tr_err(&dp_tr, "Semaphore object allocation failed");
428+
pdata->event = k_object_alloc(K_OBJ_EVENT);
429+
if (!pdata->event) {
430+
tr_err(&dp_tr, "Event object allocation failed");
431431
ret = -ENOMEM;
432432
goto err;
433433
}
@@ -459,7 +459,7 @@ int scheduler_dp_task_init(struct task **task,
459459
stack_size, dp_thread_fn, *task, NULL, NULL,
460460
CONFIG_DP_THREAD_PRIORITY, (*task)->flags, K_FOREVER);
461461

462-
k_thread_access_grant(pdata->thread_id, pdata->sem);
462+
k_thread_access_grant(pdata->thread_id, pdata->event);
463463
scheduler_dp_grant(pdata->thread_id, cpu_get_id());
464464

465465
/* pin the thread to specific core */
@@ -479,8 +479,8 @@ int scheduler_dp_task_init(struct task **task,
479479
}
480480
#endif /* CONFIG_USERSPACE */
481481

482-
/* start the thread, it should immediately stop at a semaphore, so clean it */
483-
k_sem_init(pdata->sem, 0, 1);
482+
/* start the thread, it should immediately stop at an event */
483+
k_event_init(pdata->event);
484484
k_thread_start(pdata->thread_id);
485485

486486
return 0;
@@ -493,7 +493,7 @@ int scheduler_dp_task_init(struct task **task,
493493
tr_err(&dp_tr, "user_stack_free failed!");
494494

495495
/* k_object_free looks for a pointer in the list, any invalid value can be passed */
496-
k_object_free(task_memory->pdata.sem);
496+
k_object_free(task_memory->pdata.event);
497497
k_object_free(task_memory->pdata.thread);
498498
sof_heap_free(user_heap, task_memory);
499499
return ret;

src/schedule/zephyr_dp_schedule.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ struct task_dp_pdata {
2929
uint32_t deadline_clock_ticks; /* dp module deadline in Zephyr ticks */
3030
k_thread_stack_t __sparse_cache *p_stack; /* pointer to thread stack */
3131
size_t stack_size; /* size of the stack in bytes */
32-
struct k_sem *sem; /* pointer to semaphore for task scheduling */
33-
struct k_sem sem_struct; /* semaphore for task scheduling for kernel threads */
32+
struct k_event *event; /* pointer to event for task scheduling */
33+
struct k_event event_struct; /* event for task scheduling for kernel threads */
3434
struct processing_module *mod; /* the module to be scheduled */
3535
uint32_t ll_cycles_to_start; /* current number of LL cycles till delayed start */
3636
};

src/schedule/zephyr_dp_schedule_thread.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sof/common.h>
1212
#include <sof/list.h>
1313
#include <sof/schedule/ll_schedule_domain.h>
14+
#include <sof/schedule/dp_schedule.h>
1415

1516
#include <zephyr/kernel.h>
1617

@@ -64,7 +65,7 @@ void scheduler_dp_recalculate(struct scheduler_dp_data *dp_sch, bool is_ll_post_
6465
pdata->ll_cycles_to_start = 1;
6566
}
6667
trigger_task = true;
67-
k_sem_give(pdata->sem);
68+
k_event_post(pdata->event, DP_TASK_EVENT_PROCESS);
6869
}
6970
}
7071
if (curr_task->state == SOF_TASK_STATE_RUNNING) {
@@ -115,10 +116,11 @@ void dp_thread_fn(void *p1, void *p2, void *p3)
115116

116117
do {
117118
/*
118-
* the thread is started immediately after creation, it will stop on semaphore
119-
* Semaphore will be released once the task is ready to process
119+
* the thread is started immediately after creation, it will stop on event.
120+
* Event will be signaled once the task is ready to process.
120121
*/
121-
k_sem_take(task_pdata->sem, K_FOREVER);
122+
k_event_wait_safe(task_pdata->event, DP_TASK_EVENT_PROCESS| DP_TASK_EVENT_CANCEL,
123+
false, K_FOREVER);
122124

123125
if (task->state == SOF_TASK_STATE_RUNNING)
124126
state = task_run(task);

0 commit comments

Comments
 (0)