Rev 1086 | Rev 1090 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1086 | Rev 1088 | ||
---|---|---|---|
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 | /* IPC resources management |
29 | /* IPC resources management |
30 | * |
30 | * |
31 | * The goal of this source code is to properly manage IPC resources |
31 | * The goal of this source code is to properly manage IPC resources |
32 | * and allow straight and clean clean-up procedure upon task termination. |
32 | * and allow straight and clean clean-up procedure upon task termination. |
33 | * |
33 | * |
34 | * The pattern of usage of the resources is: |
34 | * The pattern of usage of the resources is: |
35 | * - allocate empty phone slot, connect | deallocate slot |
35 | * - allocate empty phone slot, connect | deallocate slot |
36 | * - disconnect connected phone (some messages might be on the fly) |
36 | * - disconnect connected phone (some messages might be on the fly) |
37 | * - find phone in slot and send a message using phone |
37 | * - find phone in slot and send a message using phone |
38 | * - answer message to phone |
38 | * - answer message to phone |
39 | * - hangup phone (the caller has hung up) |
39 | * - hangup phone (the caller has hung up) |
40 | * - hangup phone (the answerbox is exiting) |
40 | * - hangup phone (the answerbox is exiting) |
41 | * |
41 | * |
42 | * Locking strategy |
42 | * Locking strategy |
43 | * |
43 | * |
44 | * - To use a phone, disconnect a phone etc., the phone must be |
44 | * - To use a phone, disconnect a phone etc., the phone must be |
45 | * first locked and then checked that it is connected |
45 | * first locked and then checked that it is connected |
46 | * - To connect an allocated phone it need not be locked (assigning |
46 | * - To connect an allocated phone it need not be locked (assigning |
47 | * pointer is atomic on all platforms) |
47 | * pointer is atomic on all platforms) |
48 | * |
48 | * |
49 | * - To find an empty phone slot, the TASK must be locked |
49 | * - To find an empty phone slot, the TASK must be locked |
50 | * - To answer a message, the answerbox must be locked |
50 | * - To answer a message, the answerbox must be locked |
51 | * - The locking of phone and answerbox is done at the ipc_ level. |
51 | * - The locking of phone and answerbox is done at the ipc_ level. |
52 | * It is perfectly correct to pass unconnected phone to these functions |
52 | * It is perfectly correct to pass unconnected phone to these functions |
53 | * and proper reply will be generated. |
53 | * and proper reply will be generated. |
54 | * |
54 | * |
55 | * Locking order |
55 | * Locking order |
56 | * |
56 | * |
57 | * - first phone, then answerbox |
57 | * - first phone, then answerbox |
58 | * + Easy locking on calls |
58 | * + Easy locking on calls |
59 | * - Very hard traversing list of phones when disconnecting because |
59 | * - Very hard traversing list of phones when disconnecting because |
60 | * the phones may disconnect during traversal of list of connected phones. |
60 | * the phones may disconnect during traversal of list of connected phones. |
61 | * The only possibility is try_lock with restart of list traversal. |
61 | * The only possibility is try_lock with restart of list traversal. |
62 | * |
62 | * |
63 | * Destroying is less frequent, this approach is taken. |
63 | * Destroying is less frequent, this approach is taken. |
64 | * |
64 | * |
65 | * Phone hangup |
65 | * Phone hangup |
66 | * |
66 | * |
67 | * *** The caller hangs up (sys_ipc_hangup) *** |
67 | * *** The caller hangs up (sys_ipc_hangup) *** |
68 | * - The phone is disconnected (no more messages can be sent over this phone), |
68 | * - The phone is disconnected (no more messages can be sent over this phone), |
69 | * all in-progress messages are correctly handled. The anwerbox receives |
69 | * all in-progress messages are correctly handled. The anwerbox receives |
70 | * IPC_M_PHONE_HUNGUP call from the phone that hung up. When all async |
70 | * IPC_M_PHONE_HUNGUP call from the phone that hung up. When all async |
71 | * calls are answered, the phone is deallocated. |
71 | * calls are answered, the phone is deallocated. |
72 | * |
72 | * |
73 | * *** The answerbox hangs up (ipc_answer(ESLAM)) |
73 | * *** The answerbox hangs up (ipc_answer(EHANGUP)) |
74 | * - The phone is disconnected. IPC_M_ANSWERBOX_HUNGUP notification |
74 | * - The phone is disconnected. EHANGUP response code is sent |
- | 75 | * to the calling process. All new calls through this phone |
|
75 | * is sent to source task, the calling process is expected to |
76 | * get a EHUNGUP error code, the task is expected to |
76 | * send an sys_ipc_hangup after cleaning up it's internal structures. |
77 | * send an sys_ipc_hangup after cleaning up it's internal structures. |
77 | * |
78 | * |
- | 79 | * Call forwarding |
|
- | 80 | * |
|
- | 81 | * The call can be forwarded, so that the answer to call is passed directly |
|
- | 82 | * to the original sender. However, this poses special problems regarding |
|
- | 83 | * routing of hangup messages. |
|
- | 84 | * |
|
- | 85 | * sys_ipc_hangup -> IPC_M_PHONE_HUNGUP |
|
- | 86 | * - this message CANNOT be forwarded |
|
- | 87 | * |
|
- | 88 | * EHANGUP during forward |
|
- | 89 | * - The *forwarding* phone will be closed, EFORWARD is sent to receiver. |
|
- | 90 | * |
|
- | 91 | * EHANGUP, ENOENT during forward |
|
- | 92 | * - EFORWARD is sent to the receiver, ipc_forward returns error code EFORWARD |
|
- | 93 | * |
|
78 | * Cleanup strategy |
94 | * Cleanup strategy |
79 | * |
95 | * |
80 | * 1) Disconnect all our phones ('sys_ipc_hangup') |
96 | * 1) Disconnect all our phones ('ipc_phone_hangup'). |
81 | * |
97 | * |
82 | * 2) Disconnect all phones connected to answerbox. |
98 | * 2) Disconnect all phones connected to answerbox. |
83 | * * Send message 'PHONE_DISCONNECTED' to the target application |
- | |
84 | * - Once all phones are disconnected, no further calls can arrive |
- | |
85 | * |
99 | * |
86 | * 3) Answer all messages in 'calls' and 'dispatched_calls' queues with |
100 | * 3) Answer all messages in 'calls' and 'dispatched_calls' queues with |
87 | * appropriate error code. |
101 | * appropriate error code (EHANGUP, EFORWARD). |
88 | * |
102 | * |
89 | * 4) Wait for all async answers to arrive |
103 | * 4) Wait for all async answers to arrive. |
90 | * |
104 | * |
91 | */ |
105 | */ |
92 | 106 | ||
93 | #include <synch/spinlock.h> |
107 | #include <synch/spinlock.h> |
94 | #include <ipc/ipc.h> |
108 | #include <ipc/ipc.h> |
95 | #include <arch.h> |
109 | #include <arch.h> |
96 | #include <proc/task.h> |
110 | #include <proc/task.h> |
97 | #include <ipc/ipcrsc.h> |
111 | #include <ipc/ipcrsc.h> |
98 | #include <debug.h> |
112 | #include <debug.h> |
99 | 113 | ||
100 | /** Find call_t * in call table according to callid |
114 | /** Find call_t * in call table according to callid |
101 | * |
115 | * |
102 | * @return NULL on not found, otherwise pointer to call structure |
116 | * @return NULL on not found, otherwise pointer to call structure |
103 | */ |
117 | */ |
104 | call_t * get_call(__native callid) |
118 | call_t * get_call(__native callid) |
105 | { |
119 | { |
106 | /* TODO: Traverse list of dispatched calls and find one */ |
120 | /* TODO: Traverse list of dispatched calls and find one */ |
107 | /* TODO: locking of call, ripping it from dispatched calls etc. */ |
121 | /* TODO: locking of call, ripping it from dispatched calls etc. */ |
108 | return (call_t *) callid; |
122 | return (call_t *) callid; |
109 | } |
123 | } |
110 | 124 | ||
111 | /** Allocate new phone slot in current TASK structure */ |
125 | /** Allocate new phone slot in current TASK structure */ |
112 | int phone_alloc(void) |
126 | int phone_alloc(void) |
113 | { |
127 | { |
114 | int i; |
128 | int i; |
115 | 129 | ||
116 | spinlock_lock(&TASK->lock); |
130 | spinlock_lock(&TASK->lock); |
117 | 131 | ||
118 | for (i=0; i < IPC_MAX_PHONES; i++) { |
132 | for (i=0; i < IPC_MAX_PHONES; i++) { |
119 | if (!TASK->phones[i].busy && !atomic_get(&TASK->phones[i].active_calls)) { |
133 | if (TASK->phones[i].busy==IPC_BUSY_FREE && !atomic_get(&TASK->phones[i].active_calls)) { |
120 | TASK->phones[i].busy = 1; |
134 | TASK->phones[i].busy = IPC_BUSY_CONNECTING; |
121 | break; |
135 | break; |
122 | } |
136 | } |
123 | } |
137 | } |
124 | spinlock_unlock(&TASK->lock); |
138 | spinlock_unlock(&TASK->lock); |
125 | 139 | ||
126 | if (i >= IPC_MAX_PHONES) |
140 | if (i >= IPC_MAX_PHONES) |
127 | return -1; |
141 | return -1; |
128 | return i; |
142 | return i; |
129 | } |
143 | } |
130 | 144 | ||
131 | /** Disconnect phone a free the slot |
145 | /** Disconnect phone a free the slot |
132 | * |
146 | * |
133 | * All already sent messages will be correctly processed |
147 | * All already sent messages will be correctly processed |
134 | */ |
148 | */ |
135 | void phone_dealloc(int phoneid) |
149 | void phone_dealloc(int phoneid) |
136 | { |
150 | { |
137 | spinlock_lock(&TASK->lock); |
151 | spinlock_lock(&TASK->lock); |
138 | 152 | ||
139 | ASSERT(TASK->phones[phoneid].busy); |
153 | ASSERT(TASK->phones[phoneid].busy == IPC_BUSY_CONNECTING); |
140 | ASSERT(! TASK->phones[phoneid].callee); |
154 | ASSERT(! TASK->phones[phoneid].callee); |
141 | 155 | ||
142 | TASK->phones[phoneid].busy = 0; |
156 | TASK->phones[phoneid].busy = IPC_BUSY_FREE; |
143 | spinlock_unlock(&TASK->lock); |
157 | spinlock_unlock(&TASK->lock); |
144 | } |
158 | } |
145 | 159 | ||
146 | /** Connect phone to a given answerbox |
160 | /** Connect phone to a given answerbox |
147 | * |
161 | * |
148 | * @param phoneid The slot that will be connected |
162 | * @param phoneid The slot that will be connected |
149 | * |
163 | * |
150 | * The procedure _enforces_ that the user first marks the phone |
164 | * The procedure _enforces_ that the user first marks the phone |
151 | * busy (e.g. via phone_alloc) and then connects the phone, otherwise |
165 | * busy (e.g. via phone_alloc) and then connects the phone, otherwise |
152 | * race condition may appear. |
166 | * race condition may appear. |
153 | */ |
167 | */ |
154 | void phone_connect(int phoneid, answerbox_t *box) |
168 | void phone_connect(int phoneid, answerbox_t *box) |
155 | { |
169 | { |
156 | phone_t *phone = &TASK->phones[phoneid]; |
170 | phone_t *phone = &TASK->phones[phoneid]; |
157 | 171 | ||
158 | ASSERT(phone->busy); |
172 | ASSERT(phone->busy == IPC_BUSY_CONNECTING); |
159 | ipc_phone_connect(phone, box); |
173 | ipc_phone_connect(phone, box); |
160 | } |
174 | } |
161 | 175 |