Open Dynamics Engine

odetls.h

00001 /*************************************************************************
00002 *                                                                       *
00003 * Thread local storage access stub for Open Dynamics Engine,            *
00004 * Copyright (C) 2008 Oleh Derevenko. All rights reserved.               *
00005 * Email: odar@eleks.com (change all "a" to "e")                         *
00006 *                                                                       *
00007 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
00008 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
00009 *                                                                       *
00010 *                                                                       *
00011 * This library is free software; you can redistribute it and/or         *
00012 * modify it under the terms of EITHER:                                  *
00013 *   (1) The GNU Lesser General Public License as published by the Free  *
00014 *       Software Foundation; either version 2.1 of the License, or (at  *
00015 *       your option) any later version. The text of the GNU Lesser      *
00016 *       General Public License is included with this library in the     *
00017 *       file LICENSE.TXT.                                               *
00018 *   (2) The BSD-style license that is included with this library in     *
00019 *       the file LICENSE-BSD.TXT.                                       *
00020 *                                                                       *
00021 * This library is distributed in the hope that it will be useful,       *
00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
00024 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
00025 *                                                                       *
00026 *************************************************************************/
00027 
00028 /*
00029 
00030 ODE Thread Local Storage access stub interface.
00031 
00032 */
00033 
00034 
00035 #ifndef _ODE_ODETLS_H_
00036 #define _ODE_ODETLS_H_
00037 
00038 
00039 #include "odeou.h"
00040 
00041 
00042 #if dTLS_ENABLED
00043 
00044 
00045 struct TrimeshCollidersCache;
00046 
00047 
00048 enum EODETLSKIND
00049 {
00050    OTK__MIN,
00051 
00052    OTK_AUTOCLEANUP = OTK__MIN,
00053    OTK_MANUALCLEANUP,
00054 
00055    OTK__MAX,
00056 
00057    OTK__DEFAULT = OTK_AUTOCLEANUP,
00058 };
00059 
00060 enum EODETLSITEM
00061 {
00062    OTI_DATA_ALLOCATION_FLAGS,
00063    OTI_TRIMESH_TRIMESH_COLLIDER_CACHE,
00064 
00065    OTI__MAX,
00066 };
00067 
00068 
00069 class COdeTls
00070 {
00071 public:
00072    static bool Initialize(EODETLSKIND tkTLSKind);
00073    static void Finalize(EODETLSKIND tkTLSKind);
00074 
00075    static void CleanupForThread();
00076 
00077 public:
00078    static unsigned GetDataAllocationFlags(EODETLSKIND tkTLSKind)
00079    {
00080       // Must be a safe call as it is used to test if TLS slot is allocated at all
00081       return (unsigned)(size_t)CThreadLocalStorage::GetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS);
00082    }
00083 
00084    static void SignalDataAllocationFlags(EODETLSKIND tkTLSKind, unsigned uFlagsMask)
00085    {
00086       unsigned uCurrentFlags = (unsigned)(size_t)CThreadLocalStorage::UnsafeGetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS);
00087       CThreadLocalStorage::UnsafeSetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS, (tlsvaluetype)(size_t)(uCurrentFlags | uFlagsMask));
00088    }
00089 
00090    static void DropDataAllocationFlags(EODETLSKIND tkTLSKind, unsigned uFlagsMask)
00091    {
00092       unsigned uCurrentFlags = (unsigned)(size_t)CThreadLocalStorage::UnsafeGetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS);
00093       CThreadLocalStorage::UnsafeSetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS, (tlsvaluetype)(size_t)(uCurrentFlags & ~uFlagsMask));
00094    }
00095 
00096    static TrimeshCollidersCache *GetTrimeshCollidersCache(EODETLSKIND tkTLSKind)
00097    { 
00098       return (TrimeshCollidersCache *)CThreadLocalStorage::UnsafeGetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_TRIMESH_TRIMESH_COLLIDER_CACHE);
00099    }
00100 
00101 public:
00102    static bool AssignDataAllocationFlags(EODETLSKIND tkTLSKind, unsigned uInitializationFlags);
00103 
00104    static bool AssignTrimeshCollidersCache(EODETLSKIND tkTLSKind, TrimeshCollidersCache *pccInstance);
00105    static void DestroyTrimeshCollidersCache(EODETLSKIND tkTLSKind);
00106 
00107 private:
00108    static void FreeTrimeshCollidersCache(TrimeshCollidersCache *pccCacheInstance);
00109 
00110 private:
00111    static void _OU_CONVENTION_CALLBACK FreeTrimeshCollidersCache_Callback(tlsvaluetype vValueData);
00112 
00113 private:
00114    static HTLSKEY          m_ahtkStorageKeys[OTK__MAX];
00115 };
00116 
00117 
00118 #endif // dTLS_ENABLED
00119 
00120 
00121 #endif // _ODE_ODETLS_H_