Rev 1129 | Rev 1392 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1129 | Rev 1155 | ||
---|---|---|---|
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 | ||
- | 38 | #ifndef PSTHREAD_INITIAL_STACK_PAGES_NO |
|
- | 39 | #define PSTHREAD_INITIAL_STACK_PAGES_NO 1 |
|
- | 40 | #endif |
|
- | 41 | ||
37 | static LIST_INITIALIZE(ready_list); |
42 | static LIST_INITIALIZE(ready_list); |
38 | 43 | ||
39 | static void psthread_exit(void) __attribute__ ((noinline)); |
44 | static void psthread_exit(void) __attribute__ ((noinline)); |
40 | static void psthread_main(void); |
45 | static void psthread_main(void); |
41 | 46 | ||
42 | /** Setup PSthread information into TCB structure */ |
47 | /** Setup PSthread information into TCB structure */ |
43 | psthread_data_t * psthread_setup(tcb_t *tcb) |
48 | psthread_data_t * psthread_setup(tcb_t *tcb) |
44 | { |
49 | { |
45 | psthread_data_t *pt; |
50 | psthread_data_t *pt; |
46 | 51 | ||
47 | pt = malloc(sizeof(*pt)); |
52 | pt = malloc(sizeof(*pt)); |
48 | if (!pt) { |
53 | if (!pt) { |
49 | return NULL; |
54 | return NULL; |
50 | } |
55 | } |
51 | 56 | ||
52 | tcb->pst_data = pt; |
57 | tcb->pst_data = pt; |
53 | pt->tcb = tcb; |
58 | pt->tcb = tcb; |
54 | 59 | ||
55 | return pt; |
60 | return pt; |
56 | } |
61 | } |
57 | 62 | ||
58 | void psthread_teardown(psthread_data_t *pt) |
63 | void psthread_teardown(psthread_data_t *pt) |
59 | { |
64 | { |
60 | free(pt); |
65 | free(pt); |
61 | } |
66 | } |
62 | 67 | ||
63 | /** Function to preempt to other pseudo thread without adding |
68 | /** Function to preempt to other pseudo thread without adding |
64 | * currently running pseudo thread to ready_list. |
69 | * currently running pseudo thread to ready_list. |
65 | */ |
70 | */ |
66 | void psthread_exit(void) |
71 | void psthread_exit(void) |
67 | { |
72 | { |
68 | psthread_data_t *pt; |
73 | psthread_data_t *pt; |
69 | 74 | ||
70 | if (list_empty(&ready_list)) { |
75 | if (list_empty(&ready_list)) { |
71 | /* Wait on IPC queue etc... */ |
76 | /* Wait on IPC queue etc... */ |
72 | printf("Cannot exit!!!\n"); |
77 | printf("Cannot exit!!!\n"); |
73 | _exit(0); |
78 | _exit(0); |
74 | } |
79 | } |
75 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
80 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
76 | list_remove(&pt->link); |
81 | list_remove(&pt->link); |
77 | context_restore(&pt->ctx); |
82 | context_restore(&pt->ctx); |
78 | } |
83 | } |
79 | 84 | ||
80 | /** Function that is called on entry to new uspace thread */ |
85 | /** Function that is called on entry to new uspace thread */ |
81 | void psthread_main(void) |
86 | void psthread_main(void) |
82 | { |
87 | { |
83 | psthread_data_t *pt = __tcb_get()->pst_data; |
88 | psthread_data_t *pt = __tcb_get()->pst_data; |
84 | 89 | ||
85 | pt->retval = pt->func(pt->arg); |
90 | pt->retval = pt->func(pt->arg); |
86 | 91 | ||
87 | pt->finished = 1; |
92 | pt->finished = 1; |
88 | if (pt->waiter) |
93 | if (pt->waiter) |
89 | list_append(&pt->waiter->link, &ready_list); |
94 | list_append(&pt->waiter->link, &ready_list); |
90 | 95 | ||
91 | psthread_exit(); |
96 | psthread_exit(); |
92 | } |
97 | } |
93 | 98 | ||
94 | /** Schedule next userspace pseudo thread. |
99 | /** Schedule next userspace pseudo thread. |
95 | * |
100 | * |
96 | * @return 0 if there is no ready pseudo thread, 1 otherwise. |
101 | * @return 0 if there is no ready pseudo thread, 1 otherwise. |
97 | */ |
102 | */ |
98 | int psthread_schedule_next(void) |
103 | int psthread_schedule_next(void) |
99 | { |
104 | { |
100 | psthread_data_t *pt; |
105 | psthread_data_t *pt; |
101 | 106 | ||
102 | if (list_empty(&ready_list)) |
107 | if (list_empty(&ready_list)) |
103 | return 0; |
108 | return 0; |
104 | 109 | ||
105 | pt = __tcb_get()->pst_data; |
110 | pt = __tcb_get()->pst_data; |
106 | if (!context_save(&pt->ctx)) |
111 | if (!context_save(&pt->ctx)) |
107 | return 1; |
112 | return 1; |
108 | 113 | ||
109 | list_append(&pt->link, &ready_list); |
114 | list_append(&pt->link, &ready_list); |
110 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
115 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
111 | list_remove(&pt->link); |
116 | list_remove(&pt->link); |
112 | 117 | ||
113 | context_restore(&pt->ctx); |
118 | context_restore(&pt->ctx); |
114 | } |
119 | } |
115 | 120 | ||
116 | /** Wait for uspace pseudo thread to finish. |
121 | /** Wait for uspace pseudo thread to finish. |
117 | * |
122 | * |
118 | * @param psthrid Pseudo thread to wait for. |
123 | * @param psthrid Pseudo thread to wait for. |
119 | * |
124 | * |
120 | * @return Value returned by the finished thread. |
125 | * @return Value returned by the finished thread. |
121 | */ |
126 | */ |
122 | int psthread_join(pstid_t psthrid) |
127 | int psthread_join(pstid_t psthrid) |
123 | { |
128 | { |
124 | volatile psthread_data_t *pt, *mypt; |
129 | volatile psthread_data_t *pt, *mypt; |
125 | volatile int retval; |
130 | volatile int retval; |
126 | 131 | ||
127 | /* Handle psthrid = Kernel address -> it is wait for call */ |
132 | /* Handle psthrid = Kernel address -> it is wait for call */ |
128 | pt = (psthread_data_t *) psthrid; |
133 | pt = (psthread_data_t *) psthrid; |
129 | 134 | ||
130 | if (!pt->finished) { |
135 | if (!pt->finished) { |
131 | mypt = __tcb_get()->pst_data; |
136 | mypt = __tcb_get()->pst_data; |
132 | if (context_save(&((psthread_data_t *) mypt)->ctx)) { |
137 | if (context_save(&((psthread_data_t *) mypt)->ctx)) { |
133 | pt->waiter = (psthread_data_t *) mypt; |
138 | pt->waiter = (psthread_data_t *) mypt; |
134 | psthread_exit(); |
139 | psthread_exit(); |
135 | } |
140 | } |
136 | } |
141 | } |
137 | retval = pt->retval; |
142 | retval = pt->retval; |
138 | 143 | ||
139 | free(pt->stack); |
144 | free(pt->stack); |
140 | __free_tls(pt->tcb); |
145 | __free_tls(pt->tcb); |
141 | psthread_teardown((void *)pt); |
146 | psthread_teardown((void *)pt); |
142 | 147 | ||
143 | return retval; |
148 | return retval; |
144 | } |
149 | } |
145 | 150 | ||
146 | /** |
151 | /** |
147 | * Create a userspace thread and append it to ready list. |
152 | * Create a userspace thread and append it to ready list. |
148 | * |
153 | * |
149 | * @param func Pseudo thread function. |
154 | * @param func Pseudo thread function. |
150 | * @param arg Argument to pass to func. |
155 | * @param arg Argument to pass to func. |
151 | * |
156 | * |
152 | * @return 0 on failure, TLS of the new pseudo thread. |
157 | * @return 0 on failure, TLS of the new pseudo thread. |
153 | */ |
158 | */ |
154 | pstid_t psthread_create(int (*func)(void *), void *arg) |
159 | pstid_t psthread_create(int (*func)(void *), void *arg) |
155 | { |
160 | { |
156 | psthread_data_t *pt; |
161 | psthread_data_t *pt; |
157 | tcb_t *tcb; |
162 | tcb_t *tcb; |
158 | 163 | ||
159 | tcb = __make_tls(); |
164 | tcb = __make_tls(); |
160 | if (!tcb) |
165 | if (!tcb) |
161 | return 0; |
166 | return 0; |
162 | 167 | ||
163 | pt = psthread_setup(tcb); |
168 | pt = psthread_setup(tcb); |
164 | if (!pt) { |
169 | if (!pt) { |
165 | __free_tls(tcb); |
170 | __free_tls(tcb); |
166 | return 0; |
171 | return 0; |
167 | } |
172 | } |
168 | pt->stack = (char *) malloc(getpagesize()); |
173 | pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize()); |
169 | 174 | ||
170 | if (!pt->stack) { |
175 | if (!pt->stack) { |
171 | __free_tls(tcb); |
176 | __free_tls(tcb); |
172 | psthread_teardown(pt); |
177 | psthread_teardown(pt); |
173 | return 0; |
178 | return 0; |
174 | } |
179 | } |
175 | 180 | ||
176 | pt->arg= arg; |
181 | pt->arg= arg; |
177 | pt->func = func; |
182 | pt->func = func; |
178 | pt->finished = 0; |
183 | pt->finished = 0; |
179 | pt->waiter = NULL; |
184 | pt->waiter = NULL; |
180 | 185 | ||
181 | context_save(&pt->ctx); |
186 | context_save(&pt->ctx); |
182 | context_set(&pt->ctx, FADDR(psthread_main), pt->stack, getpagesize(), |
187 | context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(), |
183 | tcb); |
188 | tcb); |
184 | 189 | ||
185 | list_append(&pt->link, &ready_list); |
190 | list_append(&pt->link, &ready_list); |
186 | 191 | ||
187 | return (pstid_t )pt; |
192 | return (pstid_t )pt; |
188 | } |
193 | } |
189 | 194 |