pvAccessCPP  7.1.6
referenceCountingLock.h
1 /**
2  * Copyright - See the COPYRIGHT that is included with this distribution.
3  * pvAccessCPP is distributed subject to a Software License Agreement found
4  * in file LICENSE that is included with this distribution.
5  */
6 
7 #ifndef REFERENCECOUNTINGLOCK_H
8 #define REFERENCECOUNTINGLOCK_H
9 
10 #ifdef epicsExportSharedSymbols
11 # define referenceCountingLockEpicsExportSharedSymbols
12 # undef epicsExportSharedSymbols
13 #endif
14 
15 #include <pv/lock.h>
16 #include <pv/pvType.h>
17 #include <pv/sharedPtr.h>
18 
19 #ifdef referenceCountingLockEpicsExportSharedSymbols
20 # define epicsExportSharedSymbols
21 # undef referenceCountingLockEpicsExportSharedSymbols
22 #endif
23 
24 namespace epics {
25 namespace pvAccess {
26 
27 /**
28  * Reference counting mutex implementation w/ deadlock detection.
29  * Synchronization helper class used (intended for use) for activation/deactivation synchronization.
30  * This class enforces <code>attempt</code> method of acquiring the locks to prevent deadlocks.
31  * Class also offers reference counting.
32  * (NOTE: automatic lock counting was not implemented due to imperfect usage.)
33  *
34  */
35 class ReferenceCountingLock
36 {
37 public:
38  POINTER_DEFINITIONS(ReferenceCountingLock);
39 
40  /**
41  * Constructor of <code>ReferenceCountingLock</code>.
42  * After construction lock is free and reference count equals <code>1</code>.
43  */
45  /**
46  * Destructor of <code>ReferenceCountingLock</code>.
47  */
48  virtual ~ReferenceCountingLock();
49  /**
50  * Attempt to acquire lock.
51  *
52  * NOTE: Argument msecs is currently not supported due to
53  * Darwin OS not supporting pthread_mutex_timedlock. May be changed in the future.
54  *
55  * @param msecs the number of milleseconds to wait.
56  * An argument less than or equal to zero means not to wait at all.
57  *
58  * @return <code>true</code> if acquired, <code>false</code> otherwise.
59  * NOTE: currently this routine always returns true. Look above for explanation.
60  *
61  */
62  bool acquire(epics::pvData::int64 msecs);
63  /**
64  * Release previously acquired lock.
65  */
66  void release();
67  /**
68  * Increment number of references.
69  *
70  * @return number of references.
71  */
72  int increment();
73  /**
74  * Decrement number of references.
75  *
76  * @return number of references.
77  */
78  int decrement();
79 private:
80  int _references;
81  epics::pvData::Mutex _mutex;
82  epics::pvData::Mutex _countMutex;
83 };
84 
85 }
86 }
87 
88 #endif /* REFERENCECOUNTINGLOCK_H */
void release()
Release previously acquired lock.
virtual ~ReferenceCountingLock()
Destructor of ReferenceCountingLock.
ReferenceCountingLock()
Constructor of ReferenceCountingLock.
bool acquire(epics::pvData::int64 msecs)
Attempt to acquire lock.
int decrement()
Decrement number of references.
int increment()
Increment number of references.