fix(clang): format thread files.
This commit is contained in:
parent
ad460cc15b
commit
93ad044bb1
@ -1,12 +1,12 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#ifndef USE_PTHREAD
|
#ifndef USE_PTHREAD
|
||||||
|
|
||||||
#include "thread.h"
|
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "pthread.h"
|
#include "pthread.h"
|
||||||
#include <sys/queue.h>
|
#include "thread.h"
|
||||||
#include <bits/pthreadtypes.h>
|
#include <bits/pthreadtypes.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <sys/queue.h>
|
||||||
#include <ucontext.h>
|
#include <ucontext.h>
|
||||||
#include <valgrind/valgrind.h>
|
#include <valgrind/valgrind.h>
|
||||||
|
|
||||||
@ -19,24 +19,26 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct context_entry {
|
struct context_entry {
|
||||||
TAILQ_ENTRY(context_entry) link;
|
TAILQ_ENTRY(context_entry)
|
||||||
|
link;
|
||||||
ucontext_t context;
|
ucontext_t context;
|
||||||
thread_t id;
|
thread_t id;
|
||||||
void *retvalue;
|
void* retvalue;
|
||||||
int valgrind_id;
|
int valgrind_id;
|
||||||
char status;
|
char status;
|
||||||
};
|
};
|
||||||
|
|
||||||
static TAILQ_HEAD(context_head, context_entry) head = TAILQ_HEAD_INITIALIZER(head);
|
static TAILQ_HEAD(context_head, context_entry) head = TAILQ_HEAD_INITIALIZER(head);
|
||||||
static struct context_entry *running = 0;
|
static struct context_entry* running = 0;
|
||||||
static unsigned long long counter = 0;
|
static unsigned long long counter = 0;
|
||||||
|
|
||||||
int thread_yield(void) {
|
int thread_yield(void)
|
||||||
|
{
|
||||||
TRACE("thread_yield");
|
TRACE("thread_yield");
|
||||||
if (counter <= 1) {
|
if (counter <= 1) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
struct context_entry *first = NULL;
|
struct context_entry* first = NULL;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
do {
|
do {
|
||||||
if (count++ == counter) {
|
if (count++ == counter) {
|
||||||
@ -51,30 +53,32 @@ int thread_yield(void) {
|
|||||||
if (first->id == running->id) {
|
if (first->id == running->id) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
} while ((first->status & FINISHED) ||
|
} while ((first->status & FINISHED) || ((first->status & WAITING) && !(((struct context_entry*)first->retvalue)->status & FINISHED)));
|
||||||
((first->status & WAITING) && !(((struct context_entry *)first->retvalue)->status & FINISHED)));
|
|
||||||
TRACE("PICKING %p", first);
|
TRACE("PICKING %p", first);
|
||||||
struct context_entry *old_runner = running;
|
struct context_entry* old_runner = running;
|
||||||
running = first;
|
running = first;
|
||||||
swapcontext(&old_runner->context, &running->context);
|
swapcontext(&old_runner->context, &running->context);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
thread_t thread_self(void) {
|
thread_t thread_self(void)
|
||||||
|
{
|
||||||
if (running == NULL) {
|
if (running == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return running->id;
|
return running->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void thread_function_wrapper(void *(*func)(void *), void *funcarg) {
|
void thread_function_wrapper(void* (*func)(void*), void* funcarg)
|
||||||
|
{
|
||||||
TRACE("Wrapper for %p\n", func);
|
TRACE("Wrapper for %p\n", func);
|
||||||
thread_exit(func(funcarg));
|
thread_exit(func(funcarg));
|
||||||
}
|
}
|
||||||
|
|
||||||
int thread_create(thread_t *newthread, void *(*func)(void *), void *funcarg) {
|
int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg)
|
||||||
|
{
|
||||||
TRACE("Create a new thread that execute function %p", func);
|
TRACE("Create a new thread that execute function %p", func);
|
||||||
struct context_entry *new_entry = malloc(sizeof(*new_entry));
|
struct context_entry* new_entry = malloc(sizeof(*new_entry));
|
||||||
if (counter == 0) {
|
if (counter == 0) {
|
||||||
memset(new_entry, 0, sizeof(*new_entry));
|
memset(new_entry, 0, sizeof(*new_entry));
|
||||||
getcontext(&new_entry->context);
|
getcontext(&new_entry->context);
|
||||||
@ -92,30 +96,32 @@ int thread_create(thread_t *newthread, void *(*func)(void *), void *funcarg) {
|
|||||||
new_entry->context.uc_stack.ss_size = STACK_SIZE;
|
new_entry->context.uc_stack.ss_size = STACK_SIZE;
|
||||||
new_entry->valgrind_id = VALGRIND_STACK_REGISTER(
|
new_entry->valgrind_id = VALGRIND_STACK_REGISTER(
|
||||||
new_entry->context.uc_stack.ss_sp,
|
new_entry->context.uc_stack.ss_sp,
|
||||||
new_entry->context.uc_stack.ss_sp + new_entry->context.uc_stack.ss_size
|
new_entry->context.uc_stack.ss_sp + new_entry->context.uc_stack.ss_size);
|
||||||
);
|
|
||||||
new_entry->id = new_entry;
|
new_entry->id = new_entry;
|
||||||
new_entry->status = ALLOCATED;
|
new_entry->status = ALLOCATED;
|
||||||
new_entry->retvalue = 0;
|
new_entry->retvalue = 0;
|
||||||
*newthread = new_entry;
|
*newthread = new_entry;
|
||||||
makecontext(&new_entry->context, (void (*)(void)) thread_function_wrapper, 2, func, funcarg);
|
makecontext(&new_entry->context, (void (*)(void))thread_function_wrapper, 2, func, funcarg);
|
||||||
counter++;
|
counter++;
|
||||||
TAILQ_INSERT_HEAD(&head, new_entry, link);
|
TAILQ_INSERT_HEAD(&head, new_entry, link);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_entry(struct context_entry *entry) {
|
void print_entry(struct context_entry* entry)
|
||||||
|
{
|
||||||
TRACE("CONTEXT (%p, %p, %d);", entry, entry->id, (entry->status & FINISHED) > 0);
|
TRACE("CONTEXT (%p, %p, %d);", entry, entry->id, (entry->status & FINISHED) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int thread_join(thread_t thread, void **retval) {
|
int thread_join(thread_t thread, void** retval)
|
||||||
|
{
|
||||||
TRACE("Join thread %p", thread);
|
TRACE("Join thread %p", thread);
|
||||||
struct context_entry *entry;
|
struct context_entry* entry;
|
||||||
TAILQ_FOREACH(entry, &head, link) {
|
TAILQ_FOREACH(entry, &head, link)
|
||||||
|
{
|
||||||
if (entry->id != thread) {
|
if (entry->id != thread) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
TRACE("FIND %d",entry->status);
|
TRACE("FIND %d", entry->status);
|
||||||
running->status |= WAITING;
|
running->status |= WAITING;
|
||||||
running->retvalue = entry;
|
running->retvalue = entry;
|
||||||
while (!(entry->status & FINISHED)) {
|
while (!(entry->status & FINISHED)) {
|
||||||
@ -150,7 +156,8 @@ int thread_join(thread_t thread, void **retval) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void thread_exit(void *retval) {
|
void thread_exit(void* retval)
|
||||||
|
{
|
||||||
TRACE("Exit thread %p", running);
|
TRACE("Exit thread %p", running);
|
||||||
if (running == 0)
|
if (running == 0)
|
||||||
exit(0);
|
exit(0);
|
||||||
@ -160,17 +167,21 @@ void thread_exit(void *retval) {
|
|||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int thread_mutex_init(thread_mutex_t *mutex) {
|
int thread_mutex_init(thread_mutex_t* mutex)
|
||||||
return pthread_mutex_init((pthread_mutex_t *) mutex, NULL);
|
{
|
||||||
|
return pthread_mutex_init((pthread_mutex_t*)mutex, NULL);
|
||||||
}
|
}
|
||||||
int thread_mutex_destroy(thread_mutex_t *mutex) {
|
int thread_mutex_destroy(thread_mutex_t* mutex)
|
||||||
return pthread_mutex_destroy((pthread_mutex_t *) mutex);
|
{
|
||||||
|
return pthread_mutex_destroy((pthread_mutex_t*)mutex);
|
||||||
}
|
}
|
||||||
int thread_mutex_lock(thread_mutex_t *mutex) {
|
int thread_mutex_lock(thread_mutex_t* mutex)
|
||||||
return pthread_mutex_lock((pthread_mutex_t * )mutex);
|
{
|
||||||
|
return pthread_mutex_lock((pthread_mutex_t*)mutex);
|
||||||
}
|
}
|
||||||
int thread_mutex_unlock(thread_mutex_t *mutex) {
|
int thread_mutex_unlock(thread_mutex_t* mutex)
|
||||||
return pthread_mutex_unlock((pthread_mutex_t *)mutex);
|
{
|
||||||
|
return pthread_mutex_unlock((pthread_mutex_t*)mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
* mais attention aux inconvénient des tableaux de threads
|
* mais attention aux inconvénient des tableaux de threads
|
||||||
* (consommation mémoire, cout d'allocation, ...).
|
* (consommation mémoire, cout d'allocation, ...).
|
||||||
*/
|
*/
|
||||||
typedef void * thread_t;
|
typedef void* thread_t;
|
||||||
|
|
||||||
/* recuperer l'identifiant du thread courant.
|
/* recuperer l'identifiant du thread courant.
|
||||||
*/
|
*/
|
||||||
@ -17,7 +17,7 @@ extern thread_t thread_self(void);
|
|||||||
/* creer un nouveau thread qui va exécuter la fonction func avec l'argument funcarg.
|
/* creer un nouveau thread qui va exécuter la fonction func avec l'argument funcarg.
|
||||||
* renvoie 0 en cas de succès, -1 en cas d'erreur.
|
* renvoie 0 en cas de succès, -1 en cas d'erreur.
|
||||||
*/
|
*/
|
||||||
extern int thread_create(thread_t *newthread, void *(*func)(void *), void *funcarg);
|
extern int thread_create(thread_t* newthread, void* (*func)(void*), void* funcarg);
|
||||||
|
|
||||||
/* passer la main à un autre thread.
|
/* passer la main à un autre thread.
|
||||||
*/
|
*/
|
||||||
@ -27,7 +27,7 @@ extern int thread_yield(void);
|
|||||||
* la valeur renvoyée par le thread est placée dans *retval.
|
* la valeur renvoyée par le thread est placée dans *retval.
|
||||||
* si retval est NULL, la valeur de retour est ignorée.
|
* si retval est NULL, la valeur de retour est ignorée.
|
||||||
*/
|
*/
|
||||||
extern int thread_join(thread_t thread, void **retval);
|
extern int thread_join(thread_t thread, void** retval);
|
||||||
|
|
||||||
/* terminer le thread courant en renvoyant la valeur de retour retval.
|
/* terminer le thread courant en renvoyant la valeur de retour retval.
|
||||||
* cette fonction ne retourne jamais.
|
* cette fonction ne retourne jamais.
|
||||||
@ -37,20 +37,22 @@ extern int thread_join(thread_t thread, void **retval);
|
|||||||
* cet attribut dans votre interface tant que votre thread_exit()
|
* cet attribut dans votre interface tant que votre thread_exit()
|
||||||
* n'est pas correctement implémenté (il ne doit jamais retourner).
|
* n'est pas correctement implémenté (il ne doit jamais retourner).
|
||||||
*/
|
*/
|
||||||
extern void thread_exit(void *retval) __attribute__ ((__noreturn__));
|
extern void thread_exit(void* retval) __attribute__((__noreturn__));
|
||||||
|
|
||||||
/* Interface possible pour les mutex */
|
/* Interface possible pour les mutex */
|
||||||
typedef struct thread_mutex { int dummy; } thread_mutex_t;
|
typedef struct thread_mutex {
|
||||||
int thread_mutex_init(thread_mutex_t *mutex);
|
int dummy;
|
||||||
int thread_mutex_destroy(thread_mutex_t *mutex);
|
} thread_mutex_t;
|
||||||
int thread_mutex_lock(thread_mutex_t *mutex);
|
int thread_mutex_init(thread_mutex_t* mutex);
|
||||||
int thread_mutex_unlock(thread_mutex_t *mutex);
|
int thread_mutex_destroy(thread_mutex_t* mutex);
|
||||||
|
int thread_mutex_lock(thread_mutex_t* mutex);
|
||||||
|
int thread_mutex_unlock(thread_mutex_t* mutex);
|
||||||
|
|
||||||
#else /* USE_PTHREAD */
|
#else /* USE_PTHREAD */
|
||||||
|
|
||||||
/* Si on compile avec -DUSE_PTHREAD, ce sont les pthreads qui sont utilisés */
|
/* Si on compile avec -DUSE_PTHREAD, ce sont les pthreads qui sont utilisés */
|
||||||
#include <sched.h>
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
#include <sched.h>
|
||||||
#define thread_t pthread_t
|
#define thread_t pthread_t
|
||||||
#define thread_self pthread_self
|
#define thread_self pthread_self
|
||||||
#define thread_create(th, func, arg) pthread_create(th, NULL, func, arg)
|
#define thread_create(th, func, arg) pthread_create(th, NULL, func, arg)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user