void funcA() { int cptA = 0; while ( cptA < 3 ) { cptA ++; ctx_switch(); } }
void funcB() { int cptB = 1; while ( cptB < 6 ) { cptB += 2 ; ctx_switch(); } }
//------------------------------------------------------------------------ int kmain ( void ) { init_hw(); create_process(funcB, NULL, STACK_SIZE); create_process(funcA, NULL, STACK_SIZE); start_sched(); ctx_switch(); /* Pas atteignable vu nos 2 fonctions */ return 0; }
void sched_switch(thread_t *newtd) { if (!sched_active) return; cs_enter(); thread_t *td = thread_self(); td->td_flags &= ~(TDF_SLICEEND | TDF_NEEDSWITCH); if (td->td_state == TDS_RUNNING) sched_add(td); if (newtd == NULL) newtd = sched_choose(); newtd->td_state = TDS_RUNNING; cs_leave(); if (td != newtd) ctx_switch(td, newtd); }
static inline void _lthread_resume(struct lthread *lt) { struct lthread_sched *sched = THIS_SCHED; struct lthread_stack *s; uint64_t state = lt->state; #if LTHREAD_DIAG int init = 0; #endif sched->current_lthread = lt; if (state & (BIT(ST_LT_CANCELLED) | BIT(ST_LT_EXITED))) { /* if detached we can free the thread now */ if (state & BIT(ST_LT_DETACH)) { _lthread_free(lt); sched->current_lthread = NULL; return; } } if (state & BIT(ST_LT_INIT)) { /* first time this thread has been run */ /* assign thread to this scheduler */ lt->sched = THIS_SCHED; /* allocate stack */ s = _stack_alloc(); lt->stack_container = s; _lthread_set_stack(lt, s->stack, s->stack_size); /* allocate memory for TLS used by this thread */ _lthread_tls_alloc(lt); lt->state = BIT(ST_LT_READY); #if LTHREAD_DIAG init = 1; #endif } DIAG_EVENT(lt, LT_DIAG_LTHREAD_RESUMED, init, lt); /* switch to the new thread */ ctx_switch(<->ctx, &sched->ctx); /* If posting to a queue that could be read by another lcore * we defer the queue write till now to ensure the context has been * saved before the other core tries to resume it * This applies to blocking on mutex, cond, and to set_affinity */ if (lt->pending_wr_queue != NULL) { struct lthread_queue *dest = lt->pending_wr_queue; lt->pending_wr_queue = NULL; /* queue the current thread to the specified queue */ _lthread_queue_insert_mp(dest, lt); } sched->current_lthread = NULL; }
inline void swap(cst_impl* to) { ctx_switch(m_ctx, to->m_ctx, to); }
void yield() { ctx_switch(); }