libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
glew.h
Go to the documentation of this file.
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license in the file COPYING
10 * or http://www.opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file COPYING.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2019 Saso Kiselkov. All rights reserved.
24 */
25
26#ifndef _ACF_UTILS_GLEW_H_
27#define _ACF_UTILS_GLEW_H_
28
29#if APL || LIN
30#include <pthread.h>
31#endif
32
47#ifndef GLEW_MX
48#define GLEW_MX
49#endif
50/*
51 * We use static linking on Linux, Apple and MinGW. Everywhere else
52 * (notably Windows & MSVC), we use dynamic linking.
53 */
54#if LIN || APL || defined(__MINGW32__) || defined(ACFUTILS_DLL)
55# ifndef GLEW_STATIC
56# define GLEW_STATIC
57# endif
58#else /* !LIN && !APL && !defined(__MINGW32__) && !defined(ACFUTILS_DLL) */
59# ifndef GLEW_BUILD
60# define GLEW_BUILD
61# endif
62#endif /* !LIN && !APL && !defined(__MINGW32__) && !defined(ACFUTILS_DLL) */
63#include <GL/glew.h>
64
65#include "core.h"
66#include "safe_alloc.h"
67#include "tls.h"
68
69#ifdef __cplusplus
70extern "C" {
71#endif
72
73/*
74 * Only use native TLS support on Linux (where it works properly).
75 * On Windows, native TLS isn't reliably available to DLLs and on
76 * Mac, any kind of symbol-remapping DRM breaks the native TLS.
77 */
78#ifndef LACF_GLEW_USE_NATIVE_TLS
79#if LIN
80#define LACF_GLEW_USE_NATIVE_TLS 1
81#else /* !LIN */
82#define LACF_GLEW_USE_NATIVE_TLS 0
83#endif /* !LIN */
84#endif /* LACF_GLEW_USE_NATIVE_TLS */
85
86#if LACF_GLEW_USE_NATIVE_TLS
87
88extern THREAD_LOCAL GLEWContext lacf_glew_per_thread_ctx;
89
90#define lacf_glew_dllmain_hook(reason)
91#define lacf_glew_init()
92#define lacf_glew_thread_fini()
93#define lacf_glew_fini()
94
95static inline GLEWContext *
96glewGetContext(void)
97{
98 return (&lacf_glew_per_thread_ctx);
99}
100
101#else /* !LACF_GLEW_USE_NATIVE_TLS */
102
103#if LIN || APL
104
105/*
106 * The pthread TLS implementation doesn't rely on any need for external
107 * cooperation from the caller, so we don't need any of the init/fini
108 * functions.
109 */
110extern pthread_key_t lacf_glew_ctx_key;
111extern pthread_once_t lacf_glew_ctx_once;
112
113void lacf_glew_ctx_make_key(void);
114
115#define lacf_glew_dllmain_hook(reason)
116#define lacf_glew_init()
117#define lacf_glew_thread_fini()
118#define lacf_glew_fini()
119
120static inline GLEWContext *
121glewGetContext(void)
122{
123 GLEWContext *ctx;
124
125 (void) pthread_once(&lacf_glew_ctx_once, lacf_glew_ctx_make_key);
126 ctx = (GLEWContext *)pthread_getspecific(lacf_glew_ctx_key);
127 if (ctx == NULL) {
128 ctx = (GLEWContext *)safe_malloc(sizeof (*ctx));
129 (void) pthread_setspecific(lacf_glew_ctx_key, ctx);
130 }
131
132 return (ctx);
133}
134
135#else /* !APL && !LIN */
136
137API_EXPORT_DATA DWORD lacf_glew_ctx_key;
138
139API_EXPORT void lacf_glew_dllmain_hook(DWORD reason);
140API_EXPORT void lacf_glew_init(void);
141API_EXPORT void lacf_glew_thread_fini(void);
142API_EXPORT void lacf_glew_fini(void);
143
144static inline GLEWContext *
145glewGetContext(void)
146{
147 GLEWContext *ctx;
148
149 ASSERT(lacf_glew_ctx_key != 0);
150 ctx = (GLEWContext *)TlsGetValue(lacf_glew_ctx_key);
151 if (ctx == NULL) {
152 ctx = (GLEWContext *)lacf_malloc(sizeof (*ctx));
153 VERIFY(TlsSetValue(lacf_glew_ctx_key, (void *)ctx));
154 }
155
156 return (ctx);
157}
158
159#endif /* !APL && !LIN */
160
161#endif /* !LACF_GLEW_USE_NATIVE_TLS */
162
163#ifdef __cplusplus
164}
165#endif
166
167#endif /* _ACF_UTILS_GLEW_H_ */
#define VERIFY(x)
Definition assert.h:78
#define ASSERT(x)
Definition assert.h:208
void * lacf_malloc(size_t n)
Definition core.c:37
static void * safe_malloc(size_t size)
Definition safe_alloc.h:56