Subversion Repositories HelenOS

Rev

Rev 1027 | Rev 1050 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1027 Rev 1040
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/spinlock.h>
35
#include <synch/mutex.h>
35
#include <synch/waitq.h>
36
#include <ipc/ipc.h>
36
#include <ipc/ipc.h>
37
#include <errno.h>
37
#include <errno.h>
38
#include <mm/slab.h>
38
#include <mm/slab.h>
39
#include <arch.h>
39
#include <arch.h>
40
#include <proc/task.h>
40
#include <proc/task.h>
Line 82... Line 82...
82
 
82
 
83
/** Initialize answerbox structure
83
/** Initialize answerbox structure
84
 */
84
 */
85
void ipc_answerbox_init(answerbox_t *box)
85
void ipc_answerbox_init(answerbox_t *box)
86
{
86
{
87
    mutex_initialize(&box->mutex);
87
    spinlock_initialize(&box->lock, "ipc_box_lock");
88
    condvar_initialize(&box->cv);
88
    waitq_initialize(&box->wq);
89
    list_initialize(&box->connected_phones);
89
    list_initialize(&box->connected_phones);
90
    list_initialize(&box->calls);
90
    list_initialize(&box->calls);
91
    list_initialize(&box->dispatched_calls);
91
    list_initialize(&box->dispatched_calls);
92
    list_initialize(&box->answers);
92
    list_initialize(&box->answers);
93
    box->task = TASK;
93
    box->task = TASK;
94
}
94
}
95
 
95
 
96
/** Initialize phone structure and connect phone to naswerbox
96
/** Connect phone to answerbox */
97
 */
-
 
98
void ipc_phone_init(phone_t *phone, answerbox_t *box)
97
void ipc_phone_connect(phone_t *phone, answerbox_t *box)
99
{
98
{
100
    spinlock_initialize(&phone->lock, "phone_lock");
99
    ASSERT(!phone->callee);
101
   
100
    phone->busy = 1;
102
    phone->callee = box;
101
    phone->callee = box;
103
 
102
 
104
    mutex_lock(&box->mutex);
103
    spinlock_lock(&box->lock);
105
    list_append(&phone->list, &box->connected_phones);
104
    list_append(&phone->list, &box->connected_phones);
106
    mutex_unlock(&box->mutex);
105
    spinlock_unlock(&box->lock);
-
 
106
}
-
 
107
 
-
 
108
/** Initialize phone structure and connect phone to naswerbox
-
 
109
 */
-
 
110
void ipc_phone_init(phone_t *phone)
-
 
111
{
-
 
112
    spinlock_initialize(&phone->lock, "phone_lock");
-
 
113
    phone->callee = NULL;
-
 
114
    phone->busy = 0;
107
}
115
}
108
 
116
 
109
/** Disconnect phone from answerbox */
117
/** Disconnect phone from answerbox */
110
void ipc_phone_destroy(phone_t *phone)
118
void ipc_phone_destroy(phone_t *phone)
111
{
119
{
112
    answerbox_t *box = phone->callee;
120
    answerbox_t *box = phone->callee;
113
   
121
   
114
    ASSERT(box);
122
    ASSERT(box);
115
 
123
 
116
    mutex_lock(&box->mutex);
124
    spinlock_lock(&box->lock);
117
    list_remove(&phone->list);
125
    list_remove(&phone->list);
118
    mutex_unlock(&box->mutex);
126
    spinlock_unlock(&box->lock);
119
}
127
}
120
 
128
 
