1 /*
2  * Copyright (c) 2006-2018, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 /**
19    \file
20 
21    Four routines to be able to lock access to a file in the Linux
22    approved manner.  This seems to be the only method that works
23    over NFS.
24  */
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 /**
31    \brief Creates a uniquely-named file in the current directory.
32 
33    To work, this must be created on the same filesystem as the file which we are
34    attempting to lock.  If there are multiple processes running each competing
35    for the same lock, each gets a unique file here.
36  */
37 int __pg_make_lock_file(char *dir);
38 
39 /**
40    \brief The argument is the name of the lock.
41 
42    Each process tries to create a hard link with this name to its own
43    uniquely-named file from __pg_make_lock_file().  The one that succeeds is the
44    new lock owner.  The others fail and try again.  There is a fail-over to
45    handle the case where the process with the lock dies, which is inherently
46    unsafe, but we haven't come up with a better solution.
47  */
48 void __pg_get_lock(char *lname);
49 
50 /**
51    \brief The argument is the same name for the lock.
52 
53    The lock is released by deleting (calling unlink) for the hard link we had
54    just created.
55  */
56 void __pg_release_lock(char *lname);
57 
58 /**
59    \brief Clean up by deleting the uniquely named file we had created earlier.
60 
61    These routines only allow one lock to be managed at a time.  They dynamically
62    allocate and free memory.
63  */
64 void __pg_delete_lock_file(void);
65 
66 #ifdef __cplusplus
67 }
68 #endif
69