Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olga24912 committed Mar 28, 2016
1 parent 00c815f commit ea9088e
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 125 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
122 changes: 3 additions & 119 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -144,7 +27,8 @@ void main(void) {
init_timer();
end_critical_section();

test_timer_interrupt();
//test_timer_interrupt();
test_slab();

hang();
}
131 changes: 131 additions & 0 deletions test_thread.c
Original file line number Diff line number Diff line change
@@ -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();
}
12 changes: 12 additions & 0 deletions test_thread.h
Original file line number Diff line number Diff line change
@@ -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();
9 changes: 7 additions & 2 deletions threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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];

Expand All @@ -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();
Expand Down

0 comments on commit ea9088e

Please sign in to comment.