InterprocessLockGetNamedSemaphore(String, Int32, Int32, Boolean) Method

Gets a uniquely named inter-process NamedSemaphore associated with the specified name that identifies a source object needing concurrency locking.

Definition

Namespace: Gemstone.Threading
Assembly: Gemstone.Common (in Gemstone.Common.dll) Version: 1.0.128 -- Release Build+d050cfc5563c89a1188cc3c6b2d417530856f490
public static NamedSemaphore GetNamedSemaphore(
	string name,
	int maximumCount = 10,
	int initialCount = -1,
	bool global = true
)

Parameters

name  String
Identifying name of source object needing concurrency locking (e.g., a path and file name).
maximumCount  Int32  (Optional)
The maximum number of requests for the semaphore that can be granted concurrently.
initialCount  Int32  (Optional)
The initial number of requests for the semaphore that can be granted concurrently, or -1 to default to maximumCount.
global  Boolean  (Optional)
Determines if semaphore should be marked as global; set value to false for local.

Return Value

NamedSemaphore
A uniquely named inter-process NamedSemaphore specific to name; NamedSemaphore is created if it does not exist.

Remarks

This function uses a hash of the name when creating the NamedSemaphore, not the actual name - this way restrictions on the name length do not need to be a user concern. All processes needing an inter-process NamedSemaphore need to use this same function to ensure access to the same NamedSemaphore.

The name can be a string of any length (must not be empty, null or white space) and is not case-sensitive. All hashes of the name used to create the global NamedSemaphore are first converted to lower case.

On POSIX systems, the NamedSemaphore exhibits kernel persistence, meaning instances will remain active beyond the lifespan of the creating process. Named semaphores must be explicitly removed by invoking Unlink when they are no longer needed. Kernel persistence necessitates careful design consideration regarding the responsibility for invoking Unlink. Since the common use case for named semaphores is across multiple applications, it is advisable for the last exiting process to handle the cleanup. In cases where an application may crash before calling Unlink, the semaphore persists in the system, potentially leading to resource leakage. Implementations should include strategies to address and mitigate this risk.

Exceptions

ArgumentNullExceptionArgument name cannot be empty, null or white space.
UnauthorizedAccessExceptionThe named semaphore exists, but the user does not have the minimum needed security access rights to use it.

See Also