From c6a77f30f64be73a5e55e06336975f2ecfbb2324 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Fri, 23 May 2025 16:48:58 +0200 Subject: 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. --- tools/500_pause_resume_stop/pause_resume_stop.c | 89 +++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tools/500_pause_resume_stop/pause_resume_stop.c (limited to 'tools/500_pause_resume_stop') 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 @@ +#include "../tool.h" + +/* +This tool starts solving a scramble. It then runs for SOLVE_LEN seconds, +pauses for PAUSE_LEN seconds, runs again for SOLVE_LEN seconds and so on, +until eventually stopping after TOTAL_LEN seconds. +*/ + +#define SOL_BUFFER_LEN 10000 + +#define SOLVE_LEN 3000 +#define PAUSE_LEN 1500 +#define TOTAL_LEN 30000 + +char *scramble = + "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"; + +char *solver; +int64_t size = 0; +unsigned char *buf; + +int get_status(void *arg) { + struct timespec start, now; + double tdsec, tdnano; + int tdiff; + + start = *(struct timespec *)arg; + clock_gettime(CLOCK_MONOTONIC, &now); + + tdsec = now.tv_sec - start.tv_sec; + tdnano = now.tv_nsec - start.tv_nsec; + tdiff = 1000 * (tdsec + 1e-9 * tdnano); + + fprintf(stderr, "[tool] Status polled at %dms: ", tdiff); + + if (tdiff > TOTAL_LEN) { + fprintf(stderr, "Total time elapsed, stopping\n"); + return NISSY_STATUS_STOP; + } + + if (tdiff % (SOLVE_LEN + PAUSE_LEN) < SOLVE_LEN) { + fprintf(stderr, "Running\n"); + return NISSY_STATUS_RUN; + } + + fprintf(stderr, "Pausing\n"); + return NISSY_STATUS_PAUSE; +} + +void run(void) { + int64_t n; + long long stats[NISSY_SIZE_SOLVE_STATS]; + char sol[SOL_BUFFER_LEN], cube[NISSY_SIZE_CUBE]; + struct timespec starttime; + + nissy_applymoves(NISSY_SOLVED_CUBE, scramble, cube); + clock_gettime(CLOCK_MONOTONIC, &starttime); + n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL, + 0, 20, 100, -1, 0, size, buf, SOL_BUFFER_LEN, sol, stats, + get_status, &starttime); + if (n == 0) + printf("No solution found\n"); + else + printf("Solutions:\n%s\n", sol); +} + +int main(int argc, char **argv) { + char filename[255], dataid[NISSY_SIZE_DATAID]; + + if (argc < 2) { + printf("Error: not enough arguments. " + "A solver and must be given.\n"); return 1; + } + + solver = argv[1]; + srand(time(NULL)); + nissy_setlogger(log_stderr, NULL); + + sprintf(filename, "tables/%s", solver); + if (getdata(solver, &buf, filename) != 0) + return 1; + + size = nissy_solverinfo(solver, dataid); + + timerun(run); + + free(buf); + return 0; +} -- cgit v1.3