XRootD
Loading...
Searching...
No Matches
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
35#include "XrdSsi/XrdSsiShMat.hh"
36
38{
39public:
40
41bool AddItem(void *newdata, void *olddata, const char *key,
42 int hash, bool replace=false);
43
44bool Attach(int tout, bool isrw=false);
45
46bool Create(XrdSsiShMat::CRZParms &parms);
47
48bool Export();
49
50bool DelItem(void *data, const char *key, int hash);
51
52void Detach();
53
54bool Enumerate(void *&jar, char *&key, void *&val);
55
56bool Enumerate(void *&jar);
57
58bool GetItem(void *data, const char *key, int hash);
59
60int Info(const char *vname, char *buff=0, int blen=0);
61
62bool Resize(XrdSsiShMat::CRZParms &parms);
63
64bool Sync();
65bool Sync(bool dosync, bool syncdo);
66bool Sync(int syncqsz);
67
69
71 pthread_mutex_destroy(&lkMutex);
72 pthread_rwlock_destroy(&myMutex);
73 }
74
75enum LockType {ROLock= 0, RWLock = 1};
76
77private:
78struct MemItem {int hash; Atomic(int) next;};
79
80bool ExportIt(bool fLocked);
81int Find(MemItem *&theItem, MemItem *&prvItem, const char *key, int &hash);
82bool Flush();
83int HashVal(const char *key);
84bool Lock(bool doRW=false, bool nowait=false);
85MemItem *NewItem();
86bool ReMap(LockType iHave);
87void RetItem(MemItem *iP);
88void SetLocking(bool isrw);
89void SwapMap(XrdSsiShMam &newMap);
90void Snooze(int sec);
91void UnLock(bool isrw);
92void Updated(int mOff);
93void Updated(int mOff, int mLen);
94
95class XLockHelper
96{
97public:
98inline 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 }
116private:
117XrdSsiShMam *shmemP;
118LockType lkType;
119bool doUnLock;
120};
121
122pthread_mutex_t lkMutex;
123pthread_rwlock_t myMutex;
124
125char *shmTemp;
126long long shmSize;
127char *shmBase;
128Atomic(int)*shmIndex;
129int shmSlots;
130int shmItemSz;
131int shmInfoSz;
132int verNum;
133int keyPos;
134int maxKLen;
135int shmFD;
136int timeOut;
137int lkCount;
138int syncOpt;
139int syncQWR;
140int syncLast;
141int syncQSZ;
142int accMode;
143bool isRW;
144bool lockRO;
145bool lockRW;
146bool reUse;
147bool multW;
148bool useAtomic;
149bool syncBase;
150bool syncOn;
151};
152#endif
bool Create
#define Atomic(type)
XrdSsiShMam(XrdSsiShMat::NewParms &parms)
bool Resize(XrdSsiShMat::CRZParms &parms)
void Detach()
Detach the map from the shared memory.
bool AddItem(void *newdata, void *olddata, const char *key, int hash, bool replace=false)
bool DelItem(void *data, const char *key, int hash)
int Info(const char *vname, char *buff=0, int blen=0)
bool GetItem(void *data, const char *key, int hash)
bool Attach(int tout, bool isrw=false)
bool Enumerate(void *&jar, char *&key, void *&val)