aboutsummaryrefslogtreecommitdiff
path: root/tools/500_pause_resume_stop
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-05-23 16:48:58 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-05-23 16:54:59 +0200
commitc6a77f30f64be73a5e55e06336975f2ecfbb2324 (patch)
tree3c243fbee268b824f18c5dc4d5bff9a9413b9407 /tools/500_pause_resume_stop
parent62d87e063318cc4c842b1b2d8c184f48aeaf6659 (diff)
downloadnissy-core-c6a77f30f64be73a5e55e06336975f2ecfbb2324.tar.gz
nissy-core-c6a77f30f64be73a5e55e06336975f2ecfbb2324.zip
Do all loggin in main thread
Before this committ, the solver (via the generic solution-appender routines in src/solve/solutions.h) and the H48 data generator did some logging in the worker threads, without using any locks. This was not nice, but in practice it did not cause any problem, because the log messages were rare. However, this turned out to be a problem when building to WASM, because web workers do not have access to the main JS memory, and therefore they cannot call functions from the main JS. This includes not only the callback functions for logging, but also those for polling the status of the solver (run / pause / stop). This commit fixes this at the cost or being somewhat inelegant: the solutions are not logged as they are found, but only every 500ms.
Diffstat (limited to 'tools/500_pause_resume_stop')
-rw-r--r--tools/500_pause_resume_stop/pause_resume_stop.c89
1 files changed, 89 insertions, 0 deletions
diff --git a/tools/500_pause_resume_stop/pause_resume_stop.c b/tools/500_pause_resume_stop/pause_resume_stop.c
new file mode 100644
index 0000000..ae3b67f
--- /dev/null
+++ b/tools/500_pause_resume_stop/pause_resume_stop.c
@@ -0,0 +1,89 @@
1#include "../tool.h"
2
3/*
4This tool starts solving a scramble. It then runs for SOLVE_LEN seconds,
5pauses for PAUSE_LEN seconds, runs again for SOLVE_LEN seconds and so on,
6until eventually stopping after TOTAL_LEN seconds.
7*/
8
9#define SOL_BUFFER_LEN 10000
10
11#define SOLVE_LEN 3000
12#define PAUSE_LEN 1500
13#define TOTAL_LEN 30000
14
15char *scramble =
16 "R' U' F D2 L2 F R2 U2 R2 B D2 L B2 D' B2 L' R' B D2 B U2 L U2 R' U' F";
17
18char *solver;
19int64_t size = 0;
20unsigned char *buf;
21
22int get_status(void *arg) {
23 struct timespec start, now;
24 double tdsec, tdnano;
25 int tdiff;
26
27 start = *(struct timespec *)arg;
28 clock_gettime(CLOCK_MONOTONIC, &now);
29
30 tdsec = now.tv_sec - start.tv_sec;
31 tdnano = now.tv_nsec - start.tv_nsec;
32 tdiff = 1000 * (tdsec + 1e-9 * tdnano);
33
34 fprintf(stderr, "[tool] Status polled at %dms: ", tdiff);
35
36 if (tdiff > TOTAL_LEN) {
37 fprintf(stderr, "Total time elapsed, stopping\n");
38 return NISSY_STATUS_STOP;
39 }
40
41 if (tdiff % (SOLVE_LEN + PAUSE_LEN) < SOLVE_LEN) {
42 fprintf(stderr, "Running\n");
43 return NISSY_STATUS_RUN;
44 }
45
46 fprintf(stderr, "Pausing\n");
47 return NISSY_STATUS_PAUSE;
48}
49
50void run(void) {
51 int64_t n;
52 long long stats[NISSY_SIZE_SOLVE_STATS];
53 char sol[SOL_BUFFER_LEN], cube[NISSY_SIZE_CUBE];
54 struct timespec starttime;
55
56 nissy_applymoves(NISSY_SOLVED_CUBE, scramble, cube);
57 clock_gettime(CLOCK_MONOTONIC, &starttime);
58 n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL,
59 0, 20, 100, -1, 0, size, buf, SOL_BUFFER_LEN, sol, stats,
60 get_status, &starttime);
61 if (n == 0)
62 printf("No solution found\n");
63 else
64 printf("Solutions:\n%s\n", sol);
65}
66
67int main(int argc, char **argv) {
68 char filename[255], dataid[NISSY_SIZE_DATAID];
69
70 if (argc < 2) {
71 printf("Error: not enough arguments. "
72 "A solver and must be given.\n"); return 1;
73 }
74
75 solver = argv[1];
76 srand(time(NULL));
77 nissy_setlogger(log_stderr, NULL);
78
79 sprintf(filename, "tables/%s", solver);
80 if (getdata(solver, &buf, filename) != 0)
81 return 1;
82
83 size = nissy_solverinfo(solver, dataid);
84
85 timerun(run);
86
87 free(buf);
88 return 0;
89}

Generated with cgit - Back to sebastiano.tronto.net