Skip to content

Commit 27a8b2d

Browse files
authored
Merge pull request #203 from P-N-L/windows-static-fix
Fixed static instantiation issue on (at least) Windows.
2 parents 70f44d5 + 7d58717 commit 27a8b2d

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

src/library/lifetime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ clfftStatus clfftSetup( const clfftSetupData* sData )
2929
{
3030
// Static data is not thread safe (to create), so we implement a lock to protect instantiation for the first call
3131
// Implemented outside of FFTRepo::getInstance to minimize lock overhead; this is only necessary on first creation
32-
scopedLock sLock( FFTRepo::lockRepo, _T( "FFTRepo::getInstance" ) );
32+
scopedLock sLock( FFTRepo::lockRepo(), _T( "FFTRepo::getInstance" ) );
3333

3434
// First invocation of this function will allocate the FFTRepo singleton; thereafter the object always exists
3535
FFTRepo& fftRepo = FFTRepo::getInstance( );

src/library/repo.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
using std::map;
2525
using std::string;
2626

27-
// Static initialization of the repo lock variable
28-
lockRAII FFTRepo::lockRepo( _T( "FFTRepo" ) );
29-
3027
// Static initialization of the plan count variable
3128
size_t FFTRepo::planCount = 1;
3229

@@ -39,7 +36,7 @@ GpuStatTimer* FFTRepo::pStatTimer = NULL;
3936

4037
clfftStatus FFTRepo::releaseResources( )
4138
{
42-
scopedLock sLock( lockRepo, _T( "releaseResources" ) );
39+
scopedLock sLock( lockRepo(), _T( "releaseResources" ) );
4340

4441
// Release all handles to Kernels
4542
//
@@ -110,7 +107,7 @@ clfftStatus FFTRepo::releaseResources( )
110107

111108
clfftStatus FFTRepo::setProgramCode( const clfftGenerators gen, const FFTKernelSignatureHeader * data, const std::string& kernel, const cl_device_id &device, const cl_context& planContext )
112109
{
113-
scopedLock sLock( lockRepo, _T( "setProgramCode" ) );
110+
scopedLock sLock( lockRepo(), _T( "setProgramCode" ) );
114111

115112
FFTRepoKey key(gen, data, planContext, device);
116113

@@ -145,7 +142,7 @@ clfftStatus FFTRepo::setProgramCode( const clfftGenerators gen, const FFTKernelS
145142

146143
clfftStatus FFTRepo::getProgramCode( const clfftGenerators gen, const FFTKernelSignatureHeader * data, std::string& kernel, const cl_device_id &device, const cl_context& planContext )
147144
{
148-
scopedLock sLock( lockRepo, _T( "getProgramCode" ) );
145+
scopedLock sLock( lockRepo(), _T( "getProgramCode" ) );
149146

150147
FFTRepoKey key(gen, data, planContext, device);
151148

@@ -160,7 +157,7 @@ clfftStatus FFTRepo::getProgramCode( const clfftGenerators gen, const FFTKernelS
160157
clfftStatus FFTRepo::setProgramEntryPoints( const clfftGenerators gen, const FFTKernelSignatureHeader * data,
161158
const char * kernel_fwd, const char * kernel_back, const cl_device_id &device, const cl_context& planContext )
162159
{
163-
scopedLock sLock( lockRepo, _T( "setProgramEntryPoints" ) );
160+
scopedLock sLock( lockRepo(), _T( "setProgramEntryPoints" ) );
164161

165162
FFTRepoKey key(gen, data, planContext, device);
166163

@@ -174,7 +171,7 @@ clfftStatus FFTRepo::setProgramEntryPoints( const clfftGenerators gen, const FFT
174171
clfftStatus FFTRepo::getProgramEntryPoint( const clfftGenerators gen, const FFTKernelSignatureHeader * data,
175172
clfftDirection dir, std::string& kernel, const cl_device_id &device, const cl_context& planContext )
176173
{
177-
scopedLock sLock( lockRepo, _T( "getProgramEntryPoint" ) );
174+
scopedLock sLock( lockRepo(), _T( "getProgramEntryPoint" ) );
178175

179176
FFTRepoKey key(gen, data, planContext, device);
180177

@@ -202,7 +199,7 @@ clfftStatus FFTRepo::getProgramEntryPoint( const clfftGenerators gen, const FFTK
202199

203200
clfftStatus FFTRepo::setclProgram( const clfftGenerators gen, const FFTKernelSignatureHeader * data, const cl_program& prog, const cl_device_id &device, const cl_context& planContext )
204201
{
205-
scopedLock sLock( lockRepo, _T( "setclProgram" ) );
202+
scopedLock sLock( lockRepo(), _T( "setclProgram" ) );
206203

207204
FFTRepoKey key(gen, data, planContext, device);
208205

@@ -225,7 +222,7 @@ clfftStatus FFTRepo::setclProgram( const clfftGenerators gen, const FFTKernelSig
225222

226223
clfftStatus FFTRepo::getclProgram( const clfftGenerators gen, const FFTKernelSignatureHeader * data, cl_program& prog, const cl_device_id &device, const cl_context& planContext )
227224
{
228-
scopedLock sLock( lockRepo, _T( "getclProgram" ) );
225+
scopedLock sLock( lockRepo(), _T( "getclProgram" ) );
229226

230227
FFTRepoKey key(gen, data, planContext, device);
231228

@@ -246,7 +243,7 @@ clfftStatus FFTRepo::getclProgram( const clfftGenerators gen, const FFTKernelSig
246243

247244
clfftStatus FFTRepo::setclKernel( cl_program prog, clfftDirection dir, const cl_kernel& kernel )
248245
{
249-
scopedLock sLock( lockRepo, _T( "setclKernel" ) );
246+
scopedLock sLock( lockRepo(), _T( "setclKernel" ) );
250247

251248
fftKernels & Kernels = mapKernels[ prog ];
252249

@@ -283,7 +280,7 @@ clfftStatus FFTRepo::setclKernel( cl_program prog, clfftDirection dir, const cl_
283280

284281
clfftStatus FFTRepo::getclKernel( cl_program prog, clfftDirection dir, cl_kernel& kernel, lockRAII*& kernelLock)
285282
{
286-
scopedLock sLock( lockRepo, _T( "getclKernel" ) );
283+
scopedLock sLock( lockRepo(), _T( "getclKernel" ) );
287284

288285
Kernel_iterator pos = mapKernels.find( prog );
289286
if (pos == mapKernels.end( ) )
@@ -311,7 +308,7 @@ clfftStatus FFTRepo::getclKernel( cl_program prog, clfftDirection dir, cl_kernel
311308

312309
clfftStatus FFTRepo::createPlan( clfftPlanHandle* plHandle, FFTPlan*& fftPlan )
313310
{
314-
scopedLock sLock( lockRepo, _T( "insertPlan" ) );
311+
scopedLock sLock( lockRepo(), _T( "insertPlan" ) );
315312

316313
// We keep track of this memory in our own collection class, to make sure it's freed in releaseResources
317314
// The lifetime of a plan is tracked by the client and is freed when the client calls ::clfftDestroyPlan()
@@ -332,7 +329,7 @@ clfftStatus FFTRepo::createPlan( clfftPlanHandle* plHandle, FFTPlan*& fftPlan )
332329

333330
clfftStatus FFTRepo::getPlan( clfftPlanHandle plHandle, FFTPlan*& fftPlan, lockRAII*& planLock )
334331
{
335-
scopedLock sLock( lockRepo, _T( "getPlan" ) );
332+
scopedLock sLock( lockRepo(), _T( "getPlan" ) );
336333

337334
// First, check if we have already created a plan with this exact same FFTPlan
338335
repoPlansType::iterator iter = repoPlans.find( plHandle );
@@ -348,7 +345,7 @@ clfftStatus FFTRepo::getPlan( clfftPlanHandle plHandle, FFTPlan*& fftPlan, lockR
348345

349346
clfftStatus FFTRepo::deletePlan( clfftPlanHandle* plHandle )
350347
{
351-
scopedLock sLock( lockRepo, _T( "deletePlan" ) );
348+
scopedLock sLock( lockRepo(), _T( "deletePlan" ) );
352349

353350
// First, check if we have already created a plan with this exact same FFTPlan
354351
repoPlansType::iterator iter = repoPlans.find( *plHandle );

src/library/repo.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,14 @@ class FFTRepo
183183
// Used to make the FFTRepo struct thread safe; STL is not thread safe by default
184184
// Optimally, we could use a lock object per STL struct, as two different STL structures
185185
// can be modified at the same time, but a single lock object is easier and performance should
186-
// still be good
187-
static lockRAII lockRepo;
186+
// still be good. This is implemented as a function returning a static local reference to
187+
// assert that the lock must be instantiated before the result can be used.
188+
static lockRAII& lockRepo()
189+
{
190+
// Static initialization of the repo lock variable
191+
static lockRAII lock(_T("FFTRepo"));
192+
return lock;
193+
}
188194

189195
// Our runtime library can instrument kernel timings with a GPU timer available in a shared module
190196
// Handle/Address of the dynamic module that contains timers

0 commit comments

Comments
 (0)