*** empty log message ***
[chuck-blob.git] / exile / v1 / src / util_thread.cpp
blob8b19df610da98bd0852bbc2fdc6776e3b7bcaace
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 ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
59 pthread_cancel(thread);
60 pthread_join(thread, NULL);
61 #elif defined(__PLATFORM_WIN32__)
62 if( thread )
63 TerminateXThread((HANDLE)thread, 0);
64 #endif
70 //-----------------------------------------------------------------------------
71 // name: start()
72 // desc: ...
73 //-----------------------------------------------------------------------------
74 bool XThread::start( THREAD_FUNCTION routine, void * ptr )
76 bool result = false;
78 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
79 if( pthread_create( &thread, NULL, *routine, ptr ) == 0 )
80 result = true;
81 #elif defined(__PLATFORM_WIN32__)
82 unsigned thread_id;
83 thread = _beginthreadex( NULL, 0, routine, ptr, 0, &thread_id );
84 if( thread ) result = true;
85 #endif
86 return result;
92 //-----------------------------------------------------------------------------
93 // name: wait()
94 // desc: ...
95 //-----------------------------------------------------------------------------
96 bool XThread::wait( long milliseconds )
98 bool result = false;
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 );
110 thread = 0;
111 result = true;
113 #endif
115 return result;
121 //-----------------------------------------------------------------------------
122 // name: test()
123 // desc: ...
124 //-----------------------------------------------------------------------------
125 void XThread :: test( )
127 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
128 pthread_testcancel();
129 #endif
135 //-----------------------------------------------------------------------------
136 // name: XMutex()
137 // desc: ...
138 //-----------------------------------------------------------------------------
139 XMutex::XMutex( )
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);
145 #endif
151 //-----------------------------------------------------------------------------
152 // name: XMutex()
153 // desc: ...
154 //-----------------------------------------------------------------------------
155 XMutex::~XMutex( )
157 #if ( defined(__PLATFORM_MACOSX__) || defined(__PLATFORM_LINUX__) || defined(__WINDOWS_PTHREAD__) )
158 pthread_mutex_destroy( &mutex );
159 #elif defined(__PLATFORM_WIN32__)
160 DeleteCriticalSection(&mutex);
161 #endif
167 //-----------------------------------------------------------------------------
168 // name: acquire()
169 // desc: ...
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);
177 #endif
183 //-----------------------------------------------------------------------------
184 // name: unlock()
185 // desc: ...
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);
193 #endif