43,6 → 43,7 |
#include <synch/waitq.h> |
#include <synch/synch.h> |
#include <ipc/ipc.h> |
#include <ipc/ipc_kbox.h> |
#include <errno.h> |
#include <mm/slab.h> |
#include <arch.h> |
428,7 → 429,7 |
* |
* @param lst Head of the list to be cleaned up. |
*/ |
static void ipc_cleanup_call_list(link_t *lst) |
void ipc_cleanup_call_list(link_t *lst) |
{ |
call_t *call; |
|
449,7 → 450,7 |
* @param notify_box If true, the answerbox will get a hangup message for |
* each disconnected phone. |
*/ |
static void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box) |
void ipc_answerbox_slam_phones(answerbox_t *box, bool notify_box) |
{ |
phone_t *phone; |
DEADLOCK_PROBE_INIT(p_phonelck); |
510,46 → 511,6 |
if (call) ipc_call_free(call); |
} |
|
#ifdef CONFIG_UDEBUG |
|
static void ipc_kbox_cleanup() |
{ |
bool have_kb_thread; |
|
/* Only hold kb_cleanup_lock while setting kb_finished - this is enough */ |
mutex_lock(&TASK->kb_cleanup_lock); |
TASK->kb_finished = true; |
mutex_unlock(&TASK->kb_cleanup_lock); |
|
have_kb_thread = (TASK->kb_thread != NULL); |
|
/* From now on nobody will try to connect phones or attach kbox threads */ |
|
/* |
* Disconnect all phones connected to our kbox. Passing true for |
* notify_box causes a HANGUP message to be inserted for each |
* disconnected phone. This ensures the kbox thread is going to |
* wake up and terminate. |
*/ |
ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread); |
|
if (have_kb_thread) { |
printf("join kb_thread..\n"); |
thread_join(TASK->kb_thread); |
thread_detach(TASK->kb_thread); |
printf("join done\n"); |
TASK->kb_thread = NULL; |
} |
|
/* Answer all messages in 'calls' and 'dispatched_calls' queues */ |
spinlock_lock(&TASK->kernel_box.lock); |
ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls); |
ipc_cleanup_call_list(&TASK->kernel_box.calls); |
spinlock_unlock(&TASK->kernel_box.lock); |
} |
|
#endif |
|
/** Cleans up all IPC communication of the current task. |
* |
* Note: ipc_hangup sets returning answerbox to TASK->answerbox, you |
728,150 → 689,5 |
spinlock_unlock(&task->lock); |
} |
|
#ifdef CONFIG_UDEBUG |
|
#include <ipc/ipcrsc.h> |
#include <print.h> |
#include <udebug/udebug_ipc.h> |
|
static void kbox_thread_proc(void *arg) |
{ |
call_t *call; |
int method; |
bool done; |
ipl_t ipl; |
|
(void)arg; |
printf("kbox_thread_proc()\n"); |
done = false; |
|
while (!done) { |
//printf("kbox: wait for call\n"); |
call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT, |
SYNCH_FLAGS_NONE); |
|
if (call != NULL) { |
method = IPC_GET_METHOD(call->data); |
|
if (method == IPC_M_DEBUG_ALL) { |
udebug_call_receive(call); |
} |
|
if (method == IPC_M_PHONE_HUNGUP) { |
printf("kbox: handle hangup message\n"); |
|
/* Was it our debugger, who hung up? */ |
if (call->sender == TASK->udebug.debugger) { |
/* Terminate debugging session (if any) */ |
printf("kbox: terminate debug session\n"); |
ipl = interrupts_disable(); |
spinlock_lock(&TASK->lock); |
udebug_task_cleanup(TASK); |
spinlock_unlock(&TASK->lock); |
interrupts_restore(ipl); |
} else { |
printf("kbox: was not debugger\n"); |
} |
|
printf("kbox: continue with hangup message\n"); |
IPC_SET_RETVAL(call->data, 0); |
ipc_answer(&TASK->kernel_box, call); |
|
ipl = interrupts_disable(); |
spinlock_lock(&TASK->lock); |
spinlock_lock(&TASK->answerbox.lock); |
if (list_empty(&TASK->answerbox.connected_phones)) { |
/* Last phone has been disconnected */ |
TASK->kb_thread = NULL; |
done = true; |
printf("phone list is empty\n"); |
} |
spinlock_unlock(&TASK->answerbox.lock); |
spinlock_unlock(&TASK->lock); |
interrupts_restore(ipl); |
} |
} |
} |
|
printf("kbox: finished\n"); |
} |
|
|
/** |
* Connect phone to a task kernel-box specified by id. |
* |
* Note that this is not completely atomic. For optimisation reasons, |
* The task might start cleaning up kbox after the phone has been connected |
* and before a kbox thread has been created. This must be taken into account |
* in the cleanup code. |
* |
* @return Phone id on success, or negative error code. |
*/ |
int ipc_connect_kbox(task_id_t taskid) |
{ |
int newphid; |
task_t *ta; |
thread_t *kb_thread; |
ipl_t ipl; |
|
ipl = interrupts_disable(); |
spinlock_lock(&tasks_lock); |
|
ta = task_find_by_id(taskid); |
if (ta == NULL) { |
spinlock_unlock(&tasks_lock); |
interrupts_restore(ipl); |
return ENOENT; |
} |
|
atomic_inc(&ta->refcount); |
|
spinlock_unlock(&tasks_lock); |
interrupts_restore(ipl); |
|
mutex_lock(&ta->kb_cleanup_lock); |
|
if (atomic_predec(&ta->refcount) == 0) { |
mutex_unlock(&ta->kb_cleanup_lock); |
task_destroy(ta); |
return ENOENT; |
} |
|
if (ta->kb_finished != false) { |
mutex_unlock(&ta->kb_cleanup_lock); |
return EINVAL; |
} |
|
newphid = phone_alloc(); |
if (newphid < 0) { |
mutex_unlock(&ta->kb_cleanup_lock); |
return ELIMIT; |
} |
|
/* Connect the newly allocated phone to the kbox */ |
ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box); |
|
if (ta->kb_thread != NULL) { |
mutex_unlock(&ta->kb_cleanup_lock); |
return newphid; |
} |
|
/* Create a kbox thread */ |
kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false); |
if (!kb_thread) { |
mutex_unlock(&ta->kb_cleanup_lock); |
return ENOMEM; |
} |
|
ta->kb_thread = kb_thread; |
thread_ready(kb_thread); |
|
mutex_unlock(&ta->kb_cleanup_lock); |
|
return newphid; |
} |
|
#endif /* defined(CONFIG_UDEBUG) */ |
|
/** @} |
*/ |