aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/common/src/ApexRWLockable.cpp
blob: 40a824874fe3d7c0bdcfe6ed6a3c5ae8c4f04c52 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (c) 2008-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, related documentation
 * and any modifications thereto.  Any use, reproduction, disclosure or
 * distribution of this software and related documentation without an express
 * license agreement from NVIDIA CORPORATION is strictly prohibited.
 */

// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.  

#include "ApexRWLockable.h"
#include "PsThread.h"
#include "PsAtomic.h"
#include "ApexSDKIntl.h"

namespace nvidia
{
namespace apex
{

ApexRWLockable::ApexRWLockable()
	: 
	mEnabled (true)
	, mCurrentWriter(0)
	, mRWLock()
	, mConcurrentWriteCount	(0)
	, mConcurrentReadCount	(0)
	, mConcurrentErrorCount	(0)
{
}

ApexRWLockable::~ApexRWLockable()
{
}

void ApexRWLockable::setEnabled(bool e)
{
	mEnabled = e;
}

bool ApexRWLockable::isEnabled() const
{
	return mEnabled;
}

void ApexRWLockable::acquireReadLock(const char*, uint32_t) const
{
	mRWLock.lockReader();
}

void ApexRWLockable::acquireWriteLock(const char*, uint32_t) const
{
	mRWLock.lockWriter();
	mCurrentWriter = Thread::getId();
}

void ApexRWLockable::releaseReadLock(void) const
{
	mRWLock.unlockReader();
}

void ApexRWLockable::releaseWriteLock(void) const
{
	mCurrentWriter = 0;
	mRWLock.unlockWriter();
}

bool ApexRWLockable::startWrite(bool allowReentry) 
{
	PX_COMPILE_TIME_ASSERT(sizeof(ThreadReadWriteCount) == 4);
	mDataLock.lock();
	bool error = false;

	ThreadReadWriteCount& rwc = mData[Thread::getId()];
	// check that we are the only thread reading (this allows read->write order on a single thread)
	error |= mConcurrentReadCount != rwc.counters.readDepth;

	// check no other threads are writing 
	error |= mConcurrentWriteCount != rwc.counters.writeDepth;

	// increment shared write counter
	shdfnd::atomicIncrement(&mConcurrentWriteCount);

	// in the normal case (re-entry is allowed) then we simply increment
	// the writeDepth by 1, otherwise (re-entry is not allowed) increment
	// by 2 to force subsequent writes to fail by creating a mismatch between
	// the concurrent write counter and the local counter, any value > 1 will do
	if (allowReentry)
		rwc.counters.writeDepth++;
	else
		rwc.counters.writeDepth += 2;
	mDataLock.unlock();

	if (error)
		shdfnd::atomicIncrement(&mConcurrentErrorCount);

	return !error;
}

void ApexRWLockable::stopWrite(bool allowReentry)
{
	shdfnd::atomicDecrement(&mConcurrentWriteCount);

	// decrement depth of writes for this thread
	mDataLock.lock();
	nvidia::ThreadImpl::Id id = nvidia::Thread::getId();

	// see comment in startWrite()
	if (allowReentry)
		mData[id].counters.writeDepth--;
	else
		mData[id].counters.writeDepth -= 2;

	mDataLock.unlock();
}

nvidia::Thread::Id ApexRWLockable::getCurrentWriter() const
{
	return mCurrentWriter;
}

bool ApexRWLockable::startRead() const
{
	shdfnd::atomicIncrement(&mConcurrentReadCount);

	mDataLock.lock();
	nvidia::ThreadImpl::Id id = nvidia::Thread::getId();
	ThreadReadWriteCount& rwc = mData[id];
	rwc.counters.readDepth++;
	bool success = (rwc.counters.writeDepth > 0 || mConcurrentWriteCount == 0); 
	mDataLock.unlock();

	if (!success)
		shdfnd::atomicIncrement(&mConcurrentErrorCount);

	return success;
}

void ApexRWLockable::stopRead() const
{
	shdfnd::atomicDecrement(&mConcurrentReadCount);
	mDataLock.lock();
	nvidia::ThreadImpl::Id id = nvidia::Thread::getId();
	mData[id].counters.readDepth--;
	mDataLock.unlock();
}

uint32_t ApexRWLockable::getReadWriteErrorCount() const
{
	return static_cast<uint32_t>(mConcurrentErrorCount);
}

ApexRWLockableScopedDisable::ApexRWLockableScopedDisable(RWLockable* rw) : mLockable(static_cast<ApexRWLockable*>(rw))
{
	mLockable->setEnabled(false);
}

ApexRWLockableScopedDisable::~ApexRWLockableScopedDisable()
{
	mLockable->setEnabled(true);
}

ApexRWLockableScopedDisable::ApexRWLockableScopedDisable(const ApexRWLockableScopedDisable& o) : mLockable(o.mLockable)
{
}

ApexRWLockableScopedDisable& ApexRWLockableScopedDisable::operator=(const ApexRWLockableScopedDisable&)
{
	return *this;
}

}
}