quick addition of cosmo pthreads to python.com

- enable WITH_THREAD and _POSIX_THREADS
- add headers everywhere
- breaks only two tests (faulthandler and signal)
- disabled terminal completion because it causes segfaults for some
  reason (probably could not get the current thread)
This commit is contained in:
ahgamut 2023-02-23 10:08:42 +05:30 committed by Justine Tunney
parent a808b3e738
commit 60eb34509b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
16 changed files with 166 additions and 34 deletions

View file

@ -520,31 +520,29 @@ def _module_repr_from_spec(spec):
else: else:
return '<module {!r} ({})>'.format(spec.name, spec.origin) return '<module {!r} ({})>'.format(spec.name, spec.origin)
# Used by importlib.reload() and _load_module_shim().
def _exec(spec, module): def _exec(spec, module):
"""Execute the spec's specified module in an existing module's namespace.""" """Execute the spec's specified module in an existing module's namespace."""
name = spec.name name = spec.name
if sys.modules.get(name) is not module: with _ModuleLockManager(name):
msg = 'module {!r} not in sys.modules'.format(name) if sys.modules.get(name) is not module:
raise ImportError(msg, name=name) msg = 'module {!r} not in sys.modules'.format(name)
if spec.loader is None: raise ImportError(msg, name=name)
if spec.submodule_search_locations is None: if spec.loader is None:
raise ImportError('missing loader', name=spec.name) if spec.submodule_search_locations is None:
# namespace package raise ImportError('missing loader', name=spec.name)
# namespace package
_init_module_attrs(spec, module, override=True)
return module
_init_module_attrs(spec, module, override=True) _init_module_attrs(spec, module, override=True)
return module if not hasattr(spec.loader, 'exec_module'):
_init_module_attrs(spec, module, override=True) # (issue19713) Once BuiltinImporter and ExtensionFileLoader
if not hasattr(spec.loader, 'exec_module'): # have exec_module() implemented, we can add a deprecation
# (issue19713) Once BuiltinImporter and ExtensionFileLoader # warning here.
# have exec_module() implemented, we can add a deprecation spec.loader.load_module(name)
# warning here. else:
spec.loader.load_module(name) spec.loader.exec_module(module)
else:
spec.loader.exec_module(module)
return sys.modules[name] return sys.modules[name]
def _load_backward_compatible(spec): def _load_backward_compatible(spec):
# (issue19713) Once BuiltinImporter and ExtensionFileLoader # (issue19713) Once BuiltinImporter and ExtensionFileLoader
# have exec_module() implemented, we can add a deprecation # have exec_module() implemented, we can add a deprecation
@ -606,8 +604,8 @@ def _load(spec):
clobbered. clobbered.
""" """
return _load_unlocked(spec) with _ModuleLockManager(spec.name):
return _load_unlocked(spec)
# Loaders ##################################################################### # Loaders #####################################################################

View file

