13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements Thread Library
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * ---------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Thread-safe singleton.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deSingleton.h"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deAtomic.h"
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deThread.h"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko PoyryDE_STATIC_ASSERT(sizeof(deSingletonState) == sizeof(deUint32));
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid deInitSingleton (volatile deSingletonState* singletonState, deSingletonConstructorFunc constructor, void* arg)
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	if (*singletonState != DE_SINGLETON_STATE_INITIALIZED)
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deSingletonState curState = (deSingletonState)deAtomicCompareExchange32((volatile deUint32*)singletonState, (deUint32)DE_SINGLETON_STATE_NOT_INITIALIZED, (deUint32)DE_SINGLETON_STATE_INITIALIZING);
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		if (curState == DE_SINGLETON_STATE_NOT_INITIALIZED)
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			constructor(arg);
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			deMemoryReadWriteFence();
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			*singletonState = DE_SINGLETON_STATE_INITIALIZED;
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			deMemoryReadWriteFence();
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		else if (curState == DE_SINGLETON_STATE_INITIALIZING)
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			for (;;)
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				deMemoryReadWriteFence();
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				if (*singletonState == DE_SINGLETON_STATE_INITIALIZED)
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					break;
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				else
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					deYield();
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_ASSERT(*singletonState == DE_SINGLETON_STATE_INITIALIZED);
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
62