2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $XConsortium: rw_lock.cc /main/3 1996/06/11 17:38:50 cde-hal $
26 * Copyright (c) 1993 HAL Computer Systems International, Ltd.
27 * All rights reserved. Unpublished -- rights reserved under
28 * the Copyright Laws of the United States. USE OF A COPYRIGHT
29 * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
32 * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33 * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
34 * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35 * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
38 * RESTRICTED RIGHTS LEGEND
39 * Use, duplication, or disclosure by the Government is subject
40 * to the restrictions as set forth in subparagraph (c)(l)(ii)
41 * of the Rights in Technical Data and Computer Software clause
42 * at DFARS 252.227-7013.
44 * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
51 #include "atomic_lock.h"
54 Boolean read_lock(char* lock_file_path,
55 char* writing_lock_file_path,
57 char* reader_info, int& offset,
61 atomic_lock l(lock_file_path);
63 if ( l.lock() == false ) {
64 MESSAGE(cerr, "read_lock(): can't do atomic locking");
70 if ( false == exist_file( writing_lock_file_path ) ) {
72 fstream x(ai_path, ios::app);
74 MESSAGE(cerr, "read_lock(): can't open lock file");
75 throw(streamException(x.rdstate()));
78 offset = bytes(x.rdbuf() -> fd());
79 x << "A-" << reader_info << "\n";
86 fstream x(ai_path, ios::in);
88 MESSAGE(cerr, "read_lock(): can't open lock file");
89 throw(streamException(x.rdstate()));
92 int sz = bytes(x.rdbuf() -> fd());
94 ai_info = new char[sz+1];
97 x.getline(ai_info, sz);
104 if ( l.unlock() == false ) {
105 MESSAGE(cerr, "read_lock(): can't do atomic unlocking");
112 Boolean read_unlock(char* lock_file_path, char* ai_path, int offset)
114 atomic_lock l(lock_file_path);
116 if ( l.lock() == false ) {
117 MESSAGE(cerr, "read_lock(): can't do atomic locking");
123 fstream x(ai_path, ios::in|ios::out);
125 MESSAGE(cerr, "read_unlock(): can't open lock file");
126 throw(streamException(x.rdstate()));
129 x.seekg( offset, ios::beg );
132 ///////////////////////////////////////////////
133 // truncate the info_file if no active readers
134 // and the file size is over 1k
135 ///////////////////////////////////////////////
137 if ( bytes(x.rdbuf() -> fd()) > 1024 ) {
141 /////////////////////////////////////////
142 // scan the info file for active readers
143 /////////////////////////////////////////
144 while ( x.getline(buf, BUFSIZ) ) {
145 if ( buf[0] == 'A' ) {
152 if ( truncate(ai_path, 0) != 0 ) {
153 MESSAGE(cerr, "read_unlock(): can't truncate");
154 throw(systemException(errno));
160 if ( l.unlock() == false ) {
161 MESSAGE(cerr, "read_lock(): can't do atomic locking");
169 Boolean write_lock(char* lock_file_path,
170 char* writing_lock_path,
171 char* ai_path, char* writer_info,
175 atomic_lock l(lock_file_path);
177 if ( l.lock() == false ) {
178 MESSAGE(cerr, "write_lock(): can't do atomic locking");
184 fstream x(ai_path, ios::in|ios::out);
187 MESSAGE(cerr, "write_lock(): can't open info file");
188 throw(streamException(x.rdstate()));
193 int sz = bytes(x.rdbuf() -> fd());
194 ai_info = new char[sz+1];
197 /////////////////////////////////////////
198 // scan the info file for active readers
199 /////////////////////////////////////////
200 while ( x.getline(buf, BUFSIZ) ) {
201 if ( buf[0] == 'A' ) {
203 strcat(ai_info, buf+1);
204 strcat(ai_info, "\n");
210 if ( exist_file( writing_lock_path ) == false ) {
215 /////////////////////////////////////////
216 // create the access info file
217 /////////////////////////////////////////
218 truncate(ai_path, 0);
219 fstream x(ai_path, ios::out);
220 x << "A-" << writer_info << "\n";
222 /////////////////////////////////////////
223 // create the writing lock file
224 /////////////////////////////////////////
225 if ( creat(writing_lock_path, 0755) == -1 )
232 if ( l.unlock() == false ) {
233 MESSAGE(cerr, "write_lock(): can't do atomic unlocking");
240 Boolean write_unlock(char* lock_file_path, char* writing_lock_path,
244 atomic_lock l(lock_file_path);
246 if ( l.lock() == false ) {
247 MESSAGE(cerr, "write_unlock(): can't do atomic locking");
253 if ( del_file(writing_lock_path) == 0 &&
254 del_file(ai_path) == 0
260 if ( l.unlock() == false ) {
261 MESSAGE(cerr, "write_unlock(): can't do atomic unlocking");