From ea9088eb3db3ea95875e2cd3a50ae0428e41ec3d Mon Sep 17 00:00:00 2001 From: Olga Chernikova Date: Mon, 28 Mar 2016 13:21:25 +0300 Subject: [PATCH] add tests --- Makefile | 2 +- interrupt.c | 4 +- lock.c | 2 +- main.c | 122 ++-------------------------------------------- test_thread.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++++++ test_thread.h | 12 +++++ threads.c | 9 +++- 7 files changed, 157 insertions(+), 125 deletions(-) create mode 100644 test_thread.c create mode 100644 test_thread.h diff --git a/Makefile b/Makefile index ba65924..ddb3dc8 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ ASM := bootstrap.S videomem.S isr_wrapper.S threads_util.S AOBJ:= $(ASM:%.S=.build/%.o) ADEP:= $(ASM:%.S=.build/%.d) -SRC := main.c uart.c io.c interrupt.c timer.c memory_map.c buddy_allocator.c paging.c SLAB_allocator.c threads.c lock.c +SRC := main.c uart.c io.c interrupt.c timer.c memory_map.c buddy_allocator.c paging.c SLAB_allocator.c threads.c lock.c test_thread.c COBJ := $(SRC:%.c=.build/%.o) CDEP := $(SRC:%.c=.build/%.d) diff --git a/interrupt.c b/interrupt.c index aea13df..cdb5d77 100644 --- a/interrupt.c +++ b/interrupt.c @@ -56,8 +56,8 @@ void init_interrupt() { // инициализация прерывания. void interrupt_handler(struct interrupt_handler_args args) { - printf("interrupt_id: %d, error_code: %d, current_thread: %d\n", (int) args.interrupt_id, (int) args.error_code, - (int)get_current_thread()); + // printf("interrupt_id: %d, error_code: %d, current_thread: %d\n", (int) args.interrupt_id, (int) args.error_code, + // (int)get_current_thread()); if (args.interrupt_id == 0x20) { timer_interrupt_handler(); } diff --git a/lock.c b/lock.c index 40944e8..f3e5966 100644 --- a/lock.c +++ b/lock.c @@ -24,7 +24,7 @@ void unlock(struct spinlock *lock) volatile int critical_section_depth = 0; void start_critical_section() { - //printf("critical section start %d\n", critical_section_depth); + //printf("critical section start %d, current thread: %d \n", critical_section_depth, get_current_thread()); interrupt_off(); __sync_fetch_and_add(&critical_section_depth, 1); } diff --git a/main.c b/main.c index 6a1ed6a..e9a795e 100644 --- a/main.c +++ b/main.c @@ -7,125 +7,8 @@ #include "paging.h" #include "threads.h" #include "lock.h" -#include "assert.h" +#include "test_thread.h" -void* fun(void *arg) { - while (true) { - printf("Hello world1\n"); - run_thread(*(pid_t *)arg); - } - return 0; -} - -void* fun2(void *arg) { - for (int i = 0; i < 100; ++i) { - printf("Hello world179\n"); - run_thread(*(pid_t *)arg); - } - run_thread(1); - return 0; -} - -void test_switch_and_arg() { - pid_t t2 = 3; - pid_t t1 = create_thread(fun, &t2); - create_thread(fun2, &t1); - - printf("%d %d\n", t1, t2); - yield(); - printf("I am return to main thread\n"); -} - - -void* fun_finish(void *arg __attribute__((unused))) { - printf("Function with one printf\n"); - return 0; -} - -void* test_finish() { - pid_t t1 = create_thread(fun_finish, 0); - - printf("%d\n", t1); - yield(); - printf("I am return to main thread\n"); - return 0; -} - - -struct spinlock spinlock1; -struct spinlock spinlock2; - -int val = 0; - -void* fun_lock1(void *arg __attribute__((unused))) { - printf("I am starting working in lock1\n"); - lock(&spinlock1); - printf("I am in first lock %d\n", val); - yield(); - lock(&spinlock2); - printf("I am in second lock %d\n", val); - val = 1; - unlock(&spinlock2); - printf("I am out of second lock %d\n", val); - unlock(&spinlock1); - printf("I am out of first lock %d\n", val); - return 0; -} - -void* fun_lock2(void *arg __attribute__((unused))) { - lock(&spinlock1); - printf("I am in first lock %d\n", val); - lock(&spinlock2); - printf("I am in second lock %d\n", val); - val = 2; - unlock(&spinlock2); - printf("I am out of second lock %d\n", val); - unlock(&spinlock1); - printf("I am out of first lock %d\n", val); - return 0; -} - -void test_lock() { - create_thread(fun_lock1, 0); - create_thread(fun_lock2, 0); - - yield(); - yield(); - printf("I am return to main thread\n"); -} - -void* fun_join(void *arg __attribute__((unused))) { - for (int i = 0; i < 9; ++i) { - yield(); - printf("yield number %d\n", i + 1); - } - return fun_join; -} - -void test_join() { - pid_t t1 = create_thread(fun_join, 0); - - void* ret; - thread_join(t1, &ret); - - printf("%p\n", ret); - assert(ret == fun_join); -} - - -void* fun_loop(void *arg __attribute__((unused))) { - while(1) { - //printf("Hello, I am %d\n", get_current_thread()); - } - return 0; -} - -void test_timer_interrupt() { - create_thread(fun_loop, 0); - create_thread(fun_loop, 0); - create_thread(fun_loop, 0); - hang(); -} void main(void) { start_critical_section(); @@ -144,7 +27,8 @@ void main(void) { init_timer(); end_critical_section(); - test_timer_interrupt(); + //test_timer_interrupt(); + test_slab(); hang(); } diff --git a/test_thread.c b/test_thread.c new file mode 100644 index 0000000..31b50fe --- /dev/null +++ b/test_thread.c @@ -0,0 +1,131 @@ +#include "test_thread.h" + +void* fun(void *arg) { + while (true) { + printf("Hello world1\n"); + run_thread(*(pid_t *)arg); + } + return 0; +} + +void* fun2(void *arg) { + for (int i = 0; i < 100; ++i) { + printf("Hello world179\n"); + run_thread(*(pid_t *)arg); + } + run_thread(1); + return 0; +} + +void test_switch_and_arg() { + pid_t t2 = 3; + pid_t t1 = create_thread(fun, &t2); + create_thread(fun2, &t1); + + printf("%d %d\n", t1, t2); + yield(); + printf("I am return to main thread\n"); +} + + +void* fun_finish(void *arg __attribute__((unused))) { + printf("Function with one printf\n"); + return 0; +} + +void test_finish() { + pid_t t1 = create_thread(fun_finish, 0); + + printf("%d\n", t1); + yield(); + printf("I am return to main thread\n"); +} + + +struct spinlock spinlock1; +struct spinlock spinlock2; + +int val = 0; + +void* fun_lock1(void *arg __attribute__((unused))) { + printf("I am starting working in lock1\n"); + lock(&spinlock1); + printf("I am in first lock %d\n", val); + yield(); + lock(&spinlock2); + printf("I am in second lock %d\n", val); + val = 1; + unlock(&spinlock2); + printf("I am out of second lock %d\n", val); + unlock(&spinlock1); + printf("I am out of first lock %d\n", val); + return 0; +} + +void* fun_lock2(void *arg __attribute__((unused))) { + lock(&spinlock1); + printf("I am in first lock %d\n", val); + lock(&spinlock2); + printf("I am in second lock %d\n", val); + val = 2; + unlock(&spinlock2); + printf("I am out of second lock %d\n", val); + unlock(&spinlock1); + printf("I am out of first lock %d\n", val); + return 0; +} + +void test_lock() { + create_thread(fun_lock1, 0); + create_thread(fun_lock2, 0); + + yield(); + yield(); + printf("I am return to main thread\n"); +} + +void* fun_join(void *arg __attribute__((unused))) { + for (int i = 0; i < 9; ++i) { + yield(); + printf("yield number %d\n", i + 1); + } + return fun_join; +} + +void test_join() { + pid_t t1 = create_thread(fun_join, 0); + + void* ret; + thread_join(t1, &ret); + + printf("%p\n", ret); + assert(ret == fun_join); +} + + +void* fun_loop(void *arg __attribute__((unused))) { + while(1) { + printf("Hello, I am %d\n", get_current_thread()); + } + return 0; +} + +void test_timer_interrupt() { + create_thread(fun_loop, 0); + create_thread(fun_loop, 0); + create_thread(fun_loop, 0); + hang(); +} + + +void* fun_create_slab(void *arg __attribute__((unused))) { + struct slabctl** slab_sys = create_slab_system(3, 1); + printf("%p ", allocate_block_in_slab_system(slab_sys)); + return 0; +} + +void test_slab() { + create_thread(fun_create_slab, 0); + create_thread(fun_create_slab, 0); + hang(); +} \ No newline at end of file diff --git a/test_thread.h b/test_thread.h new file mode 100644 index 0000000..707ddfa --- /dev/null +++ b/test_thread.h @@ -0,0 +1,12 @@ +#pragma once +#include "threads.h" +#include "io.h" +#include "SLAB_allocator.h" +#include "assert.h" + +void test_switch_and_arg(); +void test_finish(); +void test_lock(); +void test_join(); +void test_timer_interrupt(); +void test_slab(); \ No newline at end of file diff --git a/threads.c b/threads.c index 0300a81..8cd1550 100644 --- a/threads.c +++ b/threads.c @@ -46,8 +46,8 @@ volatile struct thread *get_free_thread() { tp.next[first] = tp.next[1]; tp.prev[first] = 1; - tp.next[1] = first; tp.prev[tp.next[1]] = first; + tp.next[1] = first; return &tp.threads[first]; } @@ -126,13 +126,14 @@ void finish_current_thread(void* val) { start_critical_section(); int ct = get_current_thread(); - printf("thread finish %d\n", ct); + //printf("thread finish %d\n", ct); volatile struct thread* current_t = tp.threads + ct; current_t->state = FINISHED; current_t->ret_val = val; + //printf("%d %d %d\n", ct, tp.next[ct], tp.prev[ct]); tp.prev[tp.next[ct]] = tp.prev[ct]; tp.next[tp.prev[ct]] = tp.next[ct]; @@ -143,9 +144,13 @@ void finish_current_thread(void* val) { void yield() { start_critical_section(); + //printf("%d try change thread\n", get_current_thread()); for (pid_t i = tp.next[current_thread];; i = tp.next[current_thread]) { + //printf("i = %d, tp.threads[i].state = %d\n", i, tp.threads[i].state); if (i == 0 || tp.threads[i].state != RUNNING) continue; + //printf("%d\n", i); run_thread(i); + //printf("continue in %d, after change %d\n", get_current_thread(),i); break; } end_critical_section();