Rev 1125 | Rev 1129 | 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 | |
| 37 | static LIST_INITIALIZE(ready_list); |
||
| 38 | |||
| 1128 | jermar | 39 | static void psthread_exit(void) __attribute__ ((noinline)); |
| 40 | static void psthread_main(void); |
||
| 1125 | jermar | 41 | |
| 1128 | jermar | 42 | /** Function to preempt to other pseudo thread without adding |
| 43 | * currently running pseudo thread to ready_list. |
||
| 1113 | palkovsky | 44 | */ |
| 1128 | jermar | 45 | void psthread_exit(void) |
| 1113 | palkovsky | 46 | { |
| 47 | psthread_data_t *pt; |
||
| 48 | |||
| 49 | if (list_empty(&ready_list)) { |
||
| 50 | /* Wait on IPC queue etc... */ |
||
| 51 | printf("Cannot exit!!!\n"); |
||
| 52 | _exit(0); |
||
| 53 | } |
||
| 1128 | jermar | 54 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
| 55 | list_remove(&pt->link); |
||
| 1113 | palkovsky | 56 | context_restore(&pt->ctx); |
| 57 | } |
||
| 58 | |||
| 59 | /** Function that is called on entry to new uspace thread */ |
||
| 1128 | jermar | 60 | void psthread_main(void) |
| 1113 | palkovsky | 61 | { |
| 62 | psthread_data_t *pt = __tls_get(); |
||
| 63 | pt->retval = pt->func(pt->arg); |
||
| 64 | |||
| 65 | pt->finished = 1; |
||
| 66 | if (pt->waiter) |
||
| 1128 | jermar | 67 | list_append(&pt->waiter->link, &ready_list); |
| 1113 | palkovsky | 68 | |
| 1128 | jermar | 69 | psthread_exit(); |
| 1113 | palkovsky | 70 | } |
| 71 | |||
| 1128 | jermar | 72 | /** Schedule next userspace pseudo thread. |
| 73 | * |
||
| 74 | * @return 0 if there is no ready pseudo thread, 1 otherwise. |
||
| 75 | */ |
||
| 76 | int psthread_schedule_next(void) |
||
| 1113 | palkovsky | 77 | { |
| 78 | psthread_data_t *pt; |
||
| 79 | |||
| 80 | if (list_empty(&ready_list)) |
||
| 81 | return 0; |
||
| 82 | |||
| 83 | pt = __tls_get(); |
||
| 1128 | jermar | 84 | if (!context_save(&pt->ctx)) |
| 1113 | palkovsky | 85 | return 1; |
| 86 | |||
| 1128 | jermar | 87 | list_append(&pt->link, &ready_list); |
| 88 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
||
| 89 | list_remove(&pt->link); |
||
| 1113 | palkovsky | 90 | |
| 91 | context_restore(&pt->ctx); |
||
| 92 | } |
||
| 93 | |||
| 1128 | jermar | 94 | /** Wait for uspace pseudo thread to finish. |
| 95 | * |
||
| 96 | * @param psthrid Pseudo thread to wait for. |
||
| 97 | * |
||
| 98 | * @return Value returned by the finished thread. |
||
| 99 | */ |
||
| 100 | int psthread_join(pstid_t psthrid) |
||
| 1113 | palkovsky | 101 | { |
| 1125 | jermar | 102 | volatile psthread_data_t *pt, *mypt; |
| 103 | volatile int retval; |
||
| 1113 | palkovsky | 104 | |
| 105 | /* Handle psthrid = Kernel address -> it is wait for call */ |
||
| 106 | |||
| 107 | pt = (psthread_data_t *) psthrid; |
||
| 108 | |||
| 109 | if (!pt->finished) { |
||
| 110 | mypt = __tls_get(); |
||
| 1125 | jermar | 111 | if (context_save(&((psthread_data_t *) mypt)->ctx)) { |
| 112 | pt->waiter = (psthread_data_t *) mypt; |
||
| 1128 | jermar | 113 | psthread_exit(); |
| 1113 | palkovsky | 114 | } |
| 115 | } |
||
| 116 | retval = pt->retval; |
||
| 117 | |||
| 118 | free(pt->stack); |
||
| 1125 | jermar | 119 | __free_tls((psthread_data_t *) pt); |
| 1113 | palkovsky | 120 | |
| 121 | return retval; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 1128 | jermar | 125 | * Create a userspace thread and append it to ready list. |
| 1113 | palkovsky | 126 | * |
| 1128 | jermar | 127 | * @param func Pseudo thread function. |
| 128 | * @param arg Argument to pass to func. |
||
| 129 | * |
||
| 130 | * @return 0 on failure, TLS of the new pseudo thread. |
||
| 1113 | palkovsky | 131 | */ |
| 132 | pstid_t psthread_create(int (*func)(void *), void *arg) |
||
| 133 | { |
||
| 134 | psthread_data_t *pt; |
||
| 135 | |||
| 136 | pt = __make_tls(); |
||
| 137 | pt->stack = (char *) malloc(getpagesize()); |
||
| 138 | |||
| 139 | if (!pt->stack) { |
||
| 140 | return 0; |
||
| 141 | } |
||
| 142 | |||
| 143 | pt->arg= arg; |
||
| 144 | pt->func = func; |
||
| 145 | pt->finished = 0; |
||
| 146 | pt->waiter = NULL; |
||
| 147 | |||
| 148 | context_save(&pt->ctx); |
||
| 1125 | jermar | 149 | context_set(&pt->ctx, FADDR(psthread_main), pt->stack, getpagesize(), pt); |
| 1113 | palkovsky | 150 | |
| 1128 | jermar | 151 | list_append(&pt->link, &ready_list); |
| 1113 | palkovsky | 152 | |
| 153 | return (pstid_t )pt; |
||
| 154 | } |