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 //-----------------------------------------------------------------------------
60 #if defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__)
61 pthread_cancel(thread
);
62 pthread_join(thread
, NULL
);
63 #elif defined(__PLATFORM_WIN32__)
64 TerminateThread((HANDLE
)thread
, 0);
72 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 bool XThread::start( THREAD_FUNCTION routine
, void * ptr
)
80 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
81 if( pthread_create( &thread
, NULL
, *routine
, ptr
) == 0 )
83 #elif defined(__PLATFORM_WIN32__)
85 thread
= _beginthreadex( NULL
, 0, routine
, ptr
, 0, &thread_id
);
86 if( thread
) result
= true;
94 //-----------------------------------------------------------------------------
97 //-----------------------------------------------------------------------------
98 bool XThread::wait( long milliseconds
)
102 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
103 pthread_cancel(thread
);
104 pthread_join(thread
, NULL
);
105 #elif defined(__PLATFORM_WIN32__)
106 DWORD timeout
, retval
;
107 if( milliseconds
< 0 ) timeout
= INFINITE
;
108 else timeout
= milliseconds
;
109 retval
= WaitForSingleObject( (HANDLE
)thread
, timeout
);
110 if( retval
== WAIT_OBJECT_0
) {
111 CloseHandle( (HANDLE
)thread
);
123 //-----------------------------------------------------------------------------
126 //-----------------------------------------------------------------------------
127 void XThread :: test( )
129 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
130 pthread_testcancel();
137 //-----------------------------------------------------------------------------
140 //-----------------------------------------------------------------------------
143 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
144 pthread_mutex_init(&mutex
, NULL
);
145 #elif defined(__PLATFORM_WIN32__)
146 InitializeCriticalSection(&mutex
);
153 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
159 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
160 pthread_mutex_destroy( &mutex
);
161 #elif defined(__PLATFORM_WIN32__)
162 DeleteCriticalSection(&mutex
);
169 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
173 void XMutex::acquire( )
175 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
176 pthread_mutex_lock(&mutex
);
177 #elif defined(__PLATFORM_WIN32__)
178 EnterCriticalSection(&mutex
);
185 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
189 void XMutex::release( )
191 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
192 pthread_mutex_unlock(&mutex
);
193 #elif defined(__PLATFORM_WIN32__)
194 LeaveCriticalSection(&mutex
);