summaryrefslogtreecommitdiff
path: root/src/engine.c
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-07-10 16:01:57 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-07-10 16:01:57 +0530
commita14da2fe3864712dab4e7c4cfb5c323026d668de (patch)
tree418c8c8ff44a611cbe1c1269b10926527046e232 /src/engine.c
parent2761f8a533e2b025f209222a1e4ec70b91c7e8ee (diff)
moving header files to include directory & moving resources in it's own directory
i know currently there is race condition, but the code is getting too messy, i will continue to this after i make a vis tool to analyse how i should split up files & stuff the code rn needs intense restructing for it to make any more progress
Diffstat (limited to 'src/engine.c')
-rw-r--r--src/engine.c105
1 files changed, 78 insertions, 27 deletions
diff --git a/src/engine.c b/src/engine.c
index a063520..dc1f54e 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -2,49 +2,60 @@
#include <string.h>
#include <unistd.h>
#include <pthread.h>
+#include "fcntl.h"
#include "bitboard.h"
-#include "engine/moves.h"
+#include "moves.h"
#include "fen.h"
-#include "fcntl.h"
#include "uci.h"
#include "ipc.h"
-// TODO: implement break conditions from the go command
struct thread_args {
int id;
int stop;
position_t position;
+ struct go_args *go_args;
struct engine_message *message;
};
+
void *engine_thread(void *_args) {
+ printf("NIGGGGA\n");
struct thread_args *args = (struct thread_args*)_args;
+ args->stop = 0;
- int k = 0;
- while (k < 10) {
- if (args->stop) continue;
+ int depth = 0;
+ while (true) {
struct engine_message *message = args->message;
-
- k++;
- message->depth = 69;
+ message->depth = depth++;
message->multipv = args->id;
message->pv = comm_moves_init();
+
+ // pseudo engine work
+ for (volatile int k = 0; k < 1000000000; k++) {}
+
add_comm_move(&message->pv, (struct uci_move) { 14, 24 });
- if (k == 10) {
+ if (args->stop || !should_continue(message, args->go_args)) {
message->best_move = (struct uci_move) { 12, 24 };
message->ponder = (struct uci_move) { 42, 54 };
+ message->next = NULL;
+ message->ready = 1;
+ break;
}
message->next = malloc(sizeof(struct engine_message*));
+ message->next->ready = 0;
message->ready = 1;
args->message = message->next;
}
+
+ args->stop = 2;
}
struct threads {
pthread_t *threads;
struct thread_args* args;
+ struct go_args *go_args;
engine_messages *engine_messages;
size_t count;
size_t capacity;
@@ -53,24 +64,25 @@ struct threads {
};
void increase_threads(struct threads *threads, size_t change) {
+ int old_capacity = threads->capacity;
if (threads->count + change > threads->capacity) {
- threads->capacity = threads->count + change + 16; // TODO: maybe round it to the upper power of 2
+ threads->capacity = threads->count + change + 16;
}
threads->threads = threads->threads == NULL
- ? malloc(sizeof(*threads->threads) * threads->capacity)
- : realloc(threads->threads, sizeof(*threads->threads) * threads->capacity);
+ ? malloc(sizeof(pthread_t) * threads->capacity)
+ : realloc(threads->threads, sizeof(pthread_t) * threads->capacity);
threads->args = threads->args == NULL
- ? malloc(sizeof(*threads->args) * threads->capacity)
- : realloc(threads->args, sizeof(*threads->args) * threads->capacity);
+ ? malloc(sizeof(struct thread_args) * threads->capacity)
+ : realloc(threads->args, sizeof(struct thread_args) * threads->capacity);
threads->engine_messages->data = threads->engine_messages->data == NULL
- ? malloc(sizeof(*threads->engine_messages->data) * threads->capacity)
+ ? malloc(sizeof(struct engine_message*) * threads->capacity)
: realloc(
- threads->engine_messages->data,
- sizeof(*threads->engine_messages->data) * threads->capacity
- );
+ threads->engine_messages->data,
+ sizeof(struct engine_message*) * threads->capacity
+ );
for (int i = threads->count; i < threads->count + change; i++) {
threads->engine_messages->data[i] = malloc(sizeof(struct engine_message));
@@ -79,6 +91,7 @@ void increase_threads(struct threads *threads, size_t change) {
i,
0,
threads->sharing_position,
+ threads->go_args,
threads->engine_messages->data[i],
};
pthread_create(
@@ -94,7 +107,9 @@ void increase_threads(struct threads *threads, size_t change) {
void decrease_threads(struct threads *threads, size_t change) {
for (int i = 0; i < change; i++) {
- free(threads->engine_messages->data[threads->count - i - 1]);
+ struct engine_message *item = threads->engine_messages->data[i];
+ threads->engine_messages->data[i] = NULL;
+ free(item);
pthread_cancel(threads->threads[threads->count - i - 1]);
}
threads->count -= change;
@@ -102,17 +117,33 @@ void decrease_threads(struct threads *threads, size_t change) {
}
void set_threads(struct threads *threads, size_t new_size) {
- if (new_size > threads->count) {
+ if (new_size >= threads->count) {
increase_threads(threads, new_size - threads->count);
} else {
decrease_threads(threads, threads->count - new_size);
}
}
+void send_stop_signal(struct threads *threads) {
+ for (int i = 0; i < threads->count; i++) {
+ if (threads->args[i].stop) continue;
+ threads->args[i].stop = 1;
+ }
+}
+bool all_stopped(struct threads *threads) {
+ if (threads->count == 0) return false;
+ for (int i = 0; i < threads->count; i++) {
+ if (threads->args[i].stop == 2) continue;
+ return false;
+ }
+ return true;
+}
+
// TODO: someday fix that some structs have typedef, some dont
int main(int argc, char** argv) {
comms *comms = malloc(sizeof(*comms));
comms->engine_messages = malloc(sizeof(*comms->engine_messages));
+ comms->engine_messages->data = NULL;
comms->uci_state_initialized = 0;
pthread_t uci_thread;
@@ -123,24 +154,44 @@ int main(int argc, char** argv) {
struct threads engine_threads = {0};
engine_threads.engine_messages = comms->engine_messages;
- int initialized = 0;
while (1) {
if (atomic_load(&comms->state.quit)) break;
- if (!atomic_load(&comms->state.go)) continue;
-
- if (initialized == 0) {
+ if (atomic_load(&comms->state.cleanup)) {
+ set_threads(&engine_threads, 0);
+ for (int i = 0; i < engine_threads.count; i++) {
+ engine_threads.args[i].stop = -1;
+ }
+ }
+ if (atomic_load(&comms->state.go)) {
+ printf("going\n");
engine_threads.sharing_position = comms->state.position;
+ engine_threads.go_args = (struct go_args*)comms->state.go_args;
set_threads(&engine_threads, comms->state.threads);
- initialized = 1;
+ atomic_store(&comms->state.go, 0);
+ atomic_store(&comms->state.go_ready_receive, 1);
+ }
+ if (atomic_load(&comms->state.stop)) {
+ printf("stopping\n");
+ send_stop_signal(&engine_threads);
+ if (!all_stopped(&engine_threads)) continue;
+ set_threads(&engine_threads, 0);
+ for (int i = 0; i < engine_threads.count; i++) {
+ engine_threads.args[i].stop = -1;
+ }
+ atomic_store(&comms->state.stop, 0);
}
}
+ pthread_cancel(uci_thread);
set_threads(&engine_threads, 0);
free(engine_threads.threads);
free(engine_threads.args);
- free(comms->engine_messages);
+ for (int i = 0; i < engine_threads.count; i++) {
+ free(engine_threads.engine_messages->data[i]);
+ }
free(comms->engine_messages->data);
+ free(comms->engine_messages);
free(comms);
return 0;
}