From 0967858024299d6c17fc5790c0048bc313f8fb80 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Tue, 22 Oct 2013 13:42:11 +0200 Subject: sleep01: New test --- Makefile | 12 +++ freebsd-to-rtems.py | 1 + testsuite/sleep01/test_main.c | 244 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 testsuite/sleep01/test_main.c diff --git a/Makefile b/Makefile index 29a363bd..fcc08d36 100644 --- a/Makefile +++ b/Makefile @@ -779,6 +779,18 @@ LIB_C_FILES += freebsd/usr.bin/netstat/pfkey.c LIB_C_FILES += freebsd/usr.bin/netstat/sctp.c LIB_C_FILES += freebsd/usr.bin/netstat/unix.c +TEST_SLEEP01 = testsuite/sleep01/sleep01.exe +TEST_SLEEP01_O_FILES = +TEST_SLEEP01_D_FILES = +TEST_SLEEP01_O_FILES += testsuite/sleep01/test_main.o +TEST_SLEEP01_D_FILES += testsuite/sleep01/test_main.d +$(TEST_SLEEP01): $(TEST_SLEEP01_O_FILES) $(LIB) + $(LINK.c) -Wl,-Map,testsuite/sleep01/sleep01.map $^ -lm -o $@ +TESTS += $(TEST_SLEEP01) +O_FILES += $(TEST_SLEEP01_O_FILES) +D_FILES += $(TEST_SLEEP01_D_FILES) +RUN_TESTS += $(TEST_SLEEP01) + TEST_SYSCALLS01 = testsuite/syscalls01/syscalls01.exe TEST_SYSCALLS01_O_FILES = TEST_SYSCALLS01_D_FILES = diff --git a/freebsd-to-rtems.py b/freebsd-to-rtems.py index adcde557..35c9c551 100755 --- a/freebsd-to-rtems.py +++ b/freebsd-to-rtems.py @@ -2357,6 +2357,7 @@ in_cksum.addCPUDependentSourceFiles( ) tests = Module('tests') +tests.addTest('sleep01', ['test_main']) tests.addTest('syscalls01', ['test_main']) tests.addTest('commands01', ['test_main']) tests.addTest('usb01', ['init', 'test-file-system'], False) diff --git a/testsuite/sleep01/test_main.c b/testsuite/sleep01/test_main.c new file mode 100644 index 00000000..929c738d --- /dev/null +++ b/testsuite/sleep01/test_main.c @@ -0,0 +1,244 @@ +/* + * Copyright (c) 2013 embedded brains GmbH. All rights reserved. + * + * embedded brains GmbH + * Dornierstr. 4 + * 82178 Puchheim + * Germany + * + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +#define TEST_NAME "LIBBSD SLEEP 1" + +#define SLAVE_COUNT 3 + +#define PRIO_HIGH 1 + +#define PRIO_LOW 5 + +/* + * This priority assignment ensures that we can test the priority thread queue + * order. + */ +static const rtems_task_priority slave_prios[SLAVE_COUNT] = { 4, 3, 2 }; + +static rtems_id slave_ids[SLAVE_COUNT]; + +static int slave_counters[SLAVE_COUNT]; + +static int channel; + +static int counter; + +static void +set_self_prio(rtems_task_priority prio) +{ + rtems_status_code sc; + + sc = rtems_task_set_priority(RTEMS_SELF, prio, &prio); + assert(sc == RTEMS_SUCCESSFUL); +} + +static void +slave_task(rtems_task_argument slave) +{ + while (true) { + int timo = 0; + int priority = 0; + const char *wmesg = "slave_task"; + int eno; + + ++counter; + slave_counters[slave] = counter; + + printf("slave[%" PRIuPTR "]: %i\n", slave, counter); + + eno = tsleep(&channel, priority, wmesg, timo); + assert(eno == 0); + } +} + +static void +start_slaves(void) +{ + size_t i; + + set_self_prio(PRIO_HIGH); + + for (i = 0; i < SLAVE_COUNT; ++i) { + rtems_status_code sc; + + sc = rtems_task_create( + rtems_build_name('S', 'L', 'A', 'V'), + slave_prios[i], + RTEMS_MINIMUM_STACK_SIZE, + RTEMS_DEFAULT_MODES, + RTEMS_DEFAULT_ATTRIBUTES, + &slave_ids[i] + ); + assert(sc == RTEMS_SUCCESSFUL); + + sc = rtems_task_start(slave_ids[i], slave_task, i); + assert(sc == RTEMS_SUCCESSFUL); + } + + set_self_prio(PRIO_LOW); +} + +static void +test_timeouts(void) +{ + int eno; + struct mtx mtx; + int priority = 0; + const char *wmesg; + int timo = 2; + + puts("test timeouts"); + + mtx_init(&mtx, "test timeouts: mtx", NULL, MTX_DEF); + mtx_lock(&mtx); + wmesg = "test timeouts: msleep"; + eno = msleep(&channel, &mtx, priority, wmesg, timo); + assert(eno == EWOULDBLOCK); + mtx_unlock(&mtx); + mtx_destroy(&mtx); + + mtx_init(&mtx, "test timeouts: mtx spin", NULL, MTX_SPIN); + mtx_lock_spin(&mtx); + wmesg = "test timeouts: msleep_spin"; + eno = msleep_spin(&channel, &mtx, wmesg, timo); + assert(eno == EWOULDBLOCK); + mtx_unlock_spin(&mtx); + mtx_destroy(&mtx); + + wmesg = "test timeouts: pause"; + pause(wmesg, timo); + + wmesg = "test timeouts: tsleep"; + eno = tsleep(&channel, priority, wmesg, timo); + assert(eno == EWOULDBLOCK); +} + +static void +test_wakeup_without_waiter(void) +{ + int some_channel; + + puts("test wakeup without waiter"); + + wakeup_one(&some_channel); + wakeup(&some_channel); +} + +static void +test_wakeup_one(void) +{ + puts("test wakeup one"); + + assert(slave_counters[0] == 3); + assert(slave_counters[1] == 2); + assert(slave_counters[2] == 1); + + wakeup_one(&channel); + + assert(slave_counters[0] == 3); + assert(slave_counters[1] == 2); + assert(slave_counters[2] == 4); + + wakeup_one(&channel); + + assert(slave_counters[0] == 3); + assert(slave_counters[1] == 2); + assert(slave_counters[2] == 5); +} + +static void +test_wakeup(void) +{ + puts("test wakeup"); + + assert(slave_counters[0] == 3); + assert(slave_counters[1] == 2); + assert(slave_counters[2] == 5); + + wakeup(&channel); + + assert(slave_counters[0] == 8); + assert(slave_counters[1] == 7); + assert(slave_counters[2] == 6); +} + +static void +alloc_basic_resources(void) +{ + rtems_status_code sc; + + curthread; + + sc = rtems_task_wake_after(2); + assert(sc == RTEMS_SUCCESSFUL); +} + +static void +test_main(void) +{ + rtems_resource_snapshot snapshot; + + alloc_basic_resources(); + start_slaves(); + + rtems_resource_snapshot_take(&snapshot); + + test_timeouts(); + test_wakeup_without_waiter(); + test_wakeup_one(); + test_wakeup(); + + assert(rtems_resource_snapshot_check(&snapshot)); + + rtems_stack_checker_report_usage_with_plugin(NULL, + rtems_printf_plugin); + + puts("*** END OF " TEST_NAME " TEST ***"); + exit(0); +} + +#include -- cgit v1.2.3