Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WASI: Replace WASIX with configure checks and pthread stubs #95145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions Include/cpython/pthread_stubs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#ifndef Py_PTRHEAD_STUBS_H
#define Py_PTRHEAD_STUBS_H

#ifdef HAVE_PTHREAD_STUBS

// WASI's bits/alltypes.h provides type definitions when __NEED_ is set.
// The file can be included multiple times.
#ifdef __wasi__
# define __NEED_pthread_cond_t 1
# define __NEED_pthread_condattr_t 1
# define __NEED_pthread_mutex_t 1
# define __NEED_pthread_mutexattr_t 1
# define __NEED_pthread_key_t 1
# define __NEED_pthread_t 1
# define __NEED_pthread_attr_t 1
# include <bits/alltypes.h>
#else
# error "HAVE_PTHREAD_STUBS not defined for this platform"
#endif

// mutex
static inline int
pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr) {
return 0;
}

static inline int
pthread_mutex_destroy(pthread_mutex_t *mutex) {
return 0;
}

static inline int
pthread_mutex_trylock(pthread_mutex_t *mutex) {
return 0;
}

static inline int
pthread_mutex_lock(pthread_mutex_t *mutex) {
return 0;
}

static inline int
pthread_mutex_unlock(pthread_mutex_t *mutex) {
return 0;
}

// condition
static inline int
pthread_condattr_init(pthread_condattr_t *attr) {
return 0;
}

static inline int
pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr) {
return 0;
}

static inline int
pthread_cond_destroy(pthread_cond_t *cond) {
return 0;
}

static inline int
pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id) {
return 0;
}

static inline int
pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex) {
return 0;
}

static inline int
pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime) {
return 0;
}

static inline int
pthread_cond_signal(pthread_cond_t *cond) {
return 0;
}

// pthread attr
static inline int
pthread_attr_init(pthread_attr_t *attr)
{
return 0;
}

static inline int
pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
{
return 0;
}

static inline int
pthread_attr_destroy(pthread_attr_t *attr)
{
return 0;
}

// pthread

static inline int
pthread_create(pthread_t *restrict thread, const pthread_attr_t *restrict attr, void *(*start_routine)(void *), void *restrict arg)
{
errno = EAGAIN;
return -1;
}

static inline int
pthread_detach(pthread_t thread)
{
errno = ESRCH;
return -1;
}

static inline pthread_t
pthread_self(void)
{
return (pthread_t)1;
}

static inline _Noreturn void
pthread_exit(void *retval)
{
exit(0);
}

// key, implemented in thread_pthread.h
int pthread_key_create(pthread_key_t *out, void (*destructor)(void *a));
int pthread_key_delete(pthread_key_t key);
void* pthread_getspecific(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *value);

#endif // HAVE_PTHREAD_STUBS

#endif // Py_PTRHEAD_STUBS_H
3 changes: 3 additions & 0 deletions Include/cpython/pythread.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ PyAPI_FUNC(int) _PyThread_at_fork_reinit(PyThread_type_lock *lock);
but hardcode the unsigned long to avoid errors for include directive.
*/
# define NATIVE_TSS_KEY_T unsigned long
#elif defined(HAVE_PTHREAD_STUBS)
# include "cpython/pthread_stubs.h"
# define NATIVE_TSS_KEY_T pthread_key_t
#else
# error "Require native threads. See https://bugs.python.org/issue31370"
#endif
Expand Down
4 changes: 4 additions & 0 deletions Include/internal/pycore_condvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@

#ifdef HAVE_PTHREAD_H
# include <pthread.h>
#elif defined(HAVE_PTHREAD_STUBS)
# include "cpython/pthread_stubs.h"
#endif


#define PyMUTEX_T pthread_mutex_t
#define PyCOND_T pthread_cond_t


