@@ -13,30 +13,29 @@ import (
13
13
"syscall"
14
14
)
15
15
16
- // Lock is a blocking call to try and take an exclusive file lock. It will wait
17
- // until it is able to obtain the exclusive file lock. It's recommended that
18
- // TryLock() be used over this function. This function may block the ability to
19
- // query the current Locked() or RLocked() status due to a RW-mutex lock.
16
+ // Lock is a blocking call to try and take an exclusive file lock.
17
+ // It will wait until it is able to obtain the exclusive file lock.
18
+ // It's recommended that TryLock() be used over this function.
19
+ // This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.
20
20
//
21
- // If we are already exclusive-locked, this function short-circuits and returns
22
- // immediately assuming it can take the mutex lock.
21
+ // If we are already exclusive-locked,
22
+ // this function short-circuits and returns immediately assuming it can take the mutex lock.
23
23
//
24
- // If the *Flock has a shared lock (RLock), this may transparently replace the
25
- // shared lock with an exclusive lock on some UNIX-like operating systems. Be
26
- // careful when using exclusive locks in conjunction with shared locks
27
- // (RLock()), because calling Unlock() may accidentally release the exclusive
28
- // lock that was once a shared lock.
24
+ // If the *Flock has a shared lock (RLock),
25
+ // this may transparently replace the shared lock with an exclusive lock on some UNIX-like operating systems.
26
+ // Be careful when using exclusive locks in conjunction with shared locks (RLock()),
27
+ // because calling Unlock() may accidentally release the exclusive lock that was once a shared lock.
29
28
func (f * Flock ) Lock () error {
30
29
return f .lock (& f .l , syscall .LOCK_EX )
31
30
}
32
31
33
- // RLock is a blocking call to try and take a shared file lock. It will wait
34
- // until it is able to obtain the shared file lock. It's recommended that
35
- // TryRLock() be used over this function. This function may block the ability to
36
- // query the current Locked() or RLocked() status due to a RW-mutex lock.
32
+ // RLock is a blocking call to try and take a shared file lock.
33
+ // It will wait until it is able to obtain the shared file lock.
34
+ // It's recommended that TryRLock() be used over this function.
35
+ // This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.
37
36
//
38
- // If we are already shared-locked, this function short-circuits and returns
39
- // immediately assuming it can take the mutex lock.
37
+ // If we are already shared-locked,
38
+ // this function short-circuits and returns immediately assuming it can take the mutex lock.
40
39
func (f * Flock ) RLock () error {
41
40
return f .lock (& f .r , syscall .LOCK_SH )
42
41
}
@@ -77,27 +76,29 @@ func (f *Flock) lock(locked *bool, flag int) error {
77
76
return nil
78
77
}
79
78
80
- // Unlock is a function to unlock the file. This file takes a RW-mutex lock, so
81
- // while it is running the Locked() and RLocked() functions will be blocked.
79
+ // Unlock is a function to unlock the file.
80
+ // This file takes a RW-mutex lock,
81
+ // so while it is running the Locked() and RLocked() functions will be blocked.
82
82
//
83
- // This function short-circuits if we are unlocked already. If not, it calls
84
- // syscall.LOCK_UN on the file and closes the file descriptor. It does not
85
- // remove the file from disk. It's up to your application to do.
83
+ // This function short-circuits if we are unlocked already.
84
+ // If not, it calls syscall.LOCK_UN on the file and closes the file descriptor.
85
+ // It does not remove the file from disk. It's up to your application to do.
86
86
//
87
- // Please note, if your shared lock became an exclusive lock this may
88
- // unintentionally drop the exclusive lock if called by the consumer that
89
- // believes they have a shared lock. Please see Lock() for more details.
87
+ // Please note,
88
+ // if your shared lock became an exclusive lock,
89
+ // this may unintentionally drop the exclusive lock if called by the consumer that believes they have a shared lock.
90
+ // Please see Lock() for more details.
90
91
func (f * Flock ) Unlock () error {
91
92
f .m .Lock ()
92
93
defer f .m .Unlock ()
93
94
94
- // if we aren't locked or if the lockfile instance is nil
95
- // just return a nil error because we are unlocked
95
+ // If we aren't locked or if the lockfile instance is nil
96
+ // just return a nil error because we are unlocked.
96
97
if (! f .l && ! f .r ) || f .fh == nil {
97
98
return nil
98
99
}
99
100
100
- // mark the file as unlocked
101
+ // Mark the file as unlocked.
101
102
if err := syscall .Flock (int (f .fh .Fd ()), syscall .LOCK_UN ); err != nil {
102
103
return err
103
104
}
@@ -111,26 +112,28 @@ func (f *Flock) Unlock() error {
111
112
return nil
112
113
}
113
114
114
- // TryLock is the preferred function for taking an exclusive file lock. This
115
- // function takes an RW-mutex lock before it tries to lock the file, so there is
116
- // the possibility that this function may block for a short time if another
117
- // goroutine is trying to take any action.
115
+ // TryLock is the preferred function for taking an exclusive file lock.
116
+ // This function takes an RW-mutex lock before it tries to lock the file,
117
+ // so there is the possibility that this function may block for a short time
118
+ // if another goroutine is trying to take any action.
118
119
//
119
- // The actual file lock is non-blocking. If we are unable to get the exclusive
120
- // file lock, the function will return false instead of waiting for the lock. If
121
- // we get the lock, we also set the *Flock instance as being exclusive-locked.
120
+ // The actual file lock is non-blocking.
121
+ // If we are unable to get the exclusive file lock,
122
+ // the function will return false instead of waiting for the lock.
123
+ // If we get the lock, we also set the *Flock instance as being exclusive-locked.
122
124
func (f * Flock ) TryLock () (bool , error ) {
123
125
return f .try (& f .l , syscall .LOCK_EX )
124
126
}
125
127
126
- // TryRLock is the preferred function for taking a shared file lock. This
127
- // function takes an RW-mutex lock before it tries to lock the file, so there is
128
- // the possibility that this function may block for a short time if another
129
- // goroutine is trying to take any action.
128
+ // TryRLock is the preferred function for taking a shared file lock.
129
+ // This function takes an RW-mutex lock before it tries to lock the file,
130
+ // so there is the possibility that this function may block for a short time
131
+ // if another goroutine is trying to take any action.
130
132
//
131
- // The actual file lock is non-blocking. If we are unable to get the shared file
132
- // lock, the function will return false instead of waiting for the lock. If we
133
- // get the lock, we also set the *Flock instance as being share-locked.
133
+ // The actual file lock is non-blocking.
134
+ // If we are unable to get the shared file lock,
135
+ // the function will return false instead of waiting for the lock.
136
+ // If we get the lock, we also set the *Flock instance as being share-locked.
134
137
func (f * Flock ) TryRLock () (bool , error ) {
135
138
return f .try (& f .r , syscall .LOCK_SH )
136
139
}
@@ -175,8 +178,8 @@ retry:
175
178
return false , err
176
179
}
177
180
178
- // reopenFDOnError determines whether we should reopen the file handle
179
- // in readwrite mode and try again. This comes from util-linux/sys-utils/flock.c:
181
+ // reopenFDOnError determines whether we should reopen the file handle in readwrite mode and try again.
182
+ // This comes from ` util-linux/sys-utils/flock.c` :
180
183
//
181
184
// Since Linux 3.4 (commit 55725513)
182
185
// Probably NFSv4 where flock() is emulated by fcntl().
0 commit comments