diff --git a/runtime/include/sandbox.h b/runtime/include/sandbox.h index 299833a..79040ea 100644 --- a/runtime/include/sandbox.h +++ b/runtime/include/sandbox.h @@ -281,5 +281,6 @@ void sandbox_set_as_initialized(struct sandbox *sandbox, struct sandbox_request uint64_t allocation_timestamp); void sandbox_set_as_runnable(struct sandbox *sandbox); void sandbox_set_as_running(struct sandbox *sandbox); +void sandbox_set_as_blocked(struct sandbox *sandbox); void sandbox_set_as_preempted(struct sandbox *sandbox); void sandbox_set_as_complete(struct sandbox *sandbox); diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index eac9096..e0b9ee8 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -653,6 +653,39 @@ sandbox_set_as_preempted(struct sandbox *sandbox) sandbox->state = SANDBOX_PREEMPTED; } +/** + * Transitions a sandbox to the SANDBOX_BLOCKED state. + * This occurs when a sandbox is executing and it makes a blocking API call of some kind. + * Automatically removes the sandbox from the runqueue + * @param sandbox the blocking sandbox + */ +void +sandbox_set_as_blocked(struct sandbox *sandbox) +{ + assert(sandbox); + uint64_t now = __getcycles(); + uint64_t duration_of_last_state = now - sandbox->last_state_change_timestamp; + sandbox_state_t last_state = sandbox->state; + sandbox->state = SANDBOX_SET_AS_BLOCKED; + debuglog("Thread %lu | Sandbox %lu | %s => Blocked\n", pthread_self(), sandbox->allocation_timestamp, + sandbox_state_stringify(last_state)); + + switch (last_state) { + case SANDBOX_RUNNING: { + sandbox->running_duration += duration_of_last_state; + local_runqueue_delete(sandbox); + break; + } + default: { + panic("Thread %lu | Sandbox %lu | Illegal transition from %s to Blocked\n", pthread_self(), + sandbox->allocation_timestamp, sandbox_state_stringify(last_state)); + } + } + + sandbox->last_state_change_timestamp = now; + sandbox->state = SANDBOX_BLOCKED; +} + /** * Transitions a sandbox from the SANDBOX_RETURNED state to the SANDBOX_COMPLETE state. * Adds the sandbox to the completion queue diff --git a/runtime/src/worker_thread.c b/runtime/src/worker_thread.c index f389b0c..ce14ae3 100644 --- a/runtime/src/worker_thread.c +++ b/runtime/src/worker_thread.c @@ -161,8 +161,8 @@ worker_thread_block_current_sandbox(void) /* Remove the sandbox we were just executing from the runqueue and mark as blocked */ struct sandbox *current_sandbox = current_sandbox_get(); - local_runqueue_delete(current_sandbox); - current_sandbox->state = SANDBOX_BLOCKED; + sandbox_set_as_blocked(current_sandbox); + current_sandbox_set(NULL); /* Switch to the next sandbox */ struct sandbox *next_sandbox = local_runqueue_get_next();