XRootD
Loading...
Searching...
No Matches
XrdXrootdFileLock1.cc
Go to the documentation of this file.
1/******************************************************************************/
2/* */
3/* X r d X r o o t d F i l e L o c k 1 . c c */
4/* */
5/* (c) 2004 by the Board of Trustees of the Leland Stanford, Jr., University */
6/* Produced by Andrew Hanushevsky for Stanford University under contract */
7/* DE-AC02-76-SFO0515 with the Department of Energy */
8/* */
9/* This file is part of the XRootD software suite. */
10/* */
11/* XRootD is free software: you can redistribute it and/or modify it under */
12/* the terms of the GNU Lesser General Public License as published by the */
13/* Free Software Foundation, either version 3 of the License, or (at your */
14/* option) any later version. */
15/* */
16/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
17/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
18/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
19/* License for more details. */
20/* */
21/* You should have received a copy of the GNU Lesser General Public License */
22/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
23/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
24/* */
25/* The copyright holder's institutional names and contributor's names may not */
26/* be used to endorse or promote products derived from this software without */
27/* specific prior written permission of the institution or contributor. */
28/******************************************************************************/
29
30#include <cstdlib>
31
32#include "XrdOuc/XrdOucHash.hh"
33
35
36/******************************************************************************/
37/* L o c a l C l a s s e s */
38/******************************************************************************/
39
41{
42public:
43
46
48 {if (mode == 'r') {numReaders = 1; numWriters = 0;}
49 else {numReaders = 0; numWriters = 1;}
50 }
52};
53
55{
56public:
57
59 {mp = mutex; mp->Lock();}
61 {mp->UnLock();}
62private:
63XrdSysMutex *mp;
64};
65
66/******************************************************************************/
67/* G l o b a l s */
68/******************************************************************************/
69
71
72XrdSysMutex XrdXrootdFileLock1::LTMutex;
73
74const char *XrdXrootdFileLock1::TraceID = "FileLock1";
75
76/******************************************************************************/
77/* L o c k */
78/******************************************************************************/
79
80int XrdXrootdFileLock1::Lock(const char *path, char mode, bool force)
81{
82 XrdXrootdLockFileLock locker(&LTMutex);
84
85// See if we already have a lock on this file
86//
87 if ((lp = XrdXrootdLockTable.Find(path)))
88 {if (mode == 'r')
89 {if (lp->numWriters && !force)
90 return -lp->numWriters;
91 lp->numReaders++;
92 } else {
93 if ((lp->numReaders || lp->numWriters) && !force)
94 return (lp->numWriters ? -lp->numWriters : lp->numReaders);
95 lp->numWriters++;
96 }
97 return 0;
98 }
99
100// Item does not exist, add it to the table
101//
102 XrdXrootdLockTable.Add(path, new XrdXrootdFileLockInfo(mode));
103 return 0;
104}
105
106/******************************************************************************/
107/* */
108/* n u m L o c k s */
109/* */
110/******************************************************************************/
111
112void XrdXrootdFileLock1::numLocks(const char *path, int &rcnt, int &wcnt)
113{
114 XrdXrootdLockFileLock locker(&LTMutex);
116
117 if (!(lp = XrdXrootdLockTable.Find(path))) rcnt = wcnt = 0;
118 else {rcnt = lp->numReaders; wcnt = lp->numWriters;}
119}
120
121/******************************************************************************/
122/* U n l o c k */
123/******************************************************************************/
124
125int XrdXrootdFileLock1::Unlock(const char *path, char mode)
126{
127 XrdXrootdLockFileLock locker(&LTMutex);
129
130// See if we already have a lock on this file
131//
132 if (!(lp = XrdXrootdLockTable.Find(path))) return 1;
133
134// Adjust the lock information
135//
136 if (mode == 'r')
137 {if (lp->numReaders == 0) return 1;
138 lp->numReaders--;
139 } else {
140 if (lp->numWriters == 0) return 1;
141 lp->numWriters--;
142 }
143
144// Delete the entry if we no longer need it
145//
146 if (lp->numReaders == 0 && lp->numWriters == 0)
147 XrdXrootdLockTable.Del(path);
148 return 0;
149}
XrdOucHash< XrdXrootdFileLockInfo > XrdXrootdLockTable
void numLocks(const char *path, int &rcnt, int &wcnt)
int Unlock(const char *path, char mode)
int Lock(const char *path, char mode, bool force)
XrdXrootdLockFileLock(XrdSysMutex *mutex)