Subversion Repositories HelenOS

Rev

Rev 3593 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3593 Rev 3665
Line 46... Line 46...
46
void ipc_kbox_cleanup(void)
46
void ipc_kbox_cleanup(void)
47
{
47
{
48
    ipl_t ipl;
48
    ipl_t ipl;
49
    bool have_kb_thread;
49
    bool have_kb_thread;
50
 
50
 
-
 
51
    /*
51
    /* Only hold kb_cleanup_lock while setting kb_finished - this is enough */
52
     * Only hold kb.cleanup_lock while setting kb.finished -
-
 
53
     * this is enough.
-
 
54
     */
52
    mutex_lock(&TASK->kb_cleanup_lock);
55
    mutex_lock(&TASK->kb.cleanup_lock);
53
    TASK->kb_finished = true;
56
    TASK->kb.finished = true;
54
    mutex_unlock(&TASK->kb_cleanup_lock);
57
    mutex_unlock(&TASK->kb.cleanup_lock);
55
 
58
 
56
    have_kb_thread = (TASK->kb_thread != NULL);
59
    have_kb_thread = (TASK->kb.thread != NULL);
57
 
60
 
-
 
61
    /*
58
    /* From now on nobody will try to connect phones or attach kbox threads */
62
     * From now on nobody will try to connect phones or attach
-
 
63
     * kbox threads
-
 
64
     */
59
 
65
 
60
    /*
66
    /*
61
     * Disconnect all phones connected to our kbox. Passing true for
67
     * Disconnect all phones connected to our kbox. Passing true for
62
     * notify_box causes a HANGUP message to be inserted for each
68
     * notify_box causes a HANGUP message to be inserted for each
63
     * disconnected phone. This ensures the kbox thread is going to
69
     * disconnected phone. This ensures the kbox thread is going to
64
     * wake up and terminate.
70
     * wake up and terminate.
65
     */
71
     */
66
    ipc_answerbox_slam_phones(&TASK->kernel_box, have_kb_thread);
72
    ipc_answerbox_slam_phones(&TASK->kb.box, have_kb_thread);
67
 
73
 
68
    /*
74
    /*
69
     * If the task was being debugged, clean up debugging session.
75
     * If the task was being debugged, clean up debugging session.
70
     * This is necessarry as slamming the phones won't force
76
     * This is necessarry as slamming the phones won't force
71
     * kbox thread to clean it up since sender != debugger.
77
     * kbox thread to clean it up since sender != debugger.
Line 75... Line 81...
75
    udebug_task_cleanup(TASK);
81
    udebug_task_cleanup(TASK);
76
    spinlock_unlock(&TASK->lock);
82
    spinlock_unlock(&TASK->lock);
77
    interrupts_restore(ipl);
83
    interrupts_restore(ipl);
78
   
84
   
79
    if (have_kb_thread) {
85
    if (have_kb_thread) {
80
        LOG("join kb_thread..\n");
86
        LOG("join kb.thread..\n");
81
        thread_join(TASK->kb_thread);
87
        thread_join(TASK->kb.thread);
82
        thread_detach(TASK->kb_thread);
88
        thread_detach(TASK->kb.thread);
83
        LOG("join done\n");
89
        LOG("join done\n");
84
        TASK->kb_thread = NULL;
90
        TASK->kb.thread = NULL;
85
    }
91
    }
86
 
92
 
87
    /* Answer all messages in 'calls' and 'dispatched_calls' queues */
93
    /* Answer all messages in 'calls' and 'dispatched_calls' queues. */
88
    spinlock_lock(&TASK->kernel_box.lock);
94
    spinlock_lock(&TASK->kb.box.lock);
89
    ipc_cleanup_call_list(&TASK->kernel_box.dispatched_calls);
95
    ipc_cleanup_call_list(&TASK->kb.box.dispatched_calls);
90
    ipc_cleanup_call_list(&TASK->kernel_box.calls);
96
    ipc_cleanup_call_list(&TASK->kb.box.calls);
91
    spinlock_unlock(&TASK->kernel_box.lock);
97
    spinlock_unlock(&TASK->kb.box.lock);
92
}
98
}
93
 
99
 