@ -1388,6 +1388,7 @@ def _setup(_bootstrap_module):
"builtins", "builtins",
"marshal", "marshal",
"posix", "posix",
"_thread",
"_weakref", "_weakref",
): ):
self_mod_dict[name] = sys.modules.get( self_mod_dict[name] = sys.modules.get(
@ -1400,7 +1401,6 @@ def _setup(_bootstrap_module):
self_mod_dict["_os"] = sys.modules.get(builtin_os, builtin_from_name(builtin_os)) self_mod_dict["_os"] = sys.modules.get(builtin_os, builtin_from_name(builtin_os))
self_mod_dict["path_sep"] = path_separators[0] self_mod_dict["path_sep"] = path_separators[0]
self_mod_dict["path_separators"] = "".join(path_separators) self_mod_dict["path_separators"] = "".join(path_separators)
self_mod_dict["_thread"] = None
# Constants # Constants
EXTENSION_SUFFIXES.extend(_imp.extension_suffixes()) EXTENSION_SUFFIXES.extend(_imp.extension_suffixes())

View file

@ -1323,7 +1323,7 @@ def enumerate():
return list(_active.values()) + list(_limbo.values()) return list(_active.values()) + list(_limbo.values())
from _dummy_thread import stack_size from _thread import stack_size
# Create the main thread object, # Create the main thread object,
# and make it available for the interpreter # and make it available for the interpreter
@ -1371,7 +1371,7 @@ def main_thread():
# module, or from the python fallback # module, or from the python fallback
try: try:
from _dummy_thread import _local as local from _thread import _local as local
except ImportError: except ImportError:
from _threading_local import local from _threading_local import local

View file

@ -15,6 +15,7 @@
#include "third_party/python/Include/pyerrors.h" #include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pymacro.h" #include "third_party/python/Include/pymacro.h"
#include "third_party/python/Include/pymem.h" #include "third_party/python/Include/pymem.h"
#include "third_party/python/Include/pythread.h"
#include "third_party/python/Include/structmember.h" #include "third_party/python/Include/structmember.h"
#include "third_party/python/Include/yoink.h" #include "third_party/python/Include/yoink.h"
/* clang-format off */ /* clang-format off */

View file

@ -9,6 +9,7 @@
#include "third_party/python/Include/abstract.h" #include "third_party/python/Include/abstract.h"
#include "third_party/python/Include/boolobject.h" #include "third_party/python/Include/boolobject.h"
#include "third_party/python/Include/bytesobject.h" #include "third_party/python/Include/bytesobject.h"
#include "third_party/python/Include/ceval.h"
#include "third_party/python/Include/descrobject.h" #include "third_party/python/Include/descrobject.h"
#include "third_party/python/Include/longobject.h" #include "third_party/python/Include/longobject.h"
#include "third_party/python/Include/memoryobject.h" #include "third_party/python/Include/memoryobject.h"
@ -16,6 +17,7 @@
#include "third_party/python/Include/object.h" #include "third_party/python/Include/object.h"
#include "third_party/python/Include/objimpl.h" #include "third_party/python/Include/objimpl.h"
#include "third_party/python/Include/pyerrors.h" #include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pylifecycle.h"
#include "third_party/python/Include/pymacro.h" #include "third_party/python/Include/pymacro.h"
#include "third_party/python/Include/pymem.h" #include "third_party/python/Include/pymem.h"
#include "third_party/python/Include/pythread.h" #include "third_party/python/Include/pythread.h"

View file

@ -4,8 +4,91 @@
Python 3 Python 3
https://docs.python.org/3/license.html │ https://docs.python.org/3/license.html │
*/ */
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/makedev.h"
#include "libc/calls/struct/dirent.h"
#include "libc/calls/struct/iovec.h"
#include "libc/calls/struct/rusage.h"
#include "libc/calls/struct/sched_param.h"
#include "libc/calls/struct/stat.macros.h"
#include "libc/calls/struct/statvfs.h"
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timeval.h"
#include "libc/calls/struct/tms.h"
#include "libc/calls/struct/utsname.h"
#include "libc/calls/struct/winsize.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/sysparam.h"
#include "libc/calls/termios.h"
#include "libc/calls/weirdtypes.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/log/log.h"
#include "libc/mem/alg.h"
#include "libc/mem/gc.internal.h"
#include "libc/nt/createfile.h"
#include "libc/nt/dll.h"
#include "libc/nt/enum/creationdisposition.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/sw.h"
#include "libc/nt/files.h"
#include "libc/nt/runtime.h"
#include "libc/runtime/dlfcn.h"
#include "libc/runtime/pathconf.h"
#include "libc/runtime/sysconf.h"
#include "libc/sock/sendfile.internal.h"
#include "libc/sock/sock.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/dt.h"
#include "libc/sysv/consts/ex.h"
#include "libc/sysv/consts/f.h"
#include "libc/sysv/consts/grnd.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/posix.h"
#include "libc/sysv/consts/prio.h"
#include "libc/sysv/consts/s.h"
#include "libc/sysv/consts/sched.h"
#include "libc/sysv/consts/seek.h"
#include "libc/sysv/consts/sf.h"
#include "libc/sysv/consts/sicode.h"
#include "libc/sysv/consts/st.h"
#include "libc/sysv/consts/w.h"
#include "libc/sysv/consts/waitid.h"
#include "libc/sysv/errfuns.h"
#include "libc/time/struct/utimbuf.h"
#include "libc/time/time.h"
#include "libc/x/x.h"
#include "third_party/musl/lockf.h"
#include "third_party/musl/passwd.h"
#include "third_party/python/Include/abstract.h"
#include "third_party/python/Include/boolobject.h"
#include "third_party/python/Include/ceval.h"
#include "third_party/python/Include/dictobject.h"
#include "third_party/python/Include/fileobject.h"
#include "third_party/python/Include/fileutils.h"
#include "third_party/python/Include/floatobject.h"
#include "third_party/python/Include/import.h"
#include "third_party/python/Include/intrcheck.h"
#include "third_party/python/Include/longobject.h"
#include "third_party/python/Include/modsupport.h"
#include "third_party/python/Include/objimpl.h"
#include "third_party/python/Include/osmodule.h"
#include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pylifecycle.h"
#include "third_party/python/Include/pymacro.h"
#include "third_party/python/Include/pymem.h"
#include "third_party/python/Include/pythread.h"
#include "third_party/python/Include/pytime.h"
#include "third_party/python/Include/structmember.h" #include "third_party/python/Include/structmember.h"
#include "third_party/python/Include/structseq.h"
#include "third_party/python/Include/sysmodule.h"
#include "third_party/python/Include/warnings.h"
#include "third_party/python/Include/weakrefobject.h"
#include "third_party/python/Include/yoink.h" #include "third_party/python/Include/yoink.h"
#include "third_party/python/pyconfig.h"
/* clang-format off */ /* clang-format off */
PYTHON_PROVIDE("_thread"); PYTHON_PROVIDE("_thread");
@ -1029,7 +1112,7 @@ t_bootstrap(void *boot_raw)
else { else {
PyObject *file; PyObject *file;
PyObject *exc, *value, *tb; PyObject *exc, *value, *tb;
PySys_WriteStderr( PySys_FormatStderr(
"Unhandled exception in thread started by "); "Unhandled exception in thread started by ");
PyErr_Fetch(&exc, &value, &tb); PyErr_Fetch(&exc, &value, &tb);
file = _PySys_GetObjectId(&PyId_stderr); file = _PySys_GetObjectId(&PyId_stderr);
@ -1037,7 +1120,7 @@ t_bootstrap(void *boot_raw)
PyFile_WriteObject(boot->func, file, 0); PyFile_WriteObject(boot->func, file, 0);
else else
PyObject_Print(boot->func, stderr, 0); PyObject_Print(boot->func, stderr, 0);
PySys_WriteStderr("\n"); PySys_FormatStderr("\n");
PyErr_Restore(exc, value, tb); PyErr_Restore(exc, value, tb);
PyErr_PrintEx(0); PyErr_PrintEx(0);
} }
@ -1436,3 +1519,8 @@ PyInit__thread(void)
PyThread_init_thread(); PyThread_init_thread();
return m; return m;
} }
_Section(".rodata.pytab.1") const struct _inittab _PyImport_Inittab__thread = {
"_thread",
PyInit__thread,
};

