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.c
1/*
2 * CDDL HEADER START
3 *
4 * This file and its contents are supplied under the terms of the
5 * Common Development and Distribution License ("CDDL"), version 1.0.
6 * You may only use this file in accordance with the terms of version
7 * 1.0 of the CDDL.
8 *
9 * A full copy of the text of the CDDL should have accompanied this
10 * source. A copy of the CDDL is also available via the Internet at
11 * http://www.illumos.org/license/CDDL.
12 *
13 * CDDL HEADER END
14*/
15/*
16 * Copyright 2019 Saso Kiselkov. All rights reserved.
17 */
18
19#include <stdlib.h>
20
21#include "acfutils/glew.h"
22
23THREAD_LOCAL GLEWContext lacf_glew_per_thread_ctx;
24
25#if APL || LIN
26
27pthread_key_t lacf_glew_ctx_key;
28pthread_once_t lacf_glew_ctx_once = PTHREAD_ONCE_INIT;
29
30void
31lacf_glew_ctx_make_key(void)
32{
33 (void) pthread_key_create(&lacf_glew_ctx_key, free);
34}
35
36#else /* !APL && !LIN */
37
38DWORD lacf_glew_ctx_key = 0;
39
40/*
41 * Windows doesn't have a thread-exit and DLL-unload facility, so
42 * we need to hook into the DllMain function. One is provided by
43 * lacf_msvc_compat.cpp, however if the module dev wants to define
44 * their own, they'll need to call lacf_glew_dllmain_hook manually.
45 */
46void
47lacf_glew_dllmain_hook(DWORD reason)
48{
49 switch (reason) {
50 case DLL_PROCESS_ATTACH:
51 lacf_glew_init();
52 break;
53 case DLL_THREAD_DETACH:
54 lacf_glew_thread_fini();
55 break;
56 case DLL_PROCESS_DETACH:
57 lacf_glew_fini();
58 break;
59 }
60}
61
62void
63lacf_glew_init(void)
64{
65 VERIFY3U(lacf_glew_ctx_key, ==, 0);
66 lacf_glew_ctx_key = TlsAlloc();
67 VERIFY(lacf_glew_ctx_key != TLS_OUT_OF_INDEXES);
68}
69
70void
71lacf_glew_thread_fini(void)
72{
73 GLEWContext *ctx;
74
75 VERIFY(lacf_glew_ctx_key != 0);
76 ctx = TlsGetValue(lacf_glew_ctx_key);
77 if (ctx != NULL) {
78 lacf_free(ctx);
79 TlsSetValue(lacf_glew_ctx_key, NULL);
80 }
81}
82
83void
84lacf_glew_fini(void)
85{
86 ASSERT(lacf_glew_ctx_key != 0);
87 TlsFree(lacf_glew_ctx_key);
88 lacf_glew_ctx_key = 0;
89}
90
91#ifdef ACFUTILS_DLL
92
93BOOL WINAPI
94DllMain(HINSTANCE hinst, DWORD reason, LPVOID resvd)
95{
96 LACF_UNUSED(hinst);
97 LACF_UNUSED(resvd);
98 lacf_glew_dllmain_hook(reason);
99 return (TRUE);
100}
101
102#endif /* defined(ACFUTILS_DLL) */
103
104#endif /* !APL && !LIN */
#define VERIFY(x)
Definition assert.h:78
#define ASSERT(x)
Definition assert.h:208
#define VERIFY3U(x, op, y)
Definition assert.h:136
void lacf_free(void *buf)
Definition core.c:49