Skip to content

Commit

Permalink
Partially-Functional Draft 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfriend99 committed Oct 12, 2024
1 parent 5f64e08 commit dd4354d
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 92 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get remove libgd3
sudo apt-get install curl libcurl4-openssl-dev libgd-dev libavif-dev libffi-dev -y
sudo apt-get install curl libpthread-stubs0-dev libcurl4-openssl-dev libgd-dev libavif-dev libffi-dev -y
cmake -B .
cmake --build . -- -j 12
chmod +x blade/blade
Expand Down Expand Up @@ -99,7 +99,7 @@ jobs:
run: |
${{ env.VCPKG_ROOT }}/vcpkg update
${{ env.VCPKG_ROOT }}/vcpkg version
${{ env.VCPKG_ROOT }}/vcpkg install pkgconf curl:x64-windows libffi:x64-windows openssl:x64-windows libgd:x64-windows
${{ env.VCPKG_ROOT }}/vcpkg install pkgconf pthreads:x64-windows curl:x64-windows libffi:x64-windows openssl:x64-windows libgd:x64-windows
${{ env.VCPKG_ROOT }}/vcpkg list
- name: Save vcpkg dependencies cache
id: vcpkg-cache-save
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
run: |
sudo apt-get update
sudo apt-get remove libgd3
sudo apt-get install curl libcurl4-openssl-dev libgd-dev libavif-dev libffi-dev -y
sudo apt-get install curl libpthread-stubs0-dev libcurl4-openssl-dev libgd-dev libavif-dev libffi-dev -y
cmake -B .
cmake --build . -- -j 12
- name: Create ZIP Archive
Expand Down Expand Up @@ -94,7 +94,7 @@ jobs:
run: |
${{ env.VCPKG_ROOT }}/vcpkg update
${{ env.VCPKG_ROOT }}/vcpkg version
${{ env.VCPKG_ROOT }}/vcpkg install pkgconf curl:x64-windows libffi:x64-windows openssl:x64-windows libgd:x64-windows
${{ env.VCPKG_ROOT }}/vcpkg install pkgconf pthreads:x64-windows curl:x64-windows libffi:x64-windows openssl:x64-windows libgd:x64-windows
${{ env.VCPKG_ROOT }}/vcpkg list
- name: Compile
run: |
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,12 @@ if(UNIX)

target_link_libraries(libblade PRIVATE m)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
target_link_libraries(libblade PRIVATE pthread)
target_link_libraries(libblade PRIVATE dl)
endif()
endif(UNIX)

target_link_libraries(libblade PRIVATE pthread)

if(WIN32)
add_dependencies(libblade mman)
target_link_libraries(libblade PRIVATE mman)
Expand Down
18 changes: 18 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,23 @@ install_build_env() {
fi
}

install_platform_specific_tools() {
# shellcheck disable=SC2154
if [ -x "$(command -v apt-get)" ]; then
sudo apt-get install libpthread-stubs0-dev -y
elif [ -x "$(command -v apk)" ]; then
sudo apk add --no-cache glibc-headers
elif [ -x "$(command -v dnf)" ]; then
sudo dnf install glibc-headers -y
elif [ -x "$(command -v zypper)" ]; then
sudo zypper install glibc-devel -y
elif [ -x "$(command -v yum)" ]; then
sudo yum install glibc-headers -y
elif [ -x "$(command -v pacman)" ]; then
sudo pacman -Sy glibc
fi
}