View file

@ -50,6 +50,7 @@
#include "third_party/python/Include/pyerrors.h" #include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pymacro.h" #include "third_party/python/Include/pymacro.h"
#include "third_party/python/Include/pymem.h" #include "third_party/python/Include/pymem.h"
#include "third_party/python/Include/pythread.h"
#include "third_party/python/Include/structmember.h" #include "third_party/python/Include/structmember.h"
#include "third_party/python/Include/tupleobject.h" #include "third_party/python/Include/tupleobject.h"
#include "third_party/python/Include/warnings.h" #include "third_party/python/Include/warnings.h"

View file

@ -5,6 +5,7 @@
https://docs.python.org/3/license.html │ https://docs.python.org/3/license.html │
*/ */
#define PY_LOCAL_AGGRESSIVE #define PY_LOCAL_AGGRESSIVE
#include "libc/errno.h"
#include "libc/intrin/likely.h" #include "libc/intrin/likely.h"
#include "third_party/python/Include/abstract.h" #include "third_party/python/Include/abstract.h"
#include "third_party/python/Include/boolobject.h" #include "third_party/python/Include/boolobject.h"

View file

@ -4,6 +4,10 @@
Python 3 Python 3
https://docs.python.org/3/license.html https://docs.python.org/3/license.html
╚─────────────────────────────────────────────────────────────────────────────*/ ╚─────────────────────────────────────────────────────────────────────────────*/
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timeval.h"
#include "libc/thread/thread2.h"
#include "third_party/python/Include/pylifecycle.h"
#include "third_party/python/Python/condvar.h" #include "third_party/python/Python/condvar.h"
/* clang-format off */ /* clang-format off */

View file

