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 
95 class XLockHelper
96 {
97 public:
98 inline bool FLock() {if (!(shmemP->Lock(lkType))) return false;
99  doUnLock = true; return true;
100  }
101 
102  XLockHelper(XrdSsiShMam *shmemp, LockType lktype)
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:
117 XrdSsiShMam *shmemP;
118 LockType lkType;
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;
129 int shmSlots;
130 int shmItemSz;
131 int shmInfoSz;
132 int verNum;
133 int keyPos;
134 int maxKLen;
135 int shmFD;
136 int timeOut;
137 int lkCount;
138 int syncOpt;
139 int syncQWR;
140 int syncLast;
141 int syncQSZ;
142 int accMode;
143 bool isRW;
144 bool lockRO;
145 bool lockRW;
146 bool reUse;
147 bool multW;
148 bool useAtomic;
149 bool syncBase;
150 bool syncOn;
151 };
152 #endif
#define Atomic(type)
XrdSsiShMam(XrdSsiShMat::NewParms &parms)
Definition: XrdSsiShMam.cc:218
bool Resize(XrdSsiShMat::CRZParms &parms)
bool Create(XrdSsiShMat::CRZParms &parms)
Definition: XrdSsiShMam.cc:440
void Detach()
Detach the map from the shared memory.
Definition: XrdSsiShMam.cc:618
bool AddItem(void *newdata, void *olddata, const char *key, int hash, bool replace=false)
Definition: XrdSsiShMam.cc:251
bool DelItem(void *data, const char *key, int hash)
Definition: XrdSsiShMam.cc:558
int Info(const char *vname, char *buff=0, int blen=0)
Definition: XrdSsiShMam.cc:961
bool GetItem(void *data, const char *key, int hash)
Definition: XrdSsiShMam.cc:854
bool Attach(int tout, bool isrw=false)
Definition: XrdSsiShMam.cc:350
bool Export()
Definition: XrdSsiShMam.cc:714
bool Enumerate(void *&jar, char *&key, void *&val)
Definition: XrdSsiShMam.cc:644