Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/ipc/kbox.c

    r4acaa7c0 rf173404  
    4747void ipc_kbox_cleanup(void)
    4848{
    49         /*
     49        bool have_kb_thread;
     50
     51        /*
    5052         * Only hold kb.cleanup_lock while setting kb.finished -
    5153         * this is enough.
     
    5456        TASK->kb.finished = true;
    5557        mutex_unlock(&TASK->kb.cleanup_lock);
    56        
    57         bool have_kb_thread = (TASK->kb.thread != NULL);
    58        
     58
     59        have_kb_thread = (TASK->kb.thread != NULL);
     60
    5961        /*
    6062         * From now on nobody will try to connect phones or attach
    6163         * kbox threads
    6264         */
    63        
     65
    6466        /*
    6567         * Disconnect all phones connected to our kbox. Passing true for
     
    6971         */
    7072        ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread);
    71        
    72         /*
     73
     74        /* 
    7375         * If the task was being debugged, clean up debugging session.
    7476         * This is necessarry as slamming the phones won't force
     
    7880        udebug_task_cleanup(TASK);
    7981        mutex_unlock(&TASK->udebug.lock);
    80        
     82
    8183        if (have_kb_thread) {
    8284                LOG("Join kb.thread.");
     
    8688                TASK->kb.thread = NULL;
    8789        }
    88        
     90
    8991        /* Answer all messages in 'calls' and 'dispatched_calls' queues. */
    90         irq_spinlock_lock(&TASK->kb.box.lock, true);
     92        spinlock_lock(&TASK->kb.box.lock);
    9193        ipc_cleanup_call_list(&TASK->kb.box.dispatched_calls);
    9294        ipc_cleanup_call_list(&TASK->kb.box.calls);
    93         irq_spinlock_unlock(&TASK->kb.box.lock, true);
     95        spinlock_unlock(&TASK->kb.box.lock);
    9496}
    9597
    9698/** Handle hangup message in kbox.
    9799 *
    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  */
     100 * @param call  The IPC_M_PHONE_HUNGUP call structure.
     101 * @param last  Output, the function stores @c true here if
     102 *              this was the last phone, @c false otherwise.
     103 **/
    103104static void kbox_proc_phone_hungup(call_t *call, bool *last)
    104105{
     106        ipl_t ipl;
     107
    105108        /* Was it our debugger, who hung up? */
    106109        if (call->sender == TASK->udebug.debugger) {
    107110                /* Terminate debugging session (if any). */
    108111                LOG("Terminate debugging session.");
    109                 irq_spinlock_lock(&TASK->lock, true);
     112                ipl = interrupts_disable();
     113                spinlock_lock(&TASK->lock);
    110114                udebug_task_cleanup(TASK);
    111                 irq_spinlock_unlock(&TASK->lock, true);
     115                spinlock_unlock(&TASK->lock);
     116                interrupts_restore(ipl);
    112117        } else {
    113118                LOG("Was not debugger.");
    114119        }
    115        
     120
    116121        LOG("Continue with hangup message.");
    117122        IPC_SET_RETVAL(call->data, 0);
    118123        ipc_answer(&TASK->kb.box, call);
    119        
     124
    120125        mutex_lock(&TASK->kb.cleanup_lock);
    121        
    122         irq_spinlock_lock(&TASK->lock, true);
    123         irq_spinlock_lock(&TASK->kb.box.lock, false);
     126
     127        ipl = interrupts_disable();
     128        spinlock_lock(&TASK->lock);
     129        spinlock_lock(&TASK->kb.box.lock);
    124130        if (list_empty(&TASK->kb.box.connected_phones)) {
    125131                /*
     
    127133                 * gets freed and signal to the caller.
    128134                 */
    129                
     135
    130136                /* Only detach kbox thread unless already terminating. */
    131137                if (TASK->kb.finished == false) {
     
    134140                        TASK->kb.thread = NULL;
    135141                }
    136                
     142
    137143                LOG("Phone list is empty.");
    138144                *last = true;
    139         } else
     145        } else {
    140146                *last = false;
    141        
    142         irq_spinlock_unlock(&TASK->kb.box.lock, false);
    143         irq_spinlock_unlock(&TASK->lock, true);
    144        
     147        }
     148
     149        spinlock_unlock(&TASK->kb.box.lock);
     150        spinlock_unlock(&TASK->lock);
     151        interrupts_restore(ipl);
     152
    145153        mutex_unlock(&TASK->kb.cleanup_lock);
    146154}
     
    151159 * when all phones are disconnected from the kbox.
    152160 *
    153  * @param arg Ignored.
    154  *
     161 * @param arg   Ignored.
    155162 */
    156163static void kbox_thread_proc(void *arg)
    157164{
    158         (void) arg;
     165        call_t *call;
     166        bool done;
     167
     168        (void)arg;
    159169        LOG("Starting.");
    160         bool done = false;
    161        
     170        done = false;
     171
    162172        while (!done) {
    163                 call_t *call = ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
     173                call = ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
    164174                        SYNCH_FLAGS_NONE);
    165                
     175
    166176                if (call == NULL)
    167                         continue;  /* Try again. */
    168                
     177                        continue;       /* Try again. */
     178
    169179                switch (IPC_GET_METHOD(call->data)) {
    170                
     180
    171181                case IPC_M_DEBUG_ALL:
    172182                        /* Handle debug call. */
    173183                        udebug_call_receive(call);
    174184                        break;
    175                
     185
    176186                case IPC_M_PHONE_HUNGUP:
    177187                        /*
     
    182192                        kbox_proc_phone_hungup(call, &done);
    183193                        break;
    184                
     194
    185195                default:
    186196                        /* Ignore */
     
    188198                }
    189199        }
    190        
     200
    191201        LOG("Exiting.");
    192202}
    193203
    194204
    195 /** Connect phone to a task kernel-box specified by id.
     205/**
     206 * Connect phone to a task kernel-box specified by id.
    196207 *
    197208 * Note that this is not completely atomic. For optimisation reasons, the task
     
    200211 * cleanup code.
    201212 *
    202  * @return Phone id on success, or negative error code.
    203  *
     213 * @return              Phone id on success, or negative error code.
    204214 */
    205215int ipc_connect_kbox(task_id_t taskid)
    206216{
    207         irq_spinlock_lock(&tasks_lock, true);
    208        
    209         task_t *task = task_find_by_id(taskid);
    210         if (task == NULL) {
    211                 irq_spinlock_unlock(&tasks_lock, true);
     217        int newphid;
     218        task_t *ta;
     219        thread_t *kb_thread;
     220        ipl_t ipl;
     221
     222        ipl = interrupts_disable();
     223        spinlock_lock(&tasks_lock);
     224
     225        ta = task_find_by_id(taskid);
     226        if (ta == NULL) {
     227                spinlock_unlock(&tasks_lock);
     228                interrupts_restore(ipl);
    212229                return ENOENT;
    213230        }
    214        
    215         atomic_inc(&task->refcount);
    216        
    217         irq_spinlock_unlock(&tasks_lock, true);
    218        
    219         mutex_lock(&task->kb.cleanup_lock);
    220        
    221         if (atomic_predec(&task->refcount) == 0) {
    222                 mutex_unlock(&task->kb.cleanup_lock);
    223                 task_destroy(task);
     231
     232        atomic_inc(&ta->refcount);
     233
     234        spinlock_unlock(&tasks_lock);
     235        interrupts_restore(ipl);
     236
     237        mutex_lock(&ta->kb.cleanup_lock);
     238
     239        if (atomic_predec(&ta->refcount) == 0) {
     240                mutex_unlock(&ta->kb.cleanup_lock);
     241                task_destroy(ta);
    224242                return ENOENT;
    225243        }
    226        
    227         if (task->kb.finished != false) {
    228                 mutex_unlock(&task->kb.cleanup_lock);
     244
     245        if (ta->kb.finished != false) {
     246                mutex_unlock(&ta->kb.cleanup_lock);
    229247                return EINVAL;
    230248        }
    231        
    232         int newphid = phone_alloc(TASK);
     249
     250        newphid = phone_alloc(TASK);
    233251        if (newphid < 0) {
    234                 mutex_unlock(&task->kb.cleanup_lock);
     252                mutex_unlock(&ta->kb.cleanup_lock);
    235253                return ELIMIT;
    236254        }
    237        
     255
    238256        /* Connect the newly allocated phone to the kbox */
    239         ipc_phone_connect(&TASK->phones[newphid], &task->kb.box);
    240        
    241         if (task->kb.thread != NULL) {
    242                 mutex_unlock(&task->kb.cleanup_lock);
     257        ipc_phone_connect(&TASK->phones[newphid], &ta->kb.box);
     258
     259        if (ta->kb.thread != NULL) {
     260                mutex_unlock(&ta->kb.cleanup_lock);
    243261                return newphid;
    244262        }
    245        
     263
    246264        /* Create a kbox thread */
    247         thread_t *kb_thread = thread_create(kbox_thread_proc, NULL, task, 0,
     265        kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0,
    248266            "kbox", false);
    249267        if (!kb_thread) {
    250                 mutex_unlock(&task->kb.cleanup_lock);
     268                mutex_unlock(&ta->kb.cleanup_lock);
    251269                return ENOMEM;
    252270        }
    253        
    254         task->kb.thread = kb_thread;
     271
     272        ta->kb.thread = kb_thread;
    255273        thread_ready(kb_thread);
    256        
    257         mutex_unlock(&task->kb.cleanup_lock);
    258        
     274
     275        mutex_unlock(&ta->kb.cleanup_lock);
     276
    259277        return newphid;
    260278}
Note: See TracChangeset for help on using the changeset viewer.