3 //=============================================================================
7 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
9 //=============================================================================
11 #ifndef ACE_FILE_LOCK_H
12 #define ACE_FILE_LOCK_H
13 #include /**/ "ace/pre.h"
15 #include /**/ "ace/ACE_export.h"
17 #if !defined (ACE_LACKS_PRAGMA_ONCE)
19 #endif /* ACE_LACKS_PRAGMA_ONCE */
21 #include "ace/OS_NS_stdio.h"
23 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
26 * @class ACE_File_Lock
28 * @brief A wrapper around the UNIX file locking mechanism.
30 * Allows us to "adapt" the UNIX file locking mechanisms to work
31 * with all of our Guard stuff...
33 class ACE_Export ACE_File_Lock
37 * Set the <handle_> of the File_Lock to @a handle. Note that this
38 * constructor assumes ownership of the @a handle and will close it
39 * down in <remove>. If you want the @a handle to stay open when
40 * <remove> is called make sure to call <dup> on the @a handle.
41 * If you don't want the file unlinked in the destructor pass a
42 * zero value for <unlink_in_destructor>.
44 ACE_File_Lock (ACE_HANDLE handle
= ACE_INVALID_HANDLE
,
45 bool unlink_in_destructor
= true);
47 /// Open the @a filename with @a flags and @a mode and set the result
48 /// to <handle_>. If you don't want the file unlinked in the
49 /// destructor pass a false value for @a unlink_in_destructor.
50 ACE_File_Lock (const ACE_TCHAR
*filename
,
53 bool unlink_in_destructor
= true);
55 /// Open the @a filename with @a flags and @a mode and set the result to
57 int open (const ACE_TCHAR
*filename
,
61 /// Remove a File lock by releasing it and closing down the <handle_>.
64 /// Remove a File lock by releasing it and closing down the
65 /// <handle_>. If @a unlink_file is true then we unlink the file.
66 int remove (bool unlink_file
= true);
69 * Note, for interface uniformity with other synchronization
70 * wrappers we include the acquire() method. This is implemented as
71 * a write-lock to be on the safe-side...
73 int acquire (short whence
= 0, ACE_OFF_T start
= 0, ACE_OFF_T len
= 1);
76 * Note, for interface uniformity with other synchronization
77 * wrappers we include the <tryacquire> method. This is implemented
78 * as a write-lock to be on the safe-side... Returns -1 on failure.
79 * If we "failed" because someone else already had the lock, @c errno
82 int tryacquire (short whence
= 0, ACE_OFF_T start
= 0, ACE_OFF_T len
= 1);
84 /// Unlock a readers/writer lock.
85 int release (short whence
= 0, ACE_OFF_T start
= 0, ACE_OFF_T len
= 1);
87 /// Acquire a write lock, but block if any readers or a
88 /// writer hold the lock.
89 int acquire_write (short whence
= 0, ACE_OFF_T start
= 0, ACE_OFF_T len
= 1);
92 * Conditionally acquire a write lock (i.e., won't block). Returns
93 * -1 on failure. If we "failed" because someone else already had
94 * the lock, @c errno is set to @c EBUSY.
96 int tryacquire_write (short whence
= 0, ACE_OFF_T start
= 0, ACE_OFF_T len
= 1);
99 * Conditionally upgrade to a write lock (i.e., won't block). Returns
100 * -1 on failure. If we "failed" because someone else already had
101 * the lock, @c errno is set to @c EBUSY.
103 int tryacquire_write_upgrade (short whence
= 0,
108 * Acquire a read lock, but block if a writer hold the lock.
109 * Returns -1 on failure. If we "failed" because someone else
110 * already had the lock, @c errno is set to @c EBUSY.
112 int acquire_read (short whence
= 0, ACE_OFF_T start
= 0, ACE_OFF_T len
= 1);
115 * Conditionally acquire a read lock (i.e., won't block). Returns
116 * -1 on failure. If we "failed" because someone else already had
117 * the lock, @c errno is set to @c EBUSY.
119 int tryacquire_read (short whence
= 0, ACE_OFF_T start
= 0, ACE_OFF_T len
= 1);
121 /// Get underlying ACE_HANDLE for the file.
122 ACE_HANDLE
get_handle () const;
125 * Set underlying ACE_HANDLE. Note that this method assumes
126 * ownership of the @a handle and will close it down in <remove>. If
127 * you want the @a handle to stay open when <remove> is called make
128 * sure to call <dup> on the @a handle before closing it. You are
129 * responsible for the closing the existing @a handle before
132 void set_handle (ACE_HANDLE
);
134 /// Dump state of the object.
137 /// Declare the dynamic allocation hooks.
138 ACE_ALLOC_HOOK_DECLARE
;
141 /// Locking structure for OS record locks.
142 ACE_OS::ace_flock_t lock_
;
144 /// Keeps track of whether <remove> has been called yet to avoid
145 /// multiple <remove> calls, e.g., explicitly and implicitly in the
146 /// destructor. This flag isn't protected by a lock, so make sure
147 /// that you don't have multiple threads simultaneously calling
148 /// <remove> on the same object, which is a bad idea anyway...
151 /// Keeps track of whether to unlink the underlying file in the
153 bool const unlink_in_destructor_
;
156 void operator= (const ACE_File_Lock
&) = delete;
157 ACE_File_Lock (const ACE_File_Lock
&) = delete;
160 ACE_END_VERSIONED_NAMESPACE_DECL
162 #if defined (__ACE_INLINE__)
163 #include "ace/File_Lock.inl"
164 #endif /* __ACE_INLINE__ */
166 #include /**/ "ace/post.h"
167 #endif /* ACE_FILE_LOCK_H */