Skip to content

Commit 7879c2b

Browse files
committedFeb 11, 2021
Core: Support yield current coroutine for high performance timer.
1 parent f6096cf commit 7879c2b

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
 

‎public.h

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ extern st_thread_t st_thread_self(void);
9898
extern void st_thread_exit(void *retval);
9999
extern int st_thread_join(st_thread_t thread, void **retvalp);
100100
extern void st_thread_interrupt(st_thread_t thread);
101+
extern void st_thread_yield();
101102
extern st_thread_t st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stack_size);
102103
extern int st_randomize_stacks(int on);
103104
extern int st_set_utime_function(st_utime_t (*func)(void));

‎sched.c

+26
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ unsigned long long _st_stat_sched_s = 0;
6666

6767
unsigned long long _st_stat_thread_run = 0;
6868
unsigned long long _st_stat_thread_idle = 0;
69+
unsigned long long _st_stat_thread_yield = 0;
6970
#endif
7071

7172

@@ -554,6 +555,31 @@ void _st_vp_check_clock(void)
554555
}
555556

556557

558+
void st_thread_yield()
559+
{
560+
_st_thread_t *me = _ST_CURRENT_THREAD();
561+
562+
// If not thread in RunQ to yield to, ignore and continue to run.
563+
if (_ST_RUNQ.next == &_ST_RUNQ) {
564+
return;
565+
}
566+
567+
#ifdef DEBUG
568+
++_st_stat_thread_yield;
569+
#endif
570+
571+
/* Check sleep queue for expired threads */
572+
_st_vp_check_clock();
573+
574+
// Append thread to the tail of RunQ, we will back after all threads executed.
575+
me->state = _ST_ST_RUNNABLE;
576+
_ST_ADD_RUNQ(me);
577+
578+
// Yield to other threads in the RunQ.
579+
_ST_SWITCH_CONTEXT(me);
580+
}
581+
582+
557583
void st_thread_interrupt(_st_thread_t *thread)
558584
{
559585
/* If thread is already dead */

0 commit comments

Comments
 (0)