ref: e228e0f0dd56baa092726ec092e728e72727bbdd
parent: 71d5b0ec838cc99a34d1c3669fb014adb75d097f
author: zamfofex <zamfofex@twdb.moe>
date: Sun Mar 2 00:37:57 EST 2025
support threading on 9front
--- a/README.md
+++ b/README.md
@@ -170,7 +170,7 @@
~~~
# (example for x86-64)
-6c -I/sys/include/npe -Dmoonfish_no_threads chess.c search.c main.c
+6c -I/sys/include/npe -Dmoonfish_plan9 chess.c search.c main.c
6l -o moonfish chess.6 search.6 main.6
moonfish
~~~
--- a/main.c
+++ b/main.c
@@ -329,9 +329,7 @@
#ifndef moonfish_no_threads
info.has_thread = 0;
- options[0].value = sysconf(_SC_NPROCESSORS_ONLN);
- if (options[0].value > options[0].max) options[0].value = options[0].max;
- if (options[0].value < 1) options[0].value = 4;
+ options[0].value = 1;
#endif
for (;;) {--- a/search.c
+++ b/search.c
@@ -56,11 +56,10 @@
struct moonfish_node {struct moonfish_node *parent;
struct moonfish_node *children;
+ _Atomic int visits, count, uses;
_Atomic short int score;
- _Atomic long int visits;
_Atomic unsigned char bounds[2];
- _Atomic short int count;
- _Atomic unsigned char ignored, uses;
+ _Atomic unsigned char ignored;
unsigned char from, index;
};
@@ -127,7 +126,7 @@
static void moonfish_discard(struct moonfish_node *node)
{- short int i, count;
+ int i, count;
#ifdef moonfish_no_threads
count = node->count;
@@ -249,7 +248,7 @@
{struct moonfish_node *next;
float max_confidence, confidence;
- short int i, count;
+ int i, count;
for (;;) {--- a/threads.h
+++ b/threads.h
@@ -12,7 +12,34 @@
#else
+#ifndef moonfish_plan9
+
#include <stdatomic.h>
+
+#else
+
+#define moonfish_pthreads
+#define _Atomic
+
+int cas(int *pointer, int expected, int desired);
+
+static int atomic_compare_exchange_strong(int *pointer, int *expected, int desired)
+{+ int value;
+ value = *pointer;
+ if (value == *expected && cas(pointer, value, desired)) return 1;
+ *expected = value;
+ return 0;
+}
+
+static void atomic_fetch_add(int *pointer, int addend)
+{+ int value;
+ do value = *pointer;
+ while (!cas(pointer, value, value + addend));
+}
+
+#endif
#ifndef moonfish_pthreads
--
⑨