Rev 955 | Rev 965 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 955 | Rev 959 | ||
---|---|---|---|
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 | /* Lock ordering |
29 | /* Lock ordering |
30 | * |
30 | * |
31 | * First the answerbox, then the phone |
31 | * First the answerbox, then the phone |
32 | */ |
32 | */ |
33 | 33 | ||
34 | #include <synch/waitq.h> |
34 | #include <synch/waitq.h> |
35 | #include <ipc/ipc.h> |
35 | #include <ipc/ipc.h> |
36 | #include <errno.h> |
36 | #include <errno.h> |
37 | #include <mm/slab.h> |
37 | #include <mm/slab.h> |
38 | #include <arch.h> |
38 | #include <arch.h> |
39 | #include <proc/task.h> |
39 | #include <proc/task.h> |
40 | #include <memstr.h> |
40 | #include <memstr.h> |
41 | #include <debug.h> |
41 | #include <debug.h> |
42 | 42 | ||
43 | #include <print.h> |
43 | #include <print.h> |
44 | #include <proc/thread.h> |
44 | #include <proc/thread.h> |
45 | 45 | ||
46 | answerbox_t *ipc_central_box; |
46 | answerbox_t *ipc_central_box; |
47 | 47 | ||
48 | static slab_cache_t *ipc_call_slab; |
48 | static slab_cache_t *ipc_call_slab; |
49 | 49 | ||
50 | /** Allocate & initialize call structure |
50 | /** Allocate & initialize call structure |
51 | * |
51 | * |
52 | * The call is initialized, so that the reply will be directed |
52 | * The call is initialized, so that the reply will be directed |
53 | * to TASK->answerbox |
53 | * to TASK->answerbox |
54 | */ |
54 | */ |
55 | call_t * ipc_call_alloc(void) |
55 | call_t * ipc_call_alloc(void) |
56 | { |
56 | { |
57 | call_t *call; |
57 | call_t *call; |
58 | 58 | ||
59 | call = slab_alloc(ipc_call_slab, 0); |
59 | call = slab_alloc(ipc_call_slab, 0); |
60 | memsetb((__address)call, sizeof(*call), 0); |
60 | memsetb((__address)call, sizeof(*call), 0); |
61 | call->callerbox = &TASK->answerbox; |
61 | call->callerbox = &TASK->answerbox; |
62 | 62 | ||
63 | return call; |
63 | return call; |
64 | } |
64 | } |
65 | 65 | ||
66 | /** Deallocate call stracuture */ |
66 | /** Deallocate call stracuture */ |
67 | void ipc_call_free(call_t *call) |
67 | void ipc_call_free(call_t *call) |
68 | { |
68 | { |
69 | slab_free(ipc_call_slab, call); |
69 | slab_free(ipc_call_slab, call); |
70 | } |
70 | } |
71 | 71 | ||
72 | /** Initialize answerbox structure |
72 | /** Initialize answerbox structure |
73 | */ |
73 | */ |
74 | void ipc_answerbox_init(answerbox_t *box) |
74 | void ipc_answerbox_init(answerbox_t *box) |
75 | { |
75 | { |
76 | spinlock_initialize(&box->lock, "abox_lock"); |
76 | spinlock_initialize(&box->lock, "abox_lock"); |
77 | waitq_initialize(&box->wq); |
77 | waitq_initialize(&box->wq); |
78 | list_initialize(&box->connected_phones); |
78 | list_initialize(&box->connected_phones); |
79 | list_initialize(&box->calls); |
79 | list_initialize(&box->calls); |
80 | list_initialize(&box->dispatched_calls); |
80 | list_initialize(&box->dispatched_calls); |
81 | list_initialize(&box->answers); |
81 | list_initialize(&box->answers); |
82 | } |
82 | } |
83 | 83 | ||
84 | /** Initialize phone structure and connect phone to naswerbox |
84 | /** Initialize phone structure and connect phone to naswerbox |
85 | */ |
85 | */ |
86 | void ipc_phone_init(phone_t *phone, answerbox_t *box) |
86 | void ipc_phone_init(phone_t *phone, answerbox_t *box) |
87 | { |
87 | { |
88 | spinlock_initialize(&phone->lock, "phone_lock"); |
88 | spinlock_initialize(&phone->lock, "phone_lock"); |
89 | 89 | ||
90 | phone->callee = box; |
90 | phone->callee = box; |
91 | spinlock_lock(&box->lock); |
91 | spinlock_lock(&box->lock); |
92 | list_append(&phone->list, &box->connected_phones); |
92 | list_append(&phone->list, &box->connected_phones); |
93 | spinlock_unlock(&box->lock); |
93 | spinlock_unlock(&box->lock); |
94 | } |
94 | } |
95 | 95 | ||
96 | /** Disconnect phone from answerbox */ |
96 | /** Disconnect phone from answerbox */ |
97 | void ipc_phone_destroy(phone_t *phone) |
97 | void ipc_phone_destroy(phone_t *phone) |
98 | { |
98 | { |
99 | answerbox_t *box = phone->callee; |
99 | answerbox_t *box = phone->callee; |
100 | 100 | ||
101 | ASSERT(box); |
101 | ASSERT(box); |
102 | 102 | ||
103 | spinlock_lock(&box->lock); |
103 | spinlock_lock(&box->lock); |
104 | list_remove(&phone->list); |
104 | list_remove(&phone->list); |
105 | spinlock_unlock(&box->lock); |
105 | spinlock_unlock(&box->lock); |
106 | } |
106 | } |
107 | 107 | ||
- | 108 | /** Helper function to facilitate synchronous calls */ |
|
- | 109 | void ipc_call_sync(phone_t *phone, call_t *request) |
|
- | 110 | { |
|
- | 111 | answerbox_t sync_box; |
|
- | 112 | ||
- | 113 | ipc_answerbox_init(&sync_box); |
|
- | 114 | ||
- | 115 | /* We will receive data on special box */ |
|
- | 116 | request->callerbox = &sync_box; |
|
- | 117 | ||
- | 118 | ipc_call(phone, request); |
|
- | 119 | ipc_wait_for_call(&sync_box, 0); |
|
- | 120 | } |
|
108 | 121 | ||
109 | /** Send a request using phone to answerbox |
122 | /** Send a asynchronous request using phone to answerbox |
110 | * |
123 | * |
111 | * @param phone Phone connected to answerbox |
124 | * @param phone Phone connected to answerbox |
112 | * @param request Request to be sent |
125 | * @param request Request to be sent |
113 | */ |
126 | */ |
114 | void ipc_call(phone_t *phone, call_t *request) |
127 | void ipc_call(phone_t *phone, call_t *request) |
115 | { |
128 | { |
116 | answerbox_t *box = phone->callee; |
129 | answerbox_t *box = phone->callee; |
117 | 130 | ||
118 | ASSERT(box); |
131 | ASSERT(box); |
119 | 132 | ||
120 | spinlock_lock(&box->lock); |
133 | spinlock_lock(&box->lock); |
121 | list_append(&request->list, &box->calls); |
134 | list_append(&request->list, &box->calls); |
122 | spinlock_unlock(&box->lock); |
135 | spinlock_unlock(&box->lock); |
123 | waitq_wakeup(&box->wq, 0); |
136 | waitq_wakeup(&box->wq, 0); |
124 | } |
137 | } |
125 | 138 | ||
126 | /** Answer message back to phone |
139 | /** Answer message back to phone |
127 | * |
140 | * |
128 | * @param box Answerbox that is answering the message |
141 | * @param box Answerbox that is answering the message |
129 | * @param request Modified request that is being sent back |
142 | * @param request Modified request that is being sent back |
130 | */ |
143 | */ |
131 | void ipc_answer(answerbox_t *box, call_t *request) |
144 | void ipc_answer(answerbox_t *box, call_t *request) |
132 | { |
145 | { |
133 | answerbox_t *callerbox = request->callerbox; |
146 | answerbox_t *callerbox = request->callerbox; |
134 | 147 | ||
135 | request->flags |= IPC_CALL_ANSWERED; |
148 | request->flags |= IPC_CALL_ANSWERED; |
136 | 149 | ||
137 | spinlock_lock(&box->lock); |
150 | spinlock_lock(&box->lock); |
138 | spinlock_lock(&callerbox->lock); |
151 | spinlock_lock(&callerbox->lock); |
139 | 152 | ||
140 | list_remove(&request->list); |
153 | list_remove(&request->list); |
141 | list_append(&request->list, &callerbox->answers); |
154 | list_append(&request->list, &callerbox->answers); |
142 | waitq_wakeup(&callerbox->wq, 0); |
155 | waitq_wakeup(&callerbox->wq, 0); |
143 | 156 | ||
144 | spinlock_unlock(&callerbox->lock); |
157 | spinlock_unlock(&callerbox->lock); |
145 | spinlock_unlock(&box->lock); |
158 | spinlock_unlock(&box->lock); |
146 | } |
159 | } |
147 | 160 | ||
148 | /** Wait for phone call |
161 | /** Wait for phone call |
149 | * |
162 | * |
150 | * @return Recived message address |
163 | * @return Recived message address |
151 | * - to distinguish between call and answer, look at call->flags |
164 | * - to distinguish between call and answer, look at call->flags |
152 | */ |
165 | */ |
153 | call_t * ipc_wait_for_call(answerbox_t *box, int flags) |
166 | call_t * ipc_wait_for_call(answerbox_t *box, int flags) |
154 | { |
167 | { |
155 | call_t *request; |
168 | call_t *request; |
156 | 169 | ||
157 | if ((flags & IPC_WAIT_NONBLOCKING)) { |
170 | if ((flags & IPC_WAIT_NONBLOCKING)) { |
158 | if (waitq_sleep_timeout(&box->wq,SYNCH_NO_TIMEOUT,SYNCH_NON_BLOCKING) == ESYNCH_WOULD_BLOCK) |
171 | if (waitq_sleep_timeout(&box->wq,SYNCH_NO_TIMEOUT,SYNCH_NON_BLOCKING) == ESYNCH_WOULD_BLOCK) |
159 | return 0; |
172 | return 0; |
160 | } else { |
173 | } else { |
161 | waitq_sleep(&box->wq); |
174 | waitq_sleep(&box->wq); |
162 | } |
175 | } |
163 | 176 | ||
164 | 177 | ||
165 | // TODO - might need condition variable+mutex if we want to support |
178 | // TODO - might need condition variable+mutex if we want to support |
166 | // removing of requests from queue before dispatch |
179 | // removing of requests from queue before dispatch |
167 | spinlock_lock(&box->lock); |
180 | spinlock_lock(&box->lock); |
168 | /* Handle answers first */ |
181 | /* Handle answers first */ |
169 | if (!list_empty(&box->answers)) { |
182 | if (!list_empty(&box->answers)) { |
170 | request = list_get_instance(box->answers.next, call_t, list); |
183 | request = list_get_instance(box->answers.next, call_t, list); |
171 | list_remove(&request->list); |
184 | list_remove(&request->list); |
172 | } else { |
185 | } else { |
173 | ASSERT (! list_empty(&box->calls)); |
186 | ASSERT (! list_empty(&box->calls)); |
174 | request = list_get_instance(box->calls.next, call_t, list); |
187 | request = list_get_instance(box->calls.next, call_t, list); |
175 | list_remove(&request->list); |
188 | list_remove(&request->list); |
176 | /* Append request to dispatch queue */ |
189 | /* Append request to dispatch queue */ |
177 | list_append(&request->list, &box->dispatched_calls); |
190 | list_append(&request->list, &box->dispatched_calls); |
178 | } |
191 | } |
179 | spinlock_unlock(&box->lock); |
192 | spinlock_unlock(&box->lock); |
180 | 193 | ||
181 | return request; |
194 | return request; |
182 | } |
195 | } |
183 | 196 | ||
184 | /** Initilize ipc subsystem */ |
197 | /** Initilize ipc subsystem */ |
185 | void ipc_init(void) |
198 | void ipc_init(void) |
186 | { |
199 | { |
187 | ipc_call_slab = slab_cache_create("ipc_call", |
200 | ipc_call_slab = slab_cache_create("ipc_call", |
188 | sizeof(call_t), |
201 | sizeof(call_t), |
189 | 0, |
202 | 0, |
190 | NULL, NULL, 0); |
203 | NULL, NULL, 0); |
191 | } |
204 | } |
192 | 205 | ||
193 | static void ipc_phonecompany_thread(void *data) |
206 | static void ipc_phonecompany_thread(void *data) |
194 | { |
207 | { |
195 | call_t *call; |
208 | call_t *call; |
196 | 209 | ||
197 | printf("Phone company started.\n"); |
210 | printf("Phone company started.\n"); |
198 | while (1) { |
211 | while (1) { |
199 | call = ipc_wait_for_call(&TASK->answerbox, 0); |
212 | call = ipc_wait_for_call(&TASK->answerbox, 0); |
200 | printf("Received phone call - %P %P\n", |
213 | printf("Received phone call - %P %P\n", |
201 | call->data[0], call->data[1]); |
214 | call->data[0], call->data[1]); |
- | 215 | call->data[0] = 0xbabaaaee;; |
|
- | 216 | call->data[1] = 0xaaaaeeee; |
|
202 | ipc_answer(&TASK->answerbox, call); |
217 | ipc_answer(&TASK->answerbox, call); |
203 | printf("Call answered.\n"); |
218 | printf("Call answered.\n"); |
204 | } |
219 | } |
205 | } |
220 | } |
206 | 221 | ||
207 | void ipc_create_phonecompany(void) |
222 | void ipc_create_phonecompany(void) |
208 | { |
223 | { |
209 | thread_t *t; |
224 | thread_t *t; |
210 | 225 | ||
211 | if ((t = thread_create(ipc_phonecompany_thread, "phonecompany", |
226 | if ((t = thread_create(ipc_phonecompany_thread, "phonecompany", |
212 | TASK, 0))) |
227 | TASK, 0))) |
213 | thread_ready(t); |
228 | thread_ready(t); |
214 | else |
229 | else |
215 | panic("thread_create/phonecompany"); |
230 | panic("thread_create/phonecompany"); |
216 | 231 | ||
217 | ipc_central_box = &TASK->answerbox; |
232 | ipc_central_box = &TASK->answerbox; |
218 | } |
233 | } |
219 | 234 |