[Partial] Build wasi-libc for Windows (#2338)
Build wasi-libc library on Windows since libuv may be not supported. This PR is a first step to make it working, but there's still a number of changes to get it fully working.
This commit is contained in:
@ -49,7 +49,7 @@
|
||||
/* Mutex that uses the lock annotations. */
|
||||
|
||||
struct LOCKABLE mutex {
|
||||
pthread_mutex_t object;
|
||||
korp_mutex object;
|
||||
};
|
||||
|
||||
/* clang-format off */
|
||||
@ -60,69 +60,71 @@ struct LOCKABLE mutex {
|
||||
static inline bool
|
||||
mutex_init(struct mutex *lock) REQUIRES_UNLOCKED(*lock)
|
||||
{
|
||||
return pthread_mutex_init(&lock->object, NULL) == 0 ? true : false;
|
||||
return os_mutex_init(&lock->object) == BHT_OK ? true : false;
|
||||
}
|
||||
|
||||
static inline void
|
||||
mutex_destroy(struct mutex *lock) REQUIRES_UNLOCKED(*lock)
|
||||
{
|
||||
pthread_mutex_destroy(&lock->object);
|
||||
os_mutex_destroy(&lock->object);
|
||||
}
|
||||
|
||||
static inline void
|
||||
mutex_lock(struct mutex *lock) LOCKS_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS
|
||||
{
|
||||
pthread_mutex_lock(&lock->object);
|
||||
os_mutex_lock(&lock->object);
|
||||
}
|
||||
|
||||
static inline void
|
||||
mutex_unlock(struct mutex *lock) UNLOCKS(*lock) NO_LOCK_ANALYSIS
|
||||
{
|
||||
pthread_mutex_unlock(&lock->object);
|
||||
os_mutex_unlock(&lock->object);
|
||||
}
|
||||
|
||||
/* Read-write lock that uses the lock annotations. */
|
||||
|
||||
struct LOCKABLE rwlock {
|
||||
pthread_rwlock_t object;
|
||||
korp_rwlock object;
|
||||
};
|
||||
|
||||
static inline bool
|
||||
rwlock_init(struct rwlock *lock) REQUIRES_UNLOCKED(*lock)
|
||||
{
|
||||
return pthread_rwlock_init(&lock->object, NULL) == 0 ? true : false;
|
||||
return os_rwlock_init(&lock->object) == 0 ? true : false;
|
||||
}
|
||||
|
||||
static inline void
|
||||
rwlock_rdlock(struct rwlock *lock) LOCKS_SHARED(*lock) NO_LOCK_ANALYSIS
|
||||
{
|
||||
pthread_rwlock_rdlock(&lock->object);
|
||||
os_rwlock_rdlock(&lock->object);
|
||||
}
|
||||
|
||||
static inline void
|
||||
rwlock_wrlock(struct rwlock *lock) LOCKS_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS
|
||||
{
|
||||
pthread_rwlock_wrlock(&lock->object);
|
||||
os_rwlock_wrlock(&lock->object);
|
||||
}
|
||||
|
||||
static inline void
|
||||
rwlock_unlock(struct rwlock *lock) UNLOCKS(*lock) NO_LOCK_ANALYSIS
|
||||
{
|
||||
pthread_rwlock_unlock(&lock->object);
|
||||
os_rwlock_unlock(&lock->object);
|
||||
}
|
||||
|
||||
static inline void
|
||||
rwlock_destroy(struct rwlock *lock) UNLOCKS(*lock) NO_LOCK_ANALYSIS
|
||||
{
|
||||
pthread_rwlock_destroy(&lock->object);
|
||||
os_rwlock_destroy(&lock->object);
|
||||
}
|
||||
|
||||
/* Condition variable that uses the lock annotations. */
|
||||
|
||||
struct LOCKABLE cond {
|
||||
pthread_cond_t object;
|
||||
#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK \
|
||||
|| !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
|
||||
korp_cond object;
|
||||
|
||||
#if !CONFIG_HAS_CLOCK_NANOSLEEP \
|
||||
&& (!CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK \
|
||||
|| !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP)
|
||||
clockid_t clock;
|
||||
#endif
|
||||
};
|
||||
@ -147,43 +149,49 @@ cond_init_monotonic(struct cond *cond)
|
||||
fail:
|
||||
pthread_condattr_destroy(&attr);
|
||||
#else
|
||||
if (pthread_cond_init(&cond->object, NULL) != 0)
|
||||
if (os_cond_init(&cond->object) != 0)
|
||||
return false;
|
||||
ret = true;
|
||||
#endif
|
||||
|
||||
#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK \
|
||||
|| !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
|
||||
#if !CONFIG_HAS_CLOCK_NANOSLEEP \
|
||||
&& (!CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK \
|
||||
|| !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP)
|
||||
cond->clock = CLOCK_MONOTONIC;
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline bool
|
||||
cond_init_realtime(struct cond *cond)
|
||||
{
|
||||
if (pthread_cond_init(&cond->object, NULL) != 0)
|
||||
if (os_cond_init(&cond->object) != 0)
|
||||
return false;
|
||||
#if !CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK \
|
||||
|| !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
|
||||
|
||||
#if !CONFIG_HAS_CLOCK_NANOSLEEP \
|
||||
&& (!CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK \
|
||||
|| !CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP)
|
||||
cond->clock = CLOCK_REALTIME;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void
|
||||
cond_destroy(struct cond *cond)
|
||||
{
|
||||
pthread_cond_destroy(&cond->object);
|
||||
os_cond_destroy(&cond->object);
|
||||
}
|
||||
|
||||
static inline void
|
||||
cond_signal(struct cond *cond)
|
||||
{
|
||||
pthread_cond_signal(&cond->object);
|
||||
os_cond_signal(&cond->object);
|
||||
}
|
||||
|
||||
#if !CONFIG_HAS_CLOCK_NANOSLEEP
|
||||
|
||||
static inline bool
|
||||
cond_timedwait(struct cond *cond, struct mutex *lock, uint64_t timeout,
|
||||
bool abstime) REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS
|
||||
@ -259,7 +267,7 @@ static inline void
|
||||
cond_wait(struct cond *cond, struct mutex *lock)
|
||||
REQUIRES_EXCLUSIVE(*lock) NO_LOCK_ANALYSIS
|
||||
{
|
||||
pthread_cond_wait(&cond->object, &lock->object);
|
||||
os_cond_wait(&cond->object, &lock->object);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -47,6 +47,23 @@ random_buf(void *buf, size_t len)
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined(BH_PLATFORM_WINDOWS)
|
||||
|
||||
#include <wincrypt.h>
|
||||
|
||||
void
|
||||
random_buf(void *buf, size_t len)
|
||||
{
|
||||
static int crypt_initialized = 0;
|
||||
static HCRYPTPROV provider;
|
||||
if (!crypt_initialized) {
|
||||
CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
|
||||
CRYPT_VERIFYCONTEXT);
|
||||
crypt_initialized = 1;
|
||||
}
|
||||
CryptGenRandom(provider, len, buf);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
static int urandom;
|
||||
|
||||
@ -83,7 +83,7 @@
|
||||
#define CONFIG_HAS_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP 0
|
||||
#endif
|
||||
|
||||
#if !defined(__APPLE__) && !defined(BH_PLATFORM_LINUX_SGX)
|
||||
#if !defined(__APPLE__) && !defined(BH_PLATFORM_LINUX_SGX) && !defined(_WIN32)
|
||||
#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 1
|
||||
#else
|
||||
#define CONFIG_HAS_PTHREAD_CONDATTR_SETCLOCK 0
|
||||
|
||||
Reference in New Issue
Block a user