94
/** Handle hangup message in kbox.
100
/** Handle hangup message in kbox.
95
 *
101
 *
96
 * @param call  The IPC_M_PHONE_HUNGUP call structure.
102
 * @param call  The IPC_M_PHONE_HUNGUP call structure.
Line 103... Line 109...
103
 
109
 
104
    LOG("kbox_proc_phone_hungup()\n");
110
    LOG("kbox_proc_phone_hungup()\n");
105
 
111
 
106
    /* Was it our debugger, who hung up? */
112
    /* Was it our debugger, who hung up? */
107
    if (call->sender == TASK->udebug.debugger) {
113
    if (call->sender == TASK->udebug.debugger) {
108
        /* Terminate debugging session (if any) */
114
        /* Terminate debugging session (if any). */
109
        LOG("kbox: terminate debug session\n");
115
        LOG("kbox: terminate debug session\n");
110
        ipl = interrupts_disable();
116
        ipl = interrupts_disable();
111
        spinlock_lock(&TASK->lock);
117
        spinlock_lock(&TASK->lock);
112
        udebug_task_cleanup(TASK);
118
        udebug_task_cleanup(TASK);
113
        spinlock_unlock(&TASK->lock);
119
        spinlock_unlock(&TASK->lock);
Line 116... Line 122...
116
        LOG("kbox: was not debugger\n");
122
        LOG("kbox: was not debugger\n");
117
    }
123
    }
118
 
124
 
119
    LOG("kbox: continue with hangup message\n");
125
    LOG("kbox: continue with hangup message\n");
120
    IPC_SET_RETVAL(call->data, 0);
126
    IPC_SET_RETVAL(call->data, 0);
121
    ipc_answer(&TASK->kernel_box, call);
127
    ipc_answer(&TASK->kb.box, call);
122
 
128
 
123
    ipl = interrupts_disable();
129
    ipl = interrupts_disable();
124
    spinlock_lock(&TASK->lock);
130
    spinlock_lock(&TASK->lock);
125
    spinlock_lock(&TASK->answerbox.lock);
131
    spinlock_lock(&TASK->answerbox.lock);
