Skip to content

Commit

Permalink
tc_create can now return NULL, throw in thread_new
Browse files Browse the repository at this point in the history
also panic in moar.c if tc_create returned NULL.
  • Loading branch information
timo committed Feb 23, 2017
1 parent c537970 commit acb20d7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
14 changes: 7 additions & 7 deletions src/core/threadcontext.c
Expand Up @@ -10,6 +10,13 @@ MVMThreadContext * MVM_tc_create(MVMInstance *instance) {
/* Associate with VM instance. */
tc->instance = instance;

/* Use default loop for main thread; create a new one for others. */
tc->loop = instance->main_thread ? uv_loop_new() : uv_default_loop();
if (!tc->loop) {
MVM_free(tc);
return NULL;
}

/* Set up GC nursery. We only allocate tospace initially, and allocate
* fromspace the first time this thread GCs, provided it ever does. */
tc->nursery_tospace = MVM_calloc(1, MVM_NURSERY_SIZE);
Expand All @@ -32,13 +39,6 @@ MVMThreadContext * MVM_tc_create(MVMInstance *instance) {
/* Allocate an initial call stack region for the thread. */
MVM_callstack_region_init(tc);

/* Use default loop for main thread; create a new one for others. */
tc->loop = instance->main_thread ? uv_loop_new() : uv_default_loop();

if (!tc->loop) {
MVM_panic(1, "ThreadContext: could not create a new libuv loop!");
}

/* Initialize random number generator state. */
MVM_proc_seed(tc, (MVM_platform_now() / 10000) * MVM_proc_getpid(tc));

Expand Down
9 changes: 8 additions & 1 deletion src/core/threads.c
Expand Up @@ -21,8 +21,15 @@ MVMObject * MVM_thread_new(MVMThreadContext *tc, MVMObject *invokee, MVMint64 ap
MVM_ASSIGN_REF(tc, &(thread->common.header), thread->body.invokee, invokee);
thread->body.app_lifetime = app_lifetime;

/* Create a new thread context and set it up a little. */
/* Try to create the new threadcontext. Can fail if libuv can't
* create a loop for it for some reason (i.e. too many open files) */
child_tc = MVM_tc_create(tc->instance);

if (!child_tc) {
MVM_exception_throw_adhoc(tc, "Could not create a new Thread.");
}

/* Set up the new threadcontext a little. */
child_tc->thread_obj = thread;
child_tc->thread_id = 1 + MVM_incr(&tc->instance->next_user_thread_id);
/* Add one, since MVM_incr returns original. */
Expand Down
5 changes: 5 additions & 0 deletions src/moar.c
Expand Up @@ -77,6 +77,11 @@ MVMInstance * MVM_vm_create_instance(void) {

/* Create the main thread's ThreadContext and stash it. */
instance->main_thread = MVM_tc_create(instance);

if (!instance->main_thread) {
MVM_panic(1, "Could not create the main thread!");
}

instance->main_thread->thread_id = 1;

/* Next thread to be created gets ID 2 (the main thread got ID 1). */
Expand Down

0 comments on commit acb20d7

Please sign in to comment.