1 /*---------------------------------------------------------------
2 * Copyright (c) 1999,2000,2001,2002,2003
3 * The Board of Trustees of the University of Illinois
5 *---------------------------------------------------------------
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software (Iperf) and associated
8 * documentation files (the "Software"), to deal in the Software
9 * without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute,
11 * sublicense, and/or sell copies of the Software, and to permit
12 * persons to whom the Software is furnished to do
13 * so, subject to the following conditions:
16 * Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and
18 * the following disclaimers.
21 * Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following
23 * disclaimers in the documentation and/or other materials
24 * provided with the distribution.
27 * Neither the names of the University of Illinois, NCSA,
28 * nor the names of its contributors may be used to endorse
29 * or promote products derived from this Software without
30 * specific prior written permission.
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
34 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35 * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT
36 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
37 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
38 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 * ________________________________________________________________
41 * National Laboratory for Applied Network Research
42 * National Center for Supercomputing Applications
43 * University of Illinois at Urbana-Champaign
44 * http://www.ncsa.uiuc.edu
45 * ________________________________________________________________
48 * by Kevin Gibbs <kgibbs@nlanr.net>
52 * by Mark Gates <mgates@nlanr.net>
53 * -------------------------------------------------------------------
54 * The thread subsystem is responsible for all thread functions. It
55 * provides a thread implementation agnostic interface to Iperf. If
56 * threads are not available (HAVE_THREAD is undefined), thread_start
57 * does not start a new thread but just launches the specified object
58 * in the current thread. Everything that defines a thread of
59 * execution in Iperf is contained in an thread_Settings structure. To
60 * start a thread simply pass one such structure into thread_start.
61 * -------------------------------------------------------------------
68 * Thread.h may include <pthread.h>
69 * ------------------------------------------------------------------- */
81 /* -------------------------------------------------------------------
82 * define static variables.
83 * ------------------------------------------------------------------- */
85 // number of currently running threads
87 // number of non-terminating running threads (ie listener thread)
88 int nonterminating_num = 0;
89 // condition to protect updating the above and alerting on
91 Condition thread_sNum_cond;
94 /* -------------------------------------------------------------------
95 * Initialize the thread subsystems variables and set the concurrency
97 * ------------------------------------------------------------------- */
99 Condition_Initialize( &thread_sNum_cond );
101 /* Solaris apparently doesn't default to timeslicing threads,
102 * as such we force it to play nice. This may not work perfectly
103 * when _sending_ multiple _UDP_ streams.
105 pthread_setconcurrency (3);
109 /* -------------------------------------------------------------------
110 * Destroy the thread subsystems variables.
111 * ------------------------------------------------------------------- */
112 void thread_destroy( ) {
113 Condition_Destroy( &thread_sNum_cond );
116 /* -------------------------------------------------------------------
117 * Start the specified object's thread execution. Increments thread
118 * count, spawns new thread, and stores thread ID.
119 * ------------------------------------------------------------------- */
120 void thread_start( struct thread_Settings* thread ) {
122 // Make sure this object has not been started already
123 if ( thread_equalid( thread->mTID, thread_zeroid() ) ) {
125 // Check if we need to start another thread before this one
126 if ( thread->runNow != NULL ) {
127 thread_start( thread->runNow );
130 // increment thread count
131 Condition_Lock( thread_sNum_cond );
133 Condition_Unlock( thread_sNum_cond );
135 #if defined( HAVE_POSIX_THREAD )
137 // pthreads -- spawn new thread
138 if ( pthread_create( &thread->mTID, NULL, thread_run_wrapper, thread ) != 0 ) {
139 WARN( 1, "pthread_create" );
141 // decrement thread count
142 Condition_Lock( thread_sNum_cond );
144 Condition_Unlock( thread_sNum_cond );
147 #elif defined( HAVE_WIN32_THREAD )
149 // Win32 threads -- spawn new thread
150 // Win32 has a thread handle in addition to the thread ID
151 thread->mHandle = CreateThread( NULL, 0, thread_run_wrapper, thread, 0, &thread->mTID );
152 if ( thread->mHandle == NULL ) {
153 WARN( 1, "CreateThread" );
155 // decrement thread count
156 Condition_Lock( thread_sNum_cond );
158 Condition_Unlock( thread_sNum_cond );
163 // single-threaded -- call Run_Wrapper in this thread
164 thread_run_wrapper( thread );
167 } // end thread_start
169 /* -------------------------------------------------------------------
170 * Stop the specified object's thread execution (if any) immediately.
171 * Decrements thread count and resets the thread ID.
172 * ------------------------------------------------------------------- */
173 void thread_stop( struct thread_Settings* thread ) {
176 // Make sure we have been started
177 if ( ! thread_equalid( thread->mTID, thread_zeroid() ) ) {
179 // decrement thread count
180 Condition_Lock( thread_sNum_cond );
182 Condition_Signal( &thread_sNum_cond );
183 Condition_Unlock( thread_sNum_cond );
185 // use exit() if called from within this thread
186 // use cancel() if called from a different thread
187 if ( thread_equalid( thread_getid(), thread->mTID ) ) {
189 // Destroy the object
190 Settings_Destroy( thread );
193 #if defined( HAVE_POSIX_THREAD )
194 pthread_exit( NULL );
196 CloseHandle( thread->mHandle );
202 #if defined( HAVE_POSIX_THREAD )
203 // Cray J90 doesn't have pthread_cancel; Iperf works okay without
204 #ifdef HAVE_PTHREAD_CANCEL
205 pthread_cancel( oldTID );
208 // this is a somewhat dangerous function; it's not
209 // suggested to Stop() threads a lot.
210 TerminateThread( thread->mHandle, 0 );
213 // Destroy the object only after killing the thread
214 Settings_Destroy( thread );
220 /* -------------------------------------------------------------------
221 * This function is the entry point for new threads created in
223 * ------------------------------------------------------------------- */
224 #if defined( HAVE_WIN32_THREAD )
229 thread_run_wrapper( void* paramPtr ) {
230 struct thread_Settings* thread = (struct thread_Settings*) paramPtr;
232 // which type of object are we
233 switch ( thread->mThreadMode ) {
236 /* Spawn a Server thread with these settings */
237 server_spawn( thread );
241 /* Spawn a Client thread with these settings */
242 client_spawn( thread );
246 /* Spawn a Reporter thread with these settings */
247 reporter_spawn( thread );
251 // Increment the non-terminating thread count
252 thread_register_nonterm();
253 /* Spawn a Listener thread with these settings */
254 listener_spawn( thread );
255 // Decrement the non-terminating thread count
256 thread_unregister_nonterm();
260 FAIL(1, "Unknown Thread Type!\n", thread);
264 #ifdef HAVE_POSIX_THREAD
265 // detach Thread. If someone already joined it will not do anything
266 // If noone has then it will free resources upon return from this
267 // function (Run_Wrapper)
268 pthread_detach(thread->mTID);
271 // decrement thread count and send condition signal
272 Condition_Lock( thread_sNum_cond );
274 Condition_Signal( &thread_sNum_cond );
275 Condition_Unlock( thread_sNum_cond );
277 // Check if we need to start up a thread after executing this one
278 if ( thread->runNext != NULL ) {
279 thread_start( thread->runNext );
282 // Destroy this thread object
283 Settings_Destroy( thread );
288 /* -------------------------------------------------------------------
289 * Wait for all thread object's execution to complete. Depends on the
290 * thread count being accurate and the threads sending a condition
291 * signal when they terminate.
292 * ------------------------------------------------------------------- */
293 void thread_joinall( void ) {
294 Condition_Lock( thread_sNum_cond );
295 while ( thread_sNum > 0 ) {
296 Condition_Wait( &thread_sNum_cond );
298 Condition_Unlock( thread_sNum_cond );
302 /* -------------------------------------------------------------------
303 * Compare the thread ID's (inLeft == inRight); return true if they
304 * are equal. On some OS's nthread_t is a struct so == will not work.
305 * TODO use pthread_equal. Any Win32 equivalent??
306 * ------------------------------------------------------------------- */
307 int thread_equalid( nthread_t inLeft, nthread_t inRight ) {
308 return(memcmp( &inLeft, &inRight, sizeof(inLeft)) == 0);
311 /* -------------------------------------------------------------------
312 * Return a zero'd out thread ID. On some OS's nthread_t is a struct
313 * so == 0 will not work.
315 * ------------------------------------------------------------------- */
316 nthread_t thread_zeroid( void ) {
318 memset( &a, 0, sizeof(a));
322 /* -------------------------------------------------------------------
323 * set a thread to be ignorable, so joinall won't wait on it
324 * this simply decrements the thread count that joinall uses.
325 * This is utilized by the reporter thread which knows when it
326 * is ok to quit (aka no pending reports).
327 * ------------------------------------------------------------------- */
328 void thread_setignore( ) {
329 Condition_Lock( thread_sNum_cond );
331 Condition_Signal( &thread_sNum_cond );
332 Condition_Unlock( thread_sNum_cond );
335 /* -------------------------------------------------------------------
336 * unset a thread from being ignorable, so joinall will wait on it
337 * this simply increments the thread count that joinall uses.
338 * This is utilized by the reporter thread which knows when it
339 * is ok to quit (aka no pending reports).
340 * ------------------------------------------------------------------- */
341 void thread_unsetignore( void ) {
342 Condition_Lock( thread_sNum_cond );
344 Condition_Signal( &thread_sNum_cond );
345 Condition_Unlock( thread_sNum_cond );
348 /* -------------------------------------------------------------------
349 * set a thread to be non-terminating, so if you cancel through
350 * Ctrl-C they can be ignored by the joinall.
351 * ------------------------------------------------------------------- */
352 void thread_register_nonterm( void ) {
353 Condition_Lock( thread_sNum_cond );
354 nonterminating_num++;
355 Condition_Unlock( thread_sNum_cond );
358 /* -------------------------------------------------------------------
359 * unset a thread from being non-terminating, so if you cancel through
360 * Ctrl-C they can be ignored by the joinall.
361 * ------------------------------------------------------------------- */
362 void thread_unregister_nonterm( void ) {
363 Condition_Lock( thread_sNum_cond );
364 if ( nonterminating_num == 0 ) {
365 // nonterminating has been released with release_nonterm
366 // Add back to the threads to wait on
369 nonterminating_num--;
371 Condition_Unlock( thread_sNum_cond );
374 /* -------------------------------------------------------------------
375 * this function releases all non-terminating threads from the list
376 * of active threads, so that when all terminating threads quit
377 * the joinall will complete. This is called on a Ctrl-C input. It is
378 * also used by the -P usage on the server side
379 * ------------------------------------------------------------------- */
380 int thread_release_nonterm( int interrupt ) {
381 Condition_Lock( thread_sNum_cond );
382 thread_sNum -= nonterminating_num;
383 if ( thread_sNum > 1 && nonterminating_num > 0 && interrupt != 0 ) {
384 fprintf( stderr, wait_server_threads );
386 nonterminating_num = 0;
387 Condition_Signal( &thread_sNum_cond );
388 Condition_Unlock( thread_sNum_cond );
392 /* -------------------------------------------------------------------
393 * Return the number of threads currently running (doesn't include
394 * active threads that have called setdaemon (aka reporter thread))
395 * ------------------------------------------------------------------- */
396 int thread_numuserthreads( void ) {
401 * -------------------------------------------------------------------
402 * Allow another thread to execute. If no other threads are runable this
403 * is not guarenteed to actually rest.
404 * ------------------------------------------------------------------- */
405 void thread_rest ( void ) {
406 #if defined( HAVE_THREAD )
407 #if defined( HAVE_POSIX_THREAD )
415 } /* end extern "C" */