' Classes/System/Threading/Semaphore.ab

Namespace System
Namespace Threading

Class Semaphore
	Inherits System.Threading.WaitHandle

Public
	Sub Semaphore(initialCount As Long, maximumCount As Long)
		This.Handle(CreateSemaphore(NULL,initialCount,maximumCount,NULL))
	End Sub
	Sub Semaphore(initialCount As Long, maximumCount As Long, name As String)
		This.Handle(CreateSemaphore(NULL,initialCount,maximumCount,ToTCStr(name)))
	End Sub
	Sub Semaphore(initialCount As Long, maximumCount As Long, name As String, ByRef createdNew As Boolean)
		This.Handle(CreateSemaphore(NULL,initialCount,maximumCount,ToTCStr(name)))
		If ERROR_ALREADY_EXISTS=GetLastError() Then createdNew=False Else createdNew=True
		'Throw ArgumentException
		'Throw ArgumentOutOfRangeException
		'Throw IOException
		'Throw UnauthorizedAccessException
		'Throw WaitHandleCannotBeOpenedException
	End Sub
/*	Sub Semaphore(initialCount As Long, maximumCount As Long, name As String, ByRef createdNew As Boolean, semaphoreSecurity As SemaphoreSecurity)
		TODO
	End Sub*/

	Static Function OpenExisting (name As String) As Semaphore
		This.Handle(OpenSemaphore(SEMAPHORE_ALL_ACCESS,FALSE,ToTCStr(name)))
		'Throw ArgumentException
		'Throw ArgumentNullException
		'Throw WaitHandleCannotBeOpenedException
		'Throw IOException
		'Throw UnauthorizedAccessException
	End Function

/*	Static Function OpenExisting (String, SemaphoreRights) As Semaphore
		TODO
	End Function*/

	Function Release() As Long
		If ReleaseSemaphore(This.Handle(),1,Release) = 0 Then
			'SemaphoreFullException
			'IOException
			'UnauthorizedAccessException
		End If
	End Function
	Function Release(releaseCount As Long) As Long
		If ReleaseSemaphore(This.Handle(),releaseCount,Release) = 0 Then
			'ArgumentOutOfRangeException
			'SemaphoreFullException
			'IOException
			'UnauthorizedAccessException
		End If
	End Function

End Class

End Namespace 'Threading
End Namespace 'System
