refactor: rename fifos to better names
This commit is contained in:
parent
e45992ac9d
commit
95d8b9939f
@ -7,7 +7,6 @@
|
||||
#include <sys/queue.h>
|
||||
#include <ucontext.h>
|
||||
#include <valgrind/valgrind.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define HAS_STATUS(entry, value) ((entry->status) & (value))
|
||||
#define SET_STATUS(entry, value) ((entry->status) |= (value))
|
||||
@ -43,8 +42,8 @@ static ucontext_t context_for_freeing;
|
||||
struct last_thread_t;
|
||||
struct mutex_fifo_entry_t;
|
||||
|
||||
struct context_entry {
|
||||
TAILQ_ENTRY(context_entry)
|
||||
struct context_entry_t {
|
||||
TAILQ_ENTRY(context_entry_t)
|
||||
link; // Use to navigate inside the list
|
||||
ucontext_t context;
|
||||
void *retvalue; // return value or if the thread is waited, the id of the thread that wait for it
|
||||
@ -55,27 +54,32 @@ struct context_entry {
|
||||
char stack[STACK_SIZE];
|
||||
};
|
||||
|
||||
// Current running thread
|
||||
static struct context_entry_t* running = NULL;
|
||||
|
||||
TAILQ_HEAD(scheduler_fifo_t, context_entry_t);
|
||||
static struct scheduler_fifo_t scheduler_fifo = TAILQ_HEAD_INITIALIZER(scheduler_fifo);
|
||||
|
||||
// Linked list used to store what's needed to be freed at the very end
|
||||
TAILQ_HEAD(ctx_to_free_fifo_t, context_entry_t);
|
||||
static struct ctx_to_free_fifo_t context_to_freed = TAILQ_HEAD_INITIALIZER(context_to_freed);
|
||||
|
||||
// Last thread_t types and fifo
|
||||
// Used to optimize thread_join
|
||||
struct last_thread_t {
|
||||
STAILQ_ENTRY(last_thread_t)
|
||||
link;
|
||||
struct context_entry * last_thread;
|
||||
struct context_entry_t * last_thread;
|
||||
int ref; // number of reference to this struct (for free)
|
||||
};
|
||||
|
||||
// Use TailQ from queue BSD
|
||||
static TAILQ_HEAD(context_head, context_entry) head = TAILQ_HEAD_INITIALIZER(head);
|
||||
// Current running thread
|
||||
static struct context_entry* running = NULL;
|
||||
|
||||
static TAILQ_HEAD(freed_context_head, context_entry) context_to_freed = TAILQ_HEAD_INITIALIZER(context_to_freed);
|
||||
static STAILQ_HEAD(last_thread_head, last_thread_t) last_thread_freed = STAILQ_HEAD_INITIALIZER(last_thread_freed);
|
||||
|
||||
STAILQ_HEAD(last_thread_fifo_t, last_thread_t);
|
||||
struct last_thread_fifo_t last_thread_freed = STAILQ_HEAD_INITIALIZER(last_thread_freed);
|
||||
|
||||
|
||||
int thread_yield(void)
|
||||
{
|
||||
//TRACE("thread_yield");
|
||||
if (TAILQ_EMPTY(&head)) {
|
||||
if (TAILQ_EMPTY(&scheduler_fifo)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -93,14 +97,16 @@ int thread_yield(void)
|
||||
* check if the thread is not finished and is not waiting for a non finished thread
|
||||
* check if the thread is not the running one.
|
||||
*/
|
||||
struct context_entry* first = TAILQ_FIRST(&head);
|
||||
TAILQ_REMOVE(&head, first, link);
|
||||
struct context_entry_t* first = TAILQ_FIRST(&scheduler_fifo);
|
||||
TAILQ_REMOVE(&scheduler_fifo, first, link);
|
||||
|
||||
if (!IS_FINISHED(running) && !IS_WAITING(running) && !IS_MUTEX_WAITING(running)) {
|
||||
TAILQ_INSERT_TAIL(&head, running, link);
|
||||
TAILQ_INSERT_TAIL(&scheduler_fifo, running, link);
|
||||
}
|
||||
|
||||
TRACE("PICKING %p (previous was %p)", first, running);
|
||||
// Switch to the new thread.
|
||||
struct context_entry* old_runner = running;
|
||||
struct context_entry_t* old_runner = running;
|
||||
running = first;
|
||||
swapcontext(&old_runner->context, &running->context);
|
||||
return 0;
|
||||
@ -127,7 +133,7 @@ void thread_function_wrapper(void* (*func)(void*), void* funcarg)
|
||||
int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
|
||||
{
|
||||
DBG("Create a new thread that execute function %p", func);
|
||||
struct context_entry* new_entry;
|
||||
struct context_entry_t* new_entry;
|
||||
TRACE("Checking for previous allocated entry");
|
||||
if (!TAILQ_EMPTY(&context_to_freed)) {
|
||||
new_entry = TAILQ_FIRST(&context_to_freed);
|
||||
@ -160,14 +166,14 @@ int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
|
||||
makecontext(&new_entry->context, (void (*)(void))thread_function_wrapper, 2, func, funcarg);
|
||||
|
||||
#ifdef FIBO_STRAT
|
||||
TAILQ_INSERT_HEAD(&head, new_entry, link);
|
||||
TAILQ_INSERT_HEAD(&scheduler_fifo, new_entry, link);
|
||||
#else
|
||||
TAILQ_INSERT_TAIL(&head, new_entry, link);
|
||||
TAILQ_INSERT_TAIL(&scheduler_fifo, new_entry, link);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
void print_entry(struct context_entry* entry)
|
||||
void print_entry(struct context_entry_t* entry)
|
||||
{
|
||||
TRACE("CONTEXT (%p, %p, %d);", entry, entry, WAS_ALLOCATED(entry));
|
||||
}
|
||||
@ -175,7 +181,7 @@ void print_entry(struct context_entry* entry)
|
||||
int thread_join(thread_t thread, void** retval)
|
||||
{
|
||||
TRACE("Join thread %p", thread);
|
||||
struct context_entry* entry = thread;
|
||||
struct context_entry_t* entry = thread;
|
||||
// Check if the target is not already waited by another
|
||||
if (IS_WAITED(entry)) {
|
||||
return -1;
|
||||
@ -234,8 +240,8 @@ int thread_join(thread_t thread, void** retval)
|
||||
#ifdef FIBO_STRAT
|
||||
#else
|
||||
if (!IS_MUTEX_WAITING(GET_LAST_WAITED_THREAD(running))) {
|
||||
TAILQ_REMOVE(&head, GET_LAST_WAITED_THREAD(running), link);
|
||||
TAILQ_INSERT_HEAD(&head, GET_LAST_WAITED_THREAD(running), link);
|
||||
TAILQ_REMOVE(&scheduler_fifo, GET_LAST_WAITED_THREAD(running), link);
|
||||
TAILQ_INSERT_HEAD(&scheduler_fifo, GET_LAST_WAITED_THREAD(running), link);
|
||||
}
|
||||
#endif
|
||||
do {
|
||||
@ -280,16 +286,16 @@ void thread_exit(void* retval)
|
||||
SET_STATUS(running, FINISHED);
|
||||
if (IS_WAITED(running)) {
|
||||
// If the thread was waited by another thread, we need to wake it up.
|
||||
struct context_entry* waited = running->retvalue;
|
||||
struct context_entry_t* waited = running->retvalue;
|
||||
#ifdef FIBO_STRAT
|
||||
TAILQ_INSERT_HEAD(&head, waited, link);
|
||||
TAILQ_INSERT_HEAD(&scheduler_fifo, waited, link);
|
||||
#else
|
||||
TAILQ_INSERT_TAIL(&head, waited, link);
|
||||
TAILQ_INSERT_TAIL(&scheduler_fifo, waited, link);
|
||||
#endif
|
||||
}
|
||||
running->retvalue = retval;
|
||||
|
||||
if (!TAILQ_EMPTY(&head)) {
|
||||
if (!TAILQ_EMPTY(&scheduler_fifo)) {
|
||||
thread_yield();
|
||||
}
|
||||
exit(0);
|
||||
@ -298,17 +304,17 @@ void thread_exit(void* retval)
|
||||
void clear_context(void)
|
||||
{
|
||||
TRACE("INSIDE CLEAR");
|
||||
struct context_entry* last = NULL;
|
||||
struct context_entry_t* last = NULL;
|
||||
// Loop over remaining threads to clean them from the heap.
|
||||
while (!TAILQ_EMPTY(&head)) {
|
||||
last = TAILQ_FIRST(&head);
|
||||
TAILQ_REMOVE(&head, last, link);
|
||||
while (!TAILQ_EMPTY(&scheduler_fifo)) {
|
||||
last = TAILQ_FIRST(&scheduler_fifo);
|
||||
TAILQ_REMOVE(&scheduler_fifo, last, link);
|
||||
if (WAS_ALLOCATED(last)) {
|
||||
VALGRIND_STACK_DEREGISTER(last->valgrind_id);
|
||||
}
|
||||
if (IS_WAITED(last)) {
|
||||
struct context_entry* waited = last->retvalue;
|
||||
TAILQ_INSERT_TAIL(&head, waited, link);
|
||||
struct context_entry_t* waited = last->retvalue;
|
||||
TAILQ_INSERT_TAIL(&scheduler_fifo, waited, link);
|
||||
}
|
||||
free(last);
|
||||
}
|
||||
@ -336,7 +342,7 @@ void __attribute__((constructor)) setup_main_thread()
|
||||
{
|
||||
TRACE("premain");
|
||||
// Create an entry for the main thread.
|
||||
struct context_entry* main = malloc(sizeof(*main));
|
||||
struct context_entry_t* main = malloc(sizeof(*main));
|
||||
// memset(main, 0, sizeof(*main));
|
||||
getcontext(&main->context);
|
||||
main->status = 0;
|
||||
@ -357,7 +363,7 @@ void __attribute__((destructor)) clear_last_thread()
|
||||
{
|
||||
TRACE("POST");
|
||||
// Running is the initial main thread. No need to switch to a static stack.
|
||||
TAILQ_INSERT_HEAD(&head, running, link);
|
||||
TAILQ_INSERT_HEAD(&scheduler_fifo, running, link);
|
||||
if (!WAS_ALLOCATED(running)) {
|
||||
clear_context();
|
||||
exit(0);
|
||||
@ -409,7 +415,7 @@ int thread_mutex_unlock(thread_mutex_t* mutex)
|
||||
STAILQ_REMOVE_HEAD(&mutex->fifo, link);
|
||||
|
||||
UNSET_STATUS(first->thread, MUTEX_WAITING);
|
||||
TAILQ_INSERT_TAIL(&head, first->thread, link);
|
||||
TAILQ_INSERT_TAIL(&scheduler_fifo, first->thread, link);
|
||||
}
|
||||
|
||||
return mutex->dummy = 0;
|
||||
|
@ -43,7 +43,7 @@ extern void thread_exit(void *retval) __attribute__ ((__noreturn__));
|
||||
/* Interface possible pour les mutex */
|
||||
struct mutex_fifo_entry_t {
|
||||
STAILQ_ENTRY(mutex_fifo_entry_t) link;
|
||||
struct context_entry* thread;
|
||||
struct context_entry_t* thread;
|
||||
};
|
||||
STAILQ_HEAD(mutex_fifo_t, mutex_fifo_entry_t);
|
||||
typedef struct thread_mutex { int dummy; struct mutex_fifo_t fifo; } thread_mutex_t;
|
||||
|
Loading…
x
Reference in New Issue
Block a user