1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/*---------------------------------------------------------------
* Copyright (c) 1999,2000,2001,2002,2003
* The Board of Trustees of the University of Illinois
* All Rights Reserved.
*---------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software (Iperf) and associated
* documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
*
* Redistributions of source code must retain the above
* copyright notice, this list of conditions and
* the following disclaimers.
*
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimers in the documentation and/or other materials
* provided with the distribution.
*
*
* Neither the names of the University of Illinois, NCSA,
* nor the names of its contributors may be used to endorse
* or promote products derived from this Software without
* specific prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* ________________________________________________________________
* National Laboratory for Applied Network Research
* National Center for Supercomputing Applications
* University of Illinois at Urbana-Champaign
* http://www.ncsa.uiuc.edu
* ________________________________________________________________
*
* Thread.c
* by Kevin Gibbs <kgibbs@nlanr.net>
*
* Based on:
* Thread.cpp
* by Mark Gates <mgates@nlanr.net>
* -------------------------------------------------------------------
* The thread subsystem is responsible for all thread functions. It
* provides a thread implementation agnostic interface to Iperf. If
* threads are not available (HAVE_THREAD is undefined), thread_start
* does not start a new thread but just launches the specified object
* in the current thread. Everything that defines a thread of
* execution in Iperf is contained in an thread_Settings structure. To
* start a thread simply pass one such structure into thread_start.
* -------------------------------------------------------------------
* headers
* uses
* <stdlib.h>
* <stdio.h>
* <assert.h>
* <errno.h>
* Thread.h may include <pthread.h>
* ------------------------------------------------------------------- */
#include "headers.h"
#include "Thread.h"
#include "Locale.h"
#include "util.h"
#ifdef __cplusplus
extern "C" {
#endif
/* -------------------------------------------------------------------
* define static variables.
* ------------------------------------------------------------------- */
// number of currently running threads
int thread_sNum = 0;
// number of non-terminating running threads (ie listener thread)
int nonterminating_num = 0;
// condition to protect updating the above and alerting on
// changes to above
Condition thread_sNum_cond;
/* -------------------------------------------------------------------
* Initialize the thread subsystems variables and set the concurrency
* level in solaris.
* ------------------------------------------------------------------- */
void thread_init( ) {
Condition_Initialize( &thread_sNum_cond );
#if defined( sun )
/* Solaris apparently doesn't default to timeslicing threads,
* as such we force it to play nice. This may not work perfectly
* when _sending_ multiple _UDP_ streams.
*/
pthread_setconcurrency (3);
#endif
}
/* -------------------------------------------------------------------
* Destroy the thread subsystems variables.
* ------------------------------------------------------------------- */
void thread_destroy( ) {
Condition_Destroy( &thread_sNum_cond );
}
/* -------------------------------------------------------------------
* Start the specified object's thread execution. Increments thread
* count, spawns new thread, and stores thread ID.
* ------------------------------------------------------------------- */
void thread_start( struct thread_Settings* thread ) {
// Make sure this object has not been started already
if ( thread_equalid( thread->mTID, thread_zeroid() ) ) {
// Check if we need to start another thread before this one
if ( thread->runNow != NULL ) {
thread_start( thread->runNow );
}
// increment thread count
Condition_Lock( thread_sNum_cond );
thread_sNum++;
Condition_Unlock( thread_sNum_cond );
#if defined( HAVE_POSIX_THREAD )
// pthreads -- spawn new thread
if ( pthread_create( &thread->mTID, NULL, thread_run_wrapper, thread ) != 0 ) {
WARN( 1, "pthread_create" );
// decrement thread count
Condition_Lock( thread_sNum_cond );
thread_sNum--;
Condition_Unlock( thread_sNum_cond );
}
#elif defined( HAVE_WIN32_THREAD )
// Win32 threads -- spawn new thread
// Win32 has a thread handle in addition to the thread ID
thread->mHandle = CreateThread( NULL, 0, thread_run_wrapper, thread, 0, &thread->mTID );
if ( thread->mHandle == NULL ) {
WARN( 1, "CreateThread" );
// decrement thread count
Condition_Lock( thread_sNum_cond );
thread_sNum--;
Condition_Unlock( thread_sNum_cond );
}
#else
// single-threaded -- call Run_Wrapper in this thread
thread_run_wrapper( thread );
#endif
}
} // end thread_start
/* -------------------------------------------------------------------
* Stop the specified object's thread execution (if any) immediately.
* Decrements thread count and resets the thread ID.
* ------------------------------------------------------------------- */
void thread_stop( struct thread_Settings* thread ) {
#ifdef HAVE_THREAD
// Make sure we have been started
if ( ! thread_equalid( thread->mTID, thread_zeroid() ) ) {
// decrement thread count
Condition_Lock( thread_sNum_cond );
thread_sNum--;
Condition_Signal( &thread_sNum_cond );
Condition_Unlock( thread_sNum_cond );
// use exit() if called from within this thread
// use cancel() if called from a different thread
if ( thread_equalid( thread_getid(), thread->mTID ) ) {
// Destroy the object
Settings_Destroy( thread );
// Exit
#if defined( HAVE_POSIX_THREAD )
pthread_exit( NULL );
#else // Win32
CloseHandle( thread->mHandle );
ExitThread( 0 );
#endif
} else {
// Cancel
#if defined( HAVE_POSIX_THREAD )
// Cray J90 doesn't have pthread_cancel; Iperf works okay without
#ifdef HAVE_PTHREAD_CANCEL
pthread_cancel( thread->mTID );
#endif
#else // Win32
// this is a somewhat dangerous function; it's not
// suggested to Stop() threads a lot.
TerminateThread( thread->mHandle, 0 );
#endif
// Destroy the object only after killing the thread
Settings_Destroy( thread );
}
}
#endif
} // end Stop
/* -------------------------------------------------------------------
* This function is the entry point for new threads created in
* thread_start.
* ------------------------------------------------------------------- */
#if defined( HAVE_WIN32_THREAD )
DWORD WINAPI
#else
void*
#endif
thread_run_wrapper( void* paramPtr ) {
struct thread_Settings* thread = (struct thread_Settings*) paramPtr;
// which type of object are we
switch ( thread->mThreadMode ) {
case kMode_Server:
{
/* Spawn a Server thread with these settings */
server_spawn( thread );
} break;
case kMode_Client:
{
/* Spawn a Client thread with these settings */
client_spawn( thread );
} break;
case kMode_Reporter:
{
/* Spawn a Reporter thread with these settings */
reporter_spawn( thread );
} break;
case kMode_Listener:
{
// Increment the non-terminating thread count
thread_register_nonterm();
/* Spawn a Listener thread with these settings */
listener_spawn( thread );
// Decrement the non-terminating thread count
thread_unregister_nonterm();
} break;
default:
{
FAIL(1, "Unknown Thread Type!\n", thread);
} break;
}
#ifdef HAVE_POSIX_THREAD
// detach Thread. If someone already joined it will not do anything
// If noone has then it will free resources upon return from this
// function (Run_Wrapper)
pthread_detach(thread->mTID);
#endif
// decrement thread count and send condition signal
Condition_Lock( thread_sNum_cond );
thread_sNum--;
Condition_Signal( &thread_sNum_cond );
Condition_Unlock( thread_sNum_cond );
// Check if we need to start up a thread after executing this one
if ( thread->runNext != NULL ) {
thread_start( thread->runNext );
}
// Destroy this thread object
Settings_Destroy( thread );
return 0;
} // end run_wrapper
/* -------------------------------------------------------------------
* Wait for all thread object's execution to complete. Depends on the
* thread count being accurate and the threads sending a condition
* signal when they terminate.
* ------------------------------------------------------------------- */
void thread_joinall( void ) {
Condition_Lock( thread_sNum_cond );
while ( thread_sNum > 0 ) {
Condition_Wait( &thread_sNum_cond );
}
Condition_Unlock( thread_sNum_cond );
} // end Joinall
/* -------------------------------------------------------------------
* Compare the thread ID's (inLeft == inRight); return true if they
* are equal. On some OS's nthread_t is a struct so == will not work.
* TODO use pthread_equal. Any Win32 equivalent??
* ------------------------------------------------------------------- */
int thread_equalid( nthread_t inLeft, nthread_t inRight ) {
return(memcmp( &inLeft, &inRight, sizeof(inLeft)) == 0);
}
/* -------------------------------------------------------------------
* Return a zero'd out thread ID. On some OS's nthread_t is a struct
* so == 0 will not work.
* [static]
* ------------------------------------------------------------------- */
nthread_t thread_zeroid( void ) {
nthread_t a;
memset( &a, 0, sizeof(a));
return a;
}
/* -------------------------------------------------------------------
* set a thread to be ignorable, so joinall won't wait on it
* this simply decrements the thread count that joinall uses.
* This is utilized by the reporter thread which knows when it
* is ok to quit (aka no pending reports).
* ------------------------------------------------------------------- */
void thread_setignore( ) {
Condition_Lock( thread_sNum_cond );
thread_sNum--;
Condition_Signal( &thread_sNum_cond );
Condition_Unlock( thread_sNum_cond );
}
/* -------------------------------------------------------------------
* unset a thread from being ignorable, so joinall will wait on it
* this simply increments the thread count that joinall uses.
* This is utilized by the reporter thread which knows when it
* is ok to quit (aka no pending reports).
* ------------------------------------------------------------------- */
void thread_unsetignore( void ) {
Condition_Lock( thread_sNum_cond );
thread_sNum++;
Condition_Signal( &thread_sNum_cond );
Condition_Unlock( thread_sNum_cond );
}
/* -------------------------------------------------------------------
* set a thread to be non-terminating, so if you cancel through
* Ctrl-C they can be ignored by the joinall.
* ------------------------------------------------------------------- */
void thread_register_nonterm( void ) {
Condition_Lock( thread_sNum_cond );
nonterminating_num++;
Condition_Unlock( thread_sNum_cond );
}
/* -------------------------------------------------------------------
* unset a thread from being non-terminating, so if you cancel through
* Ctrl-C they can be ignored by the joinall.
* ------------------------------------------------------------------- */
void thread_unregister_nonterm( void ) {
Condition_Lock( thread_sNum_cond );
if ( nonterminating_num == 0 ) {
// nonterminating has been released with release_nonterm
// Add back to the threads to wait on
thread_sNum++;
} else {
nonterminating_num--;
}
Condition_Unlock( thread_sNum_cond );
}
/* -------------------------------------------------------------------
* this function releases all non-terminating threads from the list
* of active threads, so that when all terminating threads quit
* the joinall will complete. This is called on a Ctrl-C input. It is
* also used by the -P usage on the server side
* ------------------------------------------------------------------- */
int thread_release_nonterm( int interrupt ) {
Condition_Lock( thread_sNum_cond );
thread_sNum -= nonterminating_num;
if ( thread_sNum > 1 && nonterminating_num > 0 && interrupt != 0 ) {
fprintf( stderr, "%s", wait_server_threads );
}
nonterminating_num = 0;
Condition_Signal( &thread_sNum_cond );
Condition_Unlock( thread_sNum_cond );
return thread_sNum;
}
/* -------------------------------------------------------------------
* Return the number of threads currently running (doesn't include
* active threads that have called setdaemon (aka reporter thread))
* ------------------------------------------------------------------- */
int thread_numuserthreads( void ) {
return thread_sNum;
}
/*
* -------------------------------------------------------------------
* Allow another thread to execute. If no other threads are runable this
* is not guarenteed to actually rest.
* ------------------------------------------------------------------- */
void thread_rest ( void ) {
#if defined( HAVE_THREAD )
#if defined( HAVE_POSIX_THREAD )
#else // Win32
SwitchToThread( );
#endif
#endif
}
#ifdef __cplusplus
} /* end extern "C" */
#endif
|