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