From b433de1b2f3ea18721b1873760626f56ba80bbc5 Mon Sep 17 00:00:00 2001 From: Agustina Arzille Date: Fri, 2 Nov 2018 15:22:20 -0300 Subject: kern/thread: implement suspend/resume operations --- kern/thread.c | 70 +++++++++++++++++++-- kern/thread.h | 31 ++++++++-- kern/thread_i.h | 3 + test/Kconfig | 3 + test/Makefile | 1 + test/test_thread_suspend.c | 150 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 250 insertions(+), 8 deletions(-) create mode 100644 test/test_thread_suspend.c diff --git a/kern/thread.c b/kern/thread.c index 4800fa84..bd29ef84 100644 --- a/kern/thread.c +++ b/kern/thread.c @@ -646,6 +646,11 @@ thread_runq_schedule(struct thread_runq *runq) thread_clear_flag(prev, THREAD_YIELD); thread_runq_put_prev(runq, prev); + if (prev->suspend) { + prev->state = THREAD_SUSPENDED; + prev->suspend = false; + } + if (prev->state != THREAD_RUNNING) { thread_runq_remove(runq, prev); @@ -1854,6 +1859,7 @@ thread_init(struct thread *thread, void *stack, turnstile_td_init(&thread->turnstile_td); thread->propagate_priority = false; + thread->suspend = false; thread->preempt_level = THREAD_SUSPEND_PREEMPT_LEVEL; thread->pin_level = 0; thread->intr_level = 0; @@ -2480,7 +2486,7 @@ thread_join(struct thread *thread) } static int -thread_wakeup_common(struct thread *thread, int error) +thread_wakeup_common(struct thread *thread, int error, bool resume) { struct thread_runq *runq; unsigned long flags; @@ -2500,7 +2506,8 @@ thread_wakeup_common(struct thread *thread, int error) } else { runq = thread_lock_runq(thread, &flags); - if (thread->state == THREAD_RUNNING) { + if ((thread->state == THREAD_RUNNING) + || ((thread->state == THREAD_SUSPENDED) && !resume)) { thread_unlock_runq(runq, flags); return EINVAL; } @@ -2534,7 +2541,7 @@ thread_wakeup_common(struct thread *thread, int error) int thread_wakeup(struct thread *thread) { - return thread_wakeup_common(thread, 0); + return thread_wakeup_common(thread, 0, false); } struct thread_timeout_waiter { @@ -2548,7 +2555,7 @@ thread_timeout(struct timer *timer) struct thread_timeout_waiter *waiter; waiter = structof(timer, struct thread_timeout_waiter, timer); - thread_wakeup_common(waiter->thread, ETIMEDOUT); + thread_wakeup_common(waiter->thread, ETIMEDOUT, false); } static int @@ -2613,6 +2620,59 @@ thread_timedsleep(struct spinlock *interlock, const void *wchan_addr, return thread_sleep_common(interlock, wchan_addr, wchan_desc, true, ticks); } +int +thread_suspend(struct thread *thread) +{ + struct thread_runq *runq; + unsigned long flags; + int error; + + if (thread == NULL) { + return EINVAL; + } + + thread_preempt_disable(); + runq = thread_lock_runq(thread, &flags); + + if ((thread == runq->idler) || (thread == runq->balancer)) { + error = EINVAL; + goto done; + } else if ((thread->state == THREAD_SUSPENDED) || (thread->suspend)) { + error = EAGAIN; + goto done; + } + + if (thread->state == THREAD_SLEEPING) { + thread->state = THREAD_SUSPENDED; + } else if (thread != runq->current) { + thread->state = THREAD_SUSPENDED; + thread_runq_remove(runq, thread); + } else { + thread->suspend = true; + + if (runq == thread_runq_local()) { + runq = thread_runq_schedule(runq); + } else { + thread_set_flag(thread, THREAD_YIELD); + cpu_send_thread_schedule(thread_runq_cpu(runq)); + } + } + + error = 0; + +done: + thread_unlock_runq(runq, flags); + thread_preempt_enable(); + + return error; +} + +int +thread_resume(struct thread *thread) +{ + return thread_wakeup_common(thread, 0, true); +} + void thread_delay(uint64_t ticks, bool absolute) { @@ -2753,6 +2813,8 @@ thread_state_to_chr(unsigned int state) return 'S'; case THREAD_DEAD: return 'Z'; + case THREAD_SUSPENDED: + return 'T'; default: panic("thread: unknown state"); } diff --git a/kern/thread.h b/kern/thread.h index 787fc083..41cd6a5b 100644 --- a/kern/thread.h +++ b/kern/thread.h @@ -77,9 +77,10 @@ struct thread_sched_data { /* * Thread states. */ -#define THREAD_RUNNING 0 -#define THREAD_SLEEPING 1 -#define THREAD_DEAD 2 +#define THREAD_RUNNING 0 +#define THREAD_SLEEPING 1 +#define THREAD_DEAD 2 +#define THREAD_SUSPENDED 3 /* * Scheduling policies. @@ -237,12 +238,34 @@ int thread_timedsleep(struct spinlock *interlock, const void *wchan_addr, * Schedule a thread for execution on a processor. * * If the target thread is NULL, the calling thread, or already in the - * running state, no action is performed and EINVAL is returned. + * running state, or in the suspended state, no action is performed and + * EINVAL is returned. * * TODO Describe memory ordering with regard to thread_sleep(). */ int thread_wakeup(struct thread *thread); +/* + * Suspend a thread. + * + * A suspended thread may only be resumed by calling thread_resume(). + * + * This operation is asynchronous, i.e. the caller must not expect the target + * thread to be suspended on return. + * + * If attempting to suspend core system threads, the request is ignored and + * EINVAL is returned. + */ +int thread_suspend(struct thread *thread); + +/* + * Resume a thread. + * + * This call is equivalent to thread_wakeup(), with the exception that + * it may also wake up suspended threads. + */ +int thread_resume(struct thread *thread); + /* * Suspend execution of the calling thread. */ diff --git a/kern/thread_i.h b/kern/thread_i.h index 57871cd0..21f92a29 100644 --- a/kern/thread_i.h +++ b/kern/thread_i.h @@ -139,6 +139,9 @@ struct thread { */ bool boosted; /* (r,t) */ + /* True if the thread is marked to suspend */ + bool suspend; /* (r) */ + union { struct thread_rt_data rt_data; /* (r) */ struct thread_fs_data fs_data; /* (r) */ diff --git a/test/Kconfig b/test/Kconfig index 9f0faf44..d3cd9b5e 100644 --- a/test/Kconfig +++ b/test/Kconfig @@ -61,6 +61,9 @@ config TEST_MODULE_SREF_NOREF config TEST_MODULE_SREF_WEAKREF bool "sref_weakref" +config TEST_MODULE_THREAD_SUSPEND + bool "thread_suspend" + config TEST_MODULE_VM_PAGE_FILL bool "vm_page_fill" diff --git a/test/Makefile b/test/Makefile index 76edbf0e..81e615cf 100644 --- a/test/Makefile +++ b/test/Makefile @@ -10,5 +10,6 @@ x15_SOURCES-$(CONFIG_TEST_MODULE_RCU_DEFER) += test/test_rcu_defer.c x15_SOURCES-$(CONFIG_TEST_MODULE_SREF_DIRTY_ZEROES) += test/test_sref_dirty_zeroes.c x15_SOURCES-$(CONFIG_TEST_MODULE_SREF_NOREF) += test/test_sref_noref.c x15_SOURCES-$(CONFIG_TEST_MODULE_SREF_WEAKREF) += test/test_sref_weakref.c +x15_SOURCES-$(CONFIG_TEST_MODULE_THREAD_SUSPEND) += test/test_thread_suspend.c x15_SOURCES-$(CONFIG_TEST_MODULE_VM_PAGE_FILL) += test/test_vm_page_fill.c x15_SOURCES-$(CONFIG_TEST_MODULE_XCALL) += test/test_xcall.c diff --git a/test/test_thread_suspend.c b/test/test_thread_suspend.c new file mode 100644 index 00000000..886ec5c9 --- /dev/null +++ b/test/test_thread_suspend.c @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2018 Agustina Arzille. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * + * This test checks that thread state transitions are correctly performed + * when a thread is suspended and resumed. It does so by creating three + * threads : + * + * - The first thread spins on an atomic integer used as a lock, which + * puts it in the running state. The lock is released while the thread + * is suspended, and the thread is then resumed. + * + * - The second thread waits on a zero-valued semaphore, which puts it + * in the sleeping state. The semaphore is signalled while the thread + * is suspended, and the thread is then resumed. + * + * - The third suspends itself and is then resumed. + * + * As a result, the following transitions are tested : + * o CREATED -> RUNNING (*) -> SUSPENDED -> RUNNING + * o CREATED -> RUNNING -> SLEEPING (*) -> SUSPENDED -> RUNNING. + * + * (*) Step where a suspend request is made + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void +test_wait_for_state(const struct thread *thread, unsigned int state) +{ + while (thread_state(thread) != state) { + cpu_pause(); + } +} + +static void +test_spin(void *arg) +{ + unsigned long *lock; + + lock = arg; + + while (atomic_cas(lock, 0UL, 1UL, ATOMIC_ACQ_REL) != 0) { + cpu_pause(); + } +} + +static void +test_sleep(void *arg) +{ + struct semaphore *sem; + + sem = arg; + semaphore_wait(sem); +} + +static void +test_suspend_self(void *arg) +{ + (void)arg; + thread_suspend(thread_self()); +} + +static void +test_run(void *arg) +{ + struct thread *thread; + struct thread_attr attr; + unsigned long lock; + struct semaphore sem; + int error; + + (void)arg; + + lock = 1; + thread_attr_init(&attr, "test_spin"); + error = thread_create(&thread, &attr, test_spin, &lock); + error_check(error, "thread_create"); + + test_wait_for_state(thread, THREAD_RUNNING); + thread_suspend(thread); + test_wait_for_state(thread, THREAD_SUSPENDED); + + atomic_store(&lock, 0, ATOMIC_RELEASE); + thread_resume(thread); + thread_join(thread); + + semaphore_init(&sem, 0); + thread_attr_init(&attr, "test_sleep"); + error = thread_create(&thread, &attr, test_sleep, &sem); + error_check(error, "thread_create"); + + test_wait_for_state(thread, THREAD_SLEEPING); + thread_suspend(thread); + test_wait_for_state(thread, THREAD_SUSPENDED); + thread_wakeup(thread); + + if (thread_state(thread) != THREAD_SUSPENDED) { + panic("test: unexpected thread state"); + } + + semaphore_post(&sem); + thread_resume(thread); + thread_join(thread); + + thread_attr_init(&attr, "test_suspend_self"); + error = thread_create(&thread, &attr, test_suspend_self, NULL); + test_wait_for_state(thread, THREAD_SUSPENDED); + thread_resume(thread); + thread_join(thread); + + log_info("done"); +} + +void __init +test_setup(void) +{ + struct thread_attr attr; + int error; + + thread_attr_init(&attr, THREAD_KERNEL_PREFIX "test_run"); + thread_attr_set_detached(&attr); + error = thread_create(NULL, &attr, test_run, NULL); + error_check(error, "thread_create"); +} -- cgit v1.2.3