]> sjero.net Git - iperf/blob - compat/Thread.c
Fix CPU Usage Bug
[iperf] / compat / Thread.c
1 /*--------------------------------------------------------------- 
2  * Copyright (c) 1999,2000,2001,2002,2003                              
3  * The Board of Trustees of the University of Illinois            
4  * All Rights Reserved.                                           
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: 
14  *
15  *     
16  * Redistributions of source code must retain the above 
17  * copyright notice, this list of conditions and 
18  * the following disclaimers. 
19  *
20  *     
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. 
25  * 
26  *     
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. 
31  * 
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  * ________________________________________________________________ 
46  *
47  * Thread.c
48  * by Kevin Gibbs <kgibbs@nlanr.net>
49  *
50  * Based on:
51  * Thread.cpp
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  * -------------------------------------------------------------------
62  * headers
63  * uses
64  *   <stdlib.h>
65  *   <stdio.h>
66  *   <assert.h>
67  *   <errno.h>
68  * Thread.h may include <pthread.h>
69  * ------------------------------------------------------------------- */
70
71 #include "headers.h"
72
73 #include "Thread.h"
74 #include "Locale.h"
75 #include "util.h"
76
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
80
81 /* -------------------------------------------------------------------
82  * define static variables.
83  * ------------------------------------------------------------------- */
84
85 // number of currently running threads
86 int thread_sNum = 0;
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 
90 // changes to above
91 Condition thread_sNum_cond;
92
93
94 /* -------------------------------------------------------------------
95  * Initialize the thread subsystems variables and set the concurrency
96  * level in solaris.
97  * ------------------------------------------------------------------- */
98 void thread_init( ) {
99     Condition_Initialize( &thread_sNum_cond );
100 #if defined( sun )
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.
104      */
105     pthread_setconcurrency (3);
106 #endif
107 }
108
109 /* -------------------------------------------------------------------
110  * Destroy the thread subsystems variables.
111  * ------------------------------------------------------------------- */
112 void thread_destroy( ) {
113     Condition_Destroy( &thread_sNum_cond );
114 }
115
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 ) {
121
122     // Make sure this object has not been started already
123     if ( thread_equalid( thread->mTID, thread_zeroid() ) ) {
124
125         // Check if we need to start another thread before this one
126         if ( thread->runNow != NULL ) {
127             thread_start( thread->runNow );
128         }
129
130         // increment thread count
131         Condition_Lock( thread_sNum_cond );
132         thread_sNum++;
133         Condition_Unlock( thread_sNum_cond );
134
135 #if   defined( HAVE_POSIX_THREAD )
136
137         // pthreads -- spawn new thread
138         if ( pthread_create( &thread->mTID, NULL, thread_run_wrapper, thread ) != 0 ) {
139             WARN( 1, "pthread_create" );
140
141             // decrement thread count
142             Condition_Lock( thread_sNum_cond );
143             thread_sNum--;
144             Condition_Unlock( thread_sNum_cond );
145         }
146
147 #elif defined( HAVE_WIN32_THREAD )
148
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" );
154
155             // decrement thread count
156             Condition_Lock( thread_sNum_cond );
157             thread_sNum--;
158             Condition_Unlock( thread_sNum_cond );
159         }
160
161 #else
162
163         // single-threaded -- call Run_Wrapper in this thread
164         thread_run_wrapper( thread );
165 #endif
166     }
167 } // end thread_start
168
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 ) {
174
175 #ifdef HAVE_THREAD
176     // Make sure we have been started
177     if ( ! thread_equalid( thread->mTID, thread_zeroid() ) ) {
178
179         // decrement thread count
180         Condition_Lock( thread_sNum_cond );
181         thread_sNum--;
182         Condition_Signal( &thread_sNum_cond );
183         Condition_Unlock( thread_sNum_cond );
184
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 ) ) {
188
189             // Destroy the object
190             Settings_Destroy( thread );
191
192             // Exit
193 #if   defined( HAVE_POSIX_THREAD )
194             pthread_exit( NULL );
195 #else // Win32
196             CloseHandle( thread->mHandle );
197             ExitThread( 0 );
198 #endif
199         } else {
200
201             // Cancel
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 );
206 #endif
207 #else // Win32
208             // this is a somewhat dangerous function; it's not
209             // suggested to Stop() threads a lot.
210             TerminateThread( thread->mHandle, 0 );
211 #endif
212
213             // Destroy the object only after killing the thread
214             Settings_Destroy( thread );
215         }
216     }
217 #endif
218 } // end Stop
219
220 /* -------------------------------------------------------------------
221  * This function is the entry point for new threads created in 
222  * thread_start. 
223  * ------------------------------------------------------------------- */
224 #if   defined( HAVE_WIN32_THREAD )
225 DWORD WINAPI
226 #else
227 void*
228 #endif
229 thread_run_wrapper( void* paramPtr ) {
230     struct thread_Settings* thread = (struct thread_Settings*) paramPtr;
231
232     // which type of object are we
233     switch ( thread->mThreadMode ) {
234         case kMode_Server:
235             {
236                 /* Spawn a Server thread with these settings */
237                 server_spawn( thread );
238             } break;
239         case kMode_Client:
240             {
241                 /* Spawn a Client thread with these settings */
242                 client_spawn( thread );
243             } break;
244         case kMode_Reporter:
245             {
246                 /* Spawn a Reporter thread with these settings */
247                 reporter_spawn( thread );
248             } break;
249         case kMode_Listener:
250             {
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();
257             } break;
258         default:
259             {
260                 FAIL(1, "Unknown Thread Type!\n", thread);
261             } break;
262     }
263
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);
269 #endif
270
271     // decrement thread count and send condition signal
272     Condition_Lock( thread_sNum_cond );
273     thread_sNum--;
274     Condition_Signal( &thread_sNum_cond );
275     Condition_Unlock( thread_sNum_cond );
276
277     // Check if we need to start up a thread after executing this one
278     if ( thread->runNext != NULL ) {
279         thread_start( thread->runNext );
280     }
281
282     // Destroy this thread object
283     Settings_Destroy( thread );
284
285     return 0;
286 } // end run_wrapper
287
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 );
297     }
298     Condition_Unlock( thread_sNum_cond );
299 } // end Joinall
300
301
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);
309 }
310
311 /* -------------------------------------------------------------------
312  * Return a zero'd out thread ID. On some OS's nthread_t is a struct
313  * so == 0 will not work.
314  * [static]
315  * ------------------------------------------------------------------- */
316 nthread_t thread_zeroid( void ) {
317     nthread_t a;
318     memset( &a, 0, sizeof(a));
319     return a;
320 }
321
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 );
330     thread_sNum--;
331     Condition_Signal( &thread_sNum_cond );
332     Condition_Unlock( thread_sNum_cond );
333 }
334
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 );
343     thread_sNum++;
344     Condition_Signal( &thread_sNum_cond );
345     Condition_Unlock( thread_sNum_cond );
346 }
347
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 );
356 }
357
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
367         thread_sNum++;
368     } else {
369         nonterminating_num--; 
370     }
371     Condition_Unlock( thread_sNum_cond );
372 }
373
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 );
385     }
386     nonterminating_num = 0;
387     Condition_Signal( &thread_sNum_cond );
388     Condition_Unlock( thread_sNum_cond );
389     return thread_sNum;
390 }
391
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 ) {
397     return thread_sNum;
398 }
399
400 /*
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 )
408 #else // Win32
409     SwitchToThread( );
410 #endif
411 #endif
412 }
413
414 #ifdef __cplusplus
415 } /* end extern "C" */
416 #endif
417