From a70a83326cbea645106f939270c2b2f7c6dd693d Mon Sep 17 00:00:00 2001 From: Sean McBride Date: Thu, 19 Mar 2020 11:53:47 -0400 Subject: [PATCH] chore: de-dunder worker_thread --- runtime/include/arch/aarch64/context.h | 4 +- runtime/include/arch/x86_64/context.h | 16 +-- runtime/include/current_sandbox.h | 4 +- runtime/include/libuv_callbacks.h | 8 +- runtime/include/runtime.h | 18 ++-- runtime/include/sandbox.h | 20 ++-- runtime/src/env.c | 18 ++-- runtime/src/libc/uvio.c | 137 ++++++++++++------------- runtime/src/main.c | 2 +- runtime/src/runtime.c | 120 +++++++++++----------- runtime/src/sandbox.c | 19 ++-- runtime/src/software_interrupt.c | 12 +-- 12 files changed, 187 insertions(+), 191 deletions(-) diff --git a/runtime/include/arch/aarch64/context.h b/runtime/include/arch/aarch64/context.h index b789034..7fc2a2e 100644 --- a/runtime/include/arch/aarch64/context.h +++ b/runtime/include/arch/aarch64/context.h @@ -19,8 +19,8 @@ struct arch_context { typedef struct arch_context arch_context_t; -extern void __attribute__((noreturn)) worker_thread__sandbox_switch_preempt(void); -extern __thread arch_context_t worker_thread__base_context; +extern void __attribute__((noreturn)) worker_thread_sandbox_switch_preempt(void); +extern __thread arch_context_t worker_thread_base_context; static inline void arch_context_init(arch_context_t *actx, reg_t ip, reg_t sp) diff --git a/runtime/include/arch/x86_64/context.h b/runtime/include/arch/x86_64/context.h index e8cd4b3..4849b62 100644 --- a/runtime/include/arch/x86_64/context.h +++ b/runtime/include/arch/x86_64/context.h @@ -41,10 +41,10 @@ struct arch_context { mcontext_t mctx; }; -typedef struct arch_context arch_context_t; +typedef struct arch_context arch_context_t; -extern void __attribute__((noreturn)) worker_thread__sandbox_switch_preempt(void); -extern __thread arch_context_t worker_thread__base_context; +extern void __attribute__((noreturn)) worker_thread_sandbox_switch_preempt(void); +extern __thread arch_context_t worker_thread_base_context; static void __attribute__((noinline)) arch_context_init(arch_context_t *actx, reg_t ip, reg_t sp) { @@ -76,7 +76,7 @@ static void __attribute__((noinline)) arch_context_init(arch_context_t *actx, re static int arch_mcontext_restore(mcontext_t *mc, arch_context_t *ctx) { - assert(ctx != &worker_thread__base_context); + assert(ctx != &worker_thread_base_context); // if ctx->regs[5] is set, this was last in a user-level context switch state! // else restore mcontext.. @@ -97,7 +97,7 @@ arch_mcontext_restore(mcontext_t *mc, arch_context_t *ctx) static void arch_mcontext_save(arch_context_t *ctx, mcontext_t *mc) { - assert(ctx != &worker_thread__base_context); + assert(ctx != &worker_thread_base_context); ctx->regs[5] = 0; memcpy(&ctx->mctx, mc, sizeof(mcontext_t)); @@ -110,12 +110,12 @@ arch_context_switch(arch_context_t *ca, arch_context_t *na) if (!ca) { assert(na); // switching from "no sandbox to execute" state to "executing a sandbox" - ca = &worker_thread__base_context; + ca = &worker_thread_base_context; } else if (!na) { assert(ca); // switching from "executing a sandbox" to "no execution" state. - na = &worker_thread__base_context; + na = &worker_thread_base_context; } else { assert(na && ca); @@ -134,7 +134,7 @@ arch_context_switch(arch_context_t *ca, arch_context_t *na) "movq 40(%%rbx), %%rsp\n\t" "jmpq *128(%%rbx)\n\t" "1:\n\t" - "call worker_thread__sandbox_switch_preempt\n\t" + "call worker_thread_sandbox_switch_preempt\n\t" ".align 8\n\t" "2:\n\t" "movq $0, 40(%%rbx)\n\t" diff --git a/runtime/include/current_sandbox.h b/runtime/include/current_sandbox.h index dd5a3ce..79c9a3a 100644 --- a/runtime/include/current_sandbox.h +++ b/runtime/include/current_sandbox.h @@ -13,7 +13,7 @@ extern http_parser_settings runtime_http_parser_settings; static inline struct sandbox * current_sandbox_get(void) { - return worker_thread__current_sandbox; + return worker_thread_current_sandbox; } /** @@ -24,7 +24,7 @@ static inline void current_sandbox_set(struct sandbox *sandbox) { // FIXME: critical-section. - worker_thread__current_sandbox = sandbox; + worker_thread_current_sandbox = sandbox; if (sandbox == NULL) return; // Thread Local State about the Current Sandbox diff --git a/runtime/include/libuv_callbacks.h b/runtime/include/libuv_callbacks.h index 68c8a64..e8c3dc5 100644 --- a/runtime/include/libuv_callbacks.h +++ b/runtime/include/libuv_callbacks.h @@ -35,7 +35,7 @@ libuv_callbacks_on_read_parse_http_request(uv_stream_t *stream, ssize_t number_r // When the entire message has been read, stop the stream and wakeup the sandbox uv_read_stop(stream); - worker_thread__wakeup_sandbox(sandbox); + worker_thread_wakeup_sandbox(sandbox); } /** @@ -46,7 +46,7 @@ static inline void libuv_callbacks_on_close_wakeup_sakebox(uv_handle_t *stream) { struct sandbox *sandbox = stream->data; - worker_thread__wakeup_sandbox(sandbox); + worker_thread_wakeup_sandbox(sandbox); } /** @@ -58,7 +58,7 @@ static inline void libuv_callbacks_on_shutdown_wakeup_sakebox(uv_shutdown_t *req, int status) { struct sandbox *sandbox = req->data; - worker_thread__wakeup_sandbox(sandbox); + worker_thread_wakeup_sandbox(sandbox); } /** @@ -77,7 +77,7 @@ libuv_callbacks_on_write_wakeup_sandbox(uv_write_t *write, int status) libuv_callbacks_on_shutdown_wakeup_sakebox); return; } - worker_thread__wakeup_sandbox(sandbox); + worker_thread_wakeup_sandbox(sandbox); } static inline void diff --git a/runtime/include/runtime.h b/runtime/include/runtime.h index 19ab397..82d005e 100644 --- a/runtime/include/runtime.h +++ b/runtime/include/runtime.h @@ -11,7 +11,7 @@ extern int runtime_epoll_file_descriptor; extern struct deque_sandbox *runtime_global_deque; extern pthread_mutex_t runtime_global_deque_mutex; -extern __thread uv_loop_t worker_thread__uvio_handle; +extern __thread uv_loop_t worker_thread_uvio_handle; void alloc_linear_memory(void); void expand_memory(void); @@ -21,7 +21,7 @@ INLINE char *get_memory_ptr_for_runtime(u32 offset, u32 bounds_check); void runtime_initialize(void); void listener_thread_initialize(void); void stub_init(i32 offset); -void * worker_thread__main(void *return_code); +void * worker_thread_main(void *return_code); /** * Translates WASM offsets into runtime VM pointers @@ -30,7 +30,7 @@ void * worker_thread__main(void *return_code); * @return void pointer to something in WebAssembly linear memory **/ static inline void * -worker_thread__get_memory_ptr_void(u32 offset, u32 bounds_check) +worker_thread_get_memory_ptr_void(u32 offset, u32 bounds_check) { return (void *)get_memory_ptr_for_runtime(offset, bounds_check); } @@ -41,7 +41,7 @@ worker_thread__get_memory_ptr_void(u32 offset, u32 bounds_check) * @return char at the offset **/ static inline char -worker_thread__get_memory_character(u32 offset) +worker_thread_get_memory_character(u32 offset) { return get_memory_ptr_for_runtime(offset, 1)[0]; } @@ -53,11 +53,11 @@ worker_thread__get_memory_character(u32 offset) * @return pointer to the string or NULL if max_length is reached without finding null-terminator **/ static inline char * -worker_thread__get_memory_string(u32 offset, u32 max_length) +worker_thread_get_memory_string(u32 offset, u32 max_length) { for (int i = 0; i < max_length; i++) { - if (worker_thread__get_memory_character(offset + i) == '\0') - return worker_thread__get_memory_ptr_void(offset, 1); + if (worker_thread_get_memory_character(offset + i) == '\0') + return worker_thread_get_memory_ptr_void(offset, 1); } return NULL; } @@ -66,9 +66,9 @@ worker_thread__get_memory_string(u32 offset, u32 max_length) * Get global libuv handle **/ static inline uv_loop_t * -worker_thread__get_thread_libuv_handle(void) +worker_thread_get_libuv_handle(void) { - return &worker_thread__uvio_handle; + return &worker_thread_uvio_handle; } #endif /* SFRT_RUNTIME_H */ diff --git a/runtime/include/sandbox.h b/runtime/include/sandbox.h index f84bfd4..ce48cce 100644 --- a/runtime/include/sandbox.h +++ b/runtime/include/sandbox.h @@ -81,16 +81,16 @@ typedef struct sandbox sandbox_t; **************************/ -extern __thread struct sandbox *worker_thread__current_sandbox; -extern __thread arch_context_t *worker_thread__next_context; - -extern void worker_thread__block_current_sandbox(void); -extern void worker_thread__exit_current_sandbox(void); -extern struct sandbox *worker_thread__get_next_sandbox(int interrupt); -extern void worker_thread__process_io(void); -extern void worker_thread__push_sandbox_to_completion_queue(struct sandbox *sandbox); -extern void __attribute__((noreturn)) worker_thread__sandbox_switch_preempt(void); -extern void worker_thread__wakeup_sandbox(sandbox_t *sandbox); +extern __thread struct sandbox *worker_thread_current_sandbox; +extern __thread arch_context_t *worker_thread_next_context; + +extern void worker_thread_block_current_sandbox(void); +extern void worker_thread_exit_current_sandbox(void); +extern struct sandbox *worker_thread_get_next_sandbox(int interrupt); +extern void worker_thread_process_io(void); +extern void worker_thread_push_sandbox_to_completion_queue(struct sandbox *sandbox); +extern void __attribute__((noreturn)) worker_thread_sandbox_switch_preempt(void); +extern void worker_thread_wakeup_sandbox(sandbox_t *sandbox); /*************************** * Public API * diff --git a/runtime/src/env.c b/runtime/src/env.c index f09bc06..df80da8 100644 --- a/runtime/src/env.c +++ b/runtime/src/env.c @@ -37,7 +37,7 @@ env_a_ctz_64(u64 x) INLINE void env_a_and_64(i32 p_off, u64 v) { - uint64_t *p = worker_thread__get_memory_ptr_void(p_off, sizeof(uint64_t)); + uint64_t *p = worker_thread_get_memory_ptr_void(p_off, sizeof(uint64_t)); *p &= v; // __asm__( "lock ; and %1, %0" @@ -48,7 +48,7 @@ INLINE void env_a_or_64(i32 p_off, i64 v) { assert(sizeof(i64) == sizeof(uint64_t)); - uint64_t *p = worker_thread__get_memory_ptr_void(p_off, sizeof(i64)); + uint64_t *p = worker_thread_get_memory_ptr_void(p_off, sizeof(i64)); *p |= v; // __asm__( "lock ; or %1, %0" @@ -72,7 +72,7 @@ i32 env_a_cas(i32 p_off, i32 t, i32 s) { assert(sizeof(i32) == sizeof(volatile int)); - volatile int *p = worker_thread__get_memory_ptr_void(p_off, sizeof(i32)); + volatile int *p = worker_thread_get_memory_ptr_void(p_off, sizeof(i32)); __asm__("lock ; cmpxchg %3, %1" : "=a"(t), "=m"(*p) : "a"(t), "r"(s) : "memory"); @@ -83,7 +83,7 @@ void env_a_or(i32 p_off, i32 v) { assert(sizeof(i32) == sizeof(volatile int)); - volatile int *p = worker_thread__get_memory_ptr_void(p_off, sizeof(i32)); + volatile int *p = worker_thread_get_memory_ptr_void(p_off, sizeof(i32)); __asm__("lock ; or %1, %0" : "=m"(*p) : "r"(v) : "memory"); } @@ -97,7 +97,7 @@ i32 env_a_swap(i32 x_off, i32 v) { assert(sizeof(i32) == sizeof(volatile int)); - volatile int *x = worker_thread__get_memory_ptr_void(x_off, sizeof(i32)); + volatile int *x = worker_thread_get_memory_ptr_void(x_off, sizeof(i32)); __asm__("xchg %0, %1" : "=r"(v), "=m"(*x) : "0"(v) : "memory"); @@ -108,7 +108,7 @@ i32 env_a_fetch_add(i32 x_off, i32 v) { assert(sizeof(i32) == sizeof(volatile int)); - volatile int *x = worker_thread__get_memory_ptr_void(x_off, sizeof(i32)); + volatile int *x = worker_thread_get_memory_ptr_void(x_off, sizeof(i32)); __asm__("lock ; xadd %0, %1" : "=r"(v), "=m"(*x) : "0"(v) : "memory"); @@ -119,7 +119,7 @@ void env_a_inc(i32 x_off) { assert(sizeof(i32) == sizeof(volatile int)); - volatile int *x = worker_thread__get_memory_ptr_void(x_off, sizeof(i32)); + volatile int *x = worker_thread_get_memory_ptr_void(x_off, sizeof(i32)); __asm__("lock ; incl %0" : "=m"(*x) : "m"(*x) : "memory"); } @@ -128,7 +128,7 @@ void env_a_dec(i32 x_off) { assert(sizeof(i32) == sizeof(volatile int)); - volatile int *x = worker_thread__get_memory_ptr_void(x_off, sizeof(i32)); + volatile int *x = worker_thread_get_memory_ptr_void(x_off, sizeof(i32)); __asm__("lock ; decl %0" : "=m"(*x) : "m"(*x) : "memory"); } @@ -137,7 +137,7 @@ void env_a_store(i32 p_off, i32 x) { assert(sizeof(i32) == sizeof(volatile int)); - volatile int *p = worker_thread__get_memory_ptr_void(p_off, sizeof(i32)); + volatile int *p = worker_thread_get_memory_ptr_void(p_off, sizeof(i32)); __asm__ __volatile__("mov %1, %0 ; lock ; orl $0,(%%esp)" : "=m"(*p) : "r"(x) : "memory"); } diff --git a/runtime/src/libc/uvio.c b/runtime/src/libc/uvio.c index eaeaade..c065210 100644 --- a/runtime/src/libc/uvio.c +++ b/runtime/src/libc/uvio.c @@ -96,7 +96,7 @@ static void wasm_fs_callback(uv_fs_t *req) { debuglog("[%p]\n", req->data); - worker_thread__wakeup_sandbox((sandbox_t *)req->data); + worker_thread_wakeup_sandbox((sandbox_t *)req->data); } // We define our own syscall numbers, because WASM uses x86_64 values even on systems that are not x86_64 @@ -105,7 +105,7 @@ u32 wasm_read(i32 filedes, i32 buf_offset, i32 nbyte) { if (filedes == 0) { - char * buffer = worker_thread__get_memory_ptr_void(buf_offset, nbyte); + char * buffer = worker_thread_get_memory_ptr_void(buf_offset, nbyte); struct sandbox * s = current_sandbox_get(); struct http_request *r = &s->http_request; if (r->body_length <= 0) return 0; @@ -118,12 +118,12 @@ wasm_read(i32 filedes, i32 buf_offset, i32 nbyte) int f = current_sandbox_get_file_descriptor(filedes); // TODO: read on other file types uv_fs_t req = UV_FS_REQ_INIT(); - char * buffer = worker_thread__get_memory_ptr_void(buf_offset, nbyte); + char * buffer = worker_thread_get_memory_ptr_void(buf_offset, nbyte); debuglog("[%p] start[%d:%d, n%d]\n", uv_fs_get_data(&req), filedes, f, nbyte); uv_buf_t bufv = uv_buf_init(buffer, nbyte); - uv_fs_read(worker_thread__get_thread_libuv_handle(), &req, f, &bufv, 1, -1, wasm_fs_callback); - worker_thread__block_current_sandbox(); + uv_fs_read(worker_thread_get_libuv_handle(), &req, f, &bufv, 1, -1, wasm_fs_callback); + worker_thread_block_current_sandbox(); int ret = uv_fs_get_result(&req); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); @@ -137,7 +137,7 @@ i32 wasm_write(i32 file_descriptor, i32 buf_offset, i32 buf_size) { if (file_descriptor == 1 || file_descriptor == 2) { - char * buffer = worker_thread__get_memory_ptr_void(buf_offset, buf_size); + char * buffer = worker_thread_get_memory_ptr_void(buf_offset, buf_size); struct sandbox *s = current_sandbox_get(); int l = s->module->max_response_size - s->request_response_data_length; l = l > buf_size ? buf_size : l; @@ -150,11 +150,11 @@ wasm_write(i32 file_descriptor, i32 buf_offset, i32 buf_size) int f = current_sandbox_get_file_descriptor(file_descriptor); // TODO: read on other file types uv_fs_t req = UV_FS_REQ_INIT(); - char * buffer = worker_thread__get_memory_ptr_void(buf_offset, buf_size); + char * buffer = worker_thread_get_memory_ptr_void(buf_offset, buf_size); uv_buf_t bufv = uv_buf_init(buffer, buf_size); - uv_fs_write(worker_thread__get_thread_libuv_handle(), &req, f, &bufv, 1, -1, wasm_fs_callback); - worker_thread__block_current_sandbox(); + uv_fs_write(worker_thread_get_libuv_handle(), &req, f, &bufv, 1, -1, wasm_fs_callback); + worker_thread_block_current_sandbox(); int ret = uv_fs_get_result(&req); uv_fs_req_cleanup(&req); @@ -183,7 +183,7 @@ i32 wasm_open(i32 path_off, i32 flags, i32 mode) { uv_fs_t req = UV_FS_REQ_INIT(); - char * path = worker_thread__get_memory_string(path_off, MODULE__MAX_PATH_LENGTH); + char * path = worker_thread_get_memory_string(path_off, MODULE__MAX_PATH_LENGTH); int iofd = current_sandbox_initialize_io_handle(); if (iofd < 0) return -1; @@ -196,8 +196,8 @@ wasm_open(i32 path_off, i32 flags, i32 mode) if (flags & WO_EXCL) modified_flags |= O_EXCL; debuglog("[%p] start[%s:%d:%d]\n", uv_fs_get_data(&req), path, flags, modified_flags); - uv_fs_open(worker_thread__get_thread_libuv_handle(), &req, path, modified_flags, mode, wasm_fs_callback); - worker_thread__block_current_sandbox(); + uv_fs_open(worker_thread_get_libuv_handle(), &req, path, modified_flags, mode, wasm_fs_callback); + worker_thread_block_current_sandbox(); int ret = uv_fs_get_result(&req); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); @@ -233,8 +233,8 @@ wasm_close(i32 file_descriptor) uv_fs_t req = UV_FS_REQ_INIT(); debuglog("[%p] file[%d,%d]\n", uv_fs_get_data(&req), file_descriptor, d); - uv_fs_close(worker_thread__get_thread_libuv_handle(), &req, d, wasm_fs_callback); - worker_thread__block_current_sandbox(); + uv_fs_close(worker_thread_get_libuv_handle(), &req, d, wasm_fs_callback); + worker_thread_block_current_sandbox(); int ret = uv_fs_get_result(&req); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); @@ -296,8 +296,8 @@ struct wasm_stat { i32 wasm_stat(u32 path_str_offset, i32 stat_offset) { - char * path = worker_thread__get_memory_string(path_str_offset, MODULE__MAX_PATH_LENGTH); - struct wasm_stat *stat_ptr = worker_thread__get_memory_ptr_void(stat_offset, sizeof(struct wasm_stat)); + char * path = worker_thread_get_memory_string(path_str_offset, MODULE__MAX_PATH_LENGTH); + struct wasm_stat *stat_ptr = worker_thread_get_memory_ptr_void(stat_offset, sizeof(struct wasm_stat)); struct stat stat; i32 res = lstat(path, &stat); @@ -342,7 +342,7 @@ wasm_stat(u32 path_str_offset, i32 stat_offset) i32 wasm_fstat(i32 filedes, i32 stat_offset) { - struct wasm_stat *stat_ptr = worker_thread__get_memory_ptr_void(stat_offset, sizeof(struct wasm_stat)); + struct wasm_stat *stat_ptr = worker_thread_get_memory_ptr_void(stat_offset, sizeof(struct wasm_stat)); struct stat stat; int d = current_sandbox_get_file_descriptor(filedes); @@ -388,8 +388,8 @@ wasm_fstat(i32 filedes, i32 stat_offset) i32 wasm_lstat(i32 path_str_offset, i32 stat_offset) { - char * path = worker_thread__get_memory_string(path_str_offset, MODULE__MAX_PATH_LENGTH); - struct wasm_stat *stat_ptr = worker_thread__get_memory_ptr_void(stat_offset, sizeof(struct wasm_stat)); + char * path = worker_thread_get_memory_string(path_str_offset, MODULE__MAX_PATH_LENGTH); + struct wasm_stat *stat_ptr = worker_thread_get_memory_ptr_void(stat_offset, sizeof(struct wasm_stat)); struct stat stat; i32 res = lstat(path, &stat); @@ -498,15 +498,15 @@ wasm_readv(i32 file_descriptor, i32 iov_offset, i32 iovcnt) if (file_descriptor == 0) { // both 1 and 2 go to client. int len = 0; - struct wasm_iovec * iov = worker_thread__get_memory_ptr_void(iov_offset, - iovcnt * sizeof(struct wasm_iovec)); + struct wasm_iovec * iov = worker_thread_get_memory_ptr_void(iov_offset, + iovcnt * sizeof(struct wasm_iovec)); struct sandbox * s = current_sandbox_get(); struct http_request *r = &s->http_request; if (r->body_length <= 0) return 0; for (int i = 0; i < iovcnt; i++) { int l = iov[i].len > r->body_length ? r->body_length : iov[i].len; if (l <= 0) break; - char *b = worker_thread__get_memory_ptr_void(iov[i].base_offset, iov[i].len); + char *b = worker_thread_get_memory_ptr_void(iov[i].base_offset, iov[i].len); // http request body! memcpy(b, r->body + r->body_read_length + len, l); len += l; @@ -519,7 +519,7 @@ wasm_readv(i32 file_descriptor, i32 iov_offset, i32 iovcnt) // TODO: read on other file types int gret = 0; int d = current_sandbox_get_file_descriptor(file_descriptor); - struct wasm_iovec *iov = worker_thread__get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); + struct wasm_iovec *iov = worker_thread_get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); for (int i = 0; i < iovcnt; i += RUNTIME__READ_WRITE_VECTOR_LENGTH) { uv_fs_t req = UV_FS_REQ_INIT(); @@ -527,13 +527,12 @@ wasm_readv(i32 file_descriptor, i32 iov_offset, i32 iovcnt) int j = 0; for (j = 0; j < RUNTIME__READ_WRITE_VECTOR_LENGTH && i + j < iovcnt; j++) { - bufs[j] = uv_buf_init(worker_thread__get_memory_ptr_void(iov[i + j].base_offset, - iov[i + j].len), + bufs[j] = uv_buf_init(worker_thread_get_memory_ptr_void(iov[i + j].base_offset, iov[i + j].len), iov[i + j].len); } debuglog("[%p] start[%d,%d, n%d:%d]\n", uv_fs_get_data(&req), file_descriptor, d, i, j); - uv_fs_read(worker_thread__get_thread_libuv_handle(), &req, d, bufs, j, -1, wasm_fs_callback); - worker_thread__block_current_sandbox(); + uv_fs_read(worker_thread_get_libuv_handle(), &req, d, bufs, j, -1, wasm_fs_callback); + worker_thread_block_current_sandbox(); int ret = uv_fs_get_result(&req); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); @@ -554,10 +553,10 @@ wasm_writev(i32 file_descriptor, i32 iov_offset, i32 iovcnt) if (file_descriptor == 1 || file_descriptor == 2) { // both 1 and 2 go to client. int len = 0; - struct wasm_iovec *iov = worker_thread__get_memory_ptr_void(iov_offset, - iovcnt * sizeof(struct wasm_iovec)); + struct wasm_iovec *iov = worker_thread_get_memory_ptr_void(iov_offset, + iovcnt * sizeof(struct wasm_iovec)); for (int i = 0; i < iovcnt; i++) { - char *b = worker_thread__get_memory_ptr_void(iov[i].base_offset, iov[i].len); + char *b = worker_thread_get_memory_ptr_void(iov[i].base_offset, iov[i].len); memcpy(c->request_response_data + c->request_response_data_length, b, iov[i].len); c->request_response_data_length += iov[i].len; len += iov[i].len; @@ -568,7 +567,7 @@ wasm_writev(i32 file_descriptor, i32 iov_offset, i32 iovcnt) // TODO: read on other file types int d = current_sandbox_get_file_descriptor(file_descriptor); int gret = 0; - struct wasm_iovec *iov = worker_thread__get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); + struct wasm_iovec *iov = worker_thread_get_memory_ptr_void(iov_offset, iovcnt * sizeof(struct wasm_iovec)); for (int i = 0; i < iovcnt; i += RUNTIME__READ_WRITE_VECTOR_LENGTH) { uv_fs_t req = UV_FS_REQ_INIT(); @@ -576,13 +575,12 @@ wasm_writev(i32 file_descriptor, i32 iov_offset, i32 iovcnt) int j = 0; for (j = 0; j < RUNTIME__READ_WRITE_VECTOR_LENGTH && i + j < iovcnt; j++) { - bufs[j] = uv_buf_init(worker_thread__get_memory_ptr_void(iov[i + j].base_offset, - iov[i + j].len), + bufs[j] = uv_buf_init(worker_thread_get_memory_ptr_void(iov[i + j].base_offset, iov[i + j].len), iov[i + j].len); } debuglog("[%p] start[%d,%d, n%d:%d]\n", uv_fs_get_data(&req), file_descriptor, d, i, j); - uv_fs_write(worker_thread__get_thread_libuv_handle(), &req, d, bufs, j, -1, wasm_fs_callback); - worker_thread__block_current_sandbox(); + uv_fs_write(worker_thread_get_libuv_handle(), &req, d, bufs, j, -1, wasm_fs_callback); + worker_thread_block_current_sandbox(); int ret = uv_fs_get_result(&req); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); @@ -643,8 +641,8 @@ wasm_fsync(u32 file_descriptor) int d = current_sandbox_get_file_descriptor(file_descriptor); uv_fs_t req = UV_FS_REQ_INIT(); debuglog("[%p] start[%d,%d]\n", uv_fs_get_data(&req), file_descriptor, d); - uv_fs_fsync(worker_thread__get_thread_libuv_handle(), &req, d, wasm_fs_callback); - worker_thread__block_current_sandbox(); + uv_fs_fsync(worker_thread_get_libuv_handle(), &req, d, wasm_fs_callback); + worker_thread_block_current_sandbox(); int ret = uv_fs_get_result(&req); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); @@ -657,7 +655,7 @@ wasm_fsync(u32 file_descriptor) u32 wasm_getcwd(u32 buf_offset, u32 buf_size) { - char *buffer = worker_thread__get_memory_ptr_void(buf_offset, buf_size); + char *buffer = worker_thread_get_memory_ptr_void(buf_offset, buf_size); char *res = getcwd(buffer, buf_size); if (!res) return 0; @@ -668,11 +666,11 @@ wasm_getcwd(u32 buf_offset, u32 buf_size) u32 wasm_unlink(u32 path_str_offset) { - char * path = worker_thread__get_memory_string(path_str_offset, MODULE__MAX_PATH_LENGTH); + char * path = worker_thread_get_memory_string(path_str_offset, MODULE__MAX_PATH_LENGTH); uv_fs_t req = UV_FS_REQ_INIT(); debuglog("[%p] start[%s]\n", uv_fs_get_data(&req), path); - uv_fs_unlink(worker_thread__get_thread_libuv_handle(), &req, path, wasm_fs_callback); - worker_thread__block_current_sandbox(); + uv_fs_unlink(worker_thread_get_libuv_handle(), &req, path, wasm_fs_callback); + worker_thread_block_current_sandbox(); int ret = uv_fs_get_result(&req); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); @@ -716,8 +714,8 @@ wasm_get_time(i32 clock_id, i32 timespec_off) assert(0); } - struct wasm_time_spec *timespec = worker_thread__get_memory_ptr_void(timespec_off, - sizeof(struct wasm_time_spec)); + struct wasm_time_spec *timespec = worker_thread_get_memory_ptr_void(timespec_off, + sizeof(struct wasm_time_spec)); struct timespec native_timespec = { 0, 0 }; int res = clock_gettime(real_clock, &native_timespec); @@ -744,8 +742,8 @@ wasm_fchown(i32 file_descriptor, u32 owner, u32 group) int d = current_sandbox_get_file_descriptor(file_descriptor); uv_fs_t req = UV_FS_REQ_INIT(); debuglog("[%p] start[%d,%d]\n", uv_fs_get_data(&req), file_descriptor, d); - uv_fs_fchown(worker_thread__get_thread_libuv_handle(), &req, d, owner, group, wasm_fs_callback); - worker_thread__block_current_sandbox(); + uv_fs_fchown(worker_thread_get_libuv_handle(), &req, d, owner, group, wasm_fs_callback); + worker_thread_block_current_sandbox(); int ret = uv_fs_get_result(&req); debuglog("[%p] end[%d]\n", uv_fs_get_data(&req), ret); @@ -767,7 +765,7 @@ wasm_connection_callback(uv_stream_t *srv, int status) sandbox_t *s = srv->data; debuglog(" [%p]\n", s); s->return_value = status; - worker_thread__wakeup_sandbox(s); + worker_thread_wakeup_sandbox(s); } static void @@ -777,7 +775,7 @@ wasm_connect_callback(uv_connect_t *req, int status) sandbox_t *s = req->data; debuglog(" [%p]\n", s); s->return_value = status; - worker_thread__wakeup_sandbox(s); + worker_thread_wakeup_sandbox(s); } i32 @@ -793,12 +791,12 @@ wasm_socket(i32 domain, i32 type, i32 protocol) if (type & SOCK_DGRAM) { uv_udp_t *uh = (uv_udp_t *)h; uh->data = c; - uv_udp_init(worker_thread__get_thread_libuv_handle(), uh); + uv_udp_init(worker_thread_get_libuv_handle(), uh); debuglog(" udp init done!\n"); } else if (type & SOCK_STREAM) { uv_tcp_t *uh = (uv_tcp_t *)h; uh->data = c; - uv_tcp_init(worker_thread__get_thread_libuv_handle(), uh); + uv_tcp_init(worker_thread_get_libuv_handle(), uh); debuglog(" tcp init done!\n"); } else { assert(0); // not supported yet! @@ -819,17 +817,16 @@ wasm_connect(i32 sockfd, i32 sockaddr_offset, i32 addrlen) if (t == UV_TCP) { uv_connect_t req = { .data = c }; debuglog("[%p] connect\n", c); - int r = uv_tcp_connect(&req, (uv_tcp_t *)h, - worker_thread__get_memory_ptr_void(sockaddr_offset, addrlen), + int r = uv_tcp_connect(&req, (uv_tcp_t *)h, worker_thread_get_memory_ptr_void(sockaddr_offset, addrlen), wasm_connect_callback); - worker_thread__block_current_sandbox(); + worker_thread_block_current_sandbox(); debuglog("[%p] %d\n", c, c->return_value); return c->return_value; } else if (t == UV_UDP) { debuglog(" UDP connect not implemented!\n"); // TODO: this api is in the doc online but not in the libuv installed.. perhaps update?? - // return uv_udp_connect((uv_udp_t *)h, worker_thread__get_memory_ptr_void(sockaddr_offset, addrlen)); + // return uv_udp_connect((uv_udp_t *)h, worker_thread_get_memory_ptr_void(sockaddr_offset, addrlen)); } debuglog(" unsupported\n"); assert(0); @@ -840,8 +837,8 @@ i32 wasm_accept(i32 sockfd, i32 sockaddr_offset, i32 addrlen_offset) { // what do we do with the sockaddr TODO: ???? - socklen_t * addrlen = worker_thread__get_memory_ptr_void(addrlen_offset, sizeof(socklen_t)); - struct sockaddr * socket_address = worker_thread__get_memory_ptr_void(sockaddr_offset, *addrlen); + socklen_t * addrlen = worker_thread_get_memory_ptr_void(addrlen_offset, sizeof(socklen_t)); + struct sockaddr * socket_address = worker_thread_get_memory_ptr_void(sockaddr_offset, *addrlen); union uv_any_handle *s = current_sandbox_get_libuv_handle(sockfd); int cfd = current_sandbox_initialize_io_handle(); if (cfd < 0) return -1; @@ -851,7 +848,7 @@ wasm_accept(i32 sockfd, i32 sockaddr_offset, i32 addrlen_offset) // assert so we can look into whether we need to implement UDP or others.. assert(((uv_handle_t *)s)->type == UV_TCP); union uv_any_handle *h = current_sandbox_get_libuv_handle(cfd); - uv_tcp_init(worker_thread__get_thread_libuv_handle(), (uv_tcp_t *)h); + uv_tcp_init(worker_thread_get_libuv_handle(), (uv_tcp_t *)h); debuglog("[%p] tcp init %d\n", c, cfd); int r = uv_accept((uv_stream_t *)s, (uv_stream_t *)h); if (r < 0) return r; @@ -879,7 +876,7 @@ wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen) if (t == UV_TCP) { debuglog("[%p] tcp\n", c); - int r1 = uv_tcp_bind((uv_tcp_t *)h, worker_thread__get_memory_ptr_void(sockaddr_offset, addrlen), + int r1 = uv_tcp_bind((uv_tcp_t *)h, worker_thread_get_memory_ptr_void(sockaddr_offset, addrlen), 0 /* TODO: flags */); if (file_descriptor == SANDBOX__FILE_DESCRIPTOR_PREOPEN_MAGIC) { int r2 = -1, f = -1; @@ -890,7 +887,7 @@ wasm_bind(i32 sockfd, i32 sockaddr_offset, i32 addrlen) return r1; } else if (t == UV_UDP) { debuglog("[%p] udp\n", c); - int r1 = uv_udp_bind((uv_udp_t *)h, worker_thread__get_memory_ptr_void(sockaddr_offset, addrlen), + int r1 = uv_udp_bind((uv_udp_t *)h, worker_thread_get_memory_ptr_void(sockaddr_offset, addrlen), 0 /* TODO: flags */); if (file_descriptor == SANDBOX__FILE_DESCRIPTOR_PREOPEN_MAGIC) { int r2 = -1, f = -1; @@ -918,7 +915,7 @@ wasm_listen(i32 sockfd, i32 backlog) assert(t == UV_TCP); int r = uv_listen((uv_stream_t *)h, backlog, wasm_connection_callback); - worker_thread__block_current_sandbox(); + worker_thread_block_current_sandbox(); debuglog("[%p] %d\n", c, c->return_value); return c->return_value; @@ -937,7 +934,7 @@ wasm_read_callback(uv_stream_t *s, ssize_t nread, const uv_buf_t *buffer) c->read_length = nread; debuglog("[%p] %ld\n", c, c->read_length); uv_read_stop(s); - worker_thread__wakeup_sandbox(c); + worker_thread_wakeup_sandbox(c); } void @@ -947,7 +944,7 @@ wasm_write_callback(uv_write_t *req, int status) c->return_value = status; debuglog("[%p] %d\n", c, status); - worker_thread__wakeup_sandbox(c); + worker_thread_wakeup_sandbox(c); } void @@ -961,7 +958,7 @@ wasm_udp_recv_callback(uv_udp_t *h, ssize_t nread, const uv_buf_t *buffer, const c->read_length = nread; debuglog("[%p] %ld\n", c, c->read_length); uv_udp_recv_stop(h); - worker_thread__wakeup_sandbox(c); + worker_thread_wakeup_sandbox(c); } void @@ -971,13 +968,13 @@ wasm_udp_send_callback(uv_udp_send_t *req, int status) c->return_value = status; debuglog("[%p] %d\n", c, status); - worker_thread__wakeup_sandbox(c); + worker_thread_wakeup_sandbox(c); } i32 wasm_sendto(i32 file_descriptor, i32 buff_offset, i32 len, i32 flags, i32 sockaddr_offset, i32 sockaddr_len) { - char *buffer = worker_thread__get_memory_ptr_void(buff_offset, len); + char *buffer = worker_thread_get_memory_ptr_void(buff_offset, len); // TODO: only support "send" api for now assert(sockaddr_len == 0); struct sandbox * c = current_sandbox_get(); @@ -992,7 +989,7 @@ wasm_sendto(i32 file_descriptor, i32 buff_offset, i32 len, i32 flags, i32 sockad uv_buf_t b = uv_buf_init(buffer, len); debuglog("[%p] tcp\n", c); int ret = uv_write(&req, (uv_stream_t *)h, &b, 1, wasm_write_callback); - worker_thread__block_current_sandbox(); + worker_thread_block_current_sandbox(); debuglog("[%p] %d\n", c, c->return_value); return c->return_value; @@ -1004,7 +1001,7 @@ wasm_sendto(i32 file_descriptor, i32 buff_offset, i32 len, i32 flags, i32 sockad debuglog("[%p] udp\n", c); // TODO: sockaddr! int r = uv_udp_send(&req, (uv_udp_t *)h, &b, 1, NULL, wasm_udp_send_callback); - worker_thread__block_current_sandbox(); + worker_thread_block_current_sandbox(); debuglog("[%p] %d\n", c, c->return_value); return c->return_value; @@ -1027,8 +1024,8 @@ wasm_alloc_callback(uv_handle_t *h, size_t suggested, uv_buf_t *buffer) i32 wasm_recvfrom(i32 file_descriptor, i32 buff_offset, i32 size, i32 flags, i32 sockaddr_offset, i32 socklen_offset) { - char * buffer = worker_thread__get_memory_ptr_void(buff_offset, size); - socklen_t *len = worker_thread__get_memory_ptr_void(socklen_offset, sizeof(socklen_t)); + char * buffer = worker_thread_get_memory_ptr_void(buff_offset, size); + socklen_t *len = worker_thread_get_memory_ptr_void(socklen_offset, sizeof(socklen_t)); // TODO: only support "recv" api for now assert(*len == 0); struct sandbox * c = current_sandbox_get(); @@ -1050,7 +1047,7 @@ wasm_recvfrom(i32 file_descriptor, i32 buff_offset, i32 size, i32 flags, i32 soc ((uv_stream_t *)h)->data = c; debuglog("[%p] tcp\n", c); int r = uv_read_start((uv_stream_t *)h, wasm_alloc_callback, wasm_read_callback); - worker_thread__block_current_sandbox(); + worker_thread_block_current_sandbox(); debuglog("[%p] %d\n", c, c->return_value); if (c->return_value == -EIO) { // TODO: buffer errors?? @@ -1061,7 +1058,7 @@ wasm_recvfrom(i32 file_descriptor, i32 buff_offset, i32 size, i32 flags, i32 soc ((uv_udp_t *)h)->data = c; debuglog("[%p] udp\n", c); int r = uv_udp_recv_start((uv_udp_t *)h, wasm_alloc_callback, wasm_udp_recv_callback); - worker_thread__block_current_sandbox(); + worker_thread_block_current_sandbox(); debuglog("[%p] %d\n", c, c->return_value); if (c->return_value == -EIO) { // TODO: buffer errors?? diff --git a/runtime/src/main.c b/runtime/src/main.c index 6719e42..744be41 100644 --- a/runtime/src/main.c +++ b/runtime/src/main.c @@ -123,7 +123,7 @@ void runtime_start_runtime_worker_threads() { for (int i = 0; i < runtime_total_worker_processors; i++) { - int ret = pthread_create(&runtime_worker_threads[i], NULL, worker_thread__main, + int ret = pthread_create(&runtime_worker_threads[i], NULL, worker_thread_main, (void *)&runtime_worker_threads_argument[i]); if (ret) { errno = ret; diff --git a/runtime/src/runtime.c b/runtime/src/runtime.c index 8c740a5..ac1a4b8 100644 --- a/runtime/src/runtime.c +++ b/runtime/src/runtime.c @@ -145,30 +145,30 @@ listener_thread_initialize(void) * Worker Thread State * **************************/ -__thread static struct ps_list_head worker_thread__run_queue; -__thread static struct ps_list_head worker_thread__completion_queue; +__thread static struct ps_list_head worker_thread_run_queue; +__thread static struct ps_list_head worker_thread_completion_queue; // current sandbox that is active.. -__thread sandbox_t *worker_thread__current_sandbox = NULL; +__thread sandbox_t *worker_thread_current_sandbox = NULL; // context pointer to switch to when this thread gets a SIGUSR1 -__thread arch_context_t *worker_thread__next_context = NULL; +__thread arch_context_t *worker_thread_next_context = NULL; // context of the runtime thread before running sandboxes or to resume its "main". -__thread arch_context_t worker_thread__base_context; +__thread arch_context_t worker_thread_base_context; // libuv i/o loop handle per sandboxing thread! -__thread uv_loop_t worker_thread__uvio_handle; +__thread uv_loop_t worker_thread_uvio_handle; // Flag to signify if the thread is currently running callbacks in the libuv event loop -static __thread unsigned int worker_thread__is_in_callback; +static __thread unsigned int worker_thread_is_in_callback; /************************************************** * Worker Thread Logic *************************************************/ -static inline void worker_thread__push_sandbox_to_run_queue(struct sandbox *sandbox); +static inline void worker_thread_push_sandbox_to_run_queue(struct sandbox *sandbox); /** * @brief Switches to the next sandbox, placing the current sandbox of the completion queue if in RETURNED state @@ -176,7 +176,7 @@ static inline void worker_thread__push_sandbox_to_run_queue(struct sandbox *sand * @return void */ static inline void -worker_thread__switch_to_sandbox(struct sandbox *next_sandbox) +worker_thread_switch_to_sandbox(struct sandbox *next_sandbox) { arch_context_t *next_register_context = next_sandbox == NULL ? NULL : &next_sandbox->ctxt; software_interrupt_disable(); @@ -185,8 +185,8 @@ worker_thread__switch_to_sandbox(struct sandbox *next_sandbox) current_sandbox_set(next_sandbox); // If the current sandbox we're switching from is in a RETURNED state, add to completion queue if (current_sandbox && current_sandbox->state == RETURNED) - worker_thread__push_sandbox_to_completion_queue(current_sandbox); - worker_thread__next_context = next_register_context; + worker_thread_push_sandbox_to_completion_queue(current_sandbox); + worker_thread_next_context = next_register_context; arch_context_switch(current_register_context, next_register_context); software_interrupt_enable(); } @@ -196,7 +196,7 @@ worker_thread__switch_to_sandbox(struct sandbox *next_sandbox) * @param sandbox the sandbox to check and update if blocked **/ void -worker_thread__wakeup_sandbox(sandbox_t *sandbox) +worker_thread_wakeup_sandbox(sandbox_t *sandbox) { software_interrupt_disable(); debuglog("[%p: %s]\n", sandbox, sandbox->module->name); @@ -204,7 +204,7 @@ worker_thread__wakeup_sandbox(sandbox_t *sandbox) assert(sandbox->state == BLOCKED); assert(ps_list_singleton_d(sandbox)); sandbox->state = RUNNABLE; - ps_list_head_append_d(&worker_thread__run_queue, sandbox); + ps_list_head_append_d(&worker_thread_run_queue, sandbox); done: software_interrupt_enable(); } @@ -215,18 +215,18 @@ done: *of the runqueue **/ void -worker_thread__block_current_sandbox(void) +worker_thread_block_current_sandbox(void) { - assert(worker_thread__is_in_callback == 0); + assert(worker_thread_is_in_callback == 0); software_interrupt_disable(); struct sandbox *current_sandbox = current_sandbox_get(); ps_list_rem_d(current_sandbox); current_sandbox->state = BLOCKED; - struct sandbox *next_sandbox = worker_thread__get_next_sandbox(0); + struct sandbox *next_sandbox = worker_thread_get_next_sandbox(0); debuglog("[%p: %next_sandbox, %p: %next_sandbox]\n", current_sandbox, current_sandbox->module->name, next_sandbox, next_sandbox ? next_sandbox->module->name : ""); software_interrupt_enable(); - worker_thread__switch_to_sandbox(next_sandbox); + worker_thread_switch_to_sandbox(next_sandbox); } @@ -234,16 +234,16 @@ worker_thread__block_current_sandbox(void) * Execute I/O **/ void -worker_thread__process_io(void) +worker_thread_process_io(void) { #ifdef USE_HTTP_UVIO #ifdef USE_HTTP_SYNC // realistically, we're processing all async I/O on this core when a sandbox blocks on http processing, not // great! if there is a way (TODO), perhaps RUN_ONCE and check if your I/O is processed, if yes, return else do // async block! - uv_run(worker_thread__get_thread_libuv_handle(), UV_RUN_DEFAULT); + uv_run(worker_thread_get_libuv_handle(), UV_RUN_DEFAULT); #else /* USE_HTTP_SYNC */ - worker_thread__block_current_sandbox(); + worker_thread_block_current_sandbox(); #endif /* USE_HTTP_UVIO */ #else assert(0); @@ -254,7 +254,7 @@ worker_thread__process_io(void) /** * TODO: What is this doing? **/ -void __attribute__((noinline)) __attribute__((noreturn)) worker_thread__sandbox_switch_preempt(void) +void __attribute__((noinline)) __attribute__((noreturn)) worker_thread_sandbox_switch_preempt(void) { pthread_kill(pthread_self(), SIGUSR1); @@ -269,7 +269,7 @@ void __attribute__((noinline)) __attribute__((noreturn)) worker_thread__sandbox_ * @return the number of sandbox requests pulled */ static inline int -worker_thread__pull_and_process_sandbox_requests(void) +worker_thread_pull_and_process_sandbox_requests(void) { int total_sandboxes_pulled = 0; @@ -285,7 +285,7 @@ worker_thread__pull_and_process_sandbox_requests(void) free(sandbox_request); // Set the sandbox as runnable and place on the local runqueue sandbox->state = RUNNABLE; - worker_thread__push_sandbox_to_run_queue(sandbox); + worker_thread_push_sandbox_to_run_queue(sandbox); total_sandboxes_pulled++; } @@ -296,28 +296,28 @@ worker_thread__pull_and_process_sandbox_requests(void) * Run all outstanding events in the local thread's libuv event loop **/ void -worker_thread__execute_libuv_event_loop(void) +worker_thread_execute_libuv_event_loop(void) { - worker_thread__is_in_callback = 1; - int n = uv_run(worker_thread__get_thread_libuv_handle(), UV_RUN_NOWAIT), i = 0; + worker_thread_is_in_callback = 1; + int n = uv_run(worker_thread_get_libuv_handle(), UV_RUN_NOWAIT), i = 0; while (n > 0) { n--; - uv_run(worker_thread__get_thread_libuv_handle(), UV_RUN_NOWAIT); + uv_run(worker_thread_get_libuv_handle(), UV_RUN_NOWAIT); } - worker_thread__is_in_callback = 0; + worker_thread_is_in_callback = 0; } /** - * Append the sandbox to the worker_thread__run_queue + * Append the sandbox to the worker_thread_run_queue * @param sandbox sandbox to add */ static inline void -worker_thread__push_sandbox_to_run_queue(struct sandbox *sandbox) +worker_thread_push_sandbox_to_run_queue(struct sandbox *sandbox) { assert(ps_list_singleton_d(sandbox)); // fprintf(stderr, "(%d,%lu) %s: run %p, %s\n", sched_getcpu(), pthread_self(), __func__, s, // s->module->name); - ps_list_head_append_d(&worker_thread__run_queue, sandbox); + ps_list_head_append_d(&worker_thread_run_queue, sandbox); } /** @@ -325,7 +325,7 @@ worker_thread__push_sandbox_to_run_queue(struct sandbox *sandbox) * @param sandbox sandbox **/ static inline void -worker_thread__pop_sandbox_from_run_queue(struct sandbox *sandbox) +worker_thread_pop_sandbox_from_run_queue(struct sandbox *sandbox) { ps_list_rem_d(sandbox); } @@ -337,14 +337,14 @@ worker_thread__pop_sandbox_from_run_queue(struct sandbox *sandbox) * @return the sandbox to execute or NULL if none are available **/ struct sandbox * -worker_thread__get_next_sandbox(int in_interrupt) +worker_thread_get_next_sandbox(int in_interrupt) { // If the thread local runqueue is empty and we're not running in the context of an interupt, // pull a fresh batch of sandbox requests from the global queue - if (ps_list_head_empty(&worker_thread__run_queue)) { + if (ps_list_head_empty(&worker_thread_run_queue)) { // this is in an interrupt context, don't steal work here! if (in_interrupt) return NULL; - if (worker_thread__pull_and_process_sandbox_requests() == 0) { + if (worker_thread_pull_and_process_sandbox_requests() == 0) { // debuglog("[null: null]\n"); return NULL; } @@ -352,12 +352,12 @@ worker_thread__get_next_sandbox(int in_interrupt) // Execute Round Robin Scheduling Logic // Grab the sandbox at the head of the thread local runqueue, add it to the end, and return it - struct sandbox *sandbox = ps_list_head_first_d(&worker_thread__run_queue, struct sandbox); + struct sandbox *sandbox = ps_list_head_first_d(&worker_thread_run_queue, struct sandbox); // We are assuming that any sandboxed in the RETURNED state should have been pulled from the local runqueue by // now! assert(sandbox->state != RETURNED); ps_list_rem_d(sandbox); - ps_list_head_append_d(&worker_thread__run_queue, sandbox); + ps_list_head_append_d(&worker_thread_run_queue, sandbox); debuglog("[%p: %s]\n", sandbox, sandbox->module->name); return sandbox; } @@ -367,10 +367,10 @@ worker_thread__get_next_sandbox(int in_interrupt) * @param sandbox **/ void -worker_thread__push_sandbox_to_completion_queue(struct sandbox *sandbox) +worker_thread_push_sandbox_to_completion_queue(struct sandbox *sandbox) { assert(ps_list_singleton_d(sandbox)); - ps_list_head_append_d(&worker_thread__completion_queue, sandbox); + ps_list_head_append_d(&worker_thread_completion_queue, sandbox); } @@ -380,11 +380,11 @@ worker_thread__push_sandbox_to_completion_queue(struct sandbox *sandbox) * @return void */ static inline void -worker_thread__pop_and_free_n_sandboxes_from_completion_queue(unsigned int number_to_free) +worker_thread_pop_and_free_n_sandboxes_from_completion_queue(unsigned int number_to_free) { for (int i = 0; i < number_to_free; i++) { - if (ps_list_head_empty(&worker_thread__completion_queue)) break; - struct sandbox *sandbox = ps_list_head_first_d(&worker_thread__completion_queue, struct sandbox); + if (ps_list_head_empty(&worker_thread_completion_queue)) break; + struct sandbox *sandbox = ps_list_head_first_d(&worker_thread_completion_queue, struct sandbox); if (!sandbox) break; ps_list_rem_d(sandbox); sandbox_free(sandbox); @@ -397,17 +397,17 @@ worker_thread__pop_and_free_n_sandboxes_from_completion_queue(unsigned int numbe * @return sandbox or NULL **/ static inline struct sandbox * -worker_thread__execute_runtime_maintenance_and_get_next_sandbox(void) +worker_thread_execute_runtime_maintenance_and_get_next_sandbox(void) { assert(current_sandbox_get() == NULL); // Try to free one sandbox from the completion queue - worker_thread__pop_and_free_n_sandboxes_from_completion_queue(1); + worker_thread_pop_and_free_n_sandboxes_from_completion_queue(1); // Execute libuv callbacks - if (!worker_thread__is_in_callback) worker_thread__execute_libuv_event_loop(); + if (!worker_thread_is_in_callback) worker_thread_execute_libuv_event_loop(); // Get and return the sandbox at the head of the thread local runqueue software_interrupt_disable(); - struct sandbox *sandbox = worker_thread__get_next_sandbox(0); + struct sandbox *sandbox = worker_thread_get_next_sandbox(0); software_interrupt_enable(); assert(sandbox == NULL || sandbox->state == RUNNABLE); return sandbox; @@ -419,26 +419,26 @@ worker_thread__execute_runtime_maintenance_and_get_next_sandbox(void) * @param return_code - argument provided by pthread API. We set to -1 on error **/ void * -worker_thread__main(void *return_code) +worker_thread_main(void *return_code) { - arch_context_init(&worker_thread__base_context, 0, 0); + arch_context_init(&worker_thread_base_context, 0, 0); - ps_list_head_init(&worker_thread__run_queue); - ps_list_head_init(&worker_thread__completion_queue); + ps_list_head_init(&worker_thread_run_queue); + ps_list_head_init(&worker_thread_completion_queue); software_interrupt_is_disabled = 0; - worker_thread__next_context = NULL; + worker_thread_next_context = NULL; #ifndef PREEMPT_DISABLE software_interrupt_unmask_signal(SIGALRM); software_interrupt_unmask_signal(SIGUSR1); #endif - uv_loop_init(&worker_thread__uvio_handle); - worker_thread__is_in_callback = 0; + uv_loop_init(&worker_thread_uvio_handle); + worker_thread_is_in_callback = 0; while (true) { - struct sandbox *sandbox = worker_thread__execute_runtime_maintenance_and_get_next_sandbox(); + struct sandbox *sandbox = worker_thread_execute_runtime_maintenance_and_get_next_sandbox(); while (sandbox) { - worker_thread__switch_to_sandbox(sandbox); - sandbox = worker_thread__execute_runtime_maintenance_and_get_next_sandbox(); + worker_thread_switch_to_sandbox(sandbox); + sandbox = worker_thread_execute_runtime_maintenance_and_get_next_sandbox(); } } @@ -453,19 +453,19 @@ worker_thread__main(void *return_code) * TODO: Consider moving this to a future current_sandbox file. This has thus far proven difficult to move **/ void -worker_thread__exit_current_sandbox(void) +worker_thread_exit_current_sandbox(void) { struct sandbox *current_sandbox = current_sandbox_get(); assert(current_sandbox); software_interrupt_disable(); - worker_thread__pop_sandbox_from_run_queue(current_sandbox); + worker_thread_pop_sandbox_from_run_queue(current_sandbox); current_sandbox->state = RETURNED; - struct sandbox *next_sandbox = worker_thread__get_next_sandbox(0); + struct sandbox *next_sandbox = worker_thread_get_next_sandbox(0); assert(next_sandbox != current_sandbox); software_interrupt_enable(); // free resources from "main function execution", as stack still in use. // unmap linear memory only! munmap(current_sandbox->linear_memory_start, SBOX_MAX_MEM + PAGE_SIZE); - worker_thread__switch_to_sandbox(next_sandbox); + worker_thread_switch_to_sandbox(next_sandbox); } \ No newline at end of file diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index 29d8801..7b0cda5 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -25,7 +25,7 @@ current_sandbox_setup_arguments(i32 argument_count) assert(sandbox_lmbase == curr->linear_memory_start); expand_memory(); - i32 *array_ptr = worker_thread__get_memory_ptr_void(curr->arguments_offset, argument_count * sizeof(i32)); + i32 *array_ptr = worker_thread_get_memory_ptr_void(curr->arguments_offset, argument_count * sizeof(i32)); i32 string_off = curr->arguments_offset + (argument_count * sizeof(i32)); for (int i = 0; i < argument_count; i++) { @@ -94,7 +94,7 @@ current_sandbox_receive_and_parse_client_request(void) int r = uv_read_start((uv_stream_t *)&curr->client_libuv_stream, libuv_callbacks_on_allocate_setup_request_response_data, libuv_callbacks_on_read_parse_http_request); - worker_thread__process_io(); + worker_thread_process_io(); if (curr->request_response_data_length == 0) return 0; #endif return 1; @@ -164,7 +164,7 @@ done: uv_buf_t bufv = uv_buf_init(curr->request_response_data, sndsz); int r = uv_write(&req, (uv_stream_t *)&curr->client_libuv_stream, &bufv, 1, libuv_callbacks_on_write_wakeup_sandbox); - worker_thread__process_io(); + worker_thread_process_io(); #endif return 0; } @@ -179,11 +179,11 @@ current_sandbox_main(void) { struct sandbox *current_sandbox = current_sandbox_get(); // FIXME: is this right? this is the first time this sandbox is running.. so it wont - // return to worker_thread__switch_to_sandbox() api.. - // we'd potentially do what we'd in worker_thread__switch_to_sandbox() api here for cleanup.. + // return to worker_thread_switch_to_sandbox() api.. + // we'd potentially do what we'd in worker_thread_switch_to_sandbox() api here for cleanup.. if (!software_interrupt_is_enabled()) { arch_context_init(¤t_sandbox->ctxt, 0, 0); - worker_thread__next_context = NULL; + worker_thread_next_context = NULL; software_interrupt_enable(); } struct module *current_module = sandbox_get_module(current_sandbox); @@ -213,8 +213,7 @@ current_sandbox_main(void) #ifdef USE_HTTP_UVIO // Initialize libuv TCP stream - int r = uv_tcp_init(worker_thread__get_thread_libuv_handle(), - (uv_tcp_t *)¤t_sandbox->client_libuv_stream); + int r = uv_tcp_init(worker_thread_get_libuv_handle(), (uv_tcp_t *)¤t_sandbox->client_libuv_stream); assert(r == 0); // Set the current sandbox as the data the libuv callbacks have access to @@ -250,11 +249,11 @@ current_sandbox_main(void) #ifdef USE_HTTP_UVIO uv_close((uv_handle_t *)¤t_sandbox->client_libuv_stream, libuv_callbacks_on_close_wakeup_sakebox); - worker_thread__process_io(); + worker_thread_process_io(); #else close(current_sandbox->client_socket_descriptor); #endif - worker_thread__exit_current_sandbox(); + worker_thread_exit_current_sandbox(); } /** diff --git a/runtime/src/software_interrupt.c b/runtime/src/software_interrupt.c index 657d388..647e5c5 100644 --- a/runtime/src/software_interrupt.c +++ b/runtime/src/software_interrupt.c @@ -80,7 +80,7 @@ software_interrupt_handle_signals(int signal_type, siginfo_t *signal_info, void software_interrupt_SIGALRM_count++; // software_interrupt_supported_signals per-core.. if (curr && curr->state == RETURNED) return; - if (worker_thread__next_context) return; + if (worker_thread_next_context) return; if (!software_interrupt_is_enabled()) return; software_interrupt_schedule_alarm(user_context_raw); @@ -90,17 +90,17 @@ software_interrupt_handle_signals(int signal_type, siginfo_t *signal_info, void // make sure sigalrm doesn't mess this up if nested.. assert(!software_interrupt_is_enabled()); /* we set current before calling pthread_kill! */ - assert(worker_thread__next_context && (&curr->ctxt == worker_thread__next_context)); + assert(worker_thread_next_context && (&curr->ctxt == worker_thread_next_context)); assert(signal_info->si_code == SI_TKILL); // debuglog("usr1:%d\n", software_interrupt_SIGUSR_count); software_interrupt_SIGUSR_count++; // do not save current sandbox.. it is in co-operative switch.. - // pick the next from "worker_thread__next_context".. + // pick the next from "worker_thread_next_context".. // assert its "sp" to be zero in regs.. // memcpy from next context.. arch_mcontext_restore(&user_context->uc_mcontext, &curr->ctxt); - worker_thread__next_context = NULL; + worker_thread_next_context = NULL; software_interrupt_enable(); break; } @@ -126,7 +126,7 @@ software_interrupt_schedule_alarm(void *user_context_raw) if (curr == NULL) goto done; // find a next sandbox to run.. - struct sandbox *next = worker_thread__get_next_sandbox(1); + struct sandbox *next = worker_thread_get_next_sandbox(1); if (next == NULL) goto done; if (next == curr) goto done; // only this sandbox to schedule.. return to it! // save the current sandbox, state from user_context! @@ -139,7 +139,7 @@ software_interrupt_schedule_alarm(void *user_context_raw) // reset if SIGALRM happens before SIGUSR1 and if don't preempt..OR // perhaps switch here for SIGUSR1 and see if we can clear that signal // so it doesn't get called on SIGALRM return.. - // worker_thread__next_context = NULL; + // worker_thread_next_context = NULL; done: software_interrupt_enable();