' Classes/System/Threading/Mutex.ab

#ifndef __SYSTEM_THREADING_MUTEX_AB__
#define __SYSTEM_THREADING_MUTEX_AB__

Namespace System
Namespace Threading

Class Mutex
	Inherits System.Threading.WaitHandle

Public
	Sub Mutex()
		This.Handle(CreateMutex(NULL,FALSE,NULL))
	End Sub
	Sub Mutex(initiallyOwned As Boolean)
		This.Handle(CreateMutex(NULL,initiallyOwned As BOOL,NULL))
	End Sub
	Sub Mutex(initiallyOwned As Boolean, name As String)
		This.Handle(CreateMutex(NULL,initiallyOwned As BOOL,ToTCStr(name)))
	End Sub
	Sub Mutex(initiallyOwned As Boolean, name As String, ByRef createdNew As Boolean)
		This.Handle(CreateMutex(NULL,initiallyOwned As BOOL,ToTCStr(name)))
		If ERROR_ALREADY_EXISTS=GetLastError() Then createdNew=False Else createdNew=True
		'Throw UnauthorizedAccessException
		'Throw IOException
		'Throw ApplicationException
		'Throw ArgumentException
	End Sub
/*	Sub Mutex(Boolean, String, Boolean, MutexSecurity)  
		TODO
	End Sub*/

	Static Function OpenExisting (name As String) As Mutex
		This.Handle(OpenMutex(MUTEX_ALL_ACCESS,FALSE,ToTCStr(name)))
		'Throw ArgumentException
		'Throw ArgumentNullException
		'Throw WaitHandleCannotBeOpenedException
		'Throw IOException
		'Throw UnauthorizedAccessException
	End Function

/*	Static Function OpenExisting (String, MutexRights) As Mutex
		TODO
	End Function*/

	Sub Release()
		If ReleaseMutex(This.Handle()) = 0 Then
			'Throw ApplicationException
		End If
	End Sub

End Class

End Namespace 'Threading
End Namespace 'System

#endif
