3 Commits

Author SHA1 Message Date
7ae6604e10 Makefile: add dependency on brlib file 2024-08-08 08:43:14 +02:00
f7a6e582ed movegen: add pos_gen_legal() to simplify code 2024-08-05 08:40:03 +02:00
e1debcc3ae create (unused) thread.[ch] files.
I am still unsure if I will go to thread or process...
2024-07-31 07:30:20 +02:00
5 changed files with 150 additions and 5 deletions

View File

@@ -39,7 +39,9 @@ OBJ := $(addprefix $(OBJDIR)/,$(SRC_FN:.c=.o))
TSTSRC := $(wildcard $(TSTDIR)/*.c)
LIB := br_$(shell uname -m) # library name
LIBS := $(strip -l$(LIB))
LIB := $(strip $(LIB))
LIBFILE := ${BRLIBDIR}/lib$(strip ${LIB}).a
LIBS := $(addprefix -l,$(strip $(LIB)))
DEP_FN := $(SRC_FN)
DEP := $(addprefix $(DEPDIR)/,$(DEP_FN:.c=.d))
@@ -102,7 +104,7 @@ endif
# if no version, use last commit and date.
# else, if last commit != last tag commit, add commit and date to version number
ifeq ($(VERSION),)
VERSION := $(build)-$(COMMIT)-$(DATE)
VERSION := $(build)-git.$(COMMIT)-$(DATE)
else ifneq ($(COMMIT), $(TAG_COMMIT))
VERSION := $(VERSION)-next-$(build)-$(COMMIT)-$(DATE)
endif
@@ -344,6 +346,7 @@ brlib:
$(info calling with build=$(build))
$(MAKE) -e -C $(BRLIB) lib-static
unexport build
##################################### brchess binaries
.PHONY: targets cleanbin cleanbindir
@@ -355,9 +358,9 @@ cleanbin:
cleanbindir:
$(call rmdir,$(BINDIR),binaries)
$(TARGET): libs $(OBJ) | $(BINDIR)
$(TARGET): $(LIBFILE) $(OBJ) | $(BINDIR) libs
@echo linking $@.
$(CC) $(LDFLAGS) $(OBJ) $(LIBS) -o $@
$(CC) $(ALL_LDFLAGS) $(OBJ) -o $@
##################################### pre-processed (.i) and assembler (.s) output
.PHONY: cleanasmcpp
@@ -415,7 +418,7 @@ TEST += movedo-test perft-test tt-test
PIECE_OBJS := piece.o
FEN_OBJS := $(PIECE_OBJS) fen.o position.o bitboard.o board.o \
hq.o attack.o hash.o init.o util.o alloc.o move.o \
eval.o eval-defs.o eval-simple.o
eval.o eval-defs.o eval-simple.o hist.o
BB_OBJS := $(FEN_OBJS)
MOVEGEN_OBJS := $(BB_OBJS) move-gen.o
ATTACK_OBJS := $(MOVEGEN_OBJS)
@@ -490,6 +493,7 @@ wtf:
@#echo LIBSRC=$(LIBSRC)
zob:
echo $(LIBFILE)
@#$(CC) $(LDFLAGS) $(CPPFLAGS) $(CFLAGS) $< $(LIBS) src/util.c -o util
##################################### End of multi-targets

View File

@@ -480,3 +480,18 @@ finish:
return movelist;
//return movelist->nmoves = moves - movelist->move;
}
/**
* pos_gen_legal() - generate position legal moves
* @pos: position
* @movelist: &movelist_t array to store moves
*
* Generate all @pos legal moves for player-to-move.
* @movelist is filled with the moves.
*
* @Return: movelist
*/
movelist_t *pos_gen_legal(pos_t *pos, movelist_t *movelist)
{
return pos_legal(pos, pos_gen_pseudo(pos, movelist));
}

View File

@@ -27,5 +27,6 @@ movelist_t *pos_legal_dup(const pos_t *pos, movelist_t *pseudo, movelist_t *lega
movelist_t *pos_legal(const pos_t *pos, movelist_t *list);
movelist_t *pos_gen_pseudo(pos_t *pos, movelist_t *movelist);
movelist_t *pos_gen_legal(pos_t *pos, movelist_t *movelist);
#endif /* MOVEGEN_H */

70
src/thread.c Normal file
View File

@@ -0,0 +1,70 @@
/* thread.c - thread management.
*
* Copyright (C) 2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#include <stdio.h>
#include <brlib.h>
#include <pthread.h>
#include <poll.h>
#include <sys/socket.h>
#include "thread.h"
/* Still have to decide: thread or process ?
*
*/
thread_pool_t threadpool;
/**
* thrd_create - initialize thrd.
*/
int thrd_create(__unused int num)
{
int fd[2];
/* shall we make a communication channel via a pipe or socket ? */
int __unused ret = socketpair(AF_LOCAL, SOCK_SEQPACKET, PF_LOCAL, fd);
return 1;
}
/**
* thread_init - initialize thread pool.
*/
int thread_init(int nb)
{
nb = clamp(nb, MIN_THRDS, MAX_THRDS);
/* stop unwanted threads, always keep 1 */
for (int i = nb + 1; i < threadpool.nb; ++i) {
printf("stopping thread %d - status = \n", i);
threadpool.thread[i].cmd = THRD_DO_QUIT;
}
for (int i = threadpool.nb; i < nb; ++i) {
printf("creating thread %d - status = \n", i);
thrd_create(i);
}
return nb;
}
/*
communication:
main thread -> thread
commands via memory
thread -> main thread
status via memory
output via pipe/socket
thread output will be output/filtered by main thread
*/

55
src/thread.h Normal file
View File

@@ -0,0 +1,55 @@
/* thread.h - thread management.
*
* Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING.
*
* You should have received a copy of the GNU General Public License along with this
* program. If not, see <https://www.gnu.org/licenses/gpl-3.0-standalone.html>.
*
* SPDX-License-Identifier: GPL-3.0-or-later <https://spdx.org/licenses/GPL-3.0-or-later.html>
*
*/
#ifndef THREAD_H
#define THREAD_H
#include <pthread.h>
#include <brlib.h>
#include "position.h"
#define MIN_THRDS 1
#define MAX_THRDS 16
typedef enum {
THRD_DEAD,
THRD_IDLE,
THRD_WORKING,
} thread_status_t;
typedef enum {
/* main thread to subs */
THRD_DO_SEARCH,
THRD_DO_STOP,
THRD_DO_QUIT,
} thread_cmd_t;
typedef struct {
int id;
thread_status_t status;
thread_cmd_t cmd;
int fd[2];
pos_t pos;
} thread_t;
typedef struct {
int nb;
thread_t thread[MAX_THRDS + 1];
} thread_pool_t;
int thrd_create(__unused int num);
int thread_init(int nb);
#endif /* THREAD_H */