add PK_ENABLE_THREADS

This commit is contained in:
blueloveTH 2025-06-30 11:30:36 +08:00
parent 0a44220f66
commit a1e35b6e13
10 changed files with 50 additions and 10 deletions

View File

@ -49,6 +49,10 @@ if(PK_ENABLE_OS)
add_definitions(-DPK_ENABLE_OS=1) add_definitions(-DPK_ENABLE_OS=1)
endif() endif()
if(PK_ENABLE_THREADS)
add_definitions(-DPK_ENABLE_THREADS=1)
endif()
if(PK_ENABLE_DETERMINISM) if(PK_ENABLE_DETERMINISM)
add_subdirectory(3rd/dmath/dmath) add_subdirectory(3rd/dmath/dmath)
add_definitions(-DPK_ENABLE_DETERMINISM=1) add_definitions(-DPK_ENABLE_DETERMINISM=1)
@ -104,8 +108,10 @@ if(PK_ENABLE_DETERMINISM)
endif() endif()
endif() endif()
find_package(Threads REQUIRED) if(PK_ENABLE_THREADS)
target_link_libraries(${PROJECT_NAME} Threads::Threads) find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} Threads::Threads)
endif()
if(UNIX AND NOT APPLE) if(UNIX AND NOT APPLE)
if(NOT PK_ENABLE_DETERMINISM) if(NOT PK_ENABLE_DETERMINISM)

View File

@ -7,6 +7,7 @@ endif()
# system features # system features
option(PK_ENABLE_OS "" OFF) option(PK_ENABLE_OS "" OFF)
option(PK_ENABLE_THREADS "" OFF)
option(PK_ENABLE_DETERMINISM "" OFF) option(PK_ENABLE_DETERMINISM "" OFF)
option(PK_ENABLE_WATCHDOG "" OFF) option(PK_ENABLE_WATCHDOG "" OFF)
option(PK_ENABLE_CUSTOM_SNAME "" OFF) option(PK_ENABLE_CUSTOM_SNAME "" OFF)

View File

@ -20,7 +20,7 @@ assert config in ['Debug', 'Release', 'RelWithDebInfo']
os.chdir("build") os.chdir("build")
code = os.system(f"cmake .. -DPK_ENABLE_OS=ON -DPK_ENABLE_DETERMINISM=ON -DCMAKE_BUILD_TYPE={config} {extra_flags}") code = os.system(f"cmake .. -DPK_ENABLE_OS=ON -DPK_ENABLE_THREADS=ON -DPK_ENABLE_DETERMINISM=ON -DCMAKE_BUILD_TYPE={config} {extra_flags}")
assert code == 0 assert code == 0
code = os.system(f"cmake --build . --config {config} -j 4") code = os.system(f"cmake --build . --config {config} -j 4")
assert code == 0 assert code == 0

View File

@ -1,5 +1,9 @@
#pragma once #pragma once
#include "pocketpy/config.h"
#if PK_ENABLE_THREADS
#include <stdatomic.h> #include <stdatomic.h>
#include <stdbool.h> #include <stdbool.h>
@ -17,3 +21,5 @@ typedef int c11_thrd_retval_t;
bool c11_thrd_create(c11_thrd_t* thrd, c11_thrd_retval_t (*func)(void*), void* arg); bool c11_thrd_create(c11_thrd_t* thrd, c11_thrd_retval_t (*func)(void*), void* arg);
void c11_thrd_yield(); void c11_thrd_yield();
#endif

View File

@ -7,11 +7,14 @@
#define PK_VERSION_PATCH 0 #define PK_VERSION_PATCH 0
/*************** feature settings ***************/ /*************** feature settings ***************/
// Whether to compile os-related modules or not
#ifndef PK_ENABLE_OS // can be overridden by cmake #ifndef PK_ENABLE_OS // can be overridden by cmake
#define PK_ENABLE_OS 1 #define PK_ENABLE_OS 1
#endif #endif
#ifndef PK_ENABLE_THREADS // can be overridden by cmake
#define PK_ENABLE_THREADS 1
#endif
#ifndef PK_ENABLE_DETERMINISM // must be enabled from cmake #ifndef PK_ENABLE_DETERMINISM // must be enabled from cmake
#define PK_ENABLE_DETERMINISM 0 #define PK_ENABLE_DETERMINISM 0
#endif #endif

View File