@ -32,6 +32,12 @@
Locking: as above. Locking: as above.
*/ */
#ifdef WITH_THREAD
extern void wait_for_thread_shutdown(void);
extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
extern void _PyGILState_Fini(void);
#endif /* WITH_THREAD */
int int
Py_FinalizeEx(void) Py_FinalizeEx(void)
{ {

View file

@ -19,6 +19,7 @@
#include "third_party/python/Include/abstract.h" #include "third_party/python/Include/abstract.h"
#include "third_party/python/Include/ast.h" #include "third_party/python/Include/ast.h"
#include "third_party/python/Include/boolobject.h" #include "third_party/python/Include/boolobject.h"
#include "third_party/python/Include/ceval.h"
#include "third_party/python/Include/code.h" #include "third_party/python/Include/code.h"
#include "third_party/python/Include/codecs.h" #include "third_party/python/Include/codecs.h"
#include "third_party/python/Include/cosmo.h" #include "third_party/python/Include/cosmo.h"
@ -58,7 +59,7 @@ _Py_IDENTIFIER(stdout);
_Py_IDENTIFIER(stderr); _Py_IDENTIFIER(stderr);
/* Forward */ /* Forward */
static void wait_for_thread_shutdown(void); void wait_for_thread_shutdown(void);
#ifdef WITH_THREAD #ifdef WITH_THREAD
extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *); extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);
extern void _PyGILState_Fini(void); extern void _PyGILState_Fini(void);
@ -470,7 +471,7 @@ Py_EndInterpreter(PyThreadState *tstate)
the threading module was imported in the first place. the threading module was imported in the first place.
The shutdown routine will wait until all non-daemon The shutdown routine will wait until all non-daemon
"threading" threads have completed. */ "threading" threads have completed. */
static void void
wait_for_thread_shutdown(void) wait_for_thread_shutdown(void)
{ {
#ifdef WITH_THREAD #ifdef WITH_THREAD

View file

@ -5,6 +5,11 @@
https://docs.python.org/3/license.html │ https://docs.python.org/3/license.html │
*/ */
#include "libc/assert.h" #include "libc/assert.h"
#include "libc/calls/struct/timespec.h"
#include "libc/calls/struct/timeval.h"
#include "libc/errno.h"
#include "libc/thread/thread2.h"
#include "third_party/python/Include/dynamic_annotations.h"
#include "third_party/python/Include/pyerrors.h" #include "third_party/python/Include/pyerrors.h"
#include "third_party/python/Include/pymacro.h" #include "third_party/python/Include/pymacro.h"
#include "third_party/python/Include/pymem.h" #include "third_party/python/Include/pymem.h"
@ -77,18 +82,18 @@ static size_t _pythread_stacksize = 0;
#ifdef _POSIX_THREADS #ifdef _POSIX_THREADS
#define PYTHREAD_NAME "pthread" #define PYTHREAD_NAME "pthread"
#include "thread_pthread.inc" #include "third_party/python/Python/thread_pthread.inc"
#endif #endif
#ifdef NT_THREADS #ifdef NT_THREADS
#define PYTHREAD_NAME "nt" #define PYTHREAD_NAME "nt"
#include "thread_nt.inc" // #include "thread_nt.inc"
#endif #endif
/* /*
#ifdef FOOBAR_THREADS #ifdef FOOBAR_THREADS
#include "thread_foobar.inc" // #include "thread_foobar.inc"
#endif #endif
*/ */

View file

@ -492,7 +492,9 @@
#endif #endif
/* Define if you want to compile in rudimentary thread support */ /* Define if you want to compile in rudimentary thread support */
/* #undef WITH_THREAD */ #ifndef WITH_THREAD
#define WITH_THREAD 1
#endif
/* Define if you want pymalloc to be disabled when running under valgrind */ /* Define if you want pymalloc to be disabled when running under valgrind */
/* #undef WITH_VALGRIND */ /* #undef WITH_VALGRIND */
@ -508,7 +510,9 @@
/* #undef _POSIX_1_SOURCE */ /* #undef _POSIX_1_SOURCE */
/* Define if you have POSIX threads, and your system does not define that. */ /* Define if you have POSIX threads, and your system does not define that. */
/* #undef _POSIX_THREADS */ #ifndef _POSIX_THREADS
#define _POSIX_THREADS
#endif
/* #define _Py_MEMORY_SANITIZER */ /* #define _Py_MEMORY_SANITIZER */

View file

@ -167,6 +167,7 @@ STATIC_YOINK("_PyImport_Inittab__stat");
STATIC_YOINK("_PyImport_Inittab__struct"); STATIC_YOINK("_PyImport_Inittab__struct");
STATIC_YOINK("_PyImport_Inittab__symtable"); STATIC_YOINK("_PyImport_Inittab__symtable");
STATIC_YOINK("_PyImport_Inittab__testcapi"); STATIC_YOINK("_PyImport_Inittab__testcapi");
STATIC_YOINK("_PyImport_Inittab__thread");
STATIC_YOINK("_PyImport_Inittab__tracemalloc"); STATIC_YOINK("_PyImport_Inittab__tracemalloc");
STATIC_YOINK("_PyImport_Inittab_array"); STATIC_YOINK("_PyImport_Inittab_array");
STATIC_YOINK("_PyImport_Inittab_atexit"); STATIC_YOINK("_PyImport_Inittab_atexit");
@ -495,6 +496,23 @@ PYTHON_YOINK("nntplib");
PYTHON_YOINK("asdl"); PYTHON_YOINK("asdl");
#ifdef WITH_THREAD #ifdef WITH_THREAD
PYTHON_YOINK("_thread");
PYTHON_YOINK("_thread.LockType");
PYTHON_YOINK("_thread.RLock");
PYTHON_YOINK("_thread.TIMEOUT_MAX");
PYTHON_YOINK("_thread._count");
PYTHON_YOINK("_thread._local");
PYTHON_YOINK("_thread._set_sentinel");
PYTHON_YOINK("_thread.allocate");
PYTHON_YOINK("_thread.allocate_lock");
PYTHON_YOINK("_thread.error");
PYTHON_YOINK("_thread.exit");
PYTHON_YOINK("_thread.exit_thread");
PYTHON_YOINK("_thread.get_ident");
PYTHON_YOINK("_thread.interrupt_main");
PYTHON_YOINK("_thread.stack_size");
PYTHON_YOINK("_thread.start_new");
PYTHON_YOINK("_thread.start_new_thread");
PYTHON_YOINK("asynchat"); PYTHON_YOINK("asynchat");
PYTHON_YOINK("asyncore"); PYTHON_YOINK("asyncore");
PYTHON_YOINK("asyncio"); PYTHON_YOINK("asyncio");

View file

@ -291,6 +291,7 @@ THIRD_PARTY_PYTHON_INCS = \
third_party/python/Modules/clinic/audioop.inc third_party/python/Modules/clinic/audioop.inc
THIRD_PARTY_PYTHON_STAGE1_A_SRCS = \ THIRD_PARTY_PYTHON_STAGE1_A_SRCS = \
third_party/python/Modules/_threadmodule.c \
third_party/python/Modules/_tracemalloc.c \ third_party/python/Modules/_tracemalloc.c \
third_party/python/Modules/faulthandler.c \ third_party/python/Modules/faulthandler.c \
third_party/python/Objects/abstract.c \ third_party/python/Objects/abstract.c \
@ -408,6 +409,7 @@ THIRD_PARTY_PYTHON_STAGE1_A_SRCS = \
third_party/python/Python/symtable.c \ third_party/python/Python/symtable.c \
third_party/python/Parser/listnode.c \ third_party/python/Parser/listnode.c \
third_party/python/Python/sysmodule.c \ third_party/python/Python/sysmodule.c \
third_party/python/Python/thread.c \
third_party/python/Python/traceback.c \ third_party/python/Python/traceback.c \
third_party/python/Modules/unicodedata_3.2.0.c \ third_party/python/Modules/unicodedata_3.2.0.c \
third_party/python/Modules/unicodedata_bidirectionalnames.c \ third_party/python/Modules/unicodedata_bidirectionalnames.c \
@ -449,6 +451,7 @@ THIRD_PARTY_PYTHON_STAGE1_A_DIRECTDEPS = \
LIBC_NEXGEN32E \ LIBC_NEXGEN32E \
LIBC_NT_KERNEL32 \ LIBC_NT_KERNEL32 \
LIBC_RUNTIME \ LIBC_RUNTIME \
LIBC_THREAD \
LIBC_STDIO \ LIBC_STDIO \
LIBC_STR \ LIBC_STR \
LIBC_STUBS \ LIBC_STUBS \

View file

@ -215,7 +215,7 @@ Complete(const char *p, linenoiseCompletions *c)
static void static void
TerminalCompletion(const char *p, linenoiseCompletions *c) TerminalCompletion(const char *p, linenoiseCompletions *c)
{ {
Complete(p, c); // Complete(p, c);
if (PyErr_Occurred()) { if (PyErr_Occurred()) {
PyErr_Clear(); PyErr_Clear();
} }