Subversion Repositories HelenOS

Rev

Rev 2482 | Rev 2492 | 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
 
2482 jermar 78
    f = malloc(sizeof(*f));
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
 
2482 jermar 87
    return f;
1129 palkovsky 88
}
89
 
2482 jermar 90
void fibril_teardown(fibril_t *f)
1129 palkovsky 91
{
2482 jermar 92
    __free_tls(f->tcb);
93
    free(f);
1129 palkovsky 94
}
95
 
2482 jermar 96
/** Function that spans the whole life-cycle of a fibril.
2481 jermar 97
 *
2483 jermar 98
 * Each fibril begins execution in this function. Then the function implementing
99
 * the fibril logic is called.  After its return, the return value is saved.
100
 * The fibril then switches to another fibril, which cleans up after it.
2481 jermar 101
 */
2482 jermar 102
void fibril_main(void)
1113 palkovsky 103
{
2482 jermar 104
    fibril_t *f = __tcb_get()->fibril_data;
1129 palkovsky 105
 
2483 jermar 106
    /* Call the implementing function. */
2482 jermar 107
    f->retval = f->func(f->arg);
1113 palkovsky 108
 
2482 jermar 109
    fibril_schedule_next_adv(FIBRIL_FROM_DEAD);
2481 jermar 110
    /* not reached */
1113 palkovsky 111
}
112
 
2482 jermar 113
/** Schedule next fibril.
1128 jermar 114
 *
2482 jermar 115
 * If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
1427 palkovsky 116
 * held.
117
 *
2483 jermar 118
 * @param stype     Switch type. One of FIBRIL_PREEMPT, FIBRIL_TO_MANAGER,
2482 jermar 119
 *          FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter
120
 *          describes the circumstances of the switch.
121
 * @return      Return 0 if there is no ready fibril,
2481 jermar 122
 *          return 1 otherwise.
1128 jermar 123
 */
2482 jermar 124
int fibril_schedule_next_adv(fibril_switch_type_t stype)
1113 palkovsky 125
{
2482 jermar 126
    fibril_t *srcf, *dstf;
1392 palkovsky 127
    int retval = 0;
128
 
2482 jermar 129
    futex_down(&fibril_futex);
1113 palkovsky 130
 
2482 jermar 131
    if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
1392 palkovsky 132
        goto ret_0;
1113 palkovsky 133
 
2482 jermar 134
    if (stype == FIBRIL_FROM_MANAGER) {
1610 palkovsky 135
        if (list_empty(&ready_list) && list_empty(&serialized_list))
136
            goto ret_0;
2481 jermar 137
        /*
2483 jermar 138
         * Do not preempt if there is not sufficient count of fibril
2481 jermar 139
         * managers.
140
         */
2482 jermar 141
        if (list_empty(&serialized_list) && fibrils_in_manager <=
2483 jermar 142
            serialized_fibrils) {
1610 palkovsky 143
            goto ret_0;
144
        }
1392 palkovsky 145
    }
1407 palkovsky 146
    /* If we are going to manager and none exists, create it */
2482 jermar 147
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
1610 palkovsky 148
        while (list_empty(&manager_list)) {
2482 jermar 149
            futex_up(&fibril_futex);
1610 palkovsky 150
            async_create_manager();
2482 jermar 151
            futex_down(&fibril_futex);
1610 palkovsky 152
        }
1427 palkovsky 153
    }
1610 palkovsky 154
 
2482 jermar 155
    srcf = __tcb_get()->fibril_data;
156
    if (stype != FIBRIL_FROM_DEAD) {
1610 palkovsky 157
        /* Save current state */
2482 jermar 158
        if (!context_save(&srcf->ctx)) {
1610 palkovsky 159
            if (serialization_count)
2482 jermar 160
                srcf->flags &= ~FIBRIL_SERIALIZED;
161
            if (srcf->clean_after_me) {
2481 jermar 162
                /*
2482 jermar 163
                 * Cleanup after the dead fibril from which we
164
                 * restored context here.
2481 jermar 165
                 */
2482 jermar 166
                free(srcf->clean_after_me->stack);
167
                fibril_teardown(srcf->clean_after_me);
168
                srcf->clean_after_me = NULL;
2481 jermar 169
            }
170
            return 1;   /* futex_up already done here */
1610 palkovsky 171
        }
1392 palkovsky 172
 
2481 jermar 173
        /* Save myself to the correct run list */
2482 jermar 174
        if (stype == FIBRIL_PREEMPT)
175
            list_append(&srcf->link, &ready_list);
176
        else if (stype == FIBRIL_FROM_MANAGER) {
177
            list_append(&srcf->link, &manager_list);
178
            fibrils_in_manager--;
2481 jermar 179
        } else {   
180
            /*
2482 jermar 181
             * If stype == FIBRIL_TO_MANAGER, don't put ourselves to
2481 jermar 182
             * any list, we should already be somewhere, or we will
183
             * be lost.
184
             */
185
        }
186
    }
1392 palkovsky 187
 
2482 jermar 188
    /* Choose a new fibril to run */
189
    if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
190
        dstf = list_get_instance(manager_list.next, fibril_t, link);
191
        if (serialization_count && stype == FIBRIL_TO_MANAGER) {
2483 jermar 192
            serialized_fibrils++;
2482 jermar 193
            srcf->flags |= FIBRIL_SERIALIZED;
1610 palkovsky 194
        }
2482 jermar 195
        fibrils_in_manager++;
2481 jermar 196
 
2482 jermar 197
        if (stype == FIBRIL_FROM_DEAD)
198
            dstf->clean_after_me = srcf;
