Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
% Copyright (c) 2000 The PARI Group1%2% This file is part of the PARI/GP documentation3%4% Permission is granted to copy, distribute and/or modify this document5% under the terms of the GNU General Public License6\appendix{PARI and threads}78To use PARI in multi-threaded programs, you must configure it using9\kbd{Configure --enable-tls}. Your system must implement the \kbd{\_\_thread}10storage class. As a major side effect, this breaks the \kbd{libpari} ABI: the11resulting library is not compatible with the old one, and \kbd{-tls} is12appended to the PARI library \kbd{soname}. On the other hand, this library is13now thread-safe.1415PARI provides some functions to set up PARI subthreads\sidx{threads}. In our16model, each concurrent thread needs its own PARI stack. The following scheme17is used:1819\noindent Child thread:20\bprog21void *child_thread(void *arg)22{23GEN data = pari_thread_start((struct pari_thread*)arg);24GEN result = ...; /* Compute result from data */25pari_thread_close();26return (void*)result;27}28@eprog29\noindent Parent thread:30\bprog31pthread_t th;32struct pari_thread pth;33GEN data, result;3435pari_thread_alloc(&pth, s, data);36pthread_create(&th, NULL, &child_thread, (void*)&pth); /* start child */37... /* do stuff in parent */38pthread_join(th, (void*)&result); /* wait until child terminates */39result = gcopy(result); /* copy result from thread stack to main stack */40pari_thread_free(&pth); /* ... and clean up */41@eprog4243\fun{void}{pari_thread_valloc}{struct pari_thread *pth, size_t s, size_t v, GEN arg}44Allocate a PARI stack of size \kbd{s} which can grow to at most \kbd{v} (as45with \kbd{parisize} and \kbd{parisizemax}) and associate it, together with the46argument \kbd{arg}, with the PARI thread data \kbd{pth}.4748\fun{void}{pari_thread_alloc}{struct pari_thread *pth, size_t s, GEN arg}49As above but the stack cannot grow beyond \kbd{s}.5051\fun{void}{pari_thread_free}{struct pari_thread *pth}52Free the PARI stack attached to the PARI thread data \kbd{pth}. This53is called after the child thread terminates, i.e.~after54\tet{pthread_join} in the parent. Any \kbd{GEN} objects returned by the55child in the thread stack need to be saved before running this command.5657\fun{void}{pari_thread_init}{void}58Initialize the thread-local PARI data structures. This function is called by59\kbd{pari\_thread\_start}.6061\fun{GEN}{pari_thread_start}{struct pari_thread *t}62Initialize the thread-local PARI data structures and set up the thread stack63using the PARI thread data \kbd{pth}. This function returns the thread64argument \kbd{arg} that was given to \kbd{pari\_thread\_alloc}.6566\fun{void}{pari_thread_close}{void}67Free the thread-local PARI data structures, but keeping the thread stack, so68that a \kbd{GEN} returned by the thread remains valid.6970\noindent Under this model, some PARI states are reset in new threads. In71particular7273\item the random number generator is reset to the starting seed;7475\item the system stack exhaustion checking code, meant to catch infinite76recursions, is disabled (use \kbd{pari\_stackcheck\_init()} to reenable it);7778\item cached real constants (returned by \kbd{mppi}, \kbd{mpeuler} and79\kbd{mplog2}) are not shared between threads and will be recomputed as80needed;8182\noindent The following sample program can be compiled using83\bprog84cc thread.c -o thread.o -lpari -lpthread85@eprog\noindent86(Add \kbd{-I/-L} paths as necessary.)8788\noindent\bprogfile{../examples/thread.c}8990\vfill\eject919293