Subversion Repositories HelenOS

Rev

Rev 2492 | Rev 2586 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1113 palkovsky 1
/*
2071 jermar 2
 * Copyright (c) 2006 Ondrej Palkovsky
2481 jermar 3
 * Copyright (c) 2007 Jakub Jermar
1113 palkovsky 4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
1719 decky 30
/** @addtogroup libc
1653 cejka 31
 * @{
32
 */
33
/** @file
34
 */
35
 
1113 palkovsky 36
#include <libadt/list.h>
2482 jermar 37
#include <fibril.h>
1113 palkovsky 38
#include <malloc.h>
39
#include <unistd.h>
40
#include <thread.h>
41
#include <stdio.h>
1781 jermar 42
#include <libarch/faddr.h>
1392 palkovsky 43
#include <futex.h>
44
#include <assert.h>
1407 palkovsky 45
#include <async.h>
1113 palkovsky 46
 
2482 jermar 47
#ifndef FIBRIL_INITIAL_STACK_PAGES_NO
48
#define FIBRIL_INITIAL_STACK_PAGES_NO   1
1155 vana 49
#endif
50
 
2483 jermar 51
/** This futex serializes access to ready_list, serialized_list and manage_list.
52
 */
53
static atomic_t fibril_futex = FUTEX_INITIALIZER;
54
 
1113 palkovsky 55
static LIST_INITIALIZE(ready_list);
1610 palkovsky 56
static LIST_INITIALIZE(serialized_list);
1392 palkovsky 57
static LIST_INITIALIZE(manager_list);
1113 palkovsky 58
 
2482 jermar 59
static void fibril_main(void);
1125 jermar 60
 
2483 jermar 61
/** Number of fibrils that are in async_serialized mode */
62
static int serialized_fibrils;  /* Protected by async_futex */
1610 palkovsky 63
/** Thread-local count of serialization. If >0, we must not preempt */
1614 palkovsky 64
static __thread int serialization_count;
2482 jermar 65
/** Counter for fibrils residing in async_manager */
66
static int fibrils_in_manager;
1392 palkovsky 67
 
2482 jermar 68
/** Setup fibril information into TCB structure */
69
fibril_t *fibril_setup(void)
1129 palkovsky 70
{
2482 jermar 71
    fibril_t *f;
1392 palkovsky 72
    tcb_t *tcb;
1129 palkovsky 73
 
1392 palkovsky 74
    tcb = __make_tls();
75
    if (!tcb)
76
        return NULL;
77
 
2568 jermar 78
    f = malloc(sizeof(fibril_t));
2482 jermar 79
    if (!f) {
1392 palkovsky 80
        __free_tls(tcb);
1129 palkovsky 81
        return NULL;
82
    }
83
 
2482 jermar 84
    tcb->fibril_data = f;
85
    f->tcb = tcb;
1129 palkovsky 86
 
2568 jermar 87
    f->func = NULL;
88
    f->arg = NULL;
89
    f->stack = NULL;
90
    f->clean_after_me = NULL;
91
    f->retval = 0;
92
    f->flags = 0;
93
 
2482 jermar 94
    return f;
1129 palkovsky 95
}
96
 
2482 jermar 97
void fibril_teardown(fibril_t *f)
1129 palkovsky 98
{
2482 jermar 99
    __free_tls(f->tcb);
100
    free(f);
1129 palkovsky 101
}
102
 
2482 jermar 103
/** Function that spans the whole life-cycle of a fibril.
2481 jermar 104
 *
2483 jermar 105
 * Each fibril begins execution in this function. Then the function implementing
106
 * the fibril logic is called.  After its return, the return value is saved.
107
 * The fibril then switches to another fibril, which cleans up after it.
2481 jermar 108
 */
2482 jermar 109
void fibril_main(void)
1113 palkovsky 110
{
2482 jermar 111
    fibril_t *f = __tcb_get()->fibril_data;
1129 palkovsky 112
 
2483 jermar 113
    /* Call the implementing function. */
2482 jermar 114
    f->retval = f->func(f->arg);
1113 palkovsky 115
 
2568 jermar 116
    fibril_switch(FIBRIL_FROM_DEAD);
2481 jermar 117
    /* not reached */
1113 palkovsky 118
}
119
 
2568 jermar 120
/** Switch from the current fibril.
1128 jermar 121
 *
2482 jermar 122
 * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
1427 palkovsky 123
 * held.
124
 *
2483 jermar 125
 * @param stype     Switch type. One of FIBRIL_PREEMPT, FIBRIL_TO_MANAGER,
2482 jermar 126
 *          FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter
127
 *          describes the circumstances of the switch.
128
 * @return      Return 0 if there is no ready fibril,
2481 jermar 129
 *          return 1 otherwise.
1128 jermar 130
 */
