aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/include/ScopedPhysXLock.h
blob: bad5b7896e72dea1ebc75726dc9647591c18cb2a (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
/*
 * 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.
 */

#ifndef SCOPED_PHYS_XLOCK_H

#define SCOPED_PHYS_XLOCK_H

#include "foundation/PxPreprocessor.h"
#include "ApexDefs.h"


#include "PxScene.h"
#include "Scene.h"

namespace nvidia
{
	namespace apex
	{

		/**
		\brief This helper class creates a scoped read access to the PhysX SDK API

		This helper class is used to create a scoped read lock/unlock pair around a section of code
		which is trying to do read access against the PhysX SDK.
		*/

		class ScopedPhysXLockRead
		{
		public:

			/**
			\brief Constructor for ScopedPhysXLockRead
			\param[in] scene the APEX scene
			\param[in] fileName used to determine what file called the lock for debugging purposes
			\param[in] lineno used to determine what line number called the lock for debugging purposes
			*/
			ScopedPhysXLockRead(nvidia::Scene* scene, const char *fileName, int lineno) : mApexScene(scene), mPhysXScene(0)
			{
				if (mApexScene)
				{
					mApexScene->lockRead(fileName, (uint32_t)lineno);
				}
			}
			
			/**
			\brief Constructor for ScopedPhysXLockRead
			\param[in] scene the PhysX scene
			\param[in] fileName used to determine what file called the lock for debugging purposes
			\param[in] lineno used to determine what line number called the lock for debugging purposes
			*/
			ScopedPhysXLockRead(physx::PxScene* scene, const char *fileName, int lineno) : mPhysXScene(scene), mApexScene(0)
			{
				if (mPhysXScene)
				{
					mPhysXScene->lockRead(fileName, (uint32_t)lineno);
				}
			}
			
			~ScopedPhysXLockRead()
			{
				if (mApexScene)
				{
					mApexScene->unlockRead();
				}
				if (mPhysXScene)
				{
					mPhysXScene->unlockRead();
				}
			}
		private:
			nvidia::Scene* mApexScene;
			physx::PxScene* mPhysXScene;
		};

		/**
		\brief This helper class creates a scoped write access to the PhysX SDK API

		This helper class is used to create a scoped write lock/unlock pair around a section of code
		which is trying to do read access against the PhysX SDK.
		*/
		class ScopedPhysXLockWrite
		{
		public:
			/**
			\brief Constructor for ScopedPhysXLockWrite
			\param[in] scene the APEX scene
			\param[in] fileName used to determine what file called the lock for debugging purposes
			\param[in] lineno used to determine what line number called the lock for debugging purposes
			*/
			ScopedPhysXLockWrite(nvidia::Scene *scene, const char *fileName, int lineno) : mApexScene(scene), mPhysXScene(0)
			{
				if (mApexScene)
				{
					mApexScene->lockWrite(fileName, (uint32_t)lineno);
				}
			}
			
			/**
			\brief Constructor for ScopedPhysXLockWrite
			\param[in] scene the PhysX scene
			\param[in] fileName used to determine what file called the lock for debugging purposes
			\param[in] lineno used to determine what line number called the lock for debugging purposes
			*/
			ScopedPhysXLockWrite(physx::PxScene *scene, const char *fileName, int lineno) : mPhysXScene(scene), mApexScene(0)
			{
				if (mPhysXScene)
				{
					mPhysXScene->lockWrite(fileName, (uint32_t)lineno);
				}
			}
			
			~ScopedPhysXLockWrite()
			{
				if (mApexScene)
				{
					mApexScene->unlockWrite();
				}
				if (mPhysXScene)
				{
					mPhysXScene->unlockWrite();
				}
			}
		private:
			nvidia::Scene* mApexScene;
			physx::PxScene* mPhysXScene;
		};
	}
}


#if defined(_DEBUG) || PX_CHECKED
/**
\brief This macro creates a scoped write lock/unlock pair
*/
#define SCOPED_PHYSX_LOCK_WRITE(x) nvidia::apex::ScopedPhysXLockWrite _wlock(x,__FILE__,__LINE__);
#else
/**
\brief This macro creates a scoped write lock/unlock pair
*/
#define SCOPED_PHYSX_LOCK_WRITE(x) nvidia::apex::ScopedPhysXLockWrite _wlock(x,"",0);
#endif

#if defined(_DEBUG) || PX_CHECKED
/**
\brief This macro creates a scoped read lock/unlock pair
*/
#define SCOPED_PHYSX_LOCK_READ(x) nvidia::apex::ScopedPhysXLockRead _rlock(x,__FILE__,__LINE__);
#else
/**
\brief This macro creates a scoped read lock/unlock pair
*/
#define SCOPED_PHYSX_LOCK_READ(x) nvidia::apex::ScopedPhysXLockRead _rlock(x,"",0);
#endif



#endif