XRootD
Loading...
Searching...
No Matches
XrdSutCache.hh
Go to the documentation of this file.
1
#ifndef __SUT_CACHE_H
2
#define __SUT_CACHE_H
3
/******************************************************************************/
4
/* */
5
/* X r d S u t C a c h e . h h */
6
/* */
7
/* (c) 2005 by the Board of Trustees of the Leland Stanford, Jr., University */
8
/* Produced by Gerri Ganis for CERN */
9
/* */
10
/* This file is part of the XRootD software suite. */
11
/* */
12
/* XRootD is free software: you can redistribute it and/or modify it under */
13
/* the terms of the GNU Lesser General Public License as published by the */
14
/* Free Software Foundation, either version 3 of the License, or (at your */
15
/* option) any later version. */
16
/* */
17
/* XRootD is distributed in the hope that it will be useful, but WITHOUT */
18
/* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
19
/* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
20
/* License for more details. */
21
/* */
22
/* You should have received a copy of the GNU Lesser General Public License */
23
/* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
24
/* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
25
/* */
26
/* The copyright holder's institutional names and contributor's names may not */
27
/* be used to endorse or promote products derived from this software without */
28
/* specific prior written permission of the institution or contributor. */
29
/******************************************************************************/
30
31
#include "
XrdOuc/XrdOucHash.hh
"
32
#include "
XrdSut/XrdSutCacheEntry.hh
"
33
#include "
XrdSys/XrdSysPthread.hh
"
34
35
/******************************************************************************/
36
/* */
37
/* Class defining the basic memory cache */
38
/* */
39
/******************************************************************************/
40
41
typedef
bool (*
XrdSutCacheGet_t
)(
XrdSutCacheEntry
*,
void
*);
42
typedef
struct
{
43
long
long
arg1
;
44
long
long
arg2
;
45
long
long
arg3
;
46
long
long
arg4
;
47
}
XrdSutCacheArg_t
;
48
49
class
XrdSutCache
{
50
public
:
51
XrdSutCache
(
int
psize = 89,
int
size = 144,
int
load = 80) : table(psize, size, load) {}
52
virtual
~XrdSutCache
() {}
53
54
XrdSutCacheEntry
*
Get
(
const
char
*tag) {
55
// Get the entry with 'tag'.
56
// If found the entry is returned rd-locked.
57
// If rd-locking fails the status is set to kCE_inactive.
58
// Returns null if not found.
59
60
XrdSutCacheEntry
*cent = 0;
61
62
// Exclusive access to the table
63
XrdSysMutexHelper
raii(mtx);
64
65
// Look for an entry
66
if
(!(cent = table.Find(tag))) {
67
// none found
68
return
cent;
69
}
70
71
// We found an existing entry:
72
// lock until we get the ability to read (another thread may be valudating it)
73
int
status = 0;
74
cent->
rwmtx
.
ReadLock
( status );
75
if
( status ) {
76
// A problem occurred: fail (set the entry invalid)
77
cent->
status
=
kCE_inactive
;
78
}
79
return
cent;
80
}
81
82
XrdSutCacheEntry
*
Get
(
const
char
*tag,
bool
&rdlock,
XrdSutCacheGet_t
condition = 0,
void
*arg = 0) {
83
// Get or create the entry with 'tag'.
84
// New entries are always returned write-locked.
85
// The status of existing ones depends on condition: if condition is undefined or if applied
86
// to the entry with arguments 'arg' returns true, the entry is returned read-locked.
87
// Otherwise a write-lock is attempted on the entry: if unsuccessful (another thread is modifing
88
// the entry) the entry is read-locked.
89
// The status of the lock is returned in rdlock (true if read-locked).
90
rdlock =
false
;
91
XrdSutCacheEntry
*cent = 0;
92
93
// Exclusive access to the table
94
XrdSysMutexHelper
raii(mtx);
95
96
// Look for an entry
97
if
(!(cent = table.Find(tag))) {
98
// If none, create a new one and write-lock for validation
99
cent =
new
XrdSutCacheEntry
(tag);
100
int
status = 0;
101
cent->
rwmtx
.
WriteLock
( status );
102
if
(status) {
103
// A problem occurred: delete the entry and fail
104
delete
cent;
105
return
(
XrdSutCacheEntry
*)0;
106
}
107
// Register it in the table
108
table.Add(tag, cent);
109
return
cent;
110
}
111
112
// We found an existing entry:
113
// lock until we get the ability to read (another thread may be valudating it)
114
int
status = 0;
115
cent->
rwmtx
.
ReadLock
( status );
116
if
(status) {
117
// A problem occurred: fail (set the entry invalid)
118
cent->
status
=
kCE_inactive
;
119
return
cent;
120
}
121
122
// Check-it by apply the condition, if required
123
if
(condition) {
124
if
((*condition)(cent, arg)) {
125
// Good and valid entry
126
rdlock =
true
;
127
}
else
{
128
// Invalid entry: unlock and write-lock to be able to validate it
129
cent->
rwmtx
.
UnLock
();
130
int
status = 0;
131
cent->
rwmtx
.
WriteLock
( status );
132
if
(status) {
133
// A problem occurred: fail (set the entry invalid)
134
cent->
status
=
kCE_inactive
;
135
return
cent;
136
}
137
}
138
}
else
{
139
// Good and valid entry
140
rdlock =
true
;
141
}
142
// We are done: return read-locked so we can use it until we need it
143
return
cent;
144
}
145
146
inline
int
Num
() {
return
table.Num(); }
147
inline
void
Reset
() {
return
table.Purge(); }
148
149
private
:
150
XrdSysRecMutex
mtx;
// Protect access to table
151
XrdOucHash<XrdSutCacheEntry>
table;
// table with content
152
};
153
154
#endif
XrdOucHash.hh
XrdSutCacheEntry.hh
kCE_inactive
@ kCE_inactive
Definition
XrdSutCacheEntry.hh:41
XrdSutCacheArg_t::arg1
long long arg1
Definition
XrdSutCache.hh:43
XrdSutCacheArg_t::arg4
long long arg4
Definition
XrdSutCache.hh:46
XrdSutCacheGet_t
bool(* XrdSutCacheGet_t)(XrdSutCacheEntry *, void *)
Definition
XrdSutCache.hh:41
XrdSutCacheArg_t::arg3
long long arg3
Definition
XrdSutCache.hh:45
XrdSutCacheArg_t::arg2
long long arg2
Definition
XrdSutCache.hh:44
XrdSutCacheArg_t
Definition
XrdSutCache.hh:42
XrdSysPthread.hh
XrdOucHash
Definition
XrdOucHash.hh:128
XrdSutCacheEntry
Definition
XrdSutCacheEntry.hh:75
XrdSutCacheEntry::status
short status
Definition
XrdSutCacheEntry.hh:78
XrdSutCacheEntry::rwmtx
XrdSysRWLock rwmtx
Definition
XrdSutCacheEntry.hh:85
XrdSutCache::Get
XrdSutCacheEntry * Get(const char *tag)
Definition
XrdSutCache.hh:54
XrdSutCache::Num
int Num()
Definition
XrdSutCache.hh:146
XrdSutCache::~XrdSutCache
virtual ~XrdSutCache()
Definition
XrdSutCache.hh:52
XrdSutCache::XrdSutCache
XrdSutCache(int psize=89, int size=144, int load=80)
Definition
XrdSutCache.hh:51
XrdSutCache::Get
XrdSutCacheEntry * Get(const char *tag, bool &rdlock, XrdSutCacheGet_t condition=0, void *arg=0)
Definition
XrdSutCache.hh:82
XrdSutCache::Reset
void Reset()
Definition
XrdSutCache.hh:147
XrdSysMutexHelper
Definition
XrdSysPthread.hh:263
XrdSysRWLock::ReadLock
void ReadLock()
Definition
XrdSysPthread.hh:342
XrdSysRWLock::WriteLock
void WriteLock()
Definition
XrdSysPthread.hh:343
XrdSysRWLock::UnLock
void UnLock()
Definition
XrdSysPthread.hh:348
XrdSysRecMutex
Definition
XrdSysPthread.hh:242
XrdSut
XrdSutCache.hh
Generated by
1.13.2