Check for mutex initialization. Fix missing check on optional for certain macro situations.

This commit is contained in:
Christoffer Lerno
2023-08-12 11:54:54 +02:00
parent b6f302d1c6
commit d83f591184
5 changed files with 43 additions and 17 deletions

View File

@@ -6,7 +6,11 @@ def NativeConditionVariable = Pthread_cond_t;
def NativeThread = Pthread_t;
def NativeOnceFlag = Pthread_once_t;
fn void! NativeMutex.init(&mtx, MutexType type)
/**
* @require !self.is_initialized() : "Mutex is already initialized"
* @ensure self.is_initialized()
**/
fn void! NativeMutex.init(&self, MutexType type)
{
Pthread_mutexattr_t attr;
if (posix::pthread_mutexattr_init(&attr)) return ThreadFault.INIT_FAILED?;
@@ -15,19 +19,35 @@ fn void! NativeMutex.init(&mtx, MutexType type)
{
if (posix::pthread_mutexattr_settype(&attr, posix::PTHREAD_MUTEX_RECURSIVE)) return ThreadFault.INIT_FAILED?;
}
if (posix::pthread_mutex_init(mtx, &attr)) return ThreadFault.INIT_FAILED?;
if (posix::pthread_mutex_init(self, &attr)) return ThreadFault.INIT_FAILED?;
}
fn void! NativeMutex.destroy(&mtx)
fn bool NativeMutex.is_initialized(&self)
{
if (posix::pthread_mutex_destroy(mtx)) return ThreadFault.DESTROY_FAILED?;
return *self != NativeMutex {};
}
fn void! NativeMutex.lock(&mtx)
/**
* @require self.is_initialized() : "Mutex was not initialized"
* @ensure !self.is_initialized()
**/
fn void! NativeMutex.destroy(&self)
{
if (posix::pthread_mutex_lock(mtx)) return ThreadFault.LOCK_FAILED?;
if (posix::pthread_mutex_destroy(self)) return ThreadFault.DESTROY_FAILED?;
*self = NativeMutex {};
}
/**
* @require self.is_initialized() : "Mutex was not initialized"
**/
fn void! NativeMutex.lock(&self)
{
if (posix::pthread_mutex_lock(self)) return ThreadFault.LOCK_FAILED?;
}
/**
* @require mtx.is_initialized() : "Mutex was not initialized"
**/
fn void! NativeMutex.lock_timeoutout(&mtx, ulong ms)
{
/* Try to acquire the lock and, if we fail, sleep for 5ms. */
@@ -51,14 +71,20 @@ fn void! NativeMutex.lock_timeoutout(&mtx, ulong ms)
}
}
fn bool NativeMutex.try_lock(&mtx)
/**
* @require self.is_initialized() : "Mutex was not initialized"
**/
fn bool NativeMutex.try_lock(&self)
{
return !posix::pthread_mutex_trylock(mtx);
return !posix::pthread_mutex_trylock(self);
}
fn void! NativeMutex.unlock(&mtx)
/**
* @require self.is_initialized() : "Mutex was not initialized"
**/
fn void! NativeMutex.unlock(&self)
{
if (posix::pthread_mutex_unlock(mtx)) return ThreadFault.UNLOCK_FAILED?;
if (posix::pthread_mutex_unlock(self)) return ThreadFault.UNLOCK_FAILED?;
}
fn void! NativeConditionVariable.init(&cond)

View File

@@ -30,7 +30,7 @@ fault ThreadFault
INTERRUPTED,
}
macro void! Mutex.init(&mutex, MutexType type) => NativeMutex.init((NativeMutex*)mutex, type);
macro void! Mutex.init(&mutex, MutexType type = MUTEX_PLAIN) => NativeMutex.init((NativeMutex*)mutex, type);
macro void! Mutex.destroy(&mutex) => NativeMutex.destroy((NativeMutex*)mutex);
macro void! Mutex.lock(&mutex) => NativeMutex.lock((NativeMutex*)mutex);
macro void! Mutex.lock_timeout(&mutex, ulong ms) => NativeMutex.lock((NativeMutex*)mutex, ms);