-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_file_lock.m
56 lines (54 loc) · 1.53 KB
/
get_file_lock.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
%GET_FILE_LOCK Grab a file lock for exclusive access to a file
%
% lock = get_file_lock(fname, [force])
%
% Grab a file lock associated with a particular file. The lock is a mutex -
% only one MATLAB instance can hold the lock at a time. The lock is
% released when the output object is cleared.
%
%IN:
% fname - string name or path of the file to lock. The lock is given the
% name [fname '.lock'].
% force - indicates whether to try to grab the lock, even if the lock
% file exists. Default: false.
%
%OUT:
% lock - [] if the lock was not obtained, otherwise, not empty. The
% object should be cleared, by calling clear('lock'), when the
% lock is to be released.
function [lock, cleanup_fun] = get_file_lock(fname, force)
% Initialize outputs
lock = [];
cleanup_fun = @() [];
% Attempt to grab the lock
fname = [fname '.lock'];
% Check that the file exists (backup in case file locking not supported)
if exist(fname, 'file') && (nargin < 2 || ~force)
return
end
jh = javaObject('java.io.RandomAccessFile', fname, 'rw');
lock_ = jh.getChannel().tryLock();
if isempty(lock_)
% Failed - something else has the lock
jh.close();
else
% Succeeded - make sure the lock is deleted when finished with
cleanup_fun = @() cleanup_lock(lock_, jh, fname);
lock = onCleanup(cleanup_fun);
end
end
function cleanup_lock(lock, jh, fname)
% Free and delete the lock file
try
lock.release();
catch
end
try
jh.close();
catch
end
try
delete(fname);
catch
end
end