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()));
79 offset = bytes(x.rdbuf() -> fd());
81 offset = bytes(ai_info);
83 x << "A-" << reader_info << "\n";
90 fstream x(ai_path, ios::in);
92 MESSAGE(cerr, "read_lock(): can't open lock file");
93 throw(streamException(x.rdstate()));
97 int sz = bytes(x.rdbuf() -> fd());
99 int sz = bytes(ai_info);
102 ai_info = new char[sz+1];
105 x.getline(ai_info, sz);
112 if ( l.unlock() == false ) {
113 MESSAGE(cerr, "read_lock(): can't do atomic unlocking");
120 Boolean read_unlock(char* lock_file_path, char* ai_path, int offset)
122 atomic_lock l(lock_file_path);
124 if ( l.lock() == false ) {
125 MESSAGE(cerr, "read_lock(): can't do atomic locking");
131 fstream x(ai_path, ios::in|ios::out);
133 MESSAGE(cerr, "read_unlock(): can't open lock file");
134 throw(streamException(x.rdstate()));
137 x.seekg( offset, ios::beg );
140 ///////////////////////////////////////////////
141 // truncate the info_file if no active readers
142 // and the file size is over 1k
143 ///////////////////////////////////////////////
146 if ( bytes(x.rdbuf() -> fd()) > 1024 ) {
148 if ( bytes(ai_path) > 1024 ) {
153 /////////////////////////////////////////
154 // scan the info file for active readers
155 /////////////////////////////////////////
156 while ( x.getline(buf, BUFSIZ) ) {
157 if ( buf[0] == 'A' ) {
164 if ( truncate(ai_path, 0) != 0 ) {
165 MESSAGE(cerr, "read_unlock(): can't truncate");
166 throw(systemException(errno));
172 if ( l.unlock() == false ) {
173 MESSAGE(cerr, "read_lock(): can't do atomic locking");
181 Boolean write_lock(char* lock_file_path,
182 char* writing_lock_path,
183 char* ai_path, char* writer_info,
187 unsigned int len, slen;
188 atomic_lock l(lock_file_path);
190 if ( l.lock() == false ) {
191 MESSAGE(cerr, "write_lock(): can't do atomic locking");
197 fstream x(ai_path, ios::in|ios::out);
200 MESSAGE(cerr, "write_lock(): can't open info file");
201 throw(streamException(x.rdstate()));
207 int sz = bytes(x.rdbuf() -> fd());
209 int sz = bytes(ai_path);
211 ai_info = new char[sz+1];
214 /////////////////////////////////////////
215 // scan the info file for active readers
216 /////////////////////////////////////////
217 while ( x.getline(buf, BUFSIZ) ) {
218 if ( buf[0] == 'A' ) {
221 slen = strlen(ai_info);
222 len = MIN(strlen(buf+1), BUFSIZ - 1 - slen);
223 *((char *) memcpy(ai_info + slen, buf+1, len) + len) = '\0';
225 slen = strlen(ai_info);
226 len = MIN(1, BUFSIZ - 1 - slen);
227 *((char *) memcpy(ai_info + slen, "\n", len) + len) = '\0';
233 if ( exist_file( writing_lock_path ) == false ) {
238 /////////////////////////////////////////
239 // create the access info file
240 /////////////////////////////////////////
241 if(truncate(ai_path, 0) != 0 ) {
242 throw(systemException(errno));
244 fstream x(ai_path, ios::out);
245 x << "A-" << writer_info << "\n";
247 /////////////////////////////////////////
248 // create the writing lock file
249 /////////////////////////////////////////
250 if ( creat(writing_lock_path, 0755) == -1 )
257 if ( l.unlock() == false ) {
258 MESSAGE(cerr, "write_lock(): can't do atomic unlocking");
265 Boolean write_unlock(char* lock_file_path, char* writing_lock_path,
269 atomic_lock l(lock_file_path);
271 if ( l.lock() == false ) {
272 MESSAGE(cerr, "write_unlock(): can't do atomic locking");
278 if ( del_file(writing_lock_path) == 0 &&
279 del_file(ai_path) == 0
285 if ( l.unlock() == false ) {
286 MESSAGE(cerr, "write_unlock(): can't do atomic unlocking");