mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-01 03:53:33 +00:00
60cb435cb4
If threads are being used, then fork() will now acquire and release and runtime locks so that fork() may be safely used from threads. This also makes vfork() thread safe, because pthread mutexes will do nothing when the process is a child of vfork(). More torture tests have been written to confirm this all works like a charm. Additionally: - Invent hexpcpy() api - Rename nsync_malloc_() to kmalloc() - Complete posix named semaphore implementation - Make pthread_create() asynchronous signal safe - Add rm, rmdir, and touch to command interpreter builtins - Invent sigisprecious() and modify sigset functions to use it - Add unit tests for posix_spawn() attributes and fix its bugs One unresolved problem is the reclaiming of *NSYNC waiter memory in the forked child processes, within apps which have threads waiting on locks
104 lines
4.2 KiB
C
104 lines
4.2 KiB
C
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
│ │
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
|
│ │
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
#include "libc/assert.h"
|
|
#include "libc/calls/state.internal.h"
|
|
#include "libc/calls/struct/itimerval.internal.h"
|
|
#include "libc/calls/struct/timeval.h"
|
|
#include "libc/calls/struct/timeval.internal.h"
|
|
#include "libc/calls/syscall_support-sysv.internal.h"
|
|
#include "libc/dce.h"
|
|
#include "libc/intrin/asan.internal.h"
|
|
#include "libc/intrin/describeflags.internal.h"
|
|
#include "libc/intrin/strace.internal.h"
|
|
#include "libc/sysv/errfuns.h"
|
|
#include "libc/thread/tls.h"
|
|
#include "libc/time/struct/timezone.h"
|
|
|
|
typedef axdx_t gettimeofday_f(struct timeval *, struct timezone *, void *);
|
|
|
|
extern gettimeofday_f *__gettimeofday;
|
|
|
|
/**
|
|
* Returns system wall time in microseconds, e.g.
|
|
*
|
|
* int64_t t;
|
|
* char p[30];
|
|
* struct tm tm;
|
|
* struct timeval tv;
|
|
* gettimeofday(&tv, 0);
|
|
* t = tv.tv_sec;
|
|
* gmtime_r(&t, &tm);
|
|
* FormatHttpDateTime(p, &tm);
|
|
* printf("%s\n", p);
|
|
*
|
|
* @param tv points to timeval that receives result if non-NULL
|
|
* @param tz receives UTC timezone if non-NULL
|
|
* @error EFAULT if `tv` or `tz` isn't valid memory
|
|
* @see clock_gettime() for nanosecond precision
|
|
* @see strftime() for string formatting
|
|
*/
|
|
int gettimeofday(struct timeval *tv, struct timezone *tz) {
|
|
int rc;
|
|
axdx_t ad;
|
|
if (IsAsan() && ((tv && !__asan_is_valid(tv, sizeof(*tv))) ||
|
|
(tz && !__asan_is_valid(tz, sizeof(*tz))))) {
|
|
rc = efault();
|
|
} else {
|
|
rc = __gettimeofday(tv, tz, 0).ax;
|
|
}
|
|
#if SYSDEBUG
|
|
if (__tls_enabled && !(__get_tls()->tib_flags & TIB_FLAG_TIME_CRITICAL)) {
|
|
STRACE("gettimeofday([%s], %p) → %d% m", DescribeTimeval(rc, tv), tz, rc);
|
|
}
|
|
#endif
|
|
return rc;
|
|
}
|
|
|
|
/**
|
|
* Returns pointer to fastest gettimeofday().
|
|
*/
|
|
gettimeofday_f *__gettimeofday_get(bool *opt_out_isfast) {
|
|
bool isfast;
|
|
gettimeofday_f *res;
|
|
if (IsLinux() && (res = __vdsosym("LINUX_2.6", "__vdso_gettimeofday"))) {
|
|
isfast = true;
|
|
} else if (IsWindows()) {
|
|
isfast = true;
|
|
res = sys_gettimeofday_nt;
|
|
} else if (IsXnu()) {
|
|
isfast = false;
|
|
res = sys_gettimeofday_xnu;
|
|
} else if (IsMetal()) {
|
|
isfast = false;
|
|
res = sys_gettimeofday_metal;
|
|
} else {
|
|
isfast = false;
|
|
res = sys_gettimeofday;
|
|
}
|
|
if (opt_out_isfast) {
|
|
*opt_out_isfast = isfast;
|
|
}
|
|
return res;
|
|
}
|
|
|
|
hidden int __gettimeofday_init(struct timeval *tv, struct timezone *tz) {
|
|
gettimeofday_f *gettime;
|
|
__gettimeofday = gettime = __gettimeofday_get(0);
|
|
return gettime(tv, tz, 0).ax;
|
|
}
|