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
int Info(const char *vname, char *buff=0, int blen=0)
pthread_rwlock_t myMutex
Definition: XrdSsiShMam.hh:123
Definition: XrdSsiShMat.hh:41
int shmFD
Definition: XrdSsiShMam.hh:135
void Updated(int mOff)
void UnLock(bool isrw)
int maxKLen
Definition: XrdSsiShMam.hh:134
bool Lock(bool doRW=false, bool nowait=false)
Definition: XrdSsiShMat.hh:99
void Detach()
Detach the map from the shared memory.
char * shmTemp
Definition: XrdSsiShMam.hh:125
int syncLast
Definition: XrdSsiShMam.hh:140
Atomic(int) *shmIndex
int Find(MemItem *&theItem, MemItem *&prvItem, const char *key, int &hash)
pthread_mutex_t lkMutex
Definition: XrdSsiShMam.hh:122
int lkCount
Definition: XrdSsiShMam.hh:137
bool doUnLock
Definition: XrdSsiShMam.hh:119
bool syncOn
Definition: XrdSsiShMam.hh:150
char * shmBase
Definition: XrdSsiShMam.hh:127
void Snooze(int sec)
XrdSsiShMam * shmemP
Definition: XrdSsiShMam.hh:117
bool GetItem(void *data, const char *key, int hash)
bool DelItem(void *data, const char *key, int hash)
int keyPos
Definition: XrdSsiShMam.hh:133
bool ReMap(LockType iHave)
int shmSlots
Definition: XrdSsiShMam.hh:129
int syncOpt
Definition: XrdSsiShMam.hh:138
bool lockRW
Definition: XrdSsiShMam.hh:145
int shmInfoSz
Definition: XrdSsiShMam.hh:131
Definition: XrdSsiShMam.hh:95
XrdSsiShMam(XrdSsiShMat::NewParms &parms)
int timeOut
Definition: XrdSsiShMam.hh:136
int verNum
Definition: XrdSsiShMam.hh:132
Definition: XrdSsiShMam.hh:78
bool Flush()
int syncQSZ
Definition: XrdSsiShMam.hh:141
int syncQWR
Definition: XrdSsiShMam.hh:139
bool reUse
Definition: XrdSsiShMam.hh:146
Definition: XrdSsiShMat.hh:276
bool isRW
Definition: XrdSsiShMam.hh:143
bool syncBase
Definition: XrdSsiShMam.hh:149
int accMode
Definition: XrdSsiShMam.hh:142
int shmItemSz
Definition: XrdSsiShMam.hh:130
bool Resize(XrdSsiShMat::CRZParms &parms)
Definition: XrdSsiShMam.hh:75
bool lockRO
Definition: XrdSsiShMam.hh:144
bool Enumerate(void *&jar, char *&key, void *&val)
XLockHelper(XrdSsiShMam *shmemp, LockType lktype)
Definition: XrdSsiShMam.hh:102
bool Export()
~XrdSsiShMam()
Definition: XrdSsiShMam.hh:70
Definition: XrdSsiShMam.hh:75
Definition: XrdSsiShMam.hh:37
bool useAtomic
Definition: XrdSsiShMam.hh:148
bool Attach(int tout, bool isrw=false)
void RetItem(MemItem *iP)
MemItem * NewItem()
LockType
Definition: XrdSsiShMam.hh:75
LockType lkType
Definition: XrdSsiShMam.hh:118
bool FLock()
Definition: XrdSsiShMam.hh:98
bool Create(XrdSsiShMat::CRZParms &parms)
bool AddItem(void *newdata, void *olddata, const char *key, int hash, bool replace=false)
bool ExportIt(bool fLocked)
int hash
Definition: XrdSsiShMam.hh:78
int HashVal(const char *key)
void SwapMap(XrdSsiShMam &newMap)
long long shmSize
Definition: XrdSsiShMam.hh:126
void SetLocking(bool isrw)
~XLockHelper()
Definition: XrdSsiShMam.hh:108
bool multW
Definition: XrdSsiShMam.hh:147