#elif defined(NT_THREADS)
/*
* Windows (XP, 2003 server and later, as well as (hopefully) CE) support
Expand Down
1 change: 0 additions & 1 deletion Lib/asyncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
'Python {remove}. The recommended replacement is asyncio')
warnings._deprecated(__name__, _DEPRECATION_MSG, remove=(3, 12))


_DISCONNECTED = frozenset({ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE,
EBADF})

Expand Down
19 changes: 14 additions & 5 deletions Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@
import select
import selectors

# WASI does not provide these function.
def _notimplemented(*args):
raise NotImplementedError

_waitstatus_to_exitcode = getattr(os, "waitstatus_to_exitcode", _notimplemented)
_waitpid = getattr(os, "waitpid", _notimplemented)
_WIFSTOPPED = getattr(os, "WIFSTOPPED", _notimplemented)
_WSTOPSIG = getattr(os, "WSTOPSIG", _notimplemented)
_WNOHANG = getattr(os, "WNOHANG", None)

# Exception classes used by this module.
class SubprocessError(Exception): pass
Expand Down Expand Up @@ -1890,9 +1899,9 @@ def _execute_child(self, args, executable, preexec_fn, close_fds,


def _handle_exitstatus(self, sts,
waitstatus_to_exitcode=os.waitstatus_to_exitcode,
_WIFSTOPPED=os.WIFSTOPPED,
_WSTOPSIG=os.WSTOPSIG):
waitstatus_to_exitcode=_waitstatus_to_exitcode,
_WIFSTOPPED=_WIFSTOPPED,
_WSTOPSIG=_WSTOPSIG):
"""All callers to this function MUST hold self._waitpid_lock."""
# This method is called (indirectly) by __del__, so it cannot
# refer to anything outside of its local scope.
Expand All @@ -1901,8 +1910,8 @@ def _handle_exitstatus(self, sts,
else:
self.returncode = waitstatus_to_exitcode(sts)

def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
_WNOHANG=os.WNOHANG, _ECHILD=errno.ECHILD):
def _internal_poll(self, _deadstate=None, _waitpid=_waitpid,
_WNOHANG=_WNOHANG, _ECHILD=errno.ECHILD):
"""Check if child process has terminated. Returns returncode
attribute.

Expand Down
2 changes: 1 addition & 1 deletion Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def _arp_getnode():
import os, socket
try:
ip_addr = socket.gethostbyname(socket.gethostname())
except OSError:
except (OSError, AttributeError):
return None

# Try getting the MAC addr from arp based on our IP address (Solaris).
Expand Down
1 change: 1 addition & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/cpython/objimpl.h \
$(srcdir)/Include/cpython/odictobject.h \
$(srcdir)/Include/cpython/picklebufobject.h \
$(srcdir)/Include/cpython/pthread_stubs.h \
$(srcdir)/Include/cpython/pyctype.h \
$(srcdir)/Include/cpython/pydebug.h \
$(srcdir)/Include/cpython/pyerrors.h \
Expand Down
25 changes: 25 additions & 0 deletions Modules/addrinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,31 @@ struct sockaddr_storage {
};
#endif /* !HAVE_SOCKADDR_STORAGE */

#ifndef HAVE_NETDB_H
static int h_errno = 0;

#define HOST_NOT_FOUND 1
#define TRY_AGAIN 2
#define NO_RECOVERY 3
#define NO_DATA 4

struct hostent {
char *h_name;
char **h_aliases;
int h_addrtype;
int h_length;
char **h_addr_list;
};

struct servent {
char *s_name;
char **s_aliases;
int s_port;
char *s_proto;
};

#endif // HAVE_NETDB_H

#ifdef __cplusplus
extern "C" {
#endif
Expand Down
14 changes: 11 additions & 3 deletions Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Modules/errnomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ errno_exec(PyObject *module)
#ifdef ENOANO
add_errcode("ENOANO", ENOANO, "No anode");
#endif
#if defined(__wasi__) && !defined(ESHUTDOWN)
// WASI SDK 16 does not have ESHUTDOWN, shutdown results in EPIPE.
#define ESHUTDOWN EPIPE
#endif
#ifdef ESHUTDOWN
add_errcode("ESHUTDOWN", ESHUTDOWN, "Cannot send after transport endpoint shutdown");
#else
Expand Down
5 changes: 3 additions & 2 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -7373,7 +7373,7 @@ os_openpty_impl(PyObject *module)
#define HAVE_FALLBACK_LOGIN_TTY 1
#endif /* defined(HAVE_SETSID) && defined(TIOCSCTTY) */

#if defined(HAVE_LOGIN_TTY) || defined(HAVE_FALLBACK_LOGIN_TTY)
#if (defined(HAVE_LOGIN_TTY) || defined(HAVE_FALLBACK_LOGIN_TTY)) && defined(HAVE_DUP2)
/*[clinic input]
os.login_tty

Expand Down Expand Up @@ -9316,7 +9316,7 @@ os_dup_impl(PyObject *module, int fd)
return _Py_dup(fd);
}


#if defined(HAVE_DUP2) || defined(HAVE_DUP3)
/*[clinic input]
os.dup2 -> int
fd: int
Expand Down Expand Up @@ -9416,6 +9416,7 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)

return res;
}
#endif


#ifdef HAVE_LOCKF
Expand Down
4 changes: 4 additions & 0 deletions Modules/selectmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ extern void bzero(void *, int);
# define SOCKET int
#endif

#if defined(__wasi__) && !defined(POLLPRI)
# define POLLPRI 0
#endif

typedef struct {
PyObject *close;
PyTypeObject *poll_Type;
Expand Down
Loading