2568 jermar 131
int fibril_switch(fibril_switch_type_t stype)
1113 palkovsky 132
{
2482 jermar 133
    fibril_t *srcf, *dstf;
1392 palkovsky 134
    int retval = 0;
135
 
2482 jermar 136
    futex_down(&fibril_futex);
1113 palkovsky 137
 
2482 jermar 138
    if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
1392 palkovsky 139
        goto ret_0;
1113 palkovsky 140
 
2482 jermar 141
    if (stype == FIBRIL_FROM_MANAGER) {
1610 palkovsky 142
        if (list_empty(&ready_list) && list_empty(&serialized_list))
143
            goto ret_0;
2481 jermar 144
        /*
2483 jermar 145
         * Do not preempt if there is not sufficient count of fibril
2481 jermar 146
         * managers.
147
         */
2568 jermar 148
        if (list_empty(&serialized_list) &&
149
            fibrils_in_manager <= serialized_fibrils) {
1610 palkovsky 150
            goto ret_0;
151
        }
1392 palkovsky 152
    }
1407 palkovsky 153
    /* If we are going to manager and none exists, create it */
2482 jermar 154
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
1610 palkovsky 155
        while (list_empty(&manager_list)) {
2482 jermar 156
            futex_up(&fibril_futex);
1610 palkovsky 157
            async_create_manager();
2482 jermar 158
            futex_down(&fibril_futex);
1610 palkovsky 159
        }
1427 palkovsky 160
    }
1610 palkovsky 161
 
2482 jermar 162
    srcf = __tcb_get()->fibril_data;
163
    if (stype != FIBRIL_FROM_DEAD) {
1610 palkovsky 164
        /* Save current state */
2482 jermar 165
        if (!context_save(&srcf->ctx)) {
1610 palkovsky 166
            if (serialization_count)
2482 jermar 167
                srcf->flags &= ~FIBRIL_SERIALIZED;
168
            if (srcf->clean_after_me) {
2481 jermar 169
                /*
2482 jermar 170
                 * Cleanup after the dead fibril from which we
171
                 * restored context here.
2481 jermar 172
                 */
2568 jermar 173
                void *stack = srcf->clean_after_me->stack;
174
                if (stack) {
175
                    /*
176
                     * This check is necessary because a
177
                     * thread could have exited like a
178
                     * normal fibril using the
179
                     * FIBRIL_FROM_DEAD switch type. In that
180
                     * case, its fibril will not have the
181
                     * stack member filled.
182
                     */
183
                    free(stack);
184
                }
2482 jermar 185
                fibril_teardown(srcf->clean_after_me);
186
                srcf->clean_after_me = NULL;
2481 jermar 187
            }
188
            return 1;   /* futex_up already done here */
1610 palkovsky 189
        }
1392 palkovsky 190
 
2481 jermar 191
        /* Save myself to the correct run list */
2482 jermar 192
        if (stype == FIBRIL_PREEMPT)
193
            list_append(&srcf->link, &ready_list);
194
        else if (stype == FIBRIL_FROM_MANAGER) {
195
            list_append(&srcf->link, &manager_list);
196
            fibrils_in_manager--;
2481 jermar 197
        } else {   
198
            /*
2482 jermar 199
             * If stype == FIBRIL_TO_MANAGER, don't put ourselves to
2481 jermar 200
             * any list, we should already be somewhere, or we will
201
             * be lost.
202
             */
203
        }
204
    }
2568 jermar 205
 
2482 jermar 206
    /* Choose a new fibril to run */
207
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
208
        dstf = list_get_instance(manager_list.next, fibril_t, link);
209
        if (serialization_count && stype == FIBRIL_TO_MANAGER) {
2483 jermar 210
            serialized_fibrils++;
2482 jermar 211
            srcf->flags |= FIBRIL_SERIALIZED;
1610 palkovsky 212
        }
2482 jermar 213
        fibrils_in_manager++;
2481 jermar 214
 
2568 jermar 215
        if (stype == FIBRIL_FROM_DEAD)
2482 jermar 216
            dstf->clean_after_me = srcf;
1610 palkovsky 217
    } else {
218
        if (!list_empty(&serialized_list)) {
2482 jermar 219
            dstf = list_get_instance(serialized_list.next, fibril_t,
220
                link);
2483 jermar 221
            serialized_fibrils--;
2481 jermar 222
        } else {
2482 jermar 223
            dstf = list_get_instance(ready_list.next, fibril_t,
224
                link);
2481 jermar 225
        }
1610 palkovsky 226
    }
2482 jermar 227
    list_remove(&dstf->link);
1113 palkovsky 228
 
2482 jermar 229
    futex_up(&fibril_futex);
230
    context_restore(&dstf->ctx);
2481 jermar 231
    /* not reached */
1392 palkovsky 232
 
233
ret_0:
2482 jermar 234
    futex_up(&fibril_futex);
1392 palkovsky 235
    return retval;
1113 palkovsky 236
}
237
 