remove_redundant_libraries() {
# shellcheck disable=SC2154
if [ -x "$(command -v apt-get)" ]; then
Expand Down Expand Up @@ -151,6 +168,7 @@ echo "Beginning installation of Blade..."

install_build_env
remove_redundant_libraries
install_platform_specific_tools

if [[ -z "${IS_LINUX-}" ]]; then

Expand Down
86 changes: 73 additions & 13 deletions src/standard/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,96 @@ static b_thread_handle *create_thread_handle(b_vm *vm, b_obj_closure *closure, b

b_thread_handle *handle = ALLOCATE(b_thread_handle, 1);
if(handle != NULL) {
thrd_t *thread = ALLOCATE(thrd_t, 1);
pthread_t *thread = ALLOCATE(pthread_t, 1);

if(thread) {
handle->thread = (void *)thread;
handle->vm = copy_vm(vm, ++last_thread_vm_id);

if(handle->vm == NULL) {
FREE(pthread_t, thread);
FREE(b_thread_handle, handle);
return NULL;
}

handle->thread = (void *)thread;
handle->closure = closure;
handle->args = args;

((b_obj *)closure)->stale = true;
((b_obj *)args)->stale = true;
} else {
FREE(b_thread_handle, handle);
return NULL;
}
}

return handle;
}

static int b_thread_callback_function(void *data) {

static void push_thread(b_vm *vm, b_thread_handle *thread) {
if(vm->threads_capacity == vm->threads_count) {
size_t capacity = GROW_CAPACITY(vm->threads_capacity);
vm->threads = GROW_ARRAY(b_thread_handle *, vm->threads, vm->threads_capacity, capacity);
vm->threads_capacity = capacity;

vm->threads[vm->threads_count] = thread;
thread->parent_thead_index = vm->threads_count;
thread->parent_vm = vm;
} else {
for(int i = 0; i < vm->threads_capacity; i++) {
if(vm->threads[i] == NULL) {
vm->threads[i] = thread;
thread->parent_thead_index = i;
thread->parent_vm = vm;
break;
}
}
}

vm->threads_count++;
}

static void free_thread_handle(b_thread_handle *thread) {
if(thread != NULL && thread->parent_vm) {
// make slot available for another thread

b_vm *vm = thread->parent_vm;
thread->parent_vm->threads[thread->parent_thead_index] = NULL;
thread->parent_vm->threads_count--;

free_vm(thread->vm);
free(thread->thread);

thread->parent_vm = NULL;
thread->vm = NULL;
thread->thread = NULL;
thread->closure = NULL;
thread->args = NULL;

FREE(b_thread_handle *, thread);
thread = NULL;
}
}

static void *b_thread_callback_function(void *data) {
b_thread_handle *handle = (b_thread_handle *) data;
if(handle == NULL || handle->vm == NULL || handle->parent_vm == NULL) {
thrd_exit(0);
return 1;
pthread_exit(NULL);
}

for(int i = 0; i < handle->args->items.count; i++) {
push(handle->vm, handle->args->items.values[i]);
}

bool result = 1;
if(run_closure_call(handle->vm, handle->closure, handle->args) == PTR_OK) {
result = 0;
// do nothing for now...
}

((b_obj *)handle->closure)->stale = false;
((b_obj *)handle->args)->stale = false;

// free_thread_handle(handle);
return result;
return NULL;
}

DECLARE_MODULE_METHOD(thread__run) {
Expand All @@ -61,9 +114,17 @@ DECLARE_MODULE_METHOD(thread__run) {
b_thread_handle *thread = create_thread_handle(vm, AS_CLOSURE(args[0]), AS_LIST(args[1]));
if(thread) {
push_thread(vm, thread);
if(thrd_create((thrd_t *)thread->thread, b_thread_callback_function, thread) == thrd_success) {

pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 64 * 1024); // Reduce stack size to 8KB

if(pthread_create((pthread_t *)thread->thread, NULL, b_thread_callback_function, thread) == thrd_success) {
pthread_attr_destroy(&attr);
RETURN_NAMED_PTR(thread, B_THREAD_PTR_NAME);
}

pthread_attr_destroy(&attr);
}

RETURN_FALSE;
Expand All @@ -76,7 +137,6 @@ DECLARE_MODULE_METHOD(thread__dispose) {
b_thread_handle *thread = AS_PTR(args[0])->pointer;
if(thread) {
free_thread_handle(thread);
vm->threads_count--;
}
RETURN;
}
Expand All @@ -86,7 +146,7 @@ DECLARE_MODULE_METHOD(thread__await) {
ENFORCE_ARG_TYPE(await, 0, IS_PTR);
b_thread_handle *thread = AS_PTR(args[0])->pointer;

bool success = thrd_join(*((thrd_t *)thread->thread), 0) == thrd_success;
bool success = pthread_join(*((pthread_t *)thread->thread), NULL) == 0;
free_thread_handle(thread);

RETURN_BOOL(success);
Expand All @@ -96,7 +156,7 @@ DECLARE_MODULE_METHOD(thread__detach) {
ENFORCE_ARG_COUNT(detach, 1);
ENFORCE_ARG_TYPE(detach, 0, IS_PTR);
b_thread_handle *thread = AS_PTR(args[0])->pointer;
RETURN_BOOL(thrd_detach(*((thrd_t *)thread->thread)) == thrd_success);
RETURN_BOOL(pthread_detach(*((pthread_t *)thread->thread)) == 0);
}

CREATE_MODULE_LOADER(thread) {
Expand Down
67 changes: 0 additions & 67 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,30 +247,6 @@ inline b_error_frame* pop_error(b_vm *vm) {
return *vm->error_top;
}

inline void push_thread(b_vm *vm, b_thread_handle *thread) {
if(vm->threads_capacity == vm->threads_count) {
size_t capacity = GROW_CAPACITY(vm->threads_capacity);
vm->threads = GROW_ARRAY(b_thread_handle *, vm->threads, vm->threads_capacity, capacity);
vm->threads_capacity = capacity;

vm->threads[vm->threads_count] = thread;
thread->parent_thead_index = vm->threads_count;
thread->parent_vm = vm;
vm->threads_count++;
} else {
for(int i = 0; i < vm->threads_capacity; i++) {
if(vm->threads[i] == NULL) {
vm->threads[i] = thread;
thread->parent_thead_index = i;
thread->parent_vm = vm;
break;
}
}

vm->threads_count++;
}
}

inline b_error_frame* peek_error(b_vm *vm) {
return vm->error_top[-1];
}
Expand Down Expand Up @@ -639,27 +615,6 @@ b_vm *copy_vm(b_vm *src, uint64_t id) {

vm->id = id;

/*init_table(&vm->modules);
table_add_all(vm, &src->modules, &vm->modules);
init_table(&vm->strings);
table_add_all(vm, &src->strings, &vm->strings);
init_table(&vm->globals);
table_add_all(vm, &src->globals, &vm->globals);
// object methods tables
init_table(&vm->methods_string);
table_add_all(vm, &src->methods_string, &vm->methods_string);
init_table(&vm->methods_list);
table_add_all(vm, &src->methods_list, &vm->methods_list);
init_table(&vm->methods_dict);
table_add_all(vm, &src->methods_dict, &vm->methods_dict);
init_table(&vm->methods_file);
table_add_all(vm, &src->methods_file, &vm->methods_file);
init_table(&vm->methods_bytes);
table_add_all(vm, &src->methods_bytes, &vm->methods_bytes);
init_table(&vm->methods_range);
table_add_all(vm, &src->methods_range, &vm->methods_range);*/

return vm;
}

Expand Down Expand Up @@ -691,28 +646,6 @@ void free_vm(b_vm *vm) {
free(vm);
}

void free_thread_handle(b_thread_handle *thread) {
if(thread != NULL && thread->parent_vm) {
// make slot available for another thread

b_vm *vm = thread->parent_vm;
thread->parent_vm->threads[thread->parent_thead_index] = NULL;
thread->parent_vm->threads_count--;

free_vm(thread->vm);
free(thread->thread);

thread->parent_vm = NULL;
thread->vm = NULL;
thread->thread = NULL;
thread->closure = NULL;
thread->args = NULL;

FREE(b_thread_handle *, thread);
thread = NULL;
}
}

static inline bool is_private(b_obj_string *name) {
return name->length > 0 && name->chars[0] == '_';
}
Expand Down
3 changes: 0 additions & 3 deletions src/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,6 @@ void push_error(b_vm *vm, b_error_frame *frame);
b_error_frame* pop_error(b_vm *vm);
b_error_frame* peek_error(b_vm *vm);

void push_thread(b_vm *vm, b_thread_handle *thread);
void free_thread_handle(b_thread_handle *thread);

static inline void add_module(b_vm *vm, b_obj_module *module) {
cond_dbg(vm->current_frame, printf("Adding module %s from %s to %s in %s\n",
module->name,
Expand Down
14 changes: 10 additions & 4 deletions thirdparty/threads/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,12 @@ static inline int mtx_unlock(mtx_t *mtx) {
/*------------------- 7.25.5 Thread functions -------------------*/
// 7.25.5.1
static inline int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) {
struct impl_thrd_param *pack;
uintptr_t handle;
assert(thr != NULL);
pack = (struct impl_thrd_param *)malloc(sizeof(struct impl_thrd_param));
struct impl_thrd_param *pack = (struct impl_thrd_param *)malloc(sizeof(struct impl_thrd_param));
if (!pack) return thrd_nomem;
pack->func = func;
pack->arg = arg;
handle = _beginthreadex(NULL, 0, impl_thrd_routine, pack, 0, NULL);
uintptr_t handle = _beginthreadex(NULL, 8 * 1024, impl_thrd_routine, pack, 0, NULL);
if (handle == 0) {
if (errno == EAGAIN || errno == EACCES)
return thrd_nomem;
Expand Down Expand Up @@ -727,10 +725,18 @@ static inline int thrd_create(thrd_t *thr, thrd_start_t func, void *arg) {
if (!pack) return thrd_nomem;
pack->func = func;
pack->arg = arg;

// pthread_attr_t attr;
// pthread_attr_init(&attr);
// pthread_attr_setstacksize(&attr, 8 * 1024); // Reduce stack size to 8KB

if (pthread_create(thr, NULL, impl_thrd_routine, pack) != 0) {
free(pack);
// pthread_attr_destroy(&attr);
return thrd_error;
}

// pthread_attr_destroy(&attr);
return thrd_success;
}

Expand Down

0 comments on commit dd4354d

Please sign in to comment.