summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/ode/src/odetls.h
blob: db3306b202bd8a6fe06ffef2126cf9fab04f85cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*************************************************************************
 *                                                                       *
 * Thread local storage access stub for Open Dynamics Engine,            *
 * Copyright (C) 2008-2019 Oleh Derevenko. All rights reserved.          *
 * Email: odar@eleks.com (change all "a" to "e")                         *
 *                                                                       *
 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith.       *
 * All rights reserved.  Email: russ@q12.org   Web: www.q12.org          *
 *                                                                       *
 *                                                                       *
 * This library is free software; you can redistribute it and/or         *
 * modify it under the terms of EITHER:                                  *
 *   (1) The GNU Lesser General Public License as published by the Free  *
 *       Software Foundation; either version 2.1 of the License, or (at  *
 *       your option) any later version. The text of the GNU Lesser      *
 *       General Public License is included with this library in the     *
 *       file LICENSE.TXT.                                               *
 *   (2) The BSD-style license that is included with this library in     *
 *       the file LICENSE-BSD.TXT.                                       *
 *                                                                       *
 * This library is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files    *
 * LICENSE.TXT and LICENSE-BSD.TXT for more details.                     *
 *                                                                       *
 *************************************************************************/

/*

ODE Thread Local Storage access stub interface.

*/


#ifndef _ODE_ODETLS_H_
#define _ODE_ODETLS_H_


#include "odeou.h"


#if dTLS_ENABLED


using _OU_NAMESPACE::tlsvaluetype;
using _OU_NAMESPACE::HTLSKEY;
using _OU_NAMESPACE::CThreadLocalStorage;


struct TrimeshCollidersCache;


enum EODETLSKIND
{
    OTK__MIN,

    OTK_AUTOCLEANUP = OTK__MIN,
    OTK_MANUALCLEANUP,

    OTK__MAX,

    OTK__DEFAULT = OTK_AUTOCLEANUP,
};

enum EODETLSITEM
{
    OTI_DATA_ALLOCATION_FLAGS,
    OTI_TRIMESH_TRIMESH_COLLIDER_CACHE,

    OTI__MAX,
};


class COdeTls
{
public:
    static bool Initialize(EODETLSKIND tkTLSKind);
    static void Finalize(EODETLSKIND tkTLSKind);

    static void CleanupForThread();

public:
    static unsigned GetDataAllocationFlags(EODETLSKIND tkTLSKind)
    {
        // Must be a safe call as it is used to test if TLS slot is allocated at all
        return (unsigned)(sizeint)CThreadLocalStorage::GetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS);
    }

    static void SignalDataAllocationFlags(EODETLSKIND tkTLSKind, unsigned uFlagsMask)
    {
        unsigned uCurrentFlags = (unsigned)(sizeint)CThreadLocalStorage::UnsafeGetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS);
        CThreadLocalStorage::UnsafeSetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS, (tlsvaluetype)(sizeint)(uCurrentFlags | uFlagsMask));
    }

    static void DropDataAllocationFlags(EODETLSKIND tkTLSKind, unsigned uFlagsMask)
    {
        unsigned uCurrentFlags = (unsigned)(sizeint)CThreadLocalStorage::UnsafeGetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS);
        CThreadLocalStorage::UnsafeSetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_DATA_ALLOCATION_FLAGS, (tlsvaluetype)(sizeint)(uCurrentFlags & ~uFlagsMask));
    }

    static TrimeshCollidersCache *GetTrimeshCollidersCache(EODETLSKIND tkTLSKind)
    { 
        return (TrimeshCollidersCache *)CThreadLocalStorage::UnsafeGetStorageValue(m_ahtkStorageKeys[tkTLSKind], OTI_TRIMESH_TRIMESH_COLLIDER_CACHE);
    }

public:
    static bool AssignDataAllocationFlags(EODETLSKIND tkTLSKind, unsigned uInitializationFlags);

    static bool AssignTrimeshCollidersCache(EODETLSKIND tkTLSKind, TrimeshCollidersCache *pccInstance);
    static void DestroyTrimeshCollidersCache(EODETLSKIND tkTLSKind);

private:
    static void FreeTrimeshCollidersCache(TrimeshCollidersCache *pccCacheInstance);

private:
    static void _OU_CONVENTION_CALLBACK FreeTrimeshCollidersCache_Callback(tlsvaluetype vValueData);

private:
    static HTLSKEY				m_ahtkStorageKeys[OTK__MAX];
};


#endif // dTLS_ENABLED


#endif // _ODE_ODETLS_H_