diff --git a/libc/wqueue/work_usrthread.c b/libc/wqueue/work_usrthread.c index f55aaa2f84a..bc679700474 100644 --- a/libc/wqueue/work_usrthread.c +++ b/libc/wqueue/work_usrthread.c @@ -219,18 +219,26 @@ void work_process(FAR struct usr_wqueue_s *wqueue) work = (FAR struct work_s *)work->dq.flink; } } - else + else /* elapsed < work->delay */ { - /* This one is not ready.. will it be ready before the next - * scheduled wakeup interval? + /* This one is not ready. * * NOTE that elapsed is relative to the the current time, * not the time of beginning of this queue processing pass. * So it may need an adjustment. */ - elapsed += (ctick - stick); - remaining = elapsed - work->delay; + elapsed += (ctick - stick); + if (elapsed > work->delay) + { + /* The delay has expired while we are processing */ + + elapsed = work->delay; + } + + /* Will it be ready before the next scheduled wakeup interval? */ + + remaining = work->delay - elapsed; if (remaining < next) { /* Yes.. Then schedule to wake up when the work is ready */ diff --git a/sched/wqueue/kwork_process.c b/sched/wqueue/kwork_process.c index 87ed7900d31..6333a6c2197 100644 --- a/sched/wqueue/kwork_process.c +++ b/sched/wqueue/kwork_process.c @@ -198,18 +198,26 @@ void work_process(FAR struct kwork_wqueue_s *wqueue, uint32_t period, int wndx) work = (FAR struct work_s *)work->dq.flink; } } - else + else /* elapsed < work->delay */ { - /* This one is not ready.. will it be ready before the next - * scheduled wakeup interval? + /* This one is not ready. * * NOTE that elapsed is relative to the the current time, * not the time of beginning of this queue processing pass. * So it may need an adjustment. */ - elapsed += (ctick - stick); - remaining = elapsed - work->delay; + elapsed += (ctick - stick); + if (elapsed > work->delay) + { + /* The delay has expired while we are processing */ + + elapsed = work->delay; + } + + /* Will it be ready before the next scheduled wakeup interval? */ + + remaining = work->delay - elapsed; if (remaining < next) { /* Yes.. Then schedule to wake up when the work is ready */