Protect global data with locks
This commit is contained in:
34
baked.c
34
baked.c
@ -5,6 +5,7 @@
|
||||
#include "stb/stb_ds.h"
|
||||
|
||||
#include "baked.h"
|
||||
#include "locked.h"
|
||||
|
||||
INCBIN(gpp1, "./gpp1");
|
||||
|
||||
@ -24,7 +25,17 @@ INCBIN(blog_welcome_md, "./blog/welcome.md");
|
||||
INCBIN(blog_weird_page_md, "./blog/weird-page.md");
|
||||
INCBIN(blog_curious_case_of_gebs_md, "./blog/curious-case-of-gebs.md");
|
||||
|
||||
Baked_Resource *baked_resources = NULL;
|
||||
locked(Baked_Resource *) baked_resources = locked_init(nil);
|
||||
|
||||
void lock_baked_resources(void)
|
||||
{
|
||||
lockx(&baked_resources);
|
||||
}
|
||||
|
||||
void unlock_baked_resources(void)
|
||||
{
|
||||
unlockx(&baked_resources);
|
||||
}
|
||||
|
||||
void add_baked_resource(char *key, const uchar *data, size_t size)
|
||||
{
|
||||
@ -34,11 +45,12 @@ void add_baked_resource(char *key, const uchar *data, size_t size)
|
||||
abort();
|
||||
}
|
||||
write(fd, data, size);
|
||||
shput(baked_resources, key, fd);
|
||||
shput(baked_resources.value, key, fd);
|
||||
}
|
||||
|
||||
void init_baked_resources(void)
|
||||
{
|
||||
lockx(&baked_resources);
|
||||
add_baked_resource("home.html", home_html_data, home_html_size);
|
||||
add_baked_resource("page-missing.html", page_missing_html_data, page_missing_html_size);
|
||||
add_baked_resource("template-blog.html", template_blog_html_data, template_blog_html_size);
|
||||
@ -53,21 +65,25 @@ void init_baked_resources(void)
|
||||
add_baked_resource("blog-welcome.md", blog_welcome_md_data, blog_welcome_md_size);
|
||||
add_baked_resource("blog-weird-page.md", blog_weird_page_md_data, blog_weird_page_md_size);
|
||||
add_baked_resource("blog-curious-case-of-gebs.md", blog_curious_case_of_gebs_md_data, blog_curious_case_of_gebs_md_size);
|
||||
unlockx(&baked_resources);
|
||||
}
|
||||
|
||||
void free_baked_resources(void)
|
||||
{
|
||||
for (size_t i = 0; i < shlen(baked_resources); i++) {
|
||||
close(baked_resources[i].value);
|
||||
lockx(&baked_resources);
|
||||
for (size_t i = 0; i < shlen(baked_resources.value); i++) {
|
||||
close(baked_resources.value[i].value);
|
||||
}
|
||||
shfree(baked_resources);
|
||||
shfree(baked_resources.value);
|
||||
unlockx(&baked_resources);
|
||||
}
|
||||
|
||||
bool get_baked_resource_path(char *key, char *buf, size_t size)
|
||||
{
|
||||
if (shgeti(baked_resources, key) != -1) {
|
||||
int fd = shget(baked_resources, key);
|
||||
if (shgeti(baked_resources.value, key) != -1) {
|
||||
int fd = shget(baked_resources.value, key);
|
||||
snprintf(buf, size, "/proc/%d/fd/%d", getpid(), fd);
|
||||
unlockx(&baked_resources);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@ -75,8 +91,8 @@ bool get_baked_resource_path(char *key, char *buf, size_t size)
|
||||
|
||||
void baked_resource_each(void (*f)(Baked_Resource *resource, void *udata), void *udata)
|
||||
{
|
||||
for (size_t i = 0; i < shlen(baked_resources); i++) {
|
||||
f(&baked_resources[i], udata);
|
||||
for (size_t i = 0; i < shlen(baked_resources.value); i++) {
|
||||
f(&baked_resources.value[i], udata);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user