/// <summary> /// Creates a mutex. /// </summary> /// <param name="mutex">A handle in which a mutex will be stored.</param> /// <returns>A result code.</returns> public static ThreadResultCode CreateMutex(out MutexHandle mutex) { mutex = MutexHandle.Allocate(); var result = (ThreadResultCode)pthread_mutexattr_init(mutex.AttributePointer); if (result != ThreadResultCode.Success) { mutex.Free(); return(result); } result = (ThreadResultCode)pthread_mutexattr_settype( mutex.AttributePointer, pthread_get_mutex_errorcheck_type()); if (result != ThreadResultCode.Success) { pthread_mutexattr_destroy(mutex.AttributePointer); mutex.Free(); return(result); } result = (ThreadResultCode)pthread_mutex_init( mutex.MutexPointer, mutex.AttributePointer); if (result != ThreadResultCode.Success) { pthread_mutexattr_destroy(mutex.AttributePointer); mutex.Free(); return(result); } return(result); }
/// <summary> /// Releases resources acquired by a mutex. /// </summary> /// <param name="mutex">A handle to a mutex.</param> /// <returns>A result code.</returns> public static ThreadResultCode DisposeMutex(MutexHandle mutex) { if (mutex.IsNull) { // Nothing to do here I guess. return(ThreadResultCode.Success); } var mutexResult = (ThreadResultCode)pthread_mutex_destroy(mutex.MutexPointer); var attrResult = (ThreadResultCode)pthread_mutexattr_destroy(mutex.AttributePointer); mutex.Free(); return(mutexResult == ThreadResultCode.Success ? attrResult : mutexResult); }
/// <summary> /// Releases a lock on a mutex. /// </summary> /// <param name="mutex">A handle to a mutex.</param> /// <returns>A result code.</returns> public static ThreadResultCode UnlockMutex(MutexHandle mutex) { return((ThreadResultCode)pthread_mutex_unlock(mutex.MutexPointer)); }