|
1 /* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
2 * |
|
3 * Permission is hereby granted, free of charge, to any person obtaining a |
|
4 * copy of this software and /or associated documentation files |
|
5 * (the "Materials "), to deal in the Materials without restriction, |
|
6 * including without limitation the rights to use, copy, modify, merge, |
|
7 * publish, distribute, sublicense, and/or sell copies of the Materials, |
|
8 * and to permit persons to whom the Materials are furnished to do so, |
|
9 * subject to the following conditions: |
|
10 * |
|
11 * The above copyright notice and this permission notice shall be included |
|
12 * in all copies or substantial portions of the Materials. |
|
13 * |
|
14 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
|
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
|
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR |
|
20 * THE USE OR OTHER DEALINGS IN THE MATERIALS. |
|
21 * |
|
22 * Initial Contributors: |
|
23 * Nokia Corporation - initial contribution. |
|
24 * |
|
25 * Contributors: |
|
26 * |
|
27 * Description: |
|
28 * |
|
29 */ |
|
30 |
|
31 #include "glesOS.h" |
|
32 #include "glesInternal.h" |
|
33 |
|
34 namespace |
|
35 { |
|
36 static bool g_lockInitialized = false; |
|
37 static GLES_LOCK g_lock; |
|
38 } |
|
39 |
|
40 #if defined(_WIN32) |
|
41 static void initializeLock() |
|
42 { |
|
43 GLES_ASSERT(!g_lockInitialized); |
|
44 InitializeCriticalSection(&g_lock); |
|
45 g_lockInitialized = true; |
|
46 } |
|
47 |
|
48 void glesGetLock() |
|
49 { |
|
50 if(!g_lockInitialized) |
|
51 { |
|
52 initializeLock(); |
|
53 } |
|
54 EnterCriticalSection(&g_lock); |
|
55 } |
|
56 |
|
57 void glesReleaseLock() |
|
58 { |
|
59 GLES_ASSERT(g_lockInitialized); |
|
60 LeaveCriticalSection(&g_lock); |
|
61 } |
|
62 #else // defined(_WIN32) |
|
63 static void initializeLock() |
|
64 { |
|
65 int ret; |
|
66 |
|
67 GLES_ASSERT(!g_lockInitialized); |
|
68 |
|
69 pthread_mutexattr_t attr; |
|
70 ret = pthread_mutexattr_init(&attr); //initially not locked |
|
71 GLES_ASSERT(!ret); //check that there aren't any errors |
|
72 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); //count the number of recursive locks |
|
73 GLES_ASSERT(!ret); //check that there aren't any errors |
|
74 ret = pthread_mutex_init(&g_lock, &attr); |
|
75 pthread_mutexattr_destroy(&attr); |
|
76 GLES_ASSERT(!ret); //check that there aren't more errors |
|
77 |
|
78 g_lockInitialized = true; |
|
79 } |
|
80 |
|
81 void glesGetLock() |
|
82 { |
|
83 if(!g_lockInitialized) |
|
84 { |
|
85 initializeLock(); |
|
86 } |
|
87 int ret = pthread_mutex_lock(&g_lock); |
|
88 GLES_ASSERT(!ret); |
|
89 } |
|
90 |
|
91 void glesReleaseLock() |
|
92 { |
|
93 GLES_ASSERT(g_lockInitialized); |
|
94 int ret = pthread_mutex_unlock(&g_lock); |
|
95 GLES_ASSERT(!ret); |
|
96 } |
|
97 #endif // !defined(_WIN32) |