updated the multiplex function to handle hip_sleep()

This commit is contained in:
hyung-hwan 2025-05-02 16:04:17 +09:00
parent 3bf9936244
commit 19d2e97577

53
ctx.c
View File

@ -250,7 +250,7 @@ static void schedule(hip_uctx_t* uctx, void *ctx)
} }
} }
static void poll(hip_uctx_t* uctx, void *ctx) static void multiplex(hip_uctx_t* uctx, void *ctx)
{ {
/* this is a builtin task to handle multiplexing and sleep */ /* this is a builtin task to handle multiplexing and sleep */
hip_t* hip; hip_t* hip;
@ -270,22 +270,39 @@ static void poll(hip_uctx_t* uctx, void *ctx)
hip_nsecdur_t now; hip_nsecdur_t now;
hip_nsecdur_t wait; hip_nsecdur_t wait;
struct timespec tmout; struct timespec tmout;
struct epoll_event ee[100]; /* TODO: hold it in hip_t struct... */ struct epoll_event ee[100]; /* TODO: hold it in hip_t struct... sizing must be handled in the main struct.. */
hip_uctx_t* u;
l = HEAD(&hip->pending); l = HEAD(&hip->pending);
uctx = UCTX_FROM_LINK(l); u = UCTX_FROM_LINK(l);
now = monotime(); now = monotime();
wait = now >= uctx->wakeup_time? 0: uctx->wakeup_time - now; wait = now >= u->wakeup_time? 0: u->wakeup_time - now;
printf ("WAITING %llu\n", (unsigned long long)wait);
// TODO: support different io multiplexers... // TODO: support different io multiplexers...
tmout.tv_sec = wait / 1000000000; tmout.tv_sec = wait / 1000000000;
tmout.tv_nsec = wait % 1000000000; tmout.tv_nsec = wait % 1000000000;
epoll_pwait2(hip->mux_id, ee, 100, &tmout, HIP_NULL); epoll_pwait2(hip->mux_id, ee, 100, &tmout, HIP_NULL);
// TODO: check other io file descriptors...
now = monotime();
while (!IS_EMPTY(&hip->pending))
{
l = HEAD(&hip->pending);
u = UCTX_FROM_LINK(l);
if (now < u->wakeup_time) break;
UNCHAIN(l);
ADD_BACK(l, &hip->runnables);
}
/* go back to the scheduler */
if (!IS_EMPTY(&hip->runnables))
swapcontext(&hip->uctx_mux->uc, &hip->uctx_sched->uc);
} }
} }
} }
hip_t* hip_open(void) hip_t* hip_open(void)
{ {
hip_t* hip; hip_t* hip;
@ -399,7 +416,7 @@ int hip_schedule(hip_t* hip, int preempt)
hip->uctx_sched = hip_uctx_open(hip, 4096, schedule, hip); hip->uctx_sched = hip_uctx_open(hip, 4096, schedule, hip);
if (!hip->uctx_sched) return -1; if (!hip->uctx_sched) return -1;
hip->uctx_mux = hip_uctx_open(hip, 4096, poll, hip); hip->uctx_mux = hip_uctx_open(hip, 4096, multiplex, hip);
if (!hip->uctx_mux) if (!hip->uctx_mux)
{ {
hip_uctx_close (hip->uctx_sched); hip_uctx_close (hip->uctx_sched);
@ -494,11 +511,11 @@ int i;
for (i =0; i < 5; i++) for (i =0; i < 5; i++)
{ {
printf ("************************** uf1 \n"); printf ("************************** uf1 \n");
hip_yield(uc->hip); //hip_yield(uc->hip);
//sleep(1); hip_sleep(uc->hip, 500000000);
printf ("************************* uf1 1111\n"); printf ("************************* uf1 1111\n");
hip_yield(uc->hip); //hip_yield(uc->hip);
//sleep(1); hip_sleep(uc->hip, 1000000000);
} }
} }
@ -508,11 +525,11 @@ static void uf2(hip_uctx_t* uc, void* ctx)
for (i =0; i < 10; i++) for (i =0; i < 10; i++)
{ {
printf ("************************** uf2 \n"); printf ("************************** uf2 \n");
hip_yield(uc->hip); //hip_yield(uc->hip);
//sleep(1); hip_sleep(uc->hip, 1000000000);
printf ("************************* uf2 2222 XXXXXXXXXXXXXXxxxxxxxxxxxxxxxxxxxxxx\n"); printf ("************************* uf2 2222 XXXXXXXXXXXXXXxxxxxxxxxxxxxxxxxxxxxx\n");
hip_yield(uc->hip); //hip_yield(uc->hip);
//sleep(1); hip_sleep(uc->hip, 500000000);
} }
} }
@ -522,11 +539,11 @@ static void uf3(hip_uctx_t* uc, void* ctx)
for (i =0; i < 15; i++) for (i =0; i < 15; i++)
{ {
printf ("************************** uf3 TOP\n"); printf ("************************** uf3 TOP\n");
hip_yield(uc->hip); //hip_yield(uc->hip);
//sleep(1); hip_sleep(uc->hip, 1000000000);
printf ("************************* uf3 3333 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"); printf ("************************* uf3 3333 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n");
hip_yield(uc->hip); //hip_yield(uc->hip);
//sleep(1); hip_sleep(uc->hip, 1000000000);
if (i == 8) hip_newrtn(uc->hip, uf2, HIP_NULL); if (i == 8) hip_newrtn(uc->hip, uf2, HIP_NULL);
} }
} }