1*35238bceSAndroid Build Coastguard Worker /*-------------------------------------------------------------------------
2*35238bceSAndroid Build Coastguard Worker * drawElements Thread Library
3*35238bceSAndroid Build Coastguard Worker * ---------------------------
4*35238bceSAndroid Build Coastguard Worker *
5*35238bceSAndroid Build Coastguard Worker * Copyright 2014 The Android Open Source Project
6*35238bceSAndroid Build Coastguard Worker *
7*35238bceSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
8*35238bceSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
9*35238bceSAndroid Build Coastguard Worker * You may obtain a copy of the License at
10*35238bceSAndroid Build Coastguard Worker *
11*35238bceSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
12*35238bceSAndroid Build Coastguard Worker *
13*35238bceSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
14*35238bceSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
15*35238bceSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16*35238bceSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
17*35238bceSAndroid Build Coastguard Worker * limitations under the License.
18*35238bceSAndroid Build Coastguard Worker *
19*35238bceSAndroid Build Coastguard Worker *//*!
20*35238bceSAndroid Build Coastguard Worker * \file
21*35238bceSAndroid Build Coastguard Worker * \brief Thread-safe singleton.
22*35238bceSAndroid Build Coastguard Worker *//*--------------------------------------------------------------------*/
23*35238bceSAndroid Build Coastguard Worker
24*35238bceSAndroid Build Coastguard Worker #include "deSingleton.h"
25*35238bceSAndroid Build Coastguard Worker #include "deAtomic.h"
26*35238bceSAndroid Build Coastguard Worker #include "deThread.h"
27*35238bceSAndroid Build Coastguard Worker
28*35238bceSAndroid Build Coastguard Worker DE_STATIC_ASSERT(sizeof(deSingletonState) == sizeof(uint32_t));
29*35238bceSAndroid Build Coastguard Worker
deInitSingleton(volatile deSingletonState * singletonState,deSingletonConstructorFunc constructor,void * arg)30*35238bceSAndroid Build Coastguard Worker void deInitSingleton(volatile deSingletonState *singletonState, deSingletonConstructorFunc constructor, void *arg)
31*35238bceSAndroid Build Coastguard Worker {
32*35238bceSAndroid Build Coastguard Worker if (*singletonState != DE_SINGLETON_STATE_INITIALIZED)
33*35238bceSAndroid Build Coastguard Worker {
34*35238bceSAndroid Build Coastguard Worker deSingletonState curState = (deSingletonState)deAtomicCompareExchange32(
35*35238bceSAndroid Build Coastguard Worker (volatile uint32_t *)singletonState, (uint32_t)DE_SINGLETON_STATE_NOT_INITIALIZED,
36*35238bceSAndroid Build Coastguard Worker (uint32_t)DE_SINGLETON_STATE_INITIALIZING);
37*35238bceSAndroid Build Coastguard Worker
38*35238bceSAndroid Build Coastguard Worker if (curState == DE_SINGLETON_STATE_NOT_INITIALIZED)
39*35238bceSAndroid Build Coastguard Worker {
40*35238bceSAndroid Build Coastguard Worker constructor(arg);
41*35238bceSAndroid Build Coastguard Worker
42*35238bceSAndroid Build Coastguard Worker deMemoryReadWriteFence();
43*35238bceSAndroid Build Coastguard Worker
44*35238bceSAndroid Build Coastguard Worker *singletonState = DE_SINGLETON_STATE_INITIALIZED;
45*35238bceSAndroid Build Coastguard Worker
46*35238bceSAndroid Build Coastguard Worker deMemoryReadWriteFence();
47*35238bceSAndroid Build Coastguard Worker }
48*35238bceSAndroid Build Coastguard Worker else if (curState == DE_SINGLETON_STATE_INITIALIZING)
49*35238bceSAndroid Build Coastguard Worker {
50*35238bceSAndroid Build Coastguard Worker for (;;)
51*35238bceSAndroid Build Coastguard Worker {
52*35238bceSAndroid Build Coastguard Worker deMemoryReadWriteFence();
53*35238bceSAndroid Build Coastguard Worker
54*35238bceSAndroid Build Coastguard Worker if (*singletonState == DE_SINGLETON_STATE_INITIALIZED)
55*35238bceSAndroid Build Coastguard Worker break;
56*35238bceSAndroid Build Coastguard Worker else
57*35238bceSAndroid Build Coastguard Worker deYield();
58*35238bceSAndroid Build Coastguard Worker }
59*35238bceSAndroid Build Coastguard Worker }
60*35238bceSAndroid Build Coastguard Worker
61*35238bceSAndroid Build Coastguard Worker DE_ASSERT(*singletonState == DE_SINGLETON_STATE_INITIALIZED);
62*35238bceSAndroid Build Coastguard Worker }
63*35238bceSAndroid Build Coastguard Worker }
64