2482 jermar 238
/** Create a new fibril.
1113 palkovsky 239
 *
2482 jermar 240
 * @param func      Implementing function of the new fibril.
2481 jermar 241
 * @param arg       Argument to pass to func.
1128 jermar 242
 *
2482 jermar 243
 * @return      Return 0 on failure or TLS of the new fibril.
1113 palkovsky 244
 */
2482 jermar 245
fid_t fibril_create(int (*func)(void *), void *arg)
1113 palkovsky 246
{
2482 jermar 247
    fibril_t *f;
1113 palkovsky 248
 
2482 jermar 249
    f = fibril_setup();
250
    if (!f)
1129 palkovsky 251
        return 0;
2482 jermar 252
    f->stack = (char *) malloc(FIBRIL_INITIAL_STACK_PAGES_NO *
2481 jermar 253
        getpagesize());
2482 jermar 254
    if (!f->stack) {
255
        fibril_teardown(f);
1113 palkovsky 256
        return 0;
257
    }
2568 jermar 258
 
259
    f->func = func;
2482 jermar 260
    f->arg = arg;
1113 palkovsky 261
 
2482 jermar 262
    context_save(&f->ctx);
263
    context_set(&f->ctx, FADDR(fibril_main), f->stack,
264
        FIBRIL_INITIAL_STACK_PAGES_NO * getpagesize(), f->tcb);
1113 palkovsky 265
 
2482 jermar 266
    return (fid_t) f;
1392 palkovsky 267
}
268
 
2482 jermar 269
/** Add a fibril to the ready list.
2481 jermar 270
 *
2492 jermar 271
 * @param fid       Pinter to the fibril structure of the fibril to be
272
 *          added.
2481 jermar 273
 */
2482 jermar 274
void fibril_add_ready(fid_t fid)
1392 palkovsky 275
{
2482 jermar 276
    fibril_t *f;
1392 palkovsky 277
 
2482 jermar 278
    f = (fibril_t *) fid;
279
    futex_down(&fibril_futex);
280
    if ((f->flags & FIBRIL_SERIALIZED))
281
        list_append(&f->link, &serialized_list);
1610 palkovsky 282
    else
2482 jermar 283
        list_append(&f->link, &ready_list);
284
    futex_up(&fibril_futex);
1392 palkovsky 285
}
1113 palkovsky 286
 
2482 jermar 287
/** Add a fibril to the manager list.
2481 jermar 288
 *
2482 jermar 289
 * @param fid       Pinter to the fibril structure of the fibril to be added.
2481 jermar 290
 */
2482 jermar 291
void fibril_add_manager(fid_t fid)
1392 palkovsky 292
{
2482 jermar 293
    fibril_t *f;
1392 palkovsky 294
 
2482 jermar 295
    f = (fibril_t *) fid;
1392 palkovsky 296
 
2482 jermar 297
    futex_down(&fibril_futex);
298
    list_append(&f->link, &manager_list);
299
    futex_up(&fibril_futex);
1113 palkovsky 300
}
1392 palkovsky 301
 
2482 jermar 302
/** Remove one manager from the manager list. */
303
void fibril_remove_manager(void)
1392 palkovsky 304
{
2482 jermar 305
    futex_down(&fibril_futex);
1392 palkovsky 306
    if (list_empty(&manager_list)) {
2482 jermar 307
        futex_up(&fibril_futex);
1392 palkovsky 308
        return;
309
    }
310
    list_remove(manager_list.next);
2482 jermar 311
    futex_up(&fibril_futex);
1392 palkovsky 312
}
1427 palkovsky 313
 
2482 jermar 314
/** Return fibril id of the currently running fibril.
2481 jermar 315
 *
2482 jermar 316
 * @return      Fibril ID of the currently running pseudo thread.
2481 jermar 317
 */
2482 jermar 318
fid_t fibril_get_id(void)
1427 palkovsky 319
{
2482 jermar 320
    return (fid_t) __tcb_get()->fibril_data;
1427 palkovsky 321
}
1610 palkovsky 322
 
323
/** Disable preemption
324
 *
2482 jermar 325
 * If the fibril wants to send several message in a row and does not want to be
2481 jermar 326
 * preempted, it should start async_serialize_start() in the beginning of
327
 * communication and async_serialize_end() in the end. If it is a true
328
 * multithreaded application, it should protect the communication channel by a
329
 * futex as well. Interrupt messages can still be preempted.
1610 palkovsky 330
 */
2482 jermar 331
void fibril_inc_sercount(void)
1610 palkovsky 332
{
333
    serialization_count++;
334
}
335
 
2481 jermar 336
/** Restore the preemption counter to the previous state. */
2482 jermar 337
void fibril_dec_sercount(void)
1610 palkovsky 338
{
339
    serialization_count--;
340
}
1653 cejka 341
 
1719 decky 342
/** @}
1653 cejka 343
 */
2481 jermar 344