Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4513 → Rev 4516

/trunk/uspace/lib/libc/generic/fibril_sync.c/fibril.c
1,6 → 1,5
/*
* Copyright (c) 2006 Ondrej Palkovsky
* Copyright (c) 2007 Jakub Jermar
* Copyright (c) 2009 Jakub Jermar
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
33,314 → 32,86
/** @file
*/
 
#include <fibril_sync.h>
#include <fibril.h>
#include <async.h>
#include <adt/list.h>
#include <fibril.h>
#include <thread.h>
#include <tls.h>
#include <malloc.h>
#include <unistd.h>
#include <stdio.h>
#include <libarch/faddr.h>
#include <futex.h>
#include <assert.h>
#include <async.h>
 
#ifndef FIBRIL_INITIAL_STACK_PAGES_NO
#define FIBRIL_INITIAL_STACK_PAGES_NO 1
#endif
 
/**
* This futex serializes access to ready_list, serialized_list and manager_list.
*/
static atomic_t fibril_futex = FUTEX_INITIALIZER;
 
static LIST_INITIALIZE(ready_list);
static LIST_INITIALIZE(serialized_list);
static LIST_INITIALIZE(manager_list);
 
static void fibril_main(void);
 
/** Number of threads that are executing a manager fibril. */
static int threads_in_manager;
/** Number of threads that are executing a manager fibril and are serialized. */
static int serialized_threads; /* Protected by async_futex */
/** Thread-local count of serialization. If > 0, we must not preempt */
static __thread int serialization_count;
 
/** Setup fibril information into TCB structure */
fibril_t *fibril_setup(void)
void fibril_mutex_initialize(fibril_mutex_t *fm)
{
fibril_t *f;
tcb_t *tcb;
 
tcb = __make_tls();
if (!tcb)
return NULL;
 
f = malloc(sizeof(fibril_t));
if (!f) {
__free_tls(tcb);
return NULL;
}
 
tcb->fibril_data = f;
f->tcb = tcb;
 
f->func = NULL;
f->arg = NULL;
f->stack = NULL;
f->clean_after_me = NULL;
f->retval = 0;
f->flags = 0;
 
return f;
fm->counter = 1;
list_initialize(&fm->waiters);
}
 
void fibril_teardown(fibril_t *f)
void fibril_mutex_lock(fibril_mutex_t *fm)
{
__free_tls(f->tcb);
free(f);
futex_down(&async_futex);
if (fm->counter-- <= 0) {
fibril_t *f = (fibril_t *) fibril_get_id();
list_append(&f->link, &fm->waiters);
fibril_switch(FIBRIL_TO_MANAGER);
} else {
futex_up(&async_futex);
}
}
 
/** Function that spans the whole life-cycle of a fibril.
*
* Each fibril begins execution in this function. Then the function implementing
* the fibril logic is called. After its return, the return value is saved.
* The fibril then switches to another fibril, which cleans up after it.
*/
void fibril_main(void)
bool fibril_mutex_trylock(fibril_mutex_t *fm)
{
fibril_t *f = __tcb_get()->fibril_data;
 
/* Call the implementing function. */
f->retval = f->func(f->arg);
 
fibril_switch(FIBRIL_FROM_DEAD);
/* not reached */
}
 
/** Switch from the current fibril.
*
* If calling with FIBRIL_TO_MANAGER parameter, the async_futex should be
* held.
*
* @param stype Switch type. One of FIBRIL_PREEMPT, FIBRIL_TO_MANAGER,
* FIBRIL_FROM_MANAGER, FIBRIL_FROM_DEAD. The parameter
* describes the circumstances of the switch.
* @return Return 0 if there is no ready fibril,
* return 1 otherwise.
*/
int fibril_switch(fibril_switch_type_t stype)
{
fibril_t *srcf, *dstf;
int retval = 0;
bool locked = false;
futex_down(&fibril_futex);
 
if (stype == FIBRIL_PREEMPT && list_empty(&ready_list))
goto ret_0;
 
if (stype == FIBRIL_FROM_MANAGER) {
if (list_empty(&ready_list) && list_empty(&serialized_list))
goto ret_0;
/*
* Do not preempt if there is not enough threads to run the
* ready fibrils which are not serialized.
*/
if (list_empty(&serialized_list) &&
threads_in_manager <= serialized_threads) {
goto ret_0;
}
futex_down(&async_futex);
if (fm->counter > 0) {
fm->counter--;
locked = true;
}
/* If we are going to manager and none exists, create it */
if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
while (list_empty(&manager_list)) {
futex_up(&fibril_futex);
async_create_manager();
futex_down(&fibril_futex);
}
}
futex_up(&async_futex);
srcf = __tcb_get()->fibril_data;
if (stype != FIBRIL_FROM_DEAD) {
/* Save current state */
if (!context_save(&srcf->ctx)) {
if (serialization_count)
srcf->flags &= ~FIBRIL_SERIALIZED;
if (srcf->clean_after_me) {
/*
* Cleanup after the dead fibril from which we
* restored context here.
*/
void *stack = srcf->clean_after_me->stack;
if (stack) {
/*
* This check is necessary because a
* thread could have exited like a
* normal fibril using the
* FIBRIL_FROM_DEAD switch type. In that
* case, its fibril will not have the
* stack member filled.
*/
free(stack);
}
fibril_teardown(srcf->clean_after_me);
srcf->clean_after_me = NULL;
}
return 1; /* futex_up already done here */
}
 
/* Save myself to the correct run list */
if (stype == FIBRIL_PREEMPT)
list_append(&srcf->link, &ready_list);
else if (stype == FIBRIL_FROM_MANAGER) {
list_append(&srcf->link, &manager_list);
threads_in_manager--;
} else {
/*
* If stype == FIBRIL_TO_MANAGER, don't put ourselves to
* any list, we should already be somewhere, or we will
* be lost.
*/
}
}
/* Choose a new fibril to run */
if (stype == FIBRIL_TO_MANAGER || stype == FIBRIL_FROM_DEAD) {
dstf = list_get_instance(manager_list.next, fibril_t, link);
if (serialization_count && stype == FIBRIL_TO_MANAGER) {
serialized_threads++;
srcf->flags |= FIBRIL_SERIALIZED;
}
threads_in_manager++;
 
if (stype == FIBRIL_FROM_DEAD)
dstf->clean_after_me = srcf;
} else {
if (!list_empty(&serialized_list)) {
dstf = list_get_instance(serialized_list.next, fibril_t,
link);
serialized_threads--;
} else {
dstf = list_get_instance(ready_list.next, fibril_t,
link);
}
}
list_remove(&dstf->link);
 
futex_up(&fibril_futex);
context_restore(&dstf->ctx);
/* not reached */
 
ret_0:
futex_up(&fibril_futex);
return retval;
return locked;
}
 
