Debian Codegraph @

src/wayland-thread.c

raw
1 /*
2 * Copyright (c) 2016-2019, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /* To include PTHREAD_MUTEX_ERRORCHECK */
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE
26 #endif
27
28 #include "wayland-thread.h"
29 #include "wayland-egldisplay.h"
30 #include <stdlib.h>
31 #include <assert.h>
32
33 #if defined(__QNX__)
34 #define WL_EGL_ATTRIBUTE_DESTRUCTOR
35 #define WL_EGL_ATEXIT(func) atexit(func)
36 #else
37 #define WL_EGL_ATTRIBUTE_DESTRUCTOR __attribute__((destructor))
38 #define WL_EGL_ATEXIT(func) 0
39 #endif
40
41 static pthread_mutex_t wlMutex;
42 static pthread_once_t wlMutexOnceControl = PTHREAD_ONCE_INIT;
43 static int wlMutexInitialized = 0;
44
45 static void wlExternalApiInitializeLock(void)
46 {
47 pthread_mutexattr_t attr;
48
49 if (pthread_mutexattr_init(&attr)) {
50 assert(!"failed to initialize pthread attribute mutex");
51 return;
52 }
53
54 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)) {
55 assert(!"failed to set pthread attribute mutex errorcheck");
56 goto fail;
57 }
58
59 if (pthread_mutex_init(&wlMutex, &attr)) {
60 assert(!"failed to initialize pthread mutex");
61 goto fail;
62 }
63
64 wlMutexInitialized = 1;
65
66 fail:
67 if (pthread_mutexattr_destroy(&attr)) {
68 assert(!"failed to destroy pthread attribute mutex");
69 }
70 }
71
72 void wlExternalApiDestroyLock(void)
73 {
74 if (!wlMutexInitialized || pthread_mutex_destroy(&wlMutex)) {
75 assert(!"failed to destroy pthread mutex");
76 }
77 }
78
79 int wlExternalApiLock(void)
80 {
81 if (pthread_once(&wlMutexOnceControl, wlExternalApiInitializeLock)) {
82 assert(!"pthread once failed");
83 return -1;
84 }
85
86 if (!wlMutexInitialized || pthread_mutex_lock(&wlMutex)) {
87 assert(!"failed to lock pthread mutex");
88 return -1;
89 }
90
91 return 0;
92 }
93
94 int wlExternalApiUnlock(void)
95 {
96 if (!wlMutexInitialized || pthread_mutex_unlock(&wlMutex)) {
97 assert(!"failed to unlock pthread mutex");
98 return -1;
99 }
100
101 return 0;
102 }
103
104 bool wlEglInitializeMutex(pthread_mutex_t *mutex)
105 {
106 pthread_mutexattr_t attr;
107 bool ret = true;
108
109 if (pthread_mutexattr_init(&attr)) {
110 return false;
111 }
112
113 if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK)) {
114 ret = false;
115 goto done;
116 }
117
118 if (pthread_mutex_init(mutex, &attr)) {
119 ret = false;
120 goto done;
121 }
122
123 done:
124 pthread_mutexattr_destroy(&attr);
125 return ret;
126 }
127
128 void wlEglMutexDestroy(pthread_mutex_t *mutex)
129 {
130 pthread_mutex_destroy(mutex);
131 }