diff --git a/sys/compat/linuxkpi/common/src/linux_schedule.c b/sys/compat/linuxkpi/common/src/linux_schedule.c --- a/sys/compat/linuxkpi/common/src/linux_schedule.c +++ b/sys/compat/linuxkpi/common/src/linux_schedule.c @@ -38,6 +38,41 @@ #include #include +static int +convert_to_sleepqueue_timeout(int timeout) +{ + /* range check timeout */ + if (timeout < 1) + timeout = 1; + else if (timeout == MAX_SCHEDULE_TIMEOUT) + timeout = 0; + + return (timeout); +} + +static int +compute_timeout_expiry(int timeout) +{ + return (ticks + timeout); +} + +static int +compute_timeout_remainder(int ret, int timeout, int expiry) +{ + int remainder; + + remainder = expiry - ticks; + + /* range check return value */ + if (ret == -ERESTARTSYS && remainder < 1) + remainder = 1; + else if (remainder < 0) + remainder = 0; + else if (remainder > timeout) + remainder = timeout; + return (remainder); +} + static int linux_add_to_sleepqueue(void *wchan, struct task_struct *task, const char *wmesg, int timeout, int state) @@ -258,13 +293,8 @@ if (lock != NULL) spin_unlock_irq(lock); - /* range check timeout */ - if (timeout < 1) - timeout = 1; - else if (timeout == MAX_SCHEDULE_TIMEOUT) - timeout = 0; - task = current; + timeout = convert_to_sleepqueue_timeout(timeout); sleepq_lock(task); if (atomic_read(&task->state) != TASK_WAKING) { @@ -289,14 +319,9 @@ int remainder; task = current; + timeout = convert_to_sleepqueue_timeout(timeout); - /* range check timeout */ - if (timeout < 1) - timeout = 1; - else if (timeout == MAX_SCHEDULE_TIMEOUT) - timeout = 0; - - remainder = ticks + timeout; + remainder = compute_timeout_expiry(timeout); sleepq_lock(task); state = atomic_read(&task->state); @@ -312,16 +337,8 @@ if (timeout == 0) return (MAX_SCHEDULE_TIMEOUT); - /* range check return value */ - remainder -= ticks; + remainder = compute_timeout_remainder(ret, timeout, remainder); - /* range check return value */ - if (ret == -ERESTARTSYS && remainder < 1) - remainder = 1; - else if (remainder < 0) - remainder = 0; - else if (remainder > timeout) - remainder = timeout; return (remainder); } @@ -350,13 +367,9 @@ void *wchan; int ret; - /* range check timeout */ - if (timeout < 1) - timeout = 1; - else if (timeout == MAX_SCHEDULE_TIMEOUT) - timeout = 0; - task = current; + timeout = convert_to_sleepqueue_timeout(timeout); + wchan = bit_to_wchan(word, bit); for (;;) { sleepq_lock(wchan);