126
    if (list_empty(&TASK->answerbox.connected_phones)) {
132
    if (list_empty(&TASK->answerbox.connected_phones)) {
Line 128... Line 134...
128
         * Last phone has been disconnected. Detach this thread so it
134
         * Last phone has been disconnected. Detach this thread so it
129
         * gets freed and signal to the caller.
135
         * gets freed and signal to the caller.
130
         */
136
         */
131
 
137
 
132
        /* Only detach kbox thread unless already terminating. */
138
        /* Only detach kbox thread unless already terminating. */
133
        mutex_lock(&TASK->kb_cleanup_lock);
139
        mutex_lock(&TASK->kb.cleanup_lock);
134
        if (&TASK->kb_finished == false) {
140
        if (&TASK->kb.finished == false) {
135
            /* Detach kbox thread so it gets freed from memory. */
141
            /* Detach kbox thread so it gets freed from memory. */
136
            thread_detach(TASK->kb_thread);
142
            thread_detach(TASK->kb.thread);
137
            TASK->kb_thread = NULL;
143
            TASK->kb.thread = NULL;
138
        }
144
        }
139
        mutex_unlock(&TASK->kb_cleanup_lock);
145
        mutex_unlock(&TASK->kb.cleanup_lock);
140
 
146
 
141
        LOG("phone list is empty\n");
147
        LOG("phone list is empty\n");
142
        *last = true;
148
        *last = true;
143
    } else {
149
    } else {
144
        *last = false;
150
        *last = false;
Line 164... Line 170...
164
    (void)arg;
170
    (void)arg;
165
    LOG("kbox_thread_proc()\n");
171
    LOG("kbox_thread_proc()\n");
166
    done = false;
172
    done = false;
167
 
173
 
168
    while (!done) {
174
    while (!done) {
169
        call = ipc_wait_for_call(&TASK->kernel_box, SYNCH_NO_TIMEOUT,
175
        call = ipc_wait_for_call(&TASK->kb.box, SYNCH_NO_TIMEOUT,
170
            SYNCH_FLAGS_NONE);
176
            SYNCH_FLAGS_NONE);
171
 
177
 
172
        if (call == NULL)
178
        if (call == NULL)
173
            continue;   /* Try again. */
179
            continue;   /* Try again. */
174
 
180
 
Line 199... Line 205...
199
 
205
 
200
 
206
 
201
/**
207
/**
202
 * Connect phone to a task kernel-box specified by id.
208
 * Connect phone to a task kernel-box specified by id.
203
 *
209
 *
204
 * Note that this is not completely atomic. For optimisation reasons,
210
 * Note that this is not completely atomic. For optimisation reasons, the task
205
 * The task might start cleaning up kbox after the phone has been connected
211
 * might start cleaning up kbox after the phone has been connected and before
206
 * and before a kbox thread has been created. This must be taken into account
212
 * a kbox thread has been created. This must be taken into account in the
207
 * in the cleanup code.
213
 * cleanup code.
208
 *
214
 *
209
 * @return      Phone id on success, or negative error code.
215
 * @return      Phone id on success, or negative error code.
210
 */
216
 */
211
int ipc_connect_kbox(task_id_t taskid)
217
int ipc_connect_kbox(task_id_t taskid)
212
{
218
{
Line 228... Line 234...
228
    atomic_inc(&ta->refcount);
234
    atomic_inc(&ta->refcount);
229
 
235
 
230
    spinlock_unlock(&tasks_lock);
236
    spinlock_unlock(&tasks_lock);
231
    interrupts_restore(ipl);
237
    interrupts_restore(ipl);
232
 
238
 
233
    mutex_lock(&ta->kb_cleanup_lock);
239
    mutex_lock(&ta->kb.cleanup_lock);
234
 
240
 
235
    if (atomic_predec(&ta->refcount) == 0) {
241
    if (atomic_predec(&ta->refcount) == 0) {
236
        mutex_unlock(&ta->kb_cleanup_lock);
242
        mutex_unlock(&ta->kb.cleanup_lock);
237
        task_destroy(ta);
243
        task_destroy(ta);
238
        return ENOENT;
244
        return ENOENT;
239
    }
245
    }
240
 
246
 
241
    if (ta->kb_finished != false) {
247
    if (ta->kb.finished != false) {
242
        mutex_unlock(&ta->kb_cleanup_lock);
248
        mutex_unlock(&ta->kb.cleanup_lock);
243
        return EINVAL;
249
        return EINVAL;
244
    }
250
    }
245
 
251
 
246
    newphid = phone_alloc();
252
    newphid = phone_alloc();
247
    if (newphid < 0) {
253
    if (newphid < 0) {
248
        mutex_unlock(&ta->kb_cleanup_lock);
254
        mutex_unlock(&ta->kb.cleanup_lock);
249
        return ELIMIT;
255
        return ELIMIT;
250
    }
256
    }
251
 
257
 
252
    /* Connect the newly allocated phone to the kbox */
258
    /* Connect the newly allocated phone to the kbox */
253
    ipc_phone_connect(&TASK->phones[newphid], &ta->kernel_box);
259
    ipc_phone_connect(&TASK->phones[newphid], &ta->kb.box);
254
 
260
 
255
    if (ta->kb_thread != NULL) {
261
    if (ta->kb.thread != NULL) {
256
        mutex_unlock(&ta->kb_cleanup_lock);
262
        mutex_unlock(&ta->kb.cleanup_lock);
257
        return newphid;
263
        return newphid;
258
    }
264
    }
259
 
265
 
260
    /* Create a kbox thread */
266
    /* Create a kbox thread */
261
    kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0, "kbox", false);
267
    kb_thread = thread_create(kbox_thread_proc, NULL, ta, 0,
-
 
268
        "kbox", false);
262
    if (!kb_thread) {
269
    if (!kb_thread) {
263
        mutex_unlock(&ta->kb_cleanup_lock);
270
        mutex_unlock(&ta->kb.cleanup_lock);
264
        return ENOMEM;
271
        return ENOMEM;
265
    }
272
    }
266
 
273
 
267
    ta->kb_thread = kb_thread;
274
    ta->kb.thread = kb_thread;
268
    thread_ready(kb_thread);
275
    thread_ready(kb_thread);
269
 
276
 
270
    mutex_unlock(&ta->kb_cleanup_lock);
277
    mutex_unlock(&ta->kb.cleanup_lock);
271
 
278
 
272
    return newphid;
279
    return newphid;
273
}
280
}
274
 
281
 
275
/** @}
282
/** @}