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