fix: Make module reference count atomic

main
Sean McBride 4 years ago
parent 7dbb0a625c
commit b510214e37

@ -51,7 +51,7 @@ struct module {
uint64_t max_memory; /* perhaps a specification of the module. (max 4GB) */ uint64_t max_memory; /* perhaps a specification of the module. (max 4GB) */
uint32_t relative_deadline_us; uint32_t relative_deadline_us;
uint64_t relative_deadline; /* cycles */ uint64_t relative_deadline; /* cycles */
uint32_t reference_count; /* ref count how many instances exist here. */ _Atomic uint32_t reference_count; /* ref count how many instances exist here. */
struct indirect_table_entry indirect_table[INDIRECT_TABLE_SIZE]; struct indirect_table_entry indirect_table[INDIRECT_TABLE_SIZE];
struct sockaddr_in socket_address; struct sockaddr_in socket_address;
int socket_descriptor; int socket_descriptor;
@ -101,7 +101,9 @@ struct module {
static inline void static inline void
module_acquire(struct module *module) module_acquire(struct module *module)
{ {
module->reference_count++; assert(module->reference_count < UINT32_MAX);
atomic_fetch_add(&module->reference_count, 1);
return;
} }
/** /**
@ -200,7 +202,9 @@ module_main(struct module *module, int32_t argc, int32_t argv)
static inline void static inline void
module_release(struct module *module) module_release(struct module *module)
{ {
module->reference_count--; assert(module->reference_count > 0);
atomic_fetch_sub(&module->reference_count, 1);
return;
} }
/** /**

@ -147,8 +147,12 @@ module_new(char *name, char *path, int32_t argument_count, uint32_t stack_size,
memset(module, 0, sizeof(struct module)); memset(module, 0, sizeof(struct module));
atomic_init(&module->reference_count, 0);
/* Load the dynamic library *.so file with lazy function call binding and deep binding */ /* Load the dynamic library *.so file with lazy function call binding and deep binding */
module->dynamic_library_handle = dlopen(path, RTLD_LAZY | RTLD_DEEPBIND); // TODO: Changed to work with sanitizers. What does RTLD_DEEPBIND do?
// module->dynamic_library_handle = dlopen(path, RTLD_LAZY | RTLD_DEEPBIND);
module->dynamic_library_handle = dlopen(path, RTLD_LAZY);
if (module->dynamic_library_handle == NULL) { if (module->dynamic_library_handle == NULL) {
fprintf(stderr, "Failed to open %s with error: %s\n", path, dlerror()); fprintf(stderr, "Failed to open %s with error: %s\n", path, dlerror());
goto dl_open_error; goto dl_open_error;
@ -194,7 +198,8 @@ module_new(char *name, char *path, int32_t argument_count, uint32_t stack_size,
strncpy(module->path, path, MODULE_MAX_PATH_LENGTH); strncpy(module->path, path, MODULE_MAX_PATH_LENGTH);
module->argument_count = argument_count; module->argument_count = argument_count;
module->stack_size = round_up_to_page(stack_size == 0 ? WASM_STACK_SIZE : stack_size); module->stack_size = ((uint32_t)(round_up_to_page(stack_size == 0 ? WASM_STACK_SIZE : stack_size)));
debuglog("Stack Size: %u", module->stack_size);
module->max_memory = max_memory == 0 ? ((uint64_t)WASM_PAGE_SIZE * WASM_MAX_PAGES) : max_memory; module->max_memory = max_memory == 0 ? ((uint64_t)WASM_PAGE_SIZE * WASM_MAX_PAGES) : max_memory;
module->socket_descriptor = -1; module->socket_descriptor = -1;
module->port = port; module->port = port;
@ -209,7 +214,7 @@ module_new(char *name, char *path, int32_t argument_count, uint32_t stack_size,
module->relative_deadline = (uint64_t)relative_deadline_us * runtime_processor_speed_MHz; module->relative_deadline = (uint64_t)relative_deadline_us * runtime_processor_speed_MHz;
/* Admissions Control */ /* Admissions Control */
uint64_t expected_execution = expected_execution_us * runtime_processor_speed_MHz; uint64_t expected_execution = (uint64_t)expected_execution_us * runtime_processor_speed_MHz;
admissions_info_initialize(&module->admissions_info, admissions_percentile, expected_execution, admissions_info_initialize(&module->admissions_info, admissions_percentile, expected_execution,
module->relative_deadline); module->relative_deadline);

Loading…
Cancel
Save