Skip to content

Commit

Permalink
Updated threads
Browse files Browse the repository at this point in the history
  • Loading branch information
mcfriend99 committed Oct 14, 2024
1 parent 121475c commit 61b5f1b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
35 changes: 30 additions & 5 deletions libs/thread.b
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
import _thread

import reflect
import os

var _MIN_STACK_SIZE = 8 * 1024
var _DEFAULT_STACK_SIZE = 64 * 1024

var _main_thread_id = _thread.get_id()

def _is_main_thread(id) {
return id == _main_thread_id
}

class Thread {

var _fn
var _fn_arity

var _args
var _name = 'blade'
var _name
var _size = _DEFAULT_STACK_SIZE

var _ptr
Expand Down Expand Up @@ -67,11 +74,17 @@ class Thread {

try_await() {}

sleep(time) {}
sleep(time) {
if _is_main_thread(self.get_id())
raise Exception('cannot call sleep() from main thread')

cancel() {}
os.sleep(time)
}

yield() {}
yield() {
if _is_main_thread(self.get_id())
raise Exception('cannot call yield() from main thread')
}

set_name(name) {
if !is_string(name)
Expand All @@ -81,14 +94,26 @@ class Thread {
raise Exception('string length must be between 1 and 16')
}

if _is_main_thread(self.get_id())
raise Exception('cannot call set_name() from main thread')

if self._ptr {
self._name = name
_thread.set_name(self._ptr, name)
}
}

get_name() {
return self._name
if _is_main_thread(self.get_id())
raise Exception('cannot call get_name() from main thread')

if self._ptr {
if self._name
return self._name
return _thread.get_name(self._ptr)
}

return 'blade'
}

set_stack_size(size) {
Expand Down
19 changes: 19 additions & 0 deletions src/standard/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,19 @@ DECLARE_MODULE_METHOD(thread__set_name) {
#endif
}

DECLARE_MODULE_METHOD(thread__get_name) {
ENFORCE_ARG_COUNT(get_name, 1);
ENFORCE_ARG_TYPE(get_name, 0, IS_PTR);
b_thread_handle *thread = AS_PTR(args[0])->pointer;

char buffer[255];
if(pthread_getname_np(thread->thread, buffer, 255) == 0) {
RETURN_STRING(buffer);
}

RETURN_VALUE(EMPTY_STRING_VAL);
}

uint64_t get_thread_id(void)
{
#if defined(__linux__)
Expand Down Expand Up @@ -222,13 +235,19 @@ DECLARE_MODULE_METHOD(thread__get_id) {
RETURN_NUMBER(get_thread_id());
}

DECLARE_MODULE_METHOD(thread__main_thread) {
ENFORCE_ARG_COUNT(main_thread, 0);
RETURN;
}

CREATE_MODULE_LOADER(thread) {
static b_func_reg module_functions[] = {
{"start", false, GET_MODULE_METHOD(thread__start)},
{"dispose", false, GET_MODULE_METHOD(thread__dispose)},
{"await", false, GET_MODULE_METHOD(thread__await)},
{"detach", false, GET_MODULE_METHOD(thread__detach)},
{"set_name", false, GET_MODULE_METHOD(thread__set_name)},
{"get_name", false, GET_MODULE_METHOD(thread__get_name)},
{"get_id", false, GET_MODULE_METHOD(thread__get_id)},
{NULL, false, NULL},
};
Expand Down
3 changes: 2 additions & 1 deletion tests/libs/threads2.b
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import thread
var s = []
for i in 0..60000 {
var th = thread.start(@(t, i){
echo '${t.get_id()}, ${i}'
t.set_name('thread-${i}')
echo '${t.get_name()}, ${t.get_id()}, ${i}'
}, [i])
if(th) {
s.append(th)
Expand Down

0 comments on commit 61b5f1b

Please sign in to comment.