1610 palkovsky 199
    } else {
200
        if (!list_empty(&serialized_list)) {
2482 jermar 201
            dstf = list_get_instance(serialized_list.next, fibril_t,
202
                link);
2483 jermar 203
            serialized_fibrils--;
2481 jermar 204
        } else {
2482 jermar 205
            dstf = list_get_instance(ready_list.next, fibril_t,
206
                link);
2481 jermar 207
        }
1610 palkovsky 208
    }
2482 jermar 209
    list_remove(&dstf->link);
1113 palkovsky 210
 
2482 jermar 211
    futex_up(&fibril_futex);
212
    context_restore(&dstf->ctx);
2481 jermar 213
    /* not reached */
1392 palkovsky 214
 
215
ret_0:
2482 jermar 216
    futex_up(&fibril_futex);
1392 palkovsky 217
    return retval;
1113 palkovsky 218
}
219
 
2482 jermar 220
/** Create a new fibril.
1113 palkovsky 221
 *
2482 jermar 222
 * @param func      Implementing function of the new fibril.
2481 jermar 223
 * @param arg       Argument to pass to func.
1128 jermar 224
 *
2482 jermar 225
 * @return      Return 0 on failure or TLS of the new fibril.
1113 palkovsky 226
 */
2482 jermar 227
fid_t fibril_create(int (*func)(void *), void *arg)
1113 palkovsky 228
{
2482 jermar 229
    fibril_t *f;
1113 palkovsky 230
 
2482 jermar 231
    f = fibril_setup();
232
    if (!f)
1129 palkovsky 233
        return 0;
2482 jermar 234
    f->stack = (char *) malloc(FIBRIL_INITIAL_STACK_PAGES_NO *
2481 jermar 235
        getpagesize());
1113 palkovsky 236
 
2482 jermar 237
    if (!f->stack) {
238
        fibril_teardown(f);
1113 palkovsky 239
        return 0;
240
    }
241
 
2482 jermar 242
    f->arg = arg;
243
    f->func = func;
244
    f->clean_after_me = NULL;
245
    f->retval = 0;
246
    f->flags = 0;
1113 palkovsky 247
 
2482 jermar 248
    context_save(&f->ctx);
249
    context_set(&f->ctx, FADDR(fibril_main), f->stack,
250
        FIBRIL_INITIAL_STACK_PAGES_NO * getpagesize(), f->tcb);
1113 palkovsky 251
 
2482 jermar 252
    return (fid_t) f;
1392 palkovsky 253
}
254
 
2482 jermar 255
/** Add a fibril to the ready list.
2481 jermar 256
 *
2482 jermar 257
 * @param fid       Pinter to the fibril structure of the fibril to be added.
2481 jermar 258
 */
2482 jermar 259
void fibril_add_ready(fid_t fid)
1392 palkovsky 260
{
2482 jermar 261
    fibril_t *f;
1392 palkovsky 262
 
2482 jermar 263
    f = (fibril_t *) fid;
264
    futex_down(&fibril_futex);
265
    if ((f->flags & FIBRIL_SERIALIZED))
266
        list_append(&f->link, &serialized_list);
1610 palkovsky 267
    else
2482 jermar 268
        list_append(&f->link, &ready_list);
269
    futex_up(&fibril_futex);
1392 palkovsky 270
}
1113 palkovsky 271
 
2482 jermar 272
/** Add a fibril to the manager list.
2481 jermar 273
 *
2482 jermar 274
 * @param fid       Pinter to the fibril structure of the fibril to be added.
2481 jermar 275
 */
2482 jermar 276
void fibril_add_manager(fid_t fid)
1392 palkovsky 277
{
2482 jermar 278
    fibril_t *f;
1392 palkovsky 279
 
2482 jermar 280
    f = (fibril_t *) fid;
1392 palkovsky 281
 
2482 jermar 282
    futex_down(&fibril_futex);
283
    list_append(&f->link, &manager_list);
284
    futex_up(&fibril_futex);
1113 palkovsky 285
}
1392 palkovsky 286
 
2482 jermar 287
/** Remove one manager from the manager list. */
288
void fibril_remove_manager(void)
1392 palkovsky 289
{
2482 jermar 290
    futex_down(&fibril_futex);
1392 palkovsky 291
    if (list_empty(&manager_list)) {
2482 jermar 292
        futex_up(&fibril_futex);
1392 palkovsky 293
        return;
294
    }
295
    list_remove(manager_list.next);
2482 jermar 296
    futex_up(&fibril_futex);
1392 palkovsky 297
}
1427 palkovsky 298
 
2482 jermar 299
/** Return fibril id of the currently running fibril.
2481 jermar 300
 *
2482 jermar 301
 * @return      Fibril ID of the currently running pseudo thread.
2481 jermar 302
 */
2482 jermar 303
fid_t fibril_get_id(void)
1427 palkovsky 304
{
2482 jermar 305
    return (fid_t) __tcb_get()->fibril_data;
1427 palkovsky 306
}
1610 palkovsky 307
 
308
/** Disable preemption
309
 *
2482 jermar 310
 * If the fibril wants to send several message in a row and does not want to be
2481 jermar 311
 * preempted, it should start async_serialize_start() in the beginning of
312
 * communication and async_serialize_end() in the end. If it is a true
313
 * multithreaded application, it should protect the communication channel by a
314
 * futex as well. Interrupt messages can still be preempted.
1610 palkovsky 315
 */
2482 jermar 316
void fibril_inc_sercount(void)
1610 palkovsky 317
{
318
    serialization_count++;
319
}
320
 
2481 jermar 321
/** Restore the preemption counter to the previous state. */
2482 jermar 322
void fibril_dec_sercount(void)
1610 palkovsky 323
{
324
    serialization_count--;
325
}
1653 cejka 326
 
1719 decky 327
/** @}
1653 cejka 328
 */
2481 jermar 329