Open Dynamics Engine
odetls.h
1 /*************************************************************************
2  * *
3  * Thread local storage access stub for Open Dynamics Engine, *
4  * Copyright (C) 2008 Oleh Derevenko. All rights reserved. *
5  * Email: odar@eleks.com (change all "a" to "e") *
6  * *
7  * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
8  * All rights reserved. Email: russ@q12.org Web: www.q12.org *
9  * *
10  * *
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of EITHER: *
13  * (1) The GNU Lesser General Public License as published by the Free *
14  * Software Foundation; either version 2.1 of the License, or (at *
15  * your option) any later version. The text of the GNU Lesser *
16  * General Public License is included with this library in the *
17  * file LICENSE.TXT. *
18  * (2) The BSD-style license that is included with this library in *
19  * the file LICENSE-BSD.TXT. *
20  * *
21  * This library is distributed in the hope that it will be useful, *
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
24  * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
25  * *
26  *************************************************************************/
27 
28 /*
29 
30 ODE Thread Local Storage access stub interface.
31 
32 */
33 
34 
35 #ifndef _ODE_ODETLS_H_
36 #define _ODE_ODETLS_H_
37 
38 
39 #include "odeou.h"
40 
41 
42 #if dTLS_ENABLED
43 
44 
45 using _OU_NAMESPACE::tlsvaluetype;
46 using _OU_NAMESPACE::HTLSKEY;
47 using _OU_NAMESPACE::CThreadLocalStorage;
48 
49 
51 
52 
53 enum EODETLSKIND
54 {
55  OTK__MIN,
56 
57  OTK_AUTOCLEANUP = OTK__MIN,
58  OTK_MANUALCLEANUP,
59 
60  OTK__MAX,
61 
62  OTK__DEFAULT = OTK_AUTOCLEANUP,
63 };
64 
65 enum EODETLSITEM
66 {
67  OTI_DATA_ALLOCATION_FLAGS,
68  OTI_TRIMESH_TRIMESH_COLLIDER_CACHE,
69 
70  OTI__MAX,
71 };
72 
73 
74 class COdeTls
75 {
76 public:
77  static bool Initialize(EODETLSKIND tkTLSKind);
78  static void Finalize(EODETLSKIND tkTLSKind);
79 
80  static void CleanupForThread();
81 
82 public:
83  static unsigned GetDataAllocationFlags(EODETLSKIND tkTLSKind)
84  {
85  // Must be a safe call as it is used to test if TLS slot is allocated at all
86  return (unsigned)(size_t)CThreadLocalStorage::GetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS);
87  }
88 
89  static void SignalDataAllocationFlags(EODETLSKIND tkTLSKind, unsigned uFlagsMask)
90  {
91  unsigned uCurrentFlags = (unsigned)(size_t)CThreadLocalStorage::UnsafeGetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS);
92  CThreadLocalStorage::UnsafeSetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS, (tlsvaluetype)(size_t)(uCurrentFlags | uFlagsMask));
93  }
94 
95  static void DropDataAllocationFlags(EODETLSKIND tkTLSKind, unsigned uFlagsMask)
96  {
97  unsigned uCurrentFlags = (unsigned)(size_t)CThreadLocalStorage::UnsafeGetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS);
98  CThreadLocalStorage::UnsafeSetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS, (tlsvaluetype)(size_t)(uCurrentFlags & ~uFlagsMask));
99  }
100 
101  static TrimeshCollidersCache *GetTrimeshCollidersCache(EODETLSKIND tkTLSKind)
102  {
103  return (TrimeshCollidersCache *)CThreadLocalStorage::UnsafeGetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_TRIMESH_TRIMESH_COLLIDER_CACHE);
104  }
105 
106 public:
107  static bool AssignDataAllocationFlags(EODETLSKIND tkTLSKind, unsigned uInitializationFlags);
108 
109  static bool AssignTrimeshCollidersCache(EODETLSKIND tkTLSKind, TrimeshCollidersCache *pccInstance);
110  static void DestroyTrimeshCollidersCache(EODETLSKIND tkTLSKind);
111 
112 private:
113  static void FreeTrimeshCollidersCache(TrimeshCollidersCache *pccCacheInstance);
114 
115 private:
116  static void _OU_CONVENTION_CALLBACK FreeTrimeshCollidersCache_Callback(tlsvaluetype vValueData);
117 
118 private:
119  static HTLSKEY m_ahtkStorageKeys[OTK__MAX];
120 };
121 
122 
123 #endif // dTLS_ENABLED
124 
125 
126 #endif // _ODE_ODETLS_H_
Definition: collision_trimesh_internal.h:136