Changeset e74cb73 in mainline for generic/src/ipc/ipc.c
- Timestamp:
- 2006-03-14T09:30:07Z (19 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- d1f8a87
- Parents:
- 27810c5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
generic/src/ipc/ipc.c
r27810c5 re74cb73 32 32 */ 33 33 34 #include <synch/waitq.h> 34 #include <synch/condvar.h> 35 #include <synch/mutex.h> 35 36 #include <ipc/ipc.h> 36 37 #include <errno.h> … … 44 45 #include <proc/thread.h> 45 46 46 answerbox_t *ipc_central_box; 47 /* Open channel that is assigned automatically to new tasks */ 48 answerbox_t *ipc_phone_0 = NULL; 47 49 48 50 static slab_cache_t *ipc_call_slab; … … 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 75 /** Deallocate call stracuture */ 67 76 void ipc_call_free(call_t *call) … … 74 83 void ipc_answerbox_init(answerbox_t *box) 75 84 { 76 spinlock_initialize(&box->lock, "abox_lock");77 waitq_initialize(&box->wq);85 mutex_initialize(&box->mutex); 86 condvar_initialize(&box->cv); 78 87 list_initialize(&box->connected_phones); 79 88 list_initialize(&box->calls); … … 89 98 90 99 phone->callee = box; 91 spinlock_lock(&box->lock); 100 101 mutex_lock(&box->mutex); 92 102 list_append(&phone->list, &box->connected_phones); 93 spinlock_unlock(&box->lock);103 mutex_unlock(&box->mutex); 94 104 } 95 105 … … 101 111 ASSERT(box); 102 112 103 spinlock_lock(&box->lock);113 mutex_lock(&box->mutex); 104 114 list_remove(&phone->list); 105 spinlock_unlock(&box->lock);115 mutex_unlock(&box->mutex); 106 116 } 107 117 … … 131 141 ASSERT(box); 132 142 133 spinlock_lock(&box->lock);143 mutex_lock(&box->mutex); 134 144 list_append(&request->list, &box->calls); 135 spinlock_unlock(&box->lock);136 waitq_wakeup(&box->wq, 0);145 mutex_unlock(&box->mutex); 146 condvar_signal(&box->cv); 137 147 } 138 148 … … 148 158 request->flags |= IPC_CALL_ANSWERED; 149 159 150 spinlock_lock(&box->lock); 151 spinlock_lock(&callerbox->lock); 152 160 mutex_lock(&box->mutex); 153 161 list_remove(&request->list); 162 mutex_unlock(&box->mutex); 163 164 mutex_lock(&callerbox->mutex); 154 165 list_append(&request->list, &callerbox->answers); 155 waitq_wakeup(&callerbox->wq, 0); 156 157 spinlock_unlock(&callerbox->lock); 158 spinlock_unlock(&box->lock); 166 mutex_unlock(&callerbox->mutex); 167 condvar_signal(&callerbox->cv); 159 168 } 160 169 … … 168 177 call_t *request; 169 178 170 if ((flags & IPC_WAIT_NONBLOCKING)) { 171 if (waitq_sleep_timeout(&box->wq,SYNCH_NO_TIMEOUT,SYNCH_NON_BLOCKING) == ESYNCH_WOULD_BLOCK) 172 return 0; 173 } else { 174 waitq_sleep(&box->wq); 179 mutex_lock(&box->mutex); 180 while (1) { 181 if (!list_empty(&box->answers)) { 182 /* Handle asynchronous answers */ 183 request = list_get_instance(box->answers.next, call_t, list); 184 list_remove(&request->list); 185 } else if (!list_empty(&box->calls)) { 186 /* Handle requests */ 187 request = list_get_instance(box->calls.next, call_t, list); 188 list_remove(&request->list); 189 /* Append request to dispatch queue */ 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; 175 201 } 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 if (!list_empty(&box->answers)) { 183 request = list_get_instance(box->answers.next, call_t, list); 184 list_remove(&request->list); 185 } else { 186 ASSERT (! list_empty(&box->calls)); 187 request = list_get_instance(box->calls.next, call_t, list); 188 list_remove(&request->list); 189 /* Append request to dispatch queue */ 190 list_append(&request->list, &box->dispatched_calls); 191 } 192 spinlock_unlock(&box->lock); 193 202 mutex_unlock(&box->mutex); 194 203 return request; 195 204 } … … 203 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 else230 panic("thread_create/phonecompany");231 232 ipc_central_box = &TASK->answerbox;233 }
Note:
See TracChangeset
for help on using the changeset viewer.