Make improvements

- Document redbean's argon2 module
- Fix regressions in cthreads library
- Make testlib work better with threads
- Give the cthreads library lots of love
- Remove some of the stdio assembly code
- Implement getloadavg() across platforms
- Code size optimizations for errnos, etc.
- Only check for signals in main thread on Windows
- Make errnos for dup2 / dup3 consistent with posix

This change also fixes a bug in the argon2 module, where the NUL
terminator was being included in the hash encoded ascii string. This
shouldn't require any database migrations to folks who found this module
and productionized it, since the argon2 library treats it as a c string.
This commit is contained in:
Justine Tunney 2022-05-27 13:25:46 -07:00
parent cb67223051
commit de5de19004
234 changed files with 1728 additions and 1993 deletions

View file

@ -16,38 +16,61 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/atomic.h"
#include "libc/calls/calls.h"
#include "libc/calls/strace.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/spinlock.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/futex.h"
#include "libc/sysv/consts/nr.h"
#include "libc/thread/descriptor.h"
#include "libc/thread/join.h"
int cthread_join(cthread_t td, int* rc) {
int tid = td->tid; // tid must be loaded before lock xadd
// otherwise, tid could be set to 0 even though `state` is not finished
// mark thread as joining
int state;
asm volatile("lock xadd\t%1, %0"
: "+m"(td->state), "=r"(state)
: "1"(cthread_joining)
: "cc");
if (!(state & cthread_finished)) {
int ax;
int flags = FUTEX_WAIT; // PRIVATE makes it hang
struct timespec* timeout = NULL;
asm volatile("mov\t%5,%%r10\n\t" // timeout
"syscall"
: "=a"(ax)
: "0"(__NR_futex), "D"(&td->tid), "S"(flags), "d"(tid),
"g"(timeout)
: "rcx", "r10", "r11", "cc", "memory");
/**
* Waits for thread to terminate and frees its memory.
*
* @param td is thread descriptor memory
* @param exitcode optionally receives value returned by thread
* @return 0 on success, or error number on failure
* @raises EDEADLK when trying to join this thread
* @raises EINVAL if another thread is joining
* @raises ESRCH if no such thread exists
* @raises EINVAL if not joinable
* @threadsafe
*/
int cthread_join(cthread_t td, void **exitcode) {
int rc, tid;
// otherwise, tid could be set to 0 even though `state` is not
// finished mark thread as joining
if (!td || (IsAsan() && !__asan_is_valid(td, sizeof(*td)))) {
rc = ESRCH;
tid = -1;
} else if ((tid = td->tid) == gettid()) { // tid must load before lock xadd
rc = EDEADLK;
} else if (atomic_load(&td->state) & (cthread_detached | cthread_joining)) {
rc = EINVAL;
} else {
if (~atomic_fetch_add(&td->state, cthread_joining) & cthread_finished) {
if (IsLinux() || IsOpenbsd()) {
// FUTEX_WAIT_PRIVATE makes it hang
futex((uint32_t *)&td->tid, FUTEX_WAIT, tid, 0, 0);
}
_spinlock(&td->tid);
}
if (exitcode) {
*exitcode = td->exitcode;
}
if (!munmap(td->alloc.bottom, td->alloc.top - td->alloc.bottom)) {
rc = 0;
} else {
rc = errno;
}
}
*rc = td->rc;
size_t size = (intptr_t)(td->alloc.top) - (intptr_t)(td->alloc.bottom);
munmap(td->alloc.bottom, size);
return 0;
STRACE("cthread_join(%d, [%p]) → %s", tid, !rc && exitcode ? *exitcode : 0,
!rc ? "0" : strerrno(rc));
return rc;
}