//# FileLocker.cc: Class to handle file locking //# Copyright (C) 1997,1998,1999,2000,2001,2002 //# Associated Universities, Inc. Washington DC, USA. //# //# This library is free software; you can redistribute it and/or modify it //# under the terms of the GNU Library General Public License as published by //# the Free Software Foundation; either version 2 of the License, or (at your //# option) any later version. //# //# This library is distributed in the hope that it will be useful, but WITHOUT //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public //# License for more details. //# //# You should have received a copy of the GNU Library General Public License //# along with this library; if not, write to the Free Software Foundation, //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. //# //# Correspondence concerning AIPS++ should be addressed as follows: //# Internet email: aips2-request@nrao.edu. //# Postal address: AIPS++ Project Office //# National Radio Astronomy Observatory //# 520 Edgemont Road //# Charlottesville, VA 22903-2475 USA //# //# $Id$ #include #include #include #include #include #include #include //# Locking is not supported on Cray compute nodes. #if defined(AIPS_CRAY_PGI) && !defined(AIPS_NOFILELOCK) # define AIPS_NOFILELOCK 1 #endif namespace casacore { //# NAMESPACE CASACORE - BEGIN FileLocker::FileLocker() : itsFD (-1), itsError (0), itsStart (0), itsLength (0), itsMsgShown (False), itsReadLocked (False), itsWriteLocked (False) {} FileLocker::FileLocker (int fd, uInt start, uInt length) : itsFD (fd), itsError (0), itsStart (start), itsLength (length), itsMsgShown (False), itsReadLocked (False), itsWriteLocked (False) {} FileLocker::~FileLocker() {} Bool FileLocker::acquire (LockType type, uInt nattempts) { itsError = 0; // Always success if locking is not supported. #if defined(AIPS_NOFILELOCK) itsReadLocked = True; if (!itsWriteLocked && type == Write) { itsWriteLocked = True; } return True; #else struct flock ls; ls.l_whence = SEEK_SET; ls.l_start = itsStart; ls.l_len = itsLength; ls.l_type = F_WRLCK; // When a read-lock is acquired, it may release an existing write-lock. // We do not want that to happen, so when it is write-locked, test // if the write-lock is still valid. if (type == Read) { if (itsWriteLocked) { if (fcntl (itsFD, F_SETLK, &ls) != -1) { /// cout << "kept " << itsReadLocked << ' ' <