HelenOS sources
This source file includes following definitions.
- ipc_kbox_cleanup
- kbox_proc_phone_hungup
- kbox_thread_proc
- ipc_connect_kbox
#include <synch/spinlock.h>
#include <synch/mutex.h>
#include <ipc/ipc.h>
#include <abi/ipc/methods.h>
#include <ipc/ipcrsc.h>
#include <arch.h>
#include <errno.h>
#include <debug.h>
#include <udebug/udebug_ipc.h>
#include <ipc/kbox.h>
#include <proc/thread.h>
void ipc_kbox_cleanup(void)
{
irq_spinlock_lock(&TASK->kb.box.lock, true);
TASK->kb.box.active = false;
irq_spinlock_unlock(&TASK->kb.box.lock, true);
mutex_lock(&TASK->kb.cleanup_lock);
TASK->kb.finished = true;
mutex_unlock(&TASK->kb.cleanup_lock);
bool have_kb_thread = (TASK->kb.thread != NULL);
ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread);
mutex_lock(&TASK->udebug.lock);
udebug_task_cleanup(TASK);
mutex_unlock(&TASK->udebug.lock);
if (have_kb_thread) {
LOG("Join kb.thread.");
thread_join(TASK->kb.thread);
LOG("...join done.");
TASK->kb.thread = NULL;
}
ipc_cleanup_call_list(&TASK->kb.box, &TASK->kb.box.calls);
ipc_cleanup_call_list(&TASK->kb.box, &TASK->kb.box.dispatched_calls);
}
static void kbox_proc_phone_hungup(call_t *call, bool *last)
{
if (call->sender == TASK->udebug.debugger) {
LOG("Terminate debugging session.");
mutex_lock(&TASK->udebug.lock);
udebug_task_cleanup(TASK);
mutex_unlock(&TASK->udebug.lock);
} else {
LOG("Was not debugger.");
}
LOG("Continue with hangup message.");
ipc_set_retval(&call->data, 0);
ipc_answer(&TASK->kb.box, call);
mutex_lock(&TASK->kb.cleanup_lock);
irq_spinlock_lock(&TASK->lock, true);
irq_spinlock_lock(&TASK->kb.box.lock, false);
if (list_empty(&TASK->kb.box.connected_phones)) {
if (TASK->kb.finished == false) {
thread_put(TASK->kb.thread);
TASK->kb.thread = NULL;
}
LOG("Phone list is empty.");
*last = true;
} else
*last = false;
irq_spinlock_unlock(&TASK->kb.box.lock, false);
irq_spinlock_unlock(&TASK->lock, true);
mutex_unlock(&TASK->kb.cleanup_lock);
}
static void kbox_thread_proc(void *arg)
{
(void) arg;
LOG("Starting.");
bool done = false;
while (!done) {
call_t *call = NULL;
(void) ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
SYNCH_FLAGS_NONE, &call);
if (call == NULL)
continue;
switch (ipc_get_imethod(&call->data)) {
case IPC_M_DEBUG:
udebug_call_receive(call);
break;
case IPC_M_PHONE_HUNGUP:
kbox_proc_phone_hungup(call, &done);
break;
default:
break;
}
}
LOG("Exiting.");
}
errno_t ipc_connect_kbox(task_id_t taskid, cap_phone_handle_t *out_phone)
{
task_t *task = task_find_by_id(taskid);
if (!task)
return ENOENT;
mutex_lock(&task->kb.cleanup_lock);
if (task->kb.finished) {
mutex_unlock(&task->kb.cleanup_lock);
task_release(task);
return EINVAL;
}
if (task->kb.thread == NULL) {
thread_t *kb_thread = thread_create(kbox_thread_proc, NULL, task,
THREAD_FLAG_NONE, "kbox");
if (!kb_thread) {
mutex_unlock(&task->kb.cleanup_lock);
task_release(task);
return ENOMEM;
}
task->kb.thread = kb_thread;
thread_start(kb_thread);
}
cap_phone_handle_t phone_handle;
errno_t rc = phone_alloc(TASK, true, &phone_handle, NULL);
if (rc != EOK) {
mutex_unlock(&task->kb.cleanup_lock);
task_release(task);
return rc;
}
kobject_t *phone_obj = kobject_get(TASK, phone_handle,
KOBJECT_TYPE_PHONE);
(void) ipc_phone_connect(phone_obj->phone, &task->kb.box);
mutex_unlock(&task->kb.cleanup_lock);
task_release(task);
*out_phone = phone_handle;
return EOK;
}
HelenOS homepage, sources at GitHub