|
| 1 | +#if !os(Windows) |
| 2 | +extension FileDescriptor { |
| 3 | + /// Apply an advisory lock to the file associated with this descriptor. |
| 4 | + /// |
| 5 | + /// Advisory locks allow cooperating processes to perform consistent operations on files, |
| 6 | + /// but do not guarantee consistency (i.e., processes may still access files without using advisory locks |
| 7 | + /// possibly resulting in inconsistencies). |
| 8 | + /// |
| 9 | + /// The locking mechanism allows two types of locks: shared locks and exclusive locks. |
| 10 | + /// At any time multiple shared locks may be applied to a file, but at no time are multiple exclusive, or |
| 11 | + /// both shared and exclusive, locks allowed simultaneously on a file. |
| 12 | + /// |
| 13 | + /// A shared lock may be upgraded to an exclusive lock, and vice versa, simply by specifying the appropriate |
| 14 | + /// lock type; this results in the previous lock being released and the new lock |
| 15 | + /// applied (possibly after other processes have gained and released the lock). |
| 16 | + /// |
| 17 | + /// Requesting a lock on an object that is already locked normally causes the caller to be blocked |
| 18 | + /// until the lock may be acquired. If `nonBlocking` is passed as true, then this will not |
| 19 | + /// happen; instead the call will fail and `Errno.wouldBlock` will be thrown. |
| 20 | + /// |
| 21 | + /// Locks are on files, not file descriptors. That is, file descriptors duplicated through `FileDescriptor.duplicate` |
| 22 | + /// do not result in multiple instances of a lock, but rather multiple references to a |
| 23 | + /// single lock. If a process holding a lock on a file forks and the child explicitly unlocks the file, the parent will lose its lock. |
| 24 | + /// |
| 25 | + /// The corresponding C function is `flock()` |
| 26 | + @_alwaysEmitIntoClient |
| 27 | + public func lock( |
| 28 | + exclusive: Bool = false, |
| 29 | + nonBlocking: Bool = false, |
| 30 | + retryOnInterrupt: Bool = true |
| 31 | + ) throws { |
| 32 | + try _lock(exclusive: exclusive, nonBlocking: nonBlocking, retryOnInterrupt: retryOnInterrupt).get() |
| 33 | + } |
| 34 | + |
| 35 | + /// Unlocks an existing advisory lock on the file associated with this descriptor. |
| 36 | + /// |
| 37 | + /// The corresponding C function is `flock` passed `LOCK_UN` |
| 38 | + @_alwaysEmitIntoClient |
| 39 | + public func unlock(retryOnInterrupt: Bool = true) throws { |
| 40 | + try _unlock(retryOnInterrupt: retryOnInterrupt).get() |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + @usableFromInline |
| 45 | + internal func _lock( |
| 46 | + exclusive: Bool, |
| 47 | + nonBlocking: Bool, |
| 48 | + retryOnInterrupt: Bool |
| 49 | + ) -> Result<(), Errno> { |
| 50 | + var operation: CInt |
| 51 | + if exclusive { |
| 52 | + operation = _LOCK_EX |
| 53 | + } else { |
| 54 | + operation = _LOCK_SH |
| 55 | + } |
| 56 | + if nonBlocking { |
| 57 | + operation |= _LOCK_NB |
| 58 | + } |
| 59 | + return nothingOrErrno(retryOnInterrupt: retryOnInterrupt) { |
| 60 | + system_flock(self.rawValue, operation) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @usableFromInline |
| 65 | + internal func _unlock( |
| 66 | + retryOnInterrupt: Bool |
| 67 | + ) -> Result<(), Errno> { |
| 68 | + return nothingOrErrno(retryOnInterrupt: retryOnInterrupt) { |
| 69 | + system_flock(self.rawValue, _LOCK_UN) |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +#endif |
| 74 | + |
0 commit comments