1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // name: util_thread.cpp
29 // authors: Ge Wang (gewang@cs.princeton.edu)
30 // Perry R. Cook (prc@cs.princeton.edu)
31 // based on STK's XThread
32 // Perry R. Cook + Gary Scavone
34 //-----------------------------------------------------------------------------
35 #include "util_thread.h"
40 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
58 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
59 pthread_cancel(thread
);
60 pthread_join(thread
, NULL
);
61 #elif defined(__PLATFORM_WIN32__)
63 TerminateXThread((HANDLE
)thread
, 0);
70 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 bool XThread::start( THREAD_FUNCTION routine
, void * ptr
)
78 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
79 if( pthread_create( &thread
, NULL
, *routine
, ptr
) == 0 )
81 #elif defined(__PLATFORM_WIN32__)
83 thread
= _beginthreadex( NULL
, 0, routine
, ptr
, 0, &thread_id
);
84 if( thread
) result
= true;
92 //-----------------------------------------------------------------------------
95 //-----------------------------------------------------------------------------
96 bool XThread::wait( long milliseconds
)
100 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
101 pthread_cancel(thread
);
102 pthread_join(thread
, NULL
);
103 #elif defined(__PLATFORM_WIN32__)
104 DWORD timeout
, retval
;
105 if( milliseconds
< 0 ) timeout
= INFINITE
;
106 else timeout
= milliseconds
;
107 retval
= WaitForSingleObject( (HANDLE
)thread
, timeout
);
108 if( retval
== WAIT_OBJECT_0
) {
109 CloseHandle( (HANDLE
)thread
);
121 //-----------------------------------------------------------------------------
124 //-----------------------------------------------------------------------------
125 void XThread :: test( )
127 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
128 pthread_testcancel();
135 //-----------------------------------------------------------------------------
138 //-----------------------------------------------------------------------------
141 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
142 pthread_mutex_init(&mutex
, NULL
);
143 #elif defined(__PLATFORM_WIN32__)
144 InitializeCriticalSection(&mutex
);
151 //-----------------------------------------------------------------------------
154 //-----------------------------------------------------------------------------
157 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
158 pthread_mutex_destroy( &mutex
);
159 #elif defined(__PLATFORM_WIN32__)
160 DeleteCriticalSection(&mutex
);
167 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
171 void XMutex::acquire( )
173 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
174 pthread_mutex_lock(&mutex
);
175 #elif defined(__PLATFORM_WIN32__)
176 EnterCriticalSection(&mutex
);
183 //-----------------------------------------------------------------------------
186 //-----------------------------------------------------------------------------
187 void XMutex::release( )
189 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
190 pthread_mutex_unlock(&mutex
);
191 #elif defined(__PLATFORM_WIN32__)
192 LeaveCriticalSection(&mutex
);