diff options
Diffstat (limited to 'src/ipc.c')
| -rw-r--r-- | src/ipc.c | 142 |
1 files changed, 117 insertions, 25 deletions
@@ -1,41 +1,133 @@ #ifndef IPC_C #define IPC_C -#include <mqueue.h> -#include <stdio.h> #include <stdlib.h> +#include <sys/mman.h> -struct mq_attr attr = { - .mq_flags = 0, - .mq_maxmsg = 10, - .mq_msgsize = 1024, - .mq_curmsgs = 0, -}; - -char* read_queue(mqd_t target) { - char* buf = malloc(attr.mq_msgsize); - int n = mq_receive(target, buf, attr.mq_msgsize, NULL); - if (n == -1) { - perror("mq_receive"); - free(buf); - return NULL; +#include "engine/fen.h" + +typedef struct { + struct uci_move *moves; + int count; + int capacity; +} comm_moves; + +void add_comm_move(comm_moves *moves, struct uci_move move) { + if (moves->count + 1 > moves->capacity) { + moves->capacity += 50; + moves->moves = realloc(moves->moves, moves->capacity * sizeof(struct uci_move)); } - return buf; + moves->moves[moves->count++] = move; } -void send_queue(mqd_t target, const char* msg, int n) { - mq_send(target, msg, n, 0); +typedef struct { + bool engine_message_ready; + int depth; + int seldepth; + int multipv; + int score_cp; + int nodes; + int nps; + int hashfull; + int tbhits; + int time; + comm_moves pv; + + bool uci_message_ready; + struct fen_load from_position; + comm_moves moves; +} comms; + +// https://stackoverflow.com/a/5656561 +comms* create_shared_memory() { + int protection = PROT_READ | PROT_WRITE; + int visibility = MAP_SHARED | MAP_ANONYMOUS; + comms* state = (comms*)mmap( + NULL, + sizeof(comms), + protection, visibility, + -1, 0 + ); + state->engine_message_ready = false; + state->depth = 0; + state->seldepth = 0; + state->multipv = 0; + state->score_cp = 0; + state->nodes = 0; + state->nps = 0; + state->hashfull = 0; + state->tbhits = 0; + state->time = 0; + state->pv = (comm_moves) { + (comms*)mmap( + NULL, + sizeof(struct uci_move) * 50, + protection, visibility, + -1, 0 + ), 0, 50 + }; + state->uci_message_ready = false; + state->from_position = {0}; + state->moves = (comm_moves) { + (comms*)mmap( + NULL, + sizeof(struct uci_move) * 50, + protection, visibility, + -1, 0 + ), 0, 50 + }; + + return state; } -int get_num_messages(mqd_t target) { - struct mq_attr target_attr; +void send_uci_message( + comms *comms, + struct fen_load from_position +) { + comms->from_position = from_position; + comms->uci_message_ready = true; +} - if (mq_getattr(target, &target_attr) == -1) { - perror("mq_getattr"); - exit(1); +void send_engine_message( + comms *comms, + int depth, + int seldepth, + int multipv, + int score_cp, + int nodes, + int nps, + int hashfull, + int tbhits, + int time +) { + comms->depth = depth; + comms->seldepth = seldepth; + comms->multipv = multipv; + comms->score_cp = score_cp; + comms->nodes = nodes; + comms->nps = nps; + comms->hashfull = hashfull; + comms->tbhits = tbhits; + comms->time = time; + comms->engine_message_ready = true; +} + +bool receive_uci_message(comms *comms) { + if (comms->uci_message_ready) { + comms->uci_message_ready = false; + return true; } + return false; +} - return target_attr.mq_curmsgs; +// TODO: think about dealing with multiple engine messages +bool receive_engine_message(comms *comms) { + if (comms->engine_message_ready) { + comms->engine_message_ready = false; + return true; + } + return false; } + #endif // IPC_C |
