xrootd
XrdSsiShMam.hh
Go to the documentation of this file.
1 #ifndef __SSI_SHMAM__
2 #define __SSI_SHMAM__
3 /******************************************************************************/
4 /* */
5 /* X r d S s i S h M a m . h h */
6 /* */
7 /* (c) 2015 by the Board of Trustees of the Leland Stanford, Jr., University */
8 /* Produced by Andrew Hanushevsky for Stanford University under contract */
9 /* DE-AC02-76-SFO0515 with the Department of Energy */
10 /* */
11 /* This file is part of the XRootD software suite. */
12 /* */
13 /* XRootD is free software: you can redistribute it and/or modify it under */
14 /* the terms of the GNU Lesser General Public License as published by the */
15 /* Free Software Foundation, either version 3 of the License, or (at your */
16 /* option) any later version. */
17 /* */
18 /* XRootD is distributed in the hope that it will be useful, but WITHOUT */
19 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
20 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public */
21 /* License for more details. */
22 /* */
23 /* You should have received a copy of the GNU Lesser General Public License */
24 /* along with XRootD in a file called COPYING.LESSER (LGPL license) and file */
25 /* COPYING (GPL license). If not, see <http://www.gnu.org/licenses/>. */
26 /* */
27 /* The copyright holder's institutional names and contributor's names may not */
28 /* be used to endorse or promote products derived from this software without */
29 /* specific prior written permission of the institution or contributor. */
30 /******************************************************************************/
31 
32 #include <pthread.h>
33 
34 #include "XrdSsi/XrdSsiAtomics.hh"
35 #include "XrdSsi/XrdSsiShMat.hh"
36 
37 class XrdSsiShMam : public XrdSsiShMat
38 {
39 public:
40 
41 bool AddItem(void *newdata, void *olddata, const char *key,
42  int hash, bool replace=false);
43 
44 bool Attach(int tout, bool isrw=false);
45 
46 bool Create(XrdSsiShMat::CRZParms &parms);
47 
48 bool Export();
49 
50 bool DelItem(void *data, const char *key, int hash);
51 
52 void Detach();
53 
54 bool Enumerate(void *&jar, char *&key, void *&val);
55 
56 bool Enumerate(void *&jar);
57 
58 bool GetItem(void *data, const char *key, int hash);
59 
60 int Info(const char *vname, char *buff=0, int blen=0);
61 
62 bool Resize(XrdSsiShMat::CRZParms &parms);
63 
64 bool Sync();
65 bool Sync(bool dosync, bool syncdo);
66 bool Sync(int syncqsz);
67 
69 
71  pthread_mutex_destroy(&lkMutex);
72  pthread_rwlock_destroy(&myMutex);
73  }
74 
75 enum LockType {ROLock= 0, RWLock = 1};
76 
77 private:
78 struct MemItem {int hash; Atomic(int) next;};
79 
80 bool ExportIt(bool fLocked);
81 int Find(MemItem *&theItem, MemItem *&prvItem, const char *key, int &hash);
82 bool Flush();
83 int HashVal(const char *key);
84 bool Lock(bool doRW=false, bool nowait=false);
85 MemItem *NewItem();
86 bool ReMap(LockType iHave);
87 void RetItem(MemItem *iP);
88 void SetLocking(bool isrw);
89 void SwapMap(XrdSsiShMam &newMap);
90 void Snooze(int sec);
91 void UnLock(bool isrw);
92 void Updated(int mOff);
93 void Updated(int mOff, int mLen);
94 
96 {
97 public:
98 inline bool FLock() {if (!(shmemP->Lock(lkType))) return false;
99  doUnLock = true; return true;
100  }
101 
103  : shmemP(shmemp), lkType(lktype), doUnLock(false)
104  {if (lktype == RWLock)
105  pthread_rwlock_wrlock(&(shmemP->myMutex));
106  else pthread_rwlock_rdlock(&(shmemP->myMutex));
107  }
108  ~XLockHelper() {int rc = errno;
109  if (lkType == RWLock && shmemP->syncOn
110  && shmemP->syncQWR > shmemP->syncQSZ)
111  shmemP-> Flush();
112  if (doUnLock) shmemP->UnLock(lkType == RWLock);
113  pthread_rwlock_unlock(&(shmemP->myMutex));
114  errno = rc;
115  }
116 private:
119 bool doUnLock;
120 };
121 
122 pthread_mutex_t lkMutex;
123 pthread_rwlock_t myMutex;
124 
125 char *shmTemp;
126 long long shmSize;
127 char *shmBase;
128 Atomic(int)*shmIndex;
132 int verNum;
133 int keyPos;
135 int shmFD;
143 bool isRW;
144 bool lockRO;
145 bool lockRW;
146 bool reUse;
147 bool multW;
149 bool syncBase;
150 bool syncOn;
151 };
152 #endif
XrdSsiShMam::LockType
LockType
Definition: XrdSsiShMam.hh:75
XrdSsiShMat::CRZParms
Definition: XrdSsiShMat.hh:99
XrdSsiShMam::Updated
void Updated(int mOff)
XrdSsiShMam::Find
int Find(MemItem *&theItem, MemItem *&prvItem, const char *key, int &hash)
XrdSsiShMam::UnLock
void UnLock(bool isrw)
XrdSsiShMam::AddItem
bool AddItem(void *newdata, void *olddata, const char *key, int hash, bool replace=false)
XrdSsiShMat::NewParms
Definition: XrdSsiShMat.hh:276
XrdSsiShMam::SwapMap
void SwapMap(XrdSsiShMam &newMap)
XrdSsiShMam::shmSize
long long shmSize
Definition: XrdSsiShMam.hh:126
XrdSsiShMam::XLockHelper::~XLockHelper
~XLockHelper()
Definition: XrdSsiShMam.hh:108
XrdSsiAtomics.hh
XrdSsiShMam::Atomic
Atomic(int) *shmIndex
XrdSsiShMam::syncOn
bool syncOn
Definition: XrdSsiShMam.hh:150
XrdSsiShMam::XLockHelper::lkType
LockType lkType
Definition: XrdSsiShMam.hh:118
XrdSsiShMam::lkCount
int lkCount
Definition: XrdSsiShMam.hh:137
XrdSsiShMam::XLockHelper::XLockHelper
XLockHelper(XrdSsiShMam *shmemp, LockType lktype)
Definition: XrdSsiShMam.hh:102
XrdSsiShMam::Snooze
void Snooze(int sec)
XrdSsiShMam::MemItem
Definition: XrdSsiShMam.hh:78
XrdSsiShMam::Lock
bool Lock(bool doRW=false, bool nowait=false)
XrdSsiShMam::shmTemp
char * shmTemp
Definition: XrdSsiShMam.hh:125
XrdSsiShMam::shmFD
int shmFD
Definition: XrdSsiShMam.hh:135
XrdSsiShMam::keyPos
int keyPos
Definition: XrdSsiShMam.hh:133
XrdSsiShMam::lkMutex
pthread_mutex_t lkMutex
Definition: XrdSsiShMam.hh:122
XrdSsiShMam::maxKLen
int maxKLen
Definition: XrdSsiShMam.hh:134
XrdSsiShMam::Detach
void Detach()
Detach the map from the shared memory.
XrdSsiShMam::shmSlots
int shmSlots
Definition: XrdSsiShMam.hh:129
XrdSsiShMam::syncLast
int syncLast
Definition: XrdSsiShMam.hh:140
XrdSsiShMam::lockRW
bool lockRW
Definition: XrdSsiShMam.hh:145
XrdSsiShMam::shmInfoSz
int shmInfoSz
Definition: XrdSsiShMam.hh:131
XrdSsiShMam::ReMap
bool ReMap(LockType iHave)
XrdSsiShMam::XLockHelper::doUnLock
bool doUnLock
Definition: XrdSsiShMam.hh:119
XrdSsiShMam::Flush
bool Flush()
XrdSsiShMam::GetItem
bool GetItem(void *data, const char *key, int hash)
XrdSsiShMam::DelItem
bool DelItem(void *data, const char *key, int hash)
XrdSsiShMam::shmBase
char * shmBase
Definition: XrdSsiShMam.hh:127
XrdSsiShMat.hh
XrdSsiShMam::XrdSsiShMam
XrdSsiShMam(XrdSsiShMat::NewParms &parms)
XrdSsiShMam::syncBase
bool syncBase
Definition: XrdSsiShMam.hh:149
XrdSsiShMam::verNum
int verNum
Definition: XrdSsiShMam.hh:132
XrdSsiShMam::syncQSZ
int syncQSZ
Definition: XrdSsiShMam.hh:141
XrdSsiShMam
Definition: XrdSsiShMam.hh:37
XrdSsiShMam::syncOpt
int syncOpt
Definition: XrdSsiShMam.hh:138
XrdSsiShMam::accMode
int accMode
Definition: XrdSsiShMam.hh:142
XrdSsiShMam::XLockHelper
Definition: XrdSsiShMam.hh:95
XrdSsiShMam::timeOut
int timeOut
Definition: XrdSsiShMam.hh:136
XrdSsiShMam::Export
bool Export()
XrdSsiShMam::syncQWR
int syncQWR
Definition: XrdSsiShMam.hh:139
XrdSsiShMam::reUse
bool reUse
Definition: XrdSsiShMam.hh:146
XrdSsiShMam::Attach
bool Attach(int tout, bool isrw=false)
XrdSsiShMam::isRW
bool isRW
Definition: XrdSsiShMam.hh:143
XrdSsiShMam::NewItem
MemItem * NewItem()
XrdSsiShMam::Create
bool Create(XrdSsiShMat::CRZParms &parms)
XrdSsiShMam::XLockHelper::FLock
bool FLock()
Definition: XrdSsiShMam.hh:98
XrdSsiShMat
Definition: XrdSsiShMat.hh:41
XrdSsiShMam::useAtomic
bool useAtomic
Definition: XrdSsiShMam.hh:148
XrdSsiShMam::XLockHelper::shmemP
XrdSsiShMam * shmemP
Definition: XrdSsiShMam.hh:117
XrdSsiShMam::Resize
bool Resize(XrdSsiShMat::CRZParms &parms)
XrdSsiShMam::RetItem
void RetItem(MemItem *iP)
XrdSsiShMam::ROLock
Definition: XrdSsiShMam.hh:75
XrdSsiShMam::SetLocking
void SetLocking(bool isrw)
XrdSsiShMam::multW
bool multW
Definition: XrdSsiShMam.hh:147
XrdSsiShMam::shmItemSz
int shmItemSz
Definition: XrdSsiShMam.hh:130
XrdSsiShMam::MemItem::hash
int hash
Definition: XrdSsiShMam.hh:78
XrdSsiShMam::Info
int Info(const char *vname, char *buff=0, int blen=0)
XrdSsiShMam::myMutex
pthread_rwlock_t myMutex
Definition: XrdSsiShMam.hh:123
XrdSsiShMam::lockRO
bool lockRO
Definition: XrdSsiShMam.hh:144
XrdSsiShMam::Sync
bool Sync()
XrdSsiShMam::Enumerate
bool Enumerate(void *&jar, char *&key, void *&val)
XrdSsiShMam::MemItem::Atomic
Atomic(int) next
XrdSsiShMam::~XrdSsiShMam
~XrdSsiShMam()
Definition: XrdSsiShMam.hh:70
XrdSsiShMam::ExportIt
bool ExportIt(bool fLocked)
XrdSsiShMam::HashVal
int HashVal(const char *key)
XrdSsiShMam::RWLock
Definition: XrdSsiShMam.hh:75