Rev 1129 | Rev 1392 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1113 | palkovsky | 1 | /* |
| 2 | * Copyright (C) 2006 Ondrej Palkovsky |
||
| 3 | * All rights reserved. |
||
| 4 | * |
||
| 5 | * Redistribution and use in source and binary forms, with or without |
||
| 6 | * modification, are permitted provided that the following conditions |
||
| 7 | * are met: |
||
| 8 | * |
||
| 9 | * - Redistributions of source code must retain the above copyright |
||
| 10 | * notice, this list of conditions and the following disclaimer. |
||
| 11 | * - Redistributions in binary form must reproduce the above copyright |
||
| 12 | * notice, this list of conditions and the following disclaimer in the |
||
| 13 | * documentation and/or other materials provided with the distribution. |
||
| 14 | * - The name of the author may not be used to endorse or promote products |
||
| 15 | * derived from this software without specific prior written permission. |
||
| 16 | * |
||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
| 27 | */ |
||
| 28 | |||
| 29 | #include <libadt/list.h> |
||
| 30 | #include <psthread.h> |
||
| 31 | #include <malloc.h> |
||
| 32 | #include <unistd.h> |
||
| 33 | #include <thread.h> |
||
| 34 | #include <stdio.h> |
||
| 1125 | jermar | 35 | #include <kernel/arch/faddr.h> |
| 1113 | palkovsky | 36 | |
| 1155 | vana | 37 | |
| 38 | #ifndef PSTHREAD_INITIAL_STACK_PAGES_NO |
||
| 39 | #define PSTHREAD_INITIAL_STACK_PAGES_NO 1 |
||
| 40 | #endif |
||
| 41 | |||
| 1113 | palkovsky | 42 | static LIST_INITIALIZE(ready_list); |
| 43 | |||
| 1128 | jermar | 44 | static void psthread_exit(void) __attribute__ ((noinline)); |
| 45 | static void psthread_main(void); |
||
| 1125 | jermar | 46 | |
| 1129 | palkovsky | 47 | /** Setup PSthread information into TCB structure */ |
| 48 | psthread_data_t * psthread_setup(tcb_t *tcb) |
||
| 49 | { |
||
| 50 | psthread_data_t *pt; |
||
| 51 | |||
| 52 | pt = malloc(sizeof(*pt)); |
||
| 53 | if (!pt) { |
||
| 54 | return NULL; |
||
| 55 | } |
||
| 56 | |||
| 57 | tcb->pst_data = pt; |
||
| 58 | pt->tcb = tcb; |
||
| 59 | |||
| 60 | return pt; |
||
| 61 | } |
||
| 62 | |||
| 63 | void psthread_teardown(psthread_data_t *pt) |
||
| 64 | { |
||
| 65 | free(pt); |
||
| 66 | } |
||
| 67 | |||
| 1128 | jermar | 68 | /** Function to preempt to other pseudo thread without adding |
| 69 | * currently running pseudo thread to ready_list. |
||
| 1113 | palkovsky | 70 | */ |
| 1128 | jermar | 71 | void psthread_exit(void) |
| 1113 | palkovsky | 72 | { |
| 73 | psthread_data_t *pt; |
||
| 74 | |||
| 75 | if (list_empty(&ready_list)) { |
||
| 76 | /* Wait on IPC queue etc... */ |
||
| 77 | printf("Cannot exit!!!\n"); |
||
| 78 | _exit(0); |
||
| 79 | } |
||
| 1128 | jermar | 80 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
| 81 | list_remove(&pt->link); |
||
| 1113 | palkovsky | 82 | context_restore(&pt->ctx); |
| 83 | } |
||
| 84 | |||
| 85 | /** Function that is called on entry to new uspace thread */ |
||
| 1128 | jermar | 86 | void psthread_main(void) |
| 1113 | palkovsky | 87 | { |
| 1129 | palkovsky | 88 | psthread_data_t *pt = __tcb_get()->pst_data; |
| 89 | |||
| 1113 | palkovsky | 90 | pt->retval = pt->func(pt->arg); |
| 91 | |||
| 92 | pt->finished = 1; |
||
| 93 | if (pt->waiter) |
||
| 1128 | jermar | 94 | list_append(&pt->waiter->link, &ready_list); |
| 1113 | palkovsky | 95 | |
| 1128 | jermar | 96 | psthread_exit(); |
| 1113 | palkovsky | 97 | } |
| 98 | |||
| 1128 | jermar | 99 | /** Schedule next userspace pseudo thread. |
| 100 | * |
||
| 101 | * @return 0 if there is no ready pseudo thread, 1 otherwise. |
||
| 102 | */ |
||
| 103 | int psthread_schedule_next(void) |
||
| 1113 | palkovsky | 104 | { |
| 105 | psthread_data_t *pt; |
||
| 106 | |||
| 107 | if (list_empty(&ready_list)) |
||
| 108 | return 0; |
||
| 109 | |||
| 1129 | palkovsky | 110 | pt = __tcb_get()->pst_data; |
| 1128 | jermar | 111 | if (!context_save(&pt->ctx)) |
| 1113 | palkovsky | 112 | return 1; |
| 113 | |||
| 1128 | jermar | 114 | list_append(&pt->link, &ready_list); |
| 115 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
||
| 116 | list_remove(&pt->link); |
||
| 1113 | palkovsky | 117 | |
| 118 | context_restore(&pt->ctx); |
||
| 119 | } |
||
| 120 | |||
| 1128 | jermar | 121 | /** Wait for uspace pseudo thread to finish. |
| 122 | * |
||
| 123 | * @param psthrid Pseudo thread to wait for. |
||
| 124 | * |
||
| 125 | * @return Value returned by the finished thread. |
||
| 126 | */ |
||
| 127 | int psthread_join(pstid_t psthrid) |
||
| 1113 | palkovsky | 128 | { |
| 1125 | jermar | 129 | volatile psthread_data_t *pt, *mypt; |
| 130 | volatile int retval; |
||
| 1113 | palkovsky | 131 | |
| 132 | /* Handle psthrid = Kernel address -> it is wait for call */ |
||
| 133 | pt = (psthread_data_t *) psthrid; |
||
| 134 | |||
| 135 | if (!pt->finished) { |
||
| 1129 | palkovsky | 136 | mypt = __tcb_get()->pst_data; |
| 1125 | jermar | 137 | if (context_save(&((psthread_data_t *) mypt)->ctx)) { |
| 138 | pt->waiter = (psthread_data_t *) mypt; |
||
| 1128 | jermar | 139 | psthread_exit(); |
| 1113 | palkovsky | 140 | } |
| 141 | } |
||
| 142 | retval = pt->retval; |
||
| 143 | |||
| 144 | free(pt->stack); |
||
| 1129 | palkovsky | 145 | __free_tls(pt->tcb); |
| 146 | psthread_teardown((void *)pt); |
||
| 1113 | palkovsky | 147 | |
| 148 | return retval; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 1128 | jermar | 152 | * Create a userspace thread and append it to ready list. |
| 1113 | palkovsky | 153 | * |
| 1128 | jermar | 154 | * @param func Pseudo thread function. |
| 155 | * @param arg Argument to pass to func. |
||
| 156 | * |
||
| 157 | * @return 0 on failure, TLS of the new pseudo thread. |
||
| 1113 | palkovsky | 158 | */ |
| 159 | pstid_t psthread_create(int (*func)(void *), void *arg) |
||
| 160 | { |
||
| 161 | psthread_data_t *pt; |
||
| 1129 | palkovsky | 162 | tcb_t *tcb; |
| 1113 | palkovsky | 163 | |
| 1129 | palkovsky | 164 | tcb = __make_tls(); |
| 165 | if (!tcb) |
||
| 166 | return 0; |
||
| 167 | |||
| 168 | pt = psthread_setup(tcb); |
||
| 169 | if (!pt) { |
||
| 170 | __free_tls(tcb); |
||
| 171 | return 0; |
||
| 172 | } |
||
| 1155 | vana | 173 | pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize()); |
| 1113 | palkovsky | 174 | |
| 175 | if (!pt->stack) { |
||
| 1129 | palkovsky | 176 | __free_tls(tcb); |
| 177 | psthread_teardown(pt); |
||
| 1113 | palkovsky | 178 | return 0; |
| 179 | } |
||
| 180 | |||
| 181 | pt->arg= arg; |
||
| 182 | pt->func = func; |
||
| 183 | pt->finished = 0; |
||
| 184 | pt->waiter = NULL; |
||
| 185 | |||
| 186 | context_save(&pt->ctx); |
||
| 1155 | vana | 187 | context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(), |
| 1129 | palkovsky | 188 | tcb); |
| 1113 | palkovsky | 189 | |
| 1128 | jermar | 190 | list_append(&pt->link, &ready_list); |
| 1113 | palkovsky | 191 | |
| 192 | return (pstid_t )pt; |
||
| 193 | } |