aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authoraspiring_aurelian <tom.wells46@protonmail.com>2023-12-08 08:44:14 -0600
committeraspiring_aurelian <tom.wells46@protonmail.com>2023-12-08 08:44:14 -0600
commit3d76f61ae704c737e9ec830c8a04cd1431fad2a7 (patch)
tree62ee28a8f91b3f7c4a87b797dd3bb7038df9eaa1 /main.c
parentde014b1fe6f14678f0b10077688c23b3f26f79f9 (diff)
parent467ccefc2e5b76906f3e5194cc23511087aa9344 (diff)
Merge branch 'f_comp_thread'
Diffstat (limited to 'main.c')
-rw-r--r--main.c40
1 files changed, 31 insertions, 9 deletions
diff --git a/main.c b/main.c
index 06fdcce..1cc6b57 100644
--- a/main.c
+++ b/main.c
@@ -7,15 +7,26 @@
#include <sys/wait.h>
#include <sys/types.h>
-int f1_newer(char *f1, char *f2) {
+typedef struct f_comp_data {
+ char *f1;
+ char *f2;
+} f_comp_data;
+
+void *f1_newer(void *arg) {
+ f_comp_data *files = (f_comp_data *) arg;
+ char *f1 = files->f1;
+ char *f2 = files->f2;
struct stat f1_attr;
struct stat f2_attr;
stat(f1, &f1_attr);
stat(f2, &f2_attr);
- if (difftime(f1_attr.st_mtime, f2_attr.st_mtime) <= 0) {
- return 0;
+ int diff = difftime(f1_attr.st_mtime, f2_attr.st_mtime);
+ int ret = 0;
+ if (diff <= 0) {
+ pthread_exit(&ret);
}
- return 1;
+ ret = 1;
+ pthread_exit(&ret);
}
void *create_make() {
@@ -30,17 +41,28 @@ void *create_make() {
int main() {
- pthread_t f_thread;
+ /* Struct to hold files */
+ f_comp_data files;
+ files.f1 = "main.c";
+ files.f2 = "./bin/debug";
+
+ /* Threads to:
+ * create makefile
+ * check age of source vs. bin
+ */
+ pthread_t f_thread, check_thread;
pthread_create(&f_thread, NULL, create_make, NULL);
+ pthread_create(&check_thread, NULL, f1_newer, (void *)&files);
mkdir("./bin", 0777);
- char *source = "main.c";
- char *bin = "./bin/debug";
+ void *newer;
pthread_join(f_thread, NULL);
+ pthread_join(check_thread, &newer);
+ int recomp = *(int *) newer;
- if (f1_newer(source, bin)) {
+ if (recomp) {
pid_t make_pid;
if ((make_pid = fork()) == 0) {
printf("Recompiling...\n");
@@ -51,7 +73,7 @@ int main() {
pid_t x_pid;
if ((x_pid = fork()) == 0) {
- execl("./bin/debug", "debug", NULL);
+ execl("./bin/tmp", "tmp", NULL);
}
waitpid(x_pid, NULL, 0);