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