Rev 1079 | Rev 1113 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 1079 | Rev 1081 | ||
|---|---|---|---|
| Line 30... | Line 30... | ||
| 30 | #include <libc.h> |
30 | #include <libc.h> |
| 31 | #include <stdlib.h> |
31 | #include <stdlib.h> |
| 32 | #include <arch/faddr.h> |
32 | #include <arch/faddr.h> |
| 33 | #include <kernel/proc/uarg.h> |
33 | #include <kernel/proc/uarg.h> |
| 34 | 34 | ||
| - | 35 | /** Main thread function. |
|
| - | 36 | * |
|
| - | 37 | * This function is called from __thread_entry() and is used |
|
| - | 38 | * to call the thread's implementing function and perform cleanup |
|
| - | 39 | * and exit when thread returns back. Do not call this function |
|
| - | 40 | * directly. |
|
| - | 41 | * |
|
| - | 42 | * @param uarg Pointer to userspace argument structure. |
|
| - | 43 | */ |
|
| 35 | void thread_main(uspace_arg_t *uarg) |
44 | void thread_main(uspace_arg_t *uarg) |
| 36 | { |
45 | { |
| 37 | uarg->uspace_thread_function(uarg->uspace_thread_arg); |
46 | uarg->uspace_thread_function(uarg->uspace_thread_arg); |
| 38 | free(uarg->uspace_stack); |
47 | free(uarg->uspace_stack); |
| 39 | free(uarg); |
48 | free(uarg); |
| 40 | thread_exit(0); |
49 | thread_exit(0); |
| 41 | } |
50 | } |
| 42 | 51 | ||
| - | 52 | /** Create userspace thread. |
|
| - | 53 | * |
|
| - | 54 | * This function creates new userspace thread and allocates userspace |
|
| - | 55 | * stack and userspace argument structure for it. |
|
| - | 56 | * |
|
| - | 57 | * @param function Function implementing the thread. |
|
| - | 58 | * @param arg Argument to be passed to thread. |
|
| - | 59 | * @param name Symbolic name of the thread. |
|
| - | 60 | * |
|
| - | 61 | * @param TID of the new thread on success or -1 on failure. |
|
| - | 62 | */ |
|
| 43 | int thread_create(void (* function)(void *), void *arg, char *name) |
63 | int thread_create(void (* function)(void *), void *arg, char *name) |
| 44 | { |
64 | { |
| 45 | char *stack; |
65 | char *stack; |
| 46 | uspace_arg_t *uarg; |
66 | uspace_arg_t *uarg; |
| 47 | 67 | ||
| Line 52... | Line 72... | ||
| 52 | uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t)); |
72 | uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t)); |
| 53 | if (!uarg) { |
73 | if (!uarg) { |
| 54 | free(stack); |
74 | free(stack); |
| 55 | return -1; |
75 | return -1; |
| 56 | } |
76 | } |
| - | 77 | ||
| 57 | uarg->uspace_entry = (void *) FADDR(__thread_entry); |
78 | uarg->uspace_entry = (void *) FADDR(__thread_entry); |
| 58 | uarg->uspace_stack = (void *) stack; |
79 | uarg->uspace_stack = (void *) stack; |
| 59 | uarg->uspace_thread_function = (void *) FADDR(function); |
80 | uarg->uspace_thread_function = function; |
| 60 | uarg->uspace_thread_arg = arg; |
81 | uarg->uspace_thread_arg = arg; |
| 61 | uarg->uspace_uarg = uarg; |
82 | uarg->uspace_uarg = uarg; |
| 62 | 83 | ||
| 63 | return __SYSCALL2(SYS_THREAD_CREATE, (sysarg_t) uarg, (sysarg_t) name); |
84 | return __SYSCALL2(SYS_THREAD_CREATE, (sysarg_t) uarg, (sysarg_t) name); |
| 64 | } |
85 | } |
| 65 | 86 | ||
| - | 87 | /** Terminate current thread. |
|
| - | 88 | * |
|
| - | 89 | * @param stat Exit status. Currently not used. |
|
| - | 90 | */ |
|
| 66 | void thread_exit(int status) |
91 | void thread_exit(int status) |
| 67 | { |
92 | { |
| 68 | __SYSCALL1(SYS_THREAD_EXIT, (sysarg_t) status); |
93 | __SYSCALL1(SYS_THREAD_EXIT, (sysarg_t) status); |
| 69 | } |
94 | } |