Skip to content

Commit

Permalink
Made improvements to threading
Browse files Browse the repository at this point in the history
Some memory issues sorted and you should be able to return a number from
a function without it segfaulting, now.
  • Loading branch information
michaelmalonenz committed Jun 14, 2022
1 parent a4e0dcc commit dda6006
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
14 changes: 10 additions & 4 deletions stdlib/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <string.h>
#include "comet.h"
#include "comet_stdlib.h"
#include "cometlib.h"
#ifdef WIN32
#include <Windows.h>
#include <conio.h>
Expand Down Expand Up @@ -36,16 +37,21 @@ void thread_constructor(void *instanceData)
void *thread_runner(void *arg)
{
ThreadData *data = (ThreadData *)arg;
return AS_OBJ(call_function(data->self, data->start_routine, 1, &data->arg));
return (void *)(uintptr_t)call_function(data->self, data->start_routine, 1, &data->arg);
}

VALUE thread_start(VM *vm, VALUE self, int UNUSED(arg_count), VALUE *arguments)
VALUE thread_start(VM *vm, VALUE self, int arg_count, VALUE *arguments)
{
ThreadData *data = GET_NATIVE_INSTANCE_DATA(ThreadData, self);
data->self = self;
data->start_routine = arguments[0];
data->arg = arguments[1];
data->arg = arg_count > 1 ? arguments[1] : NIL_VAL;
int status = 0;
if (callable_p(vm, 1, arguments) == FALSE_VAL)
{
throw_exception_native(vm, "ArgumentException", "The first argument to Thread::start was not callable");
return NIL_VAL;
}
#ifdef WIN32
data->thread_handle = CreateThread(NULL, 0, &thread_runner, data, 0, &data->thread_id);
status = data->thread_handle == NULL ? -1 : 0;
Expand Down Expand Up @@ -94,6 +100,6 @@ void thread_mark_contents(VALUE self)
void init_thread(VM *vm)
{
VALUE klass = defineNativeClass(vm, "Thread", thread_constructor, NULL, NULL, CLS_THREAD, sizeof(ThreadData), true);
defineNativeMethod(vm, klass, &thread_start, "start", 2, false);
defineNativeMethod(vm, klass, &thread_start, "start", 1, false);
defineNativeMethod(vm, klass, &thread_join, "join", 0, false);
}
1 change: 1 addition & 0 deletions test_scripts/socket_threads.cmt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ function client(arg)
print(contents)
client.write("This is from the client")
client.close()
sleep(0.5)
}

var server = Socket(SOCKET_TYPE.TCP, ADDRESS_FAMILY.IPv6)
Expand Down
5 changes: 5 additions & 0 deletions vmlib/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ static void blackenObject(Obj *object)
markValue(((ObjUpvalue *)object)->closed);
break;
case OBJ_NATIVE_METHOD:
{
ObjNativeMethod *method = (ObjNativeMethod *)object;
markValue(method->name);
break;
}
case OBJ_NATIVE:
break;
}
Expand Down
1 change: 1 addition & 0 deletions vmlib/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ VALUE call_function(VALUE receiver, VALUE method, int arg_count, VALUE *argument
{
push(frame, arguments[i]);
}
closeUpvalues(frame, NULL);
if (IS_BOUND_METHOD(method) || IS_CLOSURE(method) || IS_FUNCTION(method))
{
if (callValue(frame, method, arg_count) && run(frame) == INTERPRET_OK)
Expand Down

0 comments on commit dda6006

Please sign in to comment.