From a9a78c62c424f1d5fa6f5065d082aedffc17fe0e Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Thu, 31 Dec 2020 20:12:31 -0300 Subject: [PATCH] Add threads.c --- Makefile | 2 +- main.c | 70 +------------------------------------------------- misc/threads.c | 62 ++++++++++++++++++++++++++++++++++++++++++++ misc/threads.h | 24 +++++++++++++++++ 4 files changed, 88 insertions(+), 70 deletions(-) create mode 100644 misc/threads.c create mode 100644 misc/threads.h diff --git a/Makefile b/Makefile index 6340dcc..dcae36a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ FILES = *.c */*.c LIBRARIES = -lpthread INCLUDES = -I. -I./parser/ -I./compiler/ -I./vm/ -I./tokenizer/ -I./misc/ -CFLAGS = -std=c99 -g +CFLAGS = -std=c99 -Wall OUTFILE = jack-compiler main: ${FILES} diff --git a/main.c b/main.c index 0f80feb..8213f1f 100644 --- a/main.c +++ b/main.c @@ -2,81 +2,13 @@ #include #include #include -#include -#include +#include "threads.h" #include "parser.h" #include "compiler.h" #include "io.h" #include "os.h" -typedef struct unit { - FILELIST* file; - TOKEN* tokens; - CLASS* parsed; - COMPILER* compiler; - LINEBLOCK* compiled; - struct unit* next; -} COMPILEUNIT; - -void* parseunit(void* input) { - COMPILEUNIT* unit = (COMPILEUNIT*)input; - - unit->parsed = parse(unit->tokens, unit->file->name); - - pthread_exit(NULL); -} - -void* compileunit(void* input) { - COMPILEUNIT* unit = (COMPILEUNIT*)input; - - unit->compiled = compileclass(unit->compiler, unit->parsed); - - pthread_exit(NULL); -} - -void waitthreads(pthread_t* threads, int amount) { - void* status; - int code; - for(int i = 0; i < amount; i++) { - code = pthread_join(threads[i], &status); - if(code) { - eprintf("Error while joining thread %i: %s\n", i, strerror(code)); - exit(code); - } - } -} - -void actonunits(COMPILEUNIT* units, void*(*fun)(void*)) { - pthread_t mythreads[_SC_THREAD_THREADS_MAX]; - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - - COMPILEUNIT* curr = units; - - int i; - int code; - do { - i = 0; - while(curr != NULL && i < _SC_THREAD_THREADS_MAX) { - code = pthread_create(&mythreads[i], &attr, fun, curr); - - if(code) { - eprintf("Error while creating thread %i: %s\n", i, strerror(code)); - exit(code); - } - - curr = curr->next; - i++; - } - waitthreads(mythreads, i); - } while(i == _SC_THREAD_THREADS_MAX); - - pthread_attr_destroy(&attr); -} - int main(int argc, char* argv[]) { - if(argc < 2) { eprintf("Usage: %s {input file(s)}\n", argv[0]); return 1; diff --git a/misc/threads.c b/misc/threads.c new file mode 100644 index 0000000..e34aa69 --- /dev/null +++ b/misc/threads.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include "threads.h" + +void* parseunit(void* input) { + COMPILEUNIT* unit = (COMPILEUNIT*)input; + + unit->parsed = parse(unit->tokens, unit->file->name); + + pthread_exit(NULL); +} + +void* compileunit(void* input) { + COMPILEUNIT* unit = (COMPILEUNIT*)input; + + unit->compiled = compileclass(unit->compiler, unit->parsed); + + pthread_exit(NULL); +} + +void waitthreads(pthread_t* threads, int amount) { + void* status; + int code; + for(int i = 0; i < amount; i++) { + code = pthread_join(threads[i], &status); + if(code) { + eprintf("Error while joining thread %i: %s\n", i, strerror(code)); + exit(code); + } + } +} + +void actonunits(COMPILEUNIT* units, void*(*fun)(void*)) { + pthread_t mythreads[_SC_THREAD_THREADS_MAX]; + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + COMPILEUNIT* curr = units; + + int i; + int code; + do { + i = 0; + while(curr != NULL && i < _SC_THREAD_THREADS_MAX) { + code = pthread_create(&mythreads[i], &attr, fun, curr); + + if(code) { + eprintf("Error while creating thread %i: %s\n", i, strerror(code)); + exit(code); + } + + curr = curr->next; + i++; + } + waitthreads(mythreads, i); + } while(i == _SC_THREAD_THREADS_MAX); + + pthread_attr_destroy(&attr); +} diff --git a/misc/threads.h b/misc/threads.h new file mode 100644 index 0000000..fbbf106 --- /dev/null +++ b/misc/threads.h @@ -0,0 +1,24 @@ +#ifndef THREADS_H +#define THREADS_H +#include +#include "parser.h" +#include "compiler.h" +#include "io.h" + +/* threads + * Tools for dealing with the compiling pipeline in a parallel way */ + +typedef struct unit { + FILELIST* file; + TOKEN* tokens; + CLASS* parsed; + COMPILER* compiler; + LINEBLOCK* compiled; + struct unit* next; +} COMPILEUNIT; + +void* parseunit(void* input); +void* compileunit(void* input); +void waitthreads(pthread_t* threads, int amount); +void actonunits(COMPILEUNIT* units, void*(*fun)(void*)); +#endif