mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
add PK_ENABLE_THREADS
This commit is contained in:
parent
0a44220f66
commit
a1e35b6e13
@ -49,6 +49,10 @@ if(PK_ENABLE_OS)
|
||||
add_definitions(-DPK_ENABLE_OS=1)
|
||||
endif()
|
||||
|
||||
if(PK_ENABLE_THREADS)
|
||||
add_definitions(-DPK_ENABLE_THREADS=1)
|
||||
endif()
|
||||
|
||||
if(PK_ENABLE_DETERMINISM)
|
||||
add_subdirectory(3rd/dmath/dmath)
|
||||
add_definitions(-DPK_ENABLE_DETERMINISM=1)
|
||||
@ -104,8 +108,10 @@ if(PK_ENABLE_DETERMINISM)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(PK_ENABLE_THREADS)
|
||||
find_package(Threads REQUIRED)
|
||||
target_link_libraries(${PROJECT_NAME} Threads::Threads)
|
||||
endif()
|
||||
|
||||
if(UNIX AND NOT APPLE)
|
||||
if(NOT PK_ENABLE_DETERMINISM)
|
||||
|
@ -7,6 +7,7 @@ endif()
|
||||
|
||||
# system features
|
||||
option(PK_ENABLE_OS "" OFF)
|
||||
option(PK_ENABLE_THREADS "" OFF)
|
||||
option(PK_ENABLE_DETERMINISM "" OFF)
|
||||
option(PK_ENABLE_WATCHDOG "" OFF)
|
||||
option(PK_ENABLE_CUSTOM_SNAME "" OFF)
|
||||
|
@ -20,7 +20,7 @@ assert config in ['Debug', 'Release', 'RelWithDebInfo']
|
||||
|
||||
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
|
||||
code = os.system(f"cmake --build . --config {config} -j 4")
|
||||
assert code == 0
|
||||
|
@ -1,5 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "pocketpy/config.h"
|
||||
|
||||
#if PK_ENABLE_THREADS
|
||||
|
||||
#include <stdatomic.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);
|
||||
void c11_thrd_yield();
|
||||
|
||||
#endif
|
@ -7,11 +7,14 @@
|
||||
#define PK_VERSION_PATCH 0
|
||||
|
||||
/*************** feature settings ***************/
|
||||
// Whether to compile os-related modules or not
|
||||
#ifndef PK_ENABLE_OS // can be overridden by cmake
|
||||
#define PK_ENABLE_OS 1
|
||||
#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
|
||||
#define PK_ENABLE_DETERMINISM 0
|
||||
#endif
|
||||
|
@ -2,9 +2,8 @@
|
||||
|
||||
#include "pocketpy/common/name.h"
|
||||
#include "pocketpy/common/str.h"
|
||||
#include "pocketpy/common/threads.h"
|
||||
#include "pocketpy/pocketpy.h"
|
||||
#include <stdatomic.h>
|
||||
#include "pocketpy/common/threads.h"
|
||||
|
||||
typedef struct NameBucket NameBucket;
|
||||
|
||||
@ -17,7 +16,9 @@ typedef struct NameBucket {
|
||||
|
||||
static struct {
|
||||
NameBucket* table[0x10000];
|
||||
#if PK_ENABLE_THREADS
|
||||
atomic_flag lock;
|
||||
#endif
|
||||
} pk_string_table;
|
||||
|
||||
#define MAGIC_METHOD(x) py_Name x;
|
||||
@ -43,9 +44,11 @@ void pk_names_finalize() {
|
||||
}
|
||||
|
||||
py_Name py_namev(c11_sv name) {
|
||||
#if PK_ENABLE_THREADS
|
||||
while(atomic_flag_test_and_set(&pk_string_table.lock)) {
|
||||
c11_thrd_yield();
|
||||
}
|
||||
#endif
|
||||
uint64_t hash = c11_sv__hash(name);
|
||||
int index = hash & 0xFFFF;
|
||||
NameBucket* p = pk_string_table.table[index];
|
||||
@ -61,7 +64,9 @@ py_Name py_namev(c11_sv name) {
|
||||
p = p->next;
|
||||
}
|
||||
if(found) {
|
||||
#if PK_ENABLE_THREADS
|
||||
atomic_flag_clear(&pk_string_table.lock);
|
||||
#endif
|
||||
return (py_Name)p;
|
||||
}
|
||||
|
||||
@ -78,7 +83,9 @@ py_Name py_namev(c11_sv name) {
|
||||
assert(prev->next == NULL);
|
||||
prev->next = bucket;
|
||||
}
|
||||
#if PK_ENABLE_THREADS
|
||||
atomic_flag_clear(&pk_string_table.lock);
|
||||
#endif
|
||||
return (py_Name)bucket;
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "pocketpy/export.h"
|
||||
#include "pocketpy/common/threads.h"
|
||||
|
||||
#if PK_ENABLE_THREADS
|
||||
|
||||
#if PK_USE_PTHREADS
|
||||
|
||||
bool c11_thrd_create(c11_thrd_t* thrd, c11_thrd_retval_t (*func)(void*), void* arg) {
|
||||
@ -20,3 +21,5 @@ bool c11_thrd_create(c11_thrd_t* thrd, c11_thrd_retval_t (*func)(void*), void* a
|
||||
void c11_thrd_yield() { thrd_yield(); }
|
||||
|
||||
#endif
|
||||
|
||||
#endif // PK_ENABLE_THREADS
|
@ -2,7 +2,7 @@
|
||||
#include "pocketpy/pocketpy.h"
|
||||
#include "pocketpy/interpreter/vm.h"
|
||||
|
||||
#if PK_ENABLE_OS == 1
|
||||
#if PK_ENABLE_OS
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
|
@ -106,6 +106,8 @@ static bool pkpy_watchdog_end(int argc, py_Ref argv) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#if PK_ENABLE_THREADS
|
||||
|
||||
typedef struct c11_ComputeThread c11_ComputeThread;
|
||||
|
||||
typedef struct {
|
||||
@ -456,6 +458,8 @@ static void pk_ComputeThread__register(py_Ref mod) {
|
||||
py_bindmethod(type, "eval", ComputeThread_eval);
|
||||
}
|
||||
|
||||
#endif // PK_ENABLE_THREADS
|
||||
|
||||
static void pkpy_configmacros_add(py_Ref dict, const char* key, int val) {
|
||||
assert(dict->type == tp_dict);
|
||||
py_TValue tmp;
|
||||
@ -508,11 +512,14 @@ void pk__add_module_pkpy() {
|
||||
py_bindfunc(mod, "watchdog_end", pkpy_watchdog_end);
|
||||
#endif
|
||||
|
||||
#if PK_ENABLE_THREADS
|
||||
pk_ComputeThread__register(mod);
|
||||
#endif
|
||||
|
||||
py_Ref configmacros = py_emplacedict(mod, py_name("configmacros"));
|
||||
py_newdict(configmacros);
|
||||
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_WATCHDOG", PK_ENABLE_WATCHDOG);
|
||||
pkpy_configmacros_add(configmacros, "PK_GC_MIN_THRESHOLD", PK_GC_MIN_THRESHOLD);
|
||||
|
@ -1,4 +1,11 @@
|
||||
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
|
||||
|
||||
thread_1 = ComputeThread(1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user