source: mainline/kernel/generic/src/ipc/kbox.c@ aca4a04

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since aca4a04 was 6eef3c4, checked in by Martin Decky <martin@…>, 13 years ago

cleanup thread_create() and thread_t structure

  • remove 'flag' bitfield from thread_t, use individual boolean flags (can be optimized later on by adding : 1)
  • use an enum for call flags, add THREAD_FLAG_NONE
  • remove the 'uncounted' argument in favour of a call flag
  • introduce thread_wire() to setup wired threads
  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[9a1b20c]1/*
2 * Copyright (c) 2008 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genericipc
30 * @{
31 */
32/** @file
33 */
34
35#include <synch/spinlock.h>
36#include <synch/mutex.h>
37#include <ipc/ipc.h>
[c0699467]38#include <abi/ipc/methods.h>
[9a1b20c]39#include <ipc/ipcrsc.h>
40#include <arch.h>
41#include <errno.h>
[0108984a]42#include <debug.h>
[9a1b20c]43#include <udebug/udebug_ipc.h>
[e028660]44#include <ipc/kbox.h>
[1902113]45#include <print.h>
[9a1b20c]46
47void ipc_kbox_cleanup(void)
48{
[da1bafb]49 /*
[31696b4f]50 * Only hold kb.cleanup_lock while setting kb.finished -
51 * this is enough.
52 */
53 mutex_lock(&TASK->kb.cleanup_lock);
54 TASK->kb.finished = true;
55 mutex_unlock(&TASK->kb.cleanup_lock);
[da1bafb]56
57 bool have_kb_thread = (TASK->kb.thread != NULL);
58
[31696b4f]59 /*
60 * From now on nobody will try to connect phones or attach
61 * kbox threads
62 */
[da1bafb]63
[9a1b20c]64 /*
65 * Disconnect all phones connected to our kbox. Passing true for
66 * notify_box causes a HANGUP message to be inserted for each
67 * disconnected phone. This ensures the kbox thread is going to
68 * wake up and terminate.
69 */
[31696b4f]70 ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread);
[da1bafb]71
72 /*
[94f8e3c1]73 * If the task was being debugged, clean up debugging session.
74 * This is necessarry as slamming the phones won't force
75 * kbox thread to clean it up since sender != debugger.
76 */
[f173404]77 mutex_lock(&TASK->udebug.lock);
[94f8e3c1]78 udebug_task_cleanup(TASK);
[f173404]79 mutex_unlock(&TASK->udebug.lock);
[da1bafb]80
[9a1b20c]81 if (have_kb_thread) {
[ae5aa90]82 LOG("Join kb.thread.");
[31696b4f]83 thread_join(TASK->kb.thread);
84 thread_detach(TASK->kb.thread);
[ae5aa90]85 LOG("...join done.");
[31696b4f]86 TASK->kb.thread = NULL;
[9a1b20c]87 }
[da1bafb]88
[31696b4f]89 /* Answer all messages in 'calls' and 'dispatched_calls' queues. */
[da1bafb]90 irq_spinlock_lock(&TASK->kb.box.lock, true);
[31696b4f]91 ipc_cleanup_call_list(&TASK->kb.box.dispatched_calls);
92 ipc_cleanup_call_list(&TASK->kb.box.calls);
[da1bafb]93 irq_spinlock_unlock(&TASK->kb.box.lock, true);
[9a1b20c]94}
95
[05e69c5]96/** Handle hangup message in kbox.
97 *
[da1bafb]98 * @param call The IPC_M_PHONE_HUNGUP call structure.
99 * @param last Output, the function stores @c true here if
100 * this was the last phone, @c false otherwise.
101 *
102 */
[05e69c5]103static void kbox_proc_phone_hungup(call_t *call, bool *last)
104{
105 /* Was it our debugger, who hung up? */
106 if (call->sender == TASK->udebug.debugger) {
[31696b4f]107 /* Terminate debugging session (if any). */
[ae5aa90]108 LOG("Terminate debugging session.");
[5c088975]109 mutex_lock(&TASK->udebug.lock);
[05e69c5]110 udebug_task_cleanup(TASK);
[5c088975]111 mutex_unlock(&TASK->udebug.lock);
[05e69c5]112 } else {
[ae5aa90]113 LOG("Was not debugger.");
[05e69c5]114 }
[da1bafb]115
[ae5aa90]116 LOG("Continue with hangup message.");
[05e69c5]117 IPC_SET_RETVAL(call->data, 0);
[31696b4f]118 ipc_answer(&TASK->kb.box, call);
[da1bafb]119
[c6f0e3a2]120 mutex_lock(&TASK->kb.cleanup_lock);
[da1bafb]121
122 irq_spinlock_lock(&TASK->lock, true);
123 irq_spinlock_lock(&TASK->kb.box.lock, false);
[fd72312]124 if (list_empty(&TASK->kb.box.connected_phones)) {
[05e69c5]125 /*
126 * Last phone has been disconnected. Detach this thread so it
127 * gets freed and signal to the caller.
128 */
[da1bafb]129
[05e69c5]130 /* Only detach kbox thread unless already terminating. */
[14ecd6c]131 if (TASK->kb.finished == false) {
[05e69c5]132 /* Detach kbox thread so it gets freed from memory. */
[31696b4f]133 thread_detach(TASK->kb.thread);
134 TASK->kb.thread = NULL;
[05e69c5]135 }
[da1bafb]136
[ae5aa90]137 LOG("Phone list is empty.");
[05e69c5]138 *last = true;
[da1bafb]139 } else
[05e69c5]140 *last = false;
[da1bafb]141
[4acaa7c0]142 irq_spinlock_unlock(&TASK->kb.box.lock, false);
143 irq_spinlock_unlock(&TASK->lock, true);
[da1bafb]144
[c6f0e3a2]145 mutex_unlock(&TASK->kb.cleanup_lock);
[05e69c5]146}
147
148/** Implementing function for the kbox thread.
149 *
150 * This function listens for debug requests. It terminates
151 * when all phones are disconnected from the kbox.
152 *
[da1bafb]153 * @param arg Ignored.
154 *
[05e69c5]155 */
[9a1b20c]156static void kbox_thread_proc(void *arg)
157{
[da1bafb]158 (void) arg;
[ae5aa90]159 LOG("Starting.");
[da1bafb]160 bool done = false;
161
[9a1b20c]162 while (!done) {
[da1bafb]163 call_t *call = ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
[9a1b20c]164 SYNCH_FLAGS_NONE);
[da1bafb]165
[05e69c5]166 if (call == NULL)
[da1bafb]167 continue; /* Try again. */
168
[228e490]169 switch (IPC_GET_IMETHOD(call->data)) {
[da1bafb]170
[79ae36dd]171 case IPC_M_DEBUG:
[05e69c5]172 /* Handle debug call. */
173 udebug_call_receive(call);
174 break;
[da1bafb]175
[05e69c5]176 case IPC_M_PHONE_HUNGUP:
177 /*
178 * Process the hangup call. If this was the last
179 * phone, done will be set to true and the
180 * while loop will terminate.
181 */
182 kbox_proc_phone_hungup(call, &done);
183 break;
[da1bafb]184
[05e69c5]185 default:
186 /* Ignore */
187 break;
[9a1b20c]188 }
189 }
[da1bafb]190
[ae5aa90]191 LOG("Exiting.");
[9a1b20c]192}
193
[da1bafb]194/** Connect phone to a task kernel-box specified by id.
[9a1b20c]195 *
[31696b4f]196 * Note that this is not completely atomic. For optimisation reasons, the task
197 * might start cleaning up kbox after the phone has been connected and before
198 * a kbox thread has been created. This must be taken into account in the
199 * cleanup code.
[9a1b20c]200 *
[da1bafb]201 * @return Phone id on success, or negative error code.
202 *
[9a1b20c]203 */
204int ipc_connect_kbox(task_id_t taskid)
205{
[da1bafb]206 irq_spinlock_lock(&tasks_lock, true);
207
208 task_t *task = task_find_by_id(taskid);
209 if (task == NULL) {
210 irq_spinlock_unlock(&tasks_lock, true);
[9a1b20c]211 return ENOENT;
212 }
[da1bafb]213
214 atomic_inc(&task->refcount);
215
216 irq_spinlock_unlock(&tasks_lock, true);
217
218 mutex_lock(&task->kb.cleanup_lock);
219
220 if (atomic_predec(&task->refcount) == 0) {
221 mutex_unlock(&task->kb.cleanup_lock);
222 task_destroy(task);
[9a1b20c]223 return ENOENT;
224 }
[da1bafb]225
226 if (task->kb.finished != false) {
227 mutex_unlock(&task->kb.cleanup_lock);
[9a1b20c]228 return EINVAL;
229 }
[da1bafb]230
231 int newphid = phone_alloc(TASK);
[9a1b20c]232 if (newphid < 0) {
[da1bafb]233 mutex_unlock(&task->kb.cleanup_lock);
[9a1b20c]234 return ELIMIT;
235 }
[da1bafb]236
[9a1b20c]237 /* Connect the newly allocated phone to the kbox */
[da1bafb]238 ipc_phone_connect(&TASK->phones[newphid], &task->kb.box);
239
240 if (task->kb.thread != NULL) {
241 mutex_unlock(&task->kb.cleanup_lock);
[9a1b20c]242 return newphid;
243 }
[da1bafb]244
[9a1b20c]245 /* Create a kbox thread */
[6eef3c4]246 thread_t *kb_thread = thread_create(kbox_thread_proc, NULL, task,
247 THREAD_FLAG_NONE, "kbox");
[9a1b20c]248 if (!kb_thread) {
[da1bafb]249 mutex_unlock(&task->kb.cleanup_lock);
[9a1b20c]250 return ENOMEM;
251 }
[da1bafb]252
253 task->kb.thread = kb_thread;
[9a1b20c]254 thread_ready(kb_thread);
[da1bafb]255
256 mutex_unlock(&task->kb.cleanup_lock);
257
[9a1b20c]258 return newphid;
259}
260
261/** @}
262 */
Note: See TracBrowser for help on using the repository browser.