| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Retrieves a lock object associated with the specified key, creating it if it does not already exist. |
| | | 4 | | This lock can be used to synchronize access to shared resources across different parts of the application within the |
| | | 5 | | .DESCRIPTION |
| | | 6 | | The Get-KrLock function is designed to provide a mechanism for obtaining a lock object that is associated with a spe |
| | | 7 | | If a lock object for the given key does not already exist, it will be created. This allows for synchronization of ac |
| | | 8 | | ensuring that only one thread within the current process can access the resource at a time when using the lock. |
| | | 9 | | The function uses a process-local registry of locks to manage and retrieve lock objects based on their associated ke |
| | | 10 | | This is particularly useful in scenarios where multiple threads or runspaces in the same application instance need t |
| | | 11 | | It does not synchronize access across separate processes or application instances. |
| | | 12 | | .PARAMETER Key |
| | | 13 | | The unique identifier for the lock. This key is used to retrieve the corresponding lock object from the lock registr |
| | | 14 | | If a lock object does not already exist for this key, a new one will be created. The key should be a string that uni |
| | | 15 | | or critical section that the lock is intended to protect. It is important to use consistent keys across the applicat |
| | | 16 | | .EXAMPLE |
| | | 17 | | $lock = Get-KrLock -Key "MyResourceLock" |
| | | 18 | | This example demonstrates how to retrieve a lock object associated with the key "MyResourceLock". |
| | | 19 | | If a lock object for this key does not already exist, it will be created. The returned lock object can then be used |
| | | 20 | | access to the resource identified by "MyResourceLock" across different parts of the application within the current p |
| | | 21 | | .NOTES |
| | | 22 | | This function is part of the Kestrun framework and is used to manage locks for synchronizing access to shared resour |
| | | 23 | | It relies on a process-local lock registry to store and retrieve lock objects based on their associated keys. |
| | | 24 | | The locks returned by this function can be used in conjunction with synchronization primitives such as Monitor, Mute |
| | | 25 | | or Semaphore to control access to critical sections of code or shared resources in a thread-safe manner. |
| | | 26 | | It is important to ensure that the keys used with this function are consistent and unique to avoid unintended lockin |
| | | 27 | | Additionally, proper handling of lock acquisition and release is crucial to prevent deadlocks and ensure the smooth |
| | | 28 | | #> |
| | | 29 | | function Get-KrLock { |
| | | 30 | | [KestrunRuntimeApi('Everywhere')] |
| | | 31 | | [CmdletBinding()] |
| | | 32 | | param( |
| | | 33 | | [Parameter(Mandatory, Position = 0)] |
| | | 34 | | [ValidateNotNullOrEmpty()] |
| | | 35 | | [string]$Key |
| | | 36 | | ) |
| | | 37 | | |
| | 1 | 38 | | return [Kestrun.Utilities.KestrunLockRegistry]::GetOrCreate($key) |
| | | 39 | | } |