summaryrefslogtreecommitdiff
path: root/src/egl/main/eglglobals.c
blob: 3ae4c1ad3a3223a5814ab2f2a77159359319166e (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
#include <stdlib.h>
#include <assert.h>
#include "eglglobals.h"
#include "egldriver.h"
#include "egllog.h"
#include "eglmutex.h"


#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))


static _EGL_DECLARE_MUTEX(_eglGlobalMutex);
struct _egl_global _eglGlobal =
{
   &_eglGlobalMutex,       /* Mutex */
   NULL,                   /* DisplayList */
   1,                      /* FreeScreenHandle */
   0x0,                    /* ClientAPIsMask */
   0,                      /* NumDrivers */
   { NULL },               /* Drivers */
   2,                      /* NumAtExitCalls */
   {                       /* AtExitCalls */
      _eglFiniDisplay,
      _eglUnloadDrivers
   },
};


static void
_eglAtExit(void)
{
   EGLint i;
   for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
      _eglGlobal.AtExitCalls[i]();
}


void
_eglAddAtExitCall(void (*func)(void))
{
   if (func) {
      static EGLBoolean registered = EGL_FALSE;

      _eglLockMutex(_eglGlobal.Mutex);

      if (!registered) {
         atexit(_eglAtExit);
         registered = EGL_TRUE;
      }

      assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
      _eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;

      _eglUnlockMutex(_eglGlobal.Mutex);
   }
}