1 /*
2     csGblMtx.h:
3 
4     Copyright (C) 2005 Istvan Varga
5 
6     This file is part of Csound.
7 
8     The Csound Library is free software; you can redistribute it
9     and/or modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation; either
11     version 2.1 of the License, or (at your option) any later version.
12 
13     Csound is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU Lesser General Public License for more details.
17 
18     You should have received a copy of the GNU Lesser General Public
19     License along with Csound; if not, write to the Free Software
20     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21     02110-1301 USA
22 */
23 #ifndef CSOUND_CSGBLMTX_H
24 
25 
26 #ifdef HAVE_PTHREAD
27 #include <pthread.h>
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 static pthread_mutex_t csound_global_lock_ = PTHREAD_MUTEX_INITIALIZER;
34 
csoundLock()35 void csoundLock() {
36   pthread_mutex_lock(&csound_global_lock_);
37 }
38 
csoundUnLock()39 void csoundUnLock() {
40   pthread_mutex_unlock(&csound_global_lock_);
41 }
42 
43 
44 #ifdef __cplusplus
45 }
46 #endif
47 
48 #elif defined(_WIN32) || defined (__WIN32__)
49 #define _WIN32_WINNT 0x0600
50 #include <windows.h>
51 
52 #ifdef __cplusplus
53 extern "C" {
54   #endif
55 
56 static INIT_ONCE g_InitOnce = INIT_ONCE_STATIC_INIT;
57 static CRITICAL_SECTION* csound_global_lock;
58 
InitHandleFunction(PINIT_ONCE InitOnce,PVOID Parameter,PVOID * lpContext)59 static BOOL CALLBACK InitHandleFunction ( PINIT_ONCE InitOnce, PVOID Parameter,
60     PVOID *lpContext) {
61 
62     CRITICAL_SECTION* cs = (CRITICAL_SECTION*) malloc(sizeof(CRITICAL_SECTION));
63     InitializeCriticalSection(cs);
64     *lpContext = cs;
65     return 1;
66 }
67 
68 
69 
csoundLock()70 void csoundLock() {
71     BOOL status;
72     CRITICAL_SECTION* cs;
73 
74     status = InitOnceExecuteOnce(&g_InitOnce, InitHandleFunction, NULL, &cs);
75     if (status) {
76       EnterCriticalSection(cs);
77     }
78 }
79 
csoundUnLock()80 void csoundUnLock() {
81 
82     BOOL status;
83     CRITICAL_SECTION* cs;
84 
85     status = InitOnceExecuteOnce(&g_InitOnce, InitHandleFunction, NULL, &cs);
86     if (status) {
87       LeaveCriticalSection(cs);
88     }
89 }
90 
91 
92 #ifdef __cplusplus
93 }
94 #endif
95 
96 #else /* END WIN32 */
97 #ifdef __cplusplus
98 extern "C" {
99 #endif
100 
csoundLock()101 void csoundLock() {
102 }
103 
csoundUnLock()104 void csoundUnLock() {
105 }
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif
112 
113 
114 #endif      /* CSOUND_CSGBLMTX_H */
115