// tss header for threads library
#ifndef _DINKUM_THREADS_TSS
#define _DINKUM_THREADS_TSS
#include <Dinkum/threads/threads.h>
#include <Dinkum/threads/exceptions>


namespace Dinkum
	{	// Dinkum Libraries
	namespace threads
		{	// Dinkum C++ Threads Library

template<class _Val_type>
	class thread_specific_ptr
	{	// thread-specific pointer class
public:
	typedef thread_specific_ptr<_Val_type> _My_type;
	typedef _Val_type _My_val;

	thread_specific_ptr()
		{	// construct
		_Validate(_Tss_create(&_Key, _Dtor));
		}

	~thread_specific_ptr()
		{	// destroy
		_Tss_delete(_Key);
		}

	_My_val *get() const
		{	// return stored pointer
		return (_My_val*)_Tss_get(_Key);
		}

	_My_val& operator*() const
		{	// return reference to pointed-to object
		_THREAD_ASSERT(_Tss_get(_Key) != 0,
			"threads::thread::operator* called with null object pointer");
		return *(_My_val*)_Tss_get(_Key);
		}

	_My_val *operator->() const
		{	// return stored pointer
		return (&**this);
		}

	_My_val *release()
		{	// set stored pointer to null
		_My_val *_Ptr = (_My_val*)_Tss_get(_Key);
		_Validate(_Tss_set(_Key, 0));
		return _Ptr;
		}

	void reset(_My_val *_Arg = 0)
		{	// delete pointed-to object
		_My_val *_Ptr = (_My_val*)_Tss_get(_Key);
		if (_Ptr != _Arg)
			delete _Ptr;
		_Validate(_Tss_set(_Key, _Arg));
		}

private:
	_Tss_t _Key;

	static void _Dtor(void *_Ptr)
		{	// delete an object
		delete (_My_val*)_Ptr;
		}

	thread_specific_ptr(const _My_type&);
	_My_type& operator=(const _My_type&);
	};
		}	// namespace threads
	}	// namespace Dinkum

#endif /* _DINKUM_THREADS_TSS */

/*
 * This file is derived from software bearing the following
 * restrictions:
 *
 * (c) Copyright William E. Kempf 2001
 *
 * Permission to use, copy, modify, distribute and sell this
 * software and its documentation for any purpose is hereby
 * granted without fee, provided that the above copyright
 * notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting
 * documentation. William E. Kempf makes no representations
 * about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/*
 * Copyright (c) 1992-2010 by P.J. Plauger.  ALL RIGHTS RESERVED.
 * Consult your license regarding permissions and restrictions.
V5.30:0009 */
