Add some more necessary locks

This commit is contained in:
Justine Tunney 2022-06-12 22:20:59 -07:00
parent 8b72490431
commit 91953dd308
9 changed files with 29 additions and 15 deletions

View file

@ -23,6 +23,7 @@
#include "libc/dce.h"
#include "libc/dns/hoststxt.h"
#include "libc/fmt/fmt.h"
#include "libc/intrin/pthread.h"
#include "libc/macros.internal.h"
#include "libc/nt/systeminfo.h"
#include "libc/runtime/runtime.h"
@ -32,6 +33,7 @@
static struct HostsTxt *g_hoststxt;
static struct HostsTxtInitialStaticMemory {
struct HostsTxt ht;
pthread_mutex_t lock;
struct HostsTxtEntry entries[8];
char strings[64];
} g_hoststxt_init;
@ -53,6 +55,7 @@ static textwindows dontinline char *GetNtHostsTxtPath(char *pathbuf,
* Returns hosts.txt map.
*
* @note yoinking realloc() ensures there's no size limits
* @threadsafe
*/
const struct HostsTxt *GetHostsTxt(void) {
FILE *f;
@ -60,6 +63,7 @@ const struct HostsTxt *GetHostsTxt(void) {
char pathbuf[PATH_MAX];
struct HostsTxtInitialStaticMemory *init;
init = &g_hoststxt_init;
pthread_mutex_lock(&init->lock);
if (!g_hoststxt) {
g_hoststxt = &init->ht;
init->ht.entries.n = pushpop(ARRAYLEN(init->entries));
@ -78,5 +82,6 @@ const struct HostsTxt *GetHostsTxt(void) {
fclose(f);
}
}
pthread_mutex_unlock(&init->lock);
return g_hoststxt;
}