]> git.proxmox.com Git - wasi-libc.git/blobdiff - libc-top-half/musl/include/unistd.h
Don't define `_POSIX_THREADS` unless threads are enabled. (#356)
[wasi-libc.git] / libc-top-half / musl / include / unistd.h
index 55ad0c8c0c924edd57390cdfce207c6121014315..0be83e368c4c87224fb8a9b39808e3f642c948a9 100644 (file)
@@ -15,12 +15,16 @@ extern "C" {
 #define SEEK_SET 0
 #define SEEK_CUR 1
 #define SEEK_END 2
+#define SEEK_DATA 3
+#define SEEK_HOLE 4
 #else
 #include <__header_unistd.h>
 #endif
 
 #ifdef __wasilibc_unmodified_upstream /* Use the compiler's definition of NULL */
-#ifdef __cplusplus
+#if __cplusplus >= 201103L
+#define NULL nullptr
+#elif defined(__cplusplus)
 #define NULL 0L
 #else
 #define NULL ((void*)0)
@@ -53,6 +57,34 @@ int dup2(int, int);
 int dup3(int, int, int);
 #endif
 off_t lseek(int, off_t, int);
+#ifdef __wasilibc_unmodified_upstream /* Optimize the readonly case of lseek */
+#else
+off_t __wasilibc_tell(int);
+
+#ifndef __cplusplus
+/*
+ * Optimize lseek in the case where it's just returning the current offset.
+ * This avoids importing `__wasi_fd_seek` altogether in many common cases.
+ *
+ * But don't do this for C++ because a simple macro wouldn't handle namespaces
+ * correctly:
+ *  - User code could qualify the `lseek` call with `::`.
+ *  - There may be another `lseek` in scope from a `using` declaration.
+ */
+#define lseek(fd, offset, whence)      \
+  ({                                   \
+     off_t __f = (fd);                 \
+     off_t __o = (offset);             \
+     off_t __w = (whence);             \
+     __builtin_constant_p((offset)) && \
+     __builtin_constant_p((whence)) && \
+     __o == 0 &&                       \
+     __w == SEEK_CUR                   \
+     ? __wasilibc_tell(__f)            \
+     : lseek(__f, __o, __w);           \
+  })
+#endif
+#endif
 int fsync(int);
 int fdatasync(int);
 
@@ -90,20 +122,23 @@ int ftruncate(int, off_t);
 int access(const char *, int);
 int faccessat(int, const char *, int, int);
 
-#ifdef __wasilibc_unmodified_upstream /* WASI has no cwd */
-int chdir(const char *);
+#ifdef __wasilibc_unmodified_upstream /* WASI has no fchdir */
 int fchdir(int);
-char *getcwd(char *, size_t);
 #endif
+int chdir(const char *);
+char *getcwd(char *, size_t);
 
 #ifdef __wasilibc_unmodified_upstream /* WASI has no signals */
 unsigned alarm(unsigned);
 #endif
 unsigned sleep(unsigned);
+#ifdef __wasilibc_unmodified_upstream /* WASI has no pause */
 int pause(void);
+#endif
 
 #ifdef __wasilibc_unmodified_upstream /* WASI has no fork/exec */
 pid_t fork(void);
+pid_t _Fork(void);
 int execve(const char *, char *const [], char *const []);
 int execv(const char *, char *const []);
 int execle(const char *, const char *, ...);
@@ -114,8 +149,17 @@ int fexecve(int, char *const [], char *const []);
 #endif
 _Noreturn void _exit(int);
 
-#ifdef __wasilibc_unmodified_upstream /* WASI has no getpid etc. */
+#if defined(__wasilibc_unmodified_upstream) || defined(_WASI_EMULATED_GETPID)
 pid_t getpid(void);
+#else
+__attribute__((__deprecated__(
+"WASI lacks process identifiers; to enable emulation of the `getpid` function using "
+"a placeholder value, which doesn't reflect the host PID of the program, "
+"compile with -D_WASI_EMULATED_GETPID and link with -lwasi-emulated-getpid"
+)))
+pid_t getpid(void);
+#endif
+#ifdef __wasilibc_unmodified_upstream /* WASI has no getpid etc. */
 pid_t getppid(void);
 pid_t getpgrp(void);
 pid_t getpgid(pid_t);
@@ -200,7 +244,9 @@ void *sbrk(intptr_t);
 pid_t vfork(void);
 int vhangup(void);
 int chroot(const char *);
+#endif
 int getpagesize(void);
+#ifdef __wasilibc_unmodified_upstream /* WASI has no processes */
 int getdtablesize(void);
 int sethostname(const char *, size_t);
 int getdomainname(char *, size_t);
@@ -217,6 +263,7 @@ int execvpe(const char *, char *const [], char *const []);
 int issetugid(void);
 #endif
 int getentropy(void *, size_t);
+extern int optreset;
 #endif
 
 #ifdef _GNU_SOURCE
@@ -236,6 +283,8 @@ int syncfs(int);
 #ifdef __wasilibc_unmodified_upstream /* WASI has no eaccess */
 int euidaccess(const char *, int);
 int eaccess(const char *, int);
+ssize_t copy_file_range(int, off_t *, int, off_t *, size_t, unsigned);
+pid_t gettid(void);
 #endif
 #endif
 
@@ -287,7 +336,9 @@ int eaccess(const char *, int);
 #endif
 #define _POSIX_VDISABLE         0
 
+#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
 #define _POSIX_THREADS          _POSIX_VERSION
+#endif
 #define _POSIX_THREAD_PROCESS_SHARED _POSIX_VERSION
 #define _POSIX_THREAD_SAFE_FUNCTIONS _POSIX_VERSION
 #define _POSIX_THREAD_ATTR_STACKADDR _POSIX_VERSION