3
* VirtualBox 3D common tooling
7
* Copyright (C) 2011 Oracle Corporation
9
* This file is part of VirtualBox Open Source Edition (OSE), as
10
* available from http://www.virtualbox.org. This file is free software;
11
* you can redistribute it and/or modify it under the terms of the GNU
12
* General Public License (GPL) as published by the Free Software
13
* Foundation, in version 2 as it comes in the "COPYING" file of the
14
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17
* The contents of this file may alternatively be used under the terms
18
* of the Common Development and Distribution License Version 1.0
19
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20
* VirtualBox OSE distribution, in which case the provisions of the
21
* CDDL are applicable instead of those of the GPL.
23
* You may elect to license modified versions of this file under the
24
* terms and conditions of either the GPL or the CDDL or both.
27
#ifndef ___VBox_VBoxVideo3D_h
28
#define ___VBox_VBoxVideo3D_h
30
#include <iprt/cdefs.h>
32
#ifndef VBoxTlsRefGetImpl
33
# ifdef VBoxTlsRefSetImpl
34
# error "VBoxTlsRefSetImpl is defined, unexpected!"
36
# include <iprt/thread.h>
37
# define VBoxTlsRefGetImpl(_tls) (RTTlsGet((RTTLS)(_tls)))
38
# define VBoxTlsRefSetImpl(_tls, _val) (RTTlsSet((RTTLS)(_tls), (_val)))
40
# ifndef VBoxTlsRefSetImpl
41
# error "VBoxTlsRefSetImpl is NOT defined, unexpected!"
45
#ifndef VBoxTlsRefAssertImpl
46
# define VBoxTlsRefAssertImpl(_a) do {} while (0)
49
typedef DECLCALLBACK(void) FNVBOXTLSREFDTOR(void*);
50
typedef FNVBOXTLSREFDTOR *PFNVBOXTLSREFDTOR;
53
VBOXTLSREFDATA_STATE_UNDEFINED = 0,
54
VBOXTLSREFDATA_STATE_INITIALIZED,
55
VBOXTLSREFDATA_STATE_TOBE_DESTROYED,
56
VBOXTLSREFDATA_STATE_DESTROYING,
57
VBOXTLSREFDATA_STATE_32BIT_HACK = 0x7fffffff
58
} VBOXTLSREFDATA_STATE;
60
#define VBOXTLSREFDATA \
61
volatile int32_t cTlsRefs; \
62
uint32_t enmTlsRefState; \
63
PFNVBOXTLSREFDTOR pfnTlsRefDtor; \
65
#define VBoxTlsRefInit(_p, _pfnDtor) do { \
67
(_p)->enmTlsRefState = VBOXTLSREFDATA_STATE_INITIALIZED; \
68
(_p)->pfnTlsRefDtor = (_pfnDtor); \
71
#define VBoxTlsRefIsFunctional(_p) (!!((_p)->enmTlsRefState == VBOXTLSREFDATA_STATE_INITIALIZED))
73
#define VBoxTlsRefAddRef(_p) do { \
74
int cRefs = ASMAtomicIncS32(&(_p)->cTlsRefs); \
75
VBoxTlsRefAssertImpl(cRefs > 1 || (_p)->enmTlsRefState == VBOXTLSREFDATA_STATE_DESTROYING); \
78
#define VBoxTlsRefRelease(_p) do { \
79
int cRefs = ASMAtomicDecS32(&(_p)->cTlsRefs); \
80
VBoxTlsRefAssertImpl(cRefs >= 0); \
81
if (!cRefs && (_p)->enmTlsRefState != VBOXTLSREFDATA_STATE_DESTROYING /* <- avoid recursion if VBoxTlsRefAddRef/Release is called from dtor */) { \
82
(_p)->enmTlsRefState = VBOXTLSREFDATA_STATE_DESTROYING; \
83
(_p)->pfnTlsRefDtor((_p)); \
87
#define VBoxTlsRefReleaseMarkDestroy(_p) do { \
88
(_p)->enmTlsRefState = VBOXTLSREFDATA_STATE_TOBE_DESTROYED; \
91
#define VBoxTlsRefGetCurrent(_t, _Tsd) ((_t*) VBoxTlsRefGetImpl((_Tsd)))
93
#define VBoxTlsRefSetCurrent(_t, _Tsd, _p) do { \
94
_t * oldCur = VBoxTlsRefGetCurrent(_t, _Tsd); \
95
if (oldCur != (_p)) { \
96
VBoxTlsRefSetImpl((_Tsd), (_p)); \
98
VBoxTlsRefRelease(oldCur); \
101
VBoxTlsRefAddRef((_t*)(_p)); \
106
#endif /* #ifndef ___VBox_VBoxVideo3D_h */