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