mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-14 06:59:10 +00:00
Add some more necessary locks
This commit is contained in:
parent
8b72490431
commit
91953dd308
9 changed files with 29 additions and 15 deletions
|
@ -21,6 +21,7 @@
|
|||
|
||||
/**
|
||||
* Frees addresses returned by getaddrinfo().
|
||||
* @threadsafe
|
||||
*/
|
||||
int freeaddrinfo(struct addrinfo *ai) {
|
||||
struct addrinfo *next;
|
||||
|
|
|
@ -41,6 +41,7 @@
|
|||
* @param res receives a pointer that must be freed with freeaddrinfo(),
|
||||
* and won't be modified if -1 is returned
|
||||
* @return 0 on success or EAI_xxx value
|
||||
* @threadsafe
|
||||
*/
|
||||
int getaddrinfo(const char *name, const char *service,
|
||||
const struct addrinfo *hints, struct addrinfo **res) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "libc/dce.h"
|
||||
#include "libc/dns/resolvconf.h"
|
||||
#include "libc/fmt/fmt.h"
|
||||
#include "libc/intrin/pthread.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/sock/sock.h"
|
||||
|
@ -28,17 +29,20 @@
|
|||
static struct ResolvConf *g_resolvconf;
|
||||
static struct ResolvConfInitialStaticMemory {
|
||||
struct ResolvConf rv;
|
||||
pthread_mutex_t lock;
|
||||
struct sockaddr_in nameservers[3];
|
||||
} g_resolvconf_init;
|
||||
|
||||
/**
|
||||
* Returns singleton with DNS server address.
|
||||
* @threadsafe
|
||||
*/
|
||||
const struct ResolvConf *GetResolvConf(void) {
|
||||
int rc;
|
||||
FILE *f;
|
||||
struct ResolvConfInitialStaticMemory *init;
|
||||
init = &g_resolvconf_init;
|
||||
pthread_mutex_lock(&init->lock);
|
||||
if (!g_resolvconf) {
|
||||
g_resolvconf = &init->rv;
|
||||
pushmov(&init->rv.nameservers.n, ARRAYLEN(init->nameservers));
|
||||
|
@ -58,5 +62,6 @@ const struct ResolvConf *GetResolvConf(void) {
|
|||
/* TODO(jart): Elevate robustness. */
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&init->lock);
|
||||
return g_resolvconf;
|
||||
}
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
* @return -1 on error, or positive port number
|
||||
* @note aliases are read from file for comparison, but not returned.
|
||||
* @see LookupServicesByPort
|
||||
* @threadsafe
|
||||
*/
|
||||
int LookupServicesByName(const char *servname, char *servproto,
|
||||
size_t servprotolen, char *buf, size_t bufsize,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue