*** empty log message ***
[chuck-blob.git] / v2 / util_thread.cpp
blobff07c41d5bca2c1b931a98de3fa49d610c447d4c
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
22 U.S.A.
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // name: util_thread.cpp
27 // desc: ...
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
33 // date: autumn 2004
34 //-----------------------------------------------------------------------------
35 #include "util_thread.h"
40 //-----------------------------------------------------------------------------
41 // name: XThread()
42 // desc: ...
43 //-----------------------------------------------------------------------------
44 XThread::XThread( )
46 thread = 0;
52 //-----------------------------------------------------------------------------
53 // name: ~XThread()
54 // desc: ...
55 //-----------------------------------------------------------------------------
56 XThread::~XThread( )
58 if( thread != 0 )
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);
65 #endif
72 //-----------------------------------------------------------------------------
73 // name: start()
74 // desc: ...
75 //-----------------------------------------------------------------------------
76 bool XThread::start( THREAD_FUNCTION routine, void * ptr )
78 bool result = false;
80 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
81 if( pthread_create( &thread, NULL, *routine, ptr ) == 0 )
82 result = true;
83 #elif defined(__PLATFORM_WIN32__)
84 unsigned thread_id;
85 thread = _beginthreadex( NULL, 0, routine, ptr, 0, &thread_id );
86 if( thread ) result = true;
87 #endif
88 return result;
94 //-----------------------------------------------------------------------------
95 // name: wait()
96 // desc: ...
97 //-----------------------------------------------------------------------------
98 bool XThread::wait( long milliseconds )
100 bool result = false;
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 );
112 thread = 0;
113 result = true;
115 #endif
117 return result;
123 //-----------------------------------------------------------------------------
124 // name: test()
125 // desc: ...
126 //-----------------------------------------------------------------------------
127 void XThread :: test( )
129 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
130 pthread_testcancel();
131 #endif
137 //-----------------------------------------------------------------------------
138 // name: XMutex()
139 // desc: ...
140 //-----------------------------------------------------------------------------
141 XMutex::XMutex( )
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);
147 #endif
153 //-----------------------------------------------------------------------------
154 // name: XMutex()
155 // desc: ...
156 //-----------------------------------------------------------------------------
157 XMutex::~XMutex( )
159 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
160 pthread_mutex_destroy( &mutex );
161 #elif defined(__PLATFORM_WIN32__)
162 DeleteCriticalSection(&mutex);
163 #endif
169 //-----------------------------------------------------------------------------
170 // name: acquire()
171 // desc: ...
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);
179 #endif
185 //-----------------------------------------------------------------------------
186 // name: unlock()
187 // desc: ...
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);
195 #endif