Protect global data with locks
This commit is contained in:
49
main.c
49
main.c
@ -15,13 +15,10 @@
|
||||
#include "timer.h"
|
||||
#include "CONFIG.h"
|
||||
#include "clonestr.h"
|
||||
#include "locked.h"
|
||||
|
||||
Route *route_hashtable = nil;
|
||||
/* char *etc_dump_path = nil; */
|
||||
struct { char *value; pthread_mutex_t lock; } etc_dump_path = {
|
||||
.value = nil,
|
||||
.lock = PTHREAD_MUTEX_INITIALIZER,
|
||||
};
|
||||
locked(Route *) route_hashtable = locked_init(nil);
|
||||
locked(char *) etc_dump_path = locked_init(nil);
|
||||
|
||||
void run_in_thread(void *(*f)(void *), void *p)
|
||||
{
|
||||
@ -63,9 +60,9 @@ void *route_thread_function(void *param)
|
||||
char *file = basename(p);
|
||||
char *full_path = malloc(PATH_MAX);
|
||||
|
||||
pthread_mutex_lock(&etc_dump_path.lock);
|
||||
lockx(&etc_dump_path);
|
||||
snprintf(full_path, PATH_MAX, "%s/%s", etc_dump_path.value, file);
|
||||
pthread_mutex_unlock(&etc_dump_path.lock);
|
||||
unlockx(&etc_dump_path);
|
||||
|
||||
sb_append_nstr(&result.body, full_path);
|
||||
sb_finish(&result.body);
|
||||
@ -79,10 +76,12 @@ void *route_thread_function(void *param)
|
||||
|
||||
char key[MG_PATH_MAX] = {0};
|
||||
strncpy(key, http_msg.uri.buf, http_msg.uri.len);
|
||||
ssize_t idx = shgeti(route_hashtable, key);
|
||||
lockx(&route_hashtable);
|
||||
ssize_t idx = shgeti(route_hashtable.value, key);
|
||||
|
||||
Route_Result result = {0};
|
||||
route_hashtable[idx].value(&http_msg, &result, route_hashtable[idx].context_data);
|
||||
route_hashtable.value[idx].value(&http_msg, &result, route_hashtable.value[idx].context_data);
|
||||
unlockx(&route_hashtable);
|
||||
mg_wakeup(data->mgr, data->conn_id, &result, sizeof(result));
|
||||
|
||||
free(data->message.buf);
|
||||
@ -136,27 +135,31 @@ void route_hashtable_put_blogs(Baked_Resource *resource, void *udata)
|
||||
&& strncmp(resource->key, "blog-", strlen("blog-")) == 0) {
|
||||
char path[MG_PATH_MAX];
|
||||
snprintf(path, MG_PATH_MAX, "/%s", resource->key);
|
||||
shput(route_hashtable, path, &route_generic_blog);
|
||||
route_hashtable[shgeti(route_hashtable, path)].context_data = (void *)resource;
|
||||
shput(route_hashtable.value, path, &route_generic_blog);
|
||||
route_hashtable.value[shgeti(route_hashtable.value, path)].context_data = (void *)resource;
|
||||
}
|
||||
}
|
||||
|
||||
void init_route_hashtable(void)
|
||||
{
|
||||
shdefault(route_hashtable, &route_page_not_found);
|
||||
shput(route_hashtable, "/", &route_home);
|
||||
shput(route_hashtable, "/page-missing", &route_page_not_found);
|
||||
shput(route_hashtable, "/blog", &route_blog);
|
||||
lockx(&route_hashtable);
|
||||
shdefault(route_hashtable.value, &route_page_not_found);
|
||||
shput(route_hashtable.value, "/", &route_home);
|
||||
shput(route_hashtable.value, "/page-missing", &route_page_not_found);
|
||||
shput(route_hashtable.value, "/blog", &route_blog);
|
||||
#if MY_DEBUG
|
||||
shput(route_hashtable, "/build-id", &route_build_id);
|
||||
shput(route_hashtable.value, "/build-id", &route_build_id);
|
||||
#endif
|
||||
|
||||
baked_resource_each(&route_hashtable_put_blogs, nil);
|
||||
unlockx(&route_hashtable);
|
||||
}
|
||||
|
||||
void free_route_hashtable(void)
|
||||
{
|
||||
shfree(route_hashtable);
|
||||
lockx(&route_hashtable);
|
||||
shfree(route_hashtable.value);
|
||||
unlockx(&route_hashtable);
|
||||
}
|
||||
|
||||
char *init_etc_dump(void)
|
||||
@ -235,6 +238,7 @@ void populate_etc_dump(char *etc_dump)
|
||||
"simple.css", "me.jpg",
|
||||
};
|
||||
|
||||
lock_baked_resources();
|
||||
for (size_t i = 0; i < sizeof(files)/sizeof(files[0]); i++) {
|
||||
char path[PATH_MAX];
|
||||
get_baked_resource_path(files[i], path, sizeof(path));
|
||||
@ -242,6 +246,7 @@ void populate_etc_dump(char *etc_dump)
|
||||
snprintf(dest, sizeof(dest), "%s/%s", etc_dump, files[i]);
|
||||
cp(path, dest);
|
||||
}
|
||||
unlock_baked_resources();
|
||||
}
|
||||
|
||||
volatile bool alive = true;
|
||||
@ -254,12 +259,12 @@ int main(int argc, char ** argv)
|
||||
start_timer();
|
||||
|
||||
init_baked_resources();
|
||||
pthread_mutex_lock(&etc_dump_path.lock);
|
||||
lockx(&etc_dump_path);
|
||||
if ((etc_dump_path.value = init_etc_dump()) == nil) {
|
||||
return 1;
|
||||
}
|
||||
populate_etc_dump(etc_dump_path.value);
|
||||
pthread_mutex_unlock(&etc_dump_path.lock);
|
||||
unlockx(&etc_dump_path);
|
||||
|
||||
mg_log_set(MG_LL_DEBUG);
|
||||
struct mg_mgr mgr;
|
||||
@ -277,9 +282,9 @@ int main(int argc, char ** argv)
|
||||
|
||||
mg_mgr_free(&mgr);
|
||||
|
||||
pthread_mutex_lock(&etc_dump_path.lock);
|
||||
lockx(&etc_dump_path);
|
||||
free_etc_dump(etc_dump_path.value);
|
||||
pthread_mutex_unlock(&etc_dump_path.lock);
|
||||
unlockx(&etc_dump_path);
|
||||
free_baked_resources();
|
||||
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user