From 43d0bb8e3997022e5270f7f75f615a47819c929e Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Tue, 1 May 2012 23:48:56 +0200 Subject: Basic object system - THIS IS STILL A LONG WAY TO GO!! --- src/user/lib/Makefile | 3 +- src/user/lib/include/stdio.h | 2 ++ src/user/lib/include/tce/Folder.h | 12 +++++++ src/user/lib/include/tce/Object.h | 17 ++++++++++ src/user/lib/std/stdio.c | 1 - src/user/lib/tce/Folder.c | 24 ++++++++++++++ src/user/lib/tce/syscall.c | 48 ++++++++++++++++++++------- src/user/test/main.c | 69 ++++++++++++++++++++++++++++++++++++--- 8 files changed, 159 insertions(+), 17 deletions(-) create mode 100644 src/user/lib/include/tce/Folder.h create mode 100644 src/user/lib/include/tce/Object.h create mode 100644 src/user/lib/tce/Folder.c (limited to 'src/user') diff --git a/src/user/lib/Makefile b/src/user/lib/Makefile index 4019e88..4b88336 100644 --- a/src/user/lib/Makefile +++ b/src/user/lib/Makefile @@ -1,5 +1,6 @@ Out = _user.o -Obj = tce/syscall.o std/_dlmalloc.o \ +Obj = tce/syscall.o tce/Folder.o \ + std/_dlmalloc.o \ std/stdio.o std/stdlib.o \ start.o ExtObj = $(SrcPath)/common/_common.o diff --git a/src/user/lib/include/stdio.h b/src/user/lib/include/stdio.h index e3f9d89..3b6c2d2 100644 --- a/src/user/lib/include/stdio.h +++ b/src/user/lib/include/stdio.h @@ -1,6 +1,8 @@ #ifndef DEF_STDIO_H #define DEF_STDIO_H +#include + void printk_int(int number); void printk_hex(unsigned number); diff --git a/src/user/lib/include/tce/Folder.h b/src/user/lib/include/tce/Folder.h new file mode 100644 index 0000000..3a796d9 --- /dev/null +++ b/src/user/lib/include/tce/Folder.h @@ -0,0 +1,12 @@ +#ifndef DEF_LIB_TCE_USER_FOLDER_H +#define DEF_LIB_TCE_USER_FOLDER_H + +#include +#include + +struct FolderMethods *Folder_getMethods(); + +int Folder_GetChildNameAt(Object object, int pos, char* to); + +#endif + diff --git a/src/user/lib/include/tce/Object.h b/src/user/lib/include/tce/Object.h new file mode 100644 index 0000000..f363bd6 --- /dev/null +++ b/src/user/lib/include/tce/Object.h @@ -0,0 +1,17 @@ +#ifndef DEF_TCE_OBJ_USER_LIB_H +#define DEF_TCE_OBJ_USER_LIB_H + +#include +#include + +typedef int Object; + +Object open(char* name); +Object open_relative(char* name, Object parent); +void close(Object object); +int get_methods(char* iface, int* whereto); + +int Call(int method, Object object, size_t a, size_t b, size_t c, size_t d); + +#endif + diff --git a/src/user/lib/std/stdio.c b/src/user/lib/std/stdio.c index 63ad357..c11dd05 100644 --- a/src/user/lib/std/stdio.c +++ b/src/user/lib/std/stdio.c @@ -32,7 +32,6 @@ void printk_int(int number) { } r[order] = 0; printk(s); - free(s); } void printk_hex(unsigned v) { diff --git a/src/user/lib/tce/Folder.c b/src/user/lib/tce/Folder.c new file mode 100644 index 0000000..9b0a773 --- /dev/null +++ b/src/user/lib/tce/Folder.c @@ -0,0 +1,24 @@ +#include +#include + +static struct FolderMethods _folderMethods = { 0 }; + +struct FolderMethods *Folder_getMethods() { + int i; + if (_folderMethods.GetChildNameAt == -1) return 0; + if (_folderMethods.GetChildNameAt == 0) { + i = get_methods("Folder", (int*)&_folderMethods); + if (i < 0) { + _folderMethods.GetChildNameAt = -1; + return 0; + } + } + return &_folderMethods; +} + +int Folder_GetChildNameAt(Object object, int pos, char *to) { + struct FolderMethods *folder = Folder_getMethods(); + if (folder == 0) return E_NOT_IMPLEMENTED; + + return Call(folder->GetChildNameAt, object, pos, to, 0, 0); +} diff --git a/src/user/lib/tce/syscall.c b/src/user/lib/tce/syscall.c index f9243b0..b779bb6 100644 --- a/src/user/lib/tce/syscall.c +++ b/src/user/lib/tce/syscall.c @@ -1,4 +1,5 @@ #include +#include #include #include @@ -8,24 +9,26 @@ static size_t call(size_t a, size_t b, size_t c, size_t d, size_t e, size_t f) { return ret; } +// Standard syscalls + void thread_exit() { - call(0, 0, 0, 0, 0, 0); + call(0, 1, 0, 0, 0, 0); } void schedule() { - call(1, 0, 0,0, 0, 0); + call(0, 2, 0,0, 0, 0); } void thread_sleep(int time) { - call(2, time, 0, 0, 0, 0); + call(0, 3, time, 0, 0, 0); } void process_exit(int retval) { - call(3, retval, 0, 0, 0, 0); + call(0, 4, retval, 0, 0, 0); } void printk(char* str) { - call(4, (unsigned)str, 0, 0, 0, 0); + call(0, 5, (unsigned)str, 0, 0, 0); } //THREAD CREATION @@ -44,28 +47,51 @@ void thread_start(void *data) { if (_stack_to_free != 0) free(_stack_to_free); _stack_to_free = tsd->stack; asm volatile("movl %0, (_stack_freeing_mutex); int $64;" :: - "a"(0), "r"(MUTEX_UNLOCKED)); + "a"(0), "b"(1), "r"(MUTEX_UNLOCKED)); } + void thread_new(void (*entry)(void*), void *data) { struct thread_start_data *tsd = malloc(sizeof(struct thread_start_data)); tsd->entry = entry; tsd->data = data; tsd->stack = malloc(NEW_STACK_SIZE); - call(5, (unsigned)thread_start, (unsigned)tsd, (unsigned)(tsd->stack + NEW_STACK_SIZE), 0, 0); + call(0, 6, (unsigned)thread_start, (unsigned)tsd, (unsigned)(tsd->stack + NEW_STACK_SIZE), 0); } void irq_wait(int number) { - call(6, number, 0, 0, 0, 0); + call(0, 7, number, 0, 0, 0); } int proc_priv() { - return call(7, 0, 0, 0, 0, 0); + return call(0, 8, 0, 0, 0, 0); } void* sbrk(size_t s) { - return (void*)call(8, s, 0, 0, 0, 0); + return (void*)call(0, 9, s, 0, 0, 0); } void brk(void* ptr) { - return call (9, ptr, 0, 0, 0, 0); + return call (0, 10, ptr, 0, 0, 0); +} + +// Has to do with objects + +Object open(char* name) { + return call(0, 20, name, 0, 0, 0); +} + +Object open_relative(char* name, Object parent) { + return call(0, 21, name, parent, 0, 0); +} + +void close(Object o) { + call(0, 22, o, 0, 0, 0); +} + +int get_methods(char *iface, int* whereto) { + call(0, 23, iface, whereto, 0, 0); +} + +int Call(int method, Object object, size_t a, size_t b, size_t c, size_t d) { + return call(object, method, a, b, c, d); } diff --git a/src/user/test/main.c b/src/user/test/main.c index 945c530..f8fec48 100644 --- a/src/user/test/main.c +++ b/src/user/test/main.c @@ -1,9 +1,15 @@ #include #include +#include +#include + +int threads = 0; void thread_cascade(void* d) { int n = (int)d; + threads++; + if (d == 0) { //printk("{#} 0 cascade element started => end\n"); printk("*"); @@ -25,16 +31,71 @@ void thread_cascade(void* d) { //printk("{#} Thread cascade element finished.\n"); printk("."); } + + threads--; +} + +void read_dir(Object o, int l) { + int i = 0, j; + char name[FILENAME_MAX_LEN]; + while (1) { + for (j = 0; j < l; j++) printk(" "); + j = Folder_GetChildNameAt(o, i, name); + if (j <= 0) { + printk(" - reached end : "); + printk_int(j); + printk("\n"); + break; + } + printk(" - '"); + printk(name); + + if (strcmp(name, ".") == 0) { + printk("', skipping -_-'\n"); + } else { + Object oo = open_relative(name, o); + if (oo <= 0) { + printk("', failed to open, error "); + printk_int(oo); + printk("...\n"); + } else { + printk("', opened as "); + printk_int(oo); + printk(", content:\n"); + read_dir(oo, l+1); + } + } + i++; + } } + int main() { - printk("Hi world from test module !\n"); + printk("Test starts. "); - printk(" -> Creating thread cascade (total 2**8 = 256 threads)\n"); - thread_new(thread_cascade, (void*)8); + printk("Create thread cascade (2**5 = 32 threads). "); + thread_new(thread_cascade, (void*)5); - printk(" -> Main thread now sleeping... forever...\n"); + printk("Main thread waits.\n"); while (1) { + thread_sleep(100); + if (threads == 0) break; + } + printk("\nOk, that was fine. Now trying something else...\n"); + Object o = open("/"); + if (o <= 0) { + printk("Cannot open '/', error "); + printk_int(o); + printk("...\n"); + } else { + printk("Reading '/' ("); + printk_int(o); + printk(") :\n"); + read_dir(o, 0); + } + + printk("Ok. Now sleeping, forever and ever."); + while(1) { thread_sleep(1000); } return 0; -- cgit v1.2.3