/** Create a new fibril.
*
* @param func Implementing function of the new fibril.
* @param arg Argument to pass to func.
*
* @return Return 0 on failure or TLS of the new fibril.
*/
fid_t fibril_create(int (*func)(void *), void *arg)
void fibril_mutex_unlock(fibril_mutex_t *fm)
{
fibril_t *f;
 
f = fibril_setup();
if (!f)
return 0;
f->stack = (char *) malloc(FIBRIL_INITIAL_STACK_PAGES_NO *
getpagesize());
if (!f->stack) {
fibril_teardown(f);
return 0;
futex_down(&async_futex);
assert(fm->counter <= 0);
if (fm->counter++ < 0) {
link_t *tmp;
fibril_t *f;
assert(!list_empty(&fm->waiters));
tmp = fm->waiters.next;
f = list_get_instance(tmp, fibril_t, link);
list_remove(&f->link);
fibril_add_ready((fid_t) f);
}
f->func = func;
f->arg = arg;
 
context_save(&f->ctx);
context_set(&f->ctx, FADDR(fibril_main), f->stack,
FIBRIL_INITIAL_STACK_PAGES_NO * getpagesize(), f->tcb);
 
return (fid_t) f;
futex_up(&async_futex);
}
 
/** Add a fibril to the ready list.
*
* @param fid Pointer to the fibril structure of the fibril to be
* added.
*/
void fibril_add_ready(fid_t fid)
void fibril_rwlock_initialize(fibril_rwlock_t *frw)
{
fibril_t *f;
 
f = (fibril_t *) fid;
futex_down(&fibril_futex);
if ((f->flags & FIBRIL_SERIALIZED))
list_append(&f->link, &serialized_list);
else
list_append(&f->link, &ready_list);
futex_up(&fibril_futex);
fibril_mutex_initialize(&frw->fm);
}
 
/** Add a fibril to the manager list.
*
* @param fid Pointer to the fibril structure of the fibril to be
* added.
*/
void fibril_add_manager(fid_t fid)
void fibril_rwlock_read_lock(fibril_rwlock_t *frw)
{
fibril_t *f;
 
f = (fibril_t *) fid;
 
futex_down(&fibril_futex);
list_append(&f->link, &manager_list);
futex_up(&fibril_futex);
fibril_mutex_lock(&frw->fm);
}
 
/** Remove one manager from the manager list. */
void fibril_remove_manager(void)
void fibril_rwlock_write_lock(fibril_rwlock_t *frw)
{
futex_down(&fibril_futex);
if (list_empty(&manager_list)) {
futex_up(&fibril_futex);
return;
}
list_remove(manager_list.next);
futex_up(&fibril_futex);
fibril_mutex_lock(&frw->fm);
}
 
/** Return fibril id of the currently running fibril.
*
* @return Fibril ID of the currently running fibril.
*/
fid_t fibril_get_id(void)
void fibril_rwlock_read_unlock(fibril_rwlock_t *frw)
{
return (fid_t) __tcb_get()->fibril_data;
fibril_mutex_unlock(&frw->fm);
}
 
/** Disable preemption
*
* If the fibril wants to send several message in a row and does not want to be
* preempted, it should start async_serialize_start() in the beginning of
* communication and async_serialize_end() in the end. If it is a true
* multithreaded application, it should protect the communication channel by a
* futex as well. Interrupt messages can still be preempted.
*/
void fibril_inc_sercount(void)
void fibril_rwlock_write_unlock(fibril_rwlock_t *frw)
{
serialization_count++;
fibril_mutex_unlock(&frw->fm);
}
 
/** Restore the preemption counter to the previous state. */
void fibril_dec_sercount(void)
{
serialization_count--;
}
 
/** @}
*/