@ -2,9 +2,8 @@
#include "pocketpy/common/name.h" #include "pocketpy/common/name.h"
#include "pocketpy/common/str.h" #include "pocketpy/common/str.h"
#include "pocketpy/common/threads.h"
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#include <stdatomic.h> #include "pocketpy/common/threads.h"
typedef struct NameBucket NameBucket; typedef struct NameBucket NameBucket;
@ -17,7 +16,9 @@ typedef struct NameBucket {
static struct { static struct {
NameBucket* table[0x10000]; NameBucket* table[0x10000];
#if PK_ENABLE_THREADS
atomic_flag lock; atomic_flag lock;
#endif
} pk_string_table; } pk_string_table;
#define MAGIC_METHOD(x) py_Name x; #define MAGIC_METHOD(x) py_Name x;
@ -43,9 +44,11 @@ void pk_names_finalize() {
} }
py_Name py_namev(c11_sv name) { py_Name py_namev(c11_sv name) {
#if PK_ENABLE_THREADS
while(atomic_flag_test_and_set(&pk_string_table.lock)) { while(atomic_flag_test_and_set(&pk_string_table.lock)) {
c11_thrd_yield(); c11_thrd_yield();
} }
#endif
uint64_t hash = c11_sv__hash(name); uint64_t hash = c11_sv__hash(name);
int index = hash & 0xFFFF; int index = hash & 0xFFFF;
NameBucket* p = pk_string_table.table[index]; NameBucket* p = pk_string_table.table[index];
@ -61,7 +64,9 @@ py_Name py_namev(c11_sv name) {
p = p->next; p = p->next;
} }
if(found) { if(found) {
#if PK_ENABLE_THREADS
atomic_flag_clear(&pk_string_table.lock); atomic_flag_clear(&pk_string_table.lock);
#endif
return (py_Name)p; return (py_Name)p;
} }
@ -78,7 +83,9 @@ py_Name py_namev(c11_sv name) {
assert(prev->next == NULL); assert(prev->next == NULL);
prev->next = bucket; prev->next = bucket;
} }
#if PK_ENABLE_THREADS
atomic_flag_clear(&pk_string_table.lock); atomic_flag_clear(&pk_string_table.lock);
#endif
return (py_Name)bucket; return (py_Name)bucket;
} }

View File

@ -1,6 +1,7 @@
#include "pocketpy/export.h"
#include "pocketpy/common/threads.h" #include "pocketpy/common/threads.h"
#if PK_ENABLE_THREADS
#if PK_USE_PTHREADS #if PK_USE_PTHREADS
bool c11_thrd_create(c11_thrd_t* thrd, c11_thrd_retval_t (*func)(void*), void* arg) { bool c11_thrd_create(c11_thrd_t* thrd, c11_thrd_retval_t (*func)(void*), void* arg) {
@ -19,4 +20,6 @@ bool c11_thrd_create(c11_thrd_t* thrd, c11_thrd_retval_t (*func)(void*), void* a
void c11_thrd_yield() { thrd_yield(); } void c11_thrd_yield() { thrd_yield(); }
#endif #endif
#endif // PK_ENABLE_THREADS

View File

@ -2,7 +2,7 @@
#include "pocketpy/pocketpy.h" #include "pocketpy/pocketpy.h"
#include "pocketpy/interpreter/vm.h" #include "pocketpy/interpreter/vm.h"
#if PK_ENABLE_OS == 1 #if PK_ENABLE_OS
#include <errno.h> #include <errno.h>

View File

@ -106,6 +106,8 @@ static bool pkpy_watchdog_end(int argc, py_Ref argv) {
} }
#endif #endif
#if PK_ENABLE_THREADS
typedef struct c11_ComputeThread c11_ComputeThread; typedef struct c11_ComputeThread c11_ComputeThread;
typedef struct { typedef struct {
@ -456,6 +458,8 @@ static void pk_ComputeThread__register(py_Ref mod) {
py_bindmethod(type, "eval", ComputeThread_eval); py_bindmethod(type, "eval", ComputeThread_eval);
} }
#endif // PK_ENABLE_THREADS
static void pkpy_configmacros_add(py_Ref dict, const char* key, int val) { static void pkpy_configmacros_add(py_Ref dict, const char* key, int val) {
assert(dict->type == tp_dict); assert(dict->type == tp_dict);
py_TValue tmp; py_TValue tmp;
@ -508,11 +512,14 @@ void pk__add_module_pkpy() {
py_bindfunc(mod, "watchdog_end", pkpy_watchdog_end); py_bindfunc(mod, "watchdog_end", pkpy_watchdog_end);
#endif #endif
#if PK_ENABLE_THREADS
pk_ComputeThread__register(mod); pk_ComputeThread__register(mod);
#endif
py_Ref configmacros = py_emplacedict(mod, py_name("configmacros")); py_Ref configmacros = py_emplacedict(mod, py_name("configmacros"));
py_newdict(configmacros); py_newdict(configmacros);
pkpy_configmacros_add(configmacros, "PK_ENABLE_OS", PK_ENABLE_OS); pkpy_configmacros_add(configmacros, "PK_ENABLE_OS", PK_ENABLE_OS);
pkpy_configmacros_add(configmacros, "PK_ENABLE_THREADS", PK_ENABLE_THREADS);
pkpy_configmacros_add(configmacros, "PK_ENABLE_DETERMINISM", PK_ENABLE_DETERMINISM); pkpy_configmacros_add(configmacros, "PK_ENABLE_DETERMINISM", PK_ENABLE_DETERMINISM);
pkpy_configmacros_add(configmacros, "PK_ENABLE_WATCHDOG", PK_ENABLE_WATCHDOG); pkpy_configmacros_add(configmacros, "PK_ENABLE_WATCHDOG", PK_ENABLE_WATCHDOG);
pkpy_configmacros_add(configmacros, "PK_GC_MIN_THRESHOLD", PK_GC_MIN_THRESHOLD); pkpy_configmacros_add(configmacros, "PK_GC_MIN_THRESHOLD", PK_GC_MIN_THRESHOLD);

View File

@ -1,4 +1,11 @@
from pkpy import ComputeThread from pkpy import configmacros
if configmacros['PK_ENABLE_THREADS'] == 1:
from pkpy import ComputeThread
else:
print('threads is not enabled, skipping test...')
exit()
import time import time
thread_1 = ComputeThread(1) thread_1 = ComputeThread(1)