121
/** Helper function to facilitate synchronous calls */
129
/** Helper function to facilitate synchronous calls */
122
void ipc_call_sync(phone_t *phone, call_t *request)
130
void ipc_call_sync(phone_t *phone, call_t *request)
123
{
131
{
Line 135... Line 143...
135
/** Send a asynchronous request using phone to answerbox
143
/** Send a asynchronous request using phone to answerbox
136
 *
144
 *
137
 * @param phone Phone connected to answerbox
145
 * @param phone Phone connected to answerbox
138
 * @param request Request to be sent
146
 * @param request Request to be sent
139
 */
147
 */
140
void ipc_call(phone_t *phone, call_t *request)
148
void ipc_call(phone_t *phone, call_t *call)
141
{
149
{
142
    answerbox_t *box = phone->callee;
150
    answerbox_t *box = phone->callee;
143
 
151
 
144
    ASSERT(box);
152
    ASSERT(box);
145
 
153
 
146
    mutex_lock(&box->mutex);
154
    spinlock_lock(&box->lock);
147
    list_append(&request->list, &box->calls);
155
    list_append(&call->list, &box->calls);
148
    mutex_unlock(&box->mutex);
156
    spinlock_unlock(&box->lock);
-
 
157
    waitq_wakeup(&box->wq, 0);
-
 
158
}
-
 
159
 
-
 
160
/** Forwards call from one answerbox to a new one
-
 
161
 *
-
 
162
 * @param request Request to be forwarded
-
 
163
 * @param newbox Target answerbox
-
 
164
 * @param oldbox Old answerbox
-
 
165
 */
-
 
166
void ipc_forward(call_t *call, answerbox_t *newbox, answerbox_t *oldbox)
-
 
167
{
-
 
168
    spinlock_lock(&oldbox->lock);
-
 
169
    list_remove(&call->list);
149
    condvar_signal(&box->cv);
170
    spinlock_unlock(&oldbox->lock);
-
 
171
 
-
 
172
    spinlock_lock(&newbox->lock);
-
 
173
    list_append(&call->list, &newbox->calls);
-
 
174
    spinlock_lock(&newbox->lock);
-
 
175
    waitq_wakeup(&newbox->wq, 0);
150
}
176
}
151
 
177
 
152
/** Answer message back to phone
178
/** Answer message back to phone
153
 *
179
 *
154
 * @param box Answerbox that is answering the message
180
 * @param box Answerbox that is answering the message
Line 158... Line 184...
158
{
184
{
159
    answerbox_t *callerbox = request->callerbox;
185
    answerbox_t *callerbox = request->callerbox;
160
 
186
 
161
    request->flags |= IPC_CALL_ANSWERED;
187
    request->flags |= IPC_CALL_ANSWERED;
162
 
188
 
163
    mutex_lock(&box->mutex);
189
    spinlock_lock(&box->lock);
164
    list_remove(&request->list);
190
    list_remove(&request->list);
165
    mutex_unlock(&box->mutex);
191
    spinlock_unlock(&box->lock);
166
 
192
 
167
    mutex_lock(&callerbox->mutex);
193
    spinlock_lock(&callerbox->lock);
168
    list_append(&request->list, &callerbox->answers);
194
    list_append(&request->list, &callerbox->answers);
169
    mutex_unlock(&callerbox->mutex);
195
    spinlock_unlock(&callerbox->lock);
170
    condvar_signal(&callerbox->cv);
196
    waitq_wakeup(&callerbox->wq, 0);
171
}
197
}
172
 
198
 
173
/** Wait for phone call
199
/** Wait for phone call
174
 *
200
 *
175
 * @return Recived message address
201
 * @return Recived message address
Line 177... Line 203...
177
 */
203
 */
178
call_t * ipc_wait_for_call(answerbox_t *box, int flags)
204
call_t * ipc_wait_for_call(answerbox_t *box, int flags)
179
{
205
{
180
    call_t *request;
206
    call_t *request;
181
 
207
 
182
    mutex_lock(&box->mutex);
208
    spinlock_lock(&box->lock);
183
    while (1) {
209
    while (1) {
184
        if (!list_empty(&box->answers)) {
210
        if (!list_empty(&box->answers)) {
185
            /* Handle asynchronous answers */
211
            /* Handle asynchronous answers */
186
            request = list_get_instance(box->answers.next, call_t, list);
212
            request = list_get_instance(box->answers.next, call_t, list);
187
            list_remove(&request->list);
213
            list_remove(&request->list);
Line 192... Line 218...
192
            /* Append request to dispatch queue */
218
            /* Append request to dispatch queue */
193
            list_append(&request->list, &box->dispatched_calls);
219
            list_append(&request->list, &box->dispatched_calls);
194
        } else {
220
        } else {
195
            if (!(flags & IPC_WAIT_NONBLOCKING)) {
221
            if (!(flags & IPC_WAIT_NONBLOCKING)) {
196
                /* Wait for event to appear */
222
                /* Wait for event to appear */
197
                condvar_wait(&box->cv, &box->mutex);
223
                spinlock_unlock(&box->lock);
-
 
224
                waitq_sleep(&box->wq);
-
 
225
                spinlock_lock(&box->lock);
198
                continue;
226
                continue;
199
            }
227
            }
200
            request = NULL;
228
            request = NULL;
201
        }
229
        }
202
        break;
230
        break;
203
    }
231
    }
204
    mutex_unlock(&box->mutex);
232
    spinlock_unlock(&box->lock);
205
    return request;
233
    return request;
206
}
234
}
207
 
235
 
208
/** Initilize ipc subsystem */
236
/** Initilize ipc subsystem */
209
void ipc_init(void)
237
void ipc_init(void)