00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "config.h"
00034 #endif
00035
00036 #ifdef HAVE_SYS_TYPES_H
00037 #include <sys/types.h>
00038 #else
00039 #error need sys/types.h
00040 #endif
00041
00042
00043 #include "Exception.h"
00044 #include "MultiThreadedConnector.h"
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 static const char fileid[] = "$Id: MultiThreadedConnector.cpp,v 1.8 2006/05/18 07:13:00 darkeye Exp $";
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 void
00067 MultiThreadedConnector :: init ( bool reconnect ) throw ( Exception )
00068 {
00069 this->reconnect = reconnect;
00070
00071 pthread_mutex_init( &mutexProduce, 0);
00072 pthread_cond_init( &condProduce, 0);
00073 threads = 0;
00074 }
00075
00076
00077
00078
00079
00080 void
00081 MultiThreadedConnector :: strip ( void ) throw ( Exception )
00082 {
00083 if ( threads ) {
00084 delete[] threads;
00085 threads = 0;
00086 }
00087
00088 pthread_cond_destroy( &condProduce);
00089 pthread_mutex_destroy( &mutexProduce);
00090 }
00091
00092
00093
00094
00095
00096 MultiThreadedConnector :: MultiThreadedConnector (
00097 const MultiThreadedConnector & connector )
00098 throw ( Exception )
00099 : Connector( connector)
00100 {
00101 reconnect = connector.reconnect;
00102 mutexProduce = connector.mutexProduce;
00103 condProduce = connector.condProduce;
00104
00105 if ( threads ) {
00106 delete[] threads;
00107 }
00108 threads = new ThreadData[numSinks];
00109 for ( unsigned int i = 0; i < numSinks; ++i ) {
00110 threads[i] = connector.threads[i];
00111 }
00112 }
00113
00114
00115
00116
00117
00118 MultiThreadedConnector &
00119 MultiThreadedConnector :: operator= ( const MultiThreadedConnector & connector )
00120 throw ( Exception )
00121 {
00122 if ( this != &connector ) {
00123 Connector::operator=( connector);
00124
00125 reconnect = connector.reconnect;
00126 mutexProduce = connector.mutexProduce;
00127 condProduce = connector.condProduce;
00128
00129 if ( threads ) {
00130 delete[] threads;
00131 }
00132 threads = new ThreadData[numSinks];
00133 for ( unsigned int i = 0; i < numSinks; ++i ) {
00134 threads[i] = connector.threads[i];
00135 }
00136 }
00137
00138 return *this;
00139 }
00140
00141
00142
00143
00144
00145
00146 bool
00147 MultiThreadedConnector :: open ( void ) throw ( Exception )
00148 {
00149 unsigned int i;
00150 size_t st;
00151
00152 if ( !Connector::open() ) {
00153 return false;
00154 }
00155
00156 running = true;
00157
00158 pthread_attr_init( &threadAttr);
00159 pthread_attr_getstacksize(&threadAttr, &st);
00160 if (st < 128 * 1024) {
00161 reportEvent( 5, "MultiThreadedConnector :: open, stack size ",
00162 (long)st);
00163 st = 128 * 1024;
00164 pthread_attr_setstacksize(&threadAttr, st);
00165 }
00166 pthread_attr_setdetachstate( &threadAttr, PTHREAD_CREATE_JOINABLE);
00167
00168 threads = new ThreadData[numSinks];
00169 for ( i = 0; i < numSinks; ++i ) {
00170 ThreadData * threadData = threads + i;
00171
00172 threadData->connector = this;
00173 threadData->ixSink = i;
00174 threadData->accepting = true;
00175 threadData->isDone = true;
00176 if ( pthread_create( &(threadData->thread),
00177 &threadAttr,
00178 ThreadData::threadFunction,
00179 threadData ) ) {
00180 break;
00181 }
00182 }
00183
00184
00185 if ( i < numSinks ) {
00186 unsigned int j;
00187
00188
00189 pthread_mutex_lock( &mutexProduce);
00190 running = false;
00191 pthread_cond_broadcast( &condProduce);
00192 pthread_mutex_unlock( &mutexProduce);
00193
00194 for ( j = 0; j < i; ++j ) {
00195 pthread_join( threads[j].thread, 0);
00196 }
00197
00198 delete[] threads;
00199 threads = 0;
00200
00201 return false;
00202 }
00203
00204 return true;
00205 }
00206
00207
00208
00209
00210
00211 unsigned int
00212 MultiThreadedConnector :: transfer ( unsigned long bytes,
00213 unsigned int bufSize,
00214 unsigned int sec,
00215 unsigned int usec )
00216 throw ( Exception )
00217 {
00218 unsigned int b;
00219
00220 if ( numSinks == 0 ) {
00221 return 0;
00222 }
00223
00224 if ( bufSize == 0 ) {
00225 return 0;
00226 }
00227
00228 dataBuffer = new unsigned char[bufSize];
00229 dataSize = 0;
00230
00231 reportEvent( 6, "MultiThreadedConnector :: tranfer, bytes", bytes);
00232
00233 for ( b = 0; !bytes || b < bytes; ) {
00234 if ( source->canRead( sec, usec) ) {
00235 unsigned int i;
00236
00237 pthread_mutex_lock( &mutexProduce);
00238 dataSize = source->read( dataBuffer, bufSize);
00239 b += dataSize;
00240
00241
00242 if ( dataSize == 0 ) {
00243 reportEvent( 3, "MultiThreadedConnector :: transfer, EOF");
00244 pthread_mutex_unlock( &mutexProduce);
00245 break;
00246 }
00247
00248 for ( i = 0; i < numSinks; ++i ) {
00249 threads[i].isDone = false;
00250 }
00251
00252
00253 pthread_cond_broadcast( &condProduce);
00254
00255
00256 while ( true ) {
00257 for ( i = 0; i < numSinks && threads[i].isDone; ++i );
00258 if ( i == numSinks ) {
00259 break;
00260 }
00261 pthread_cond_wait( &condProduce, &mutexProduce);
00262 }
00263 pthread_mutex_unlock( &mutexProduce);
00264 } else {
00265 reportEvent( 3, "MultiThreadedConnector :: transfer, can't read");
00266 break;
00267 }
00268 }
00269
00270 delete[] dataBuffer;
00271 return b;
00272 }
00273
00274
00275
00276
00277
00278
00279 void
00280 MultiThreadedConnector :: sinkThread( int ixSink )
00281 {
00282 ThreadData * threadData = &threads[ixSink];
00283 Sink * sink = sinks[ixSink].get();
00284
00285 while ( running ) {
00286
00287 pthread_mutex_lock( &mutexProduce);
00288 while ( running && threadData->isDone ) {
00289 pthread_cond_wait( &condProduce, &mutexProduce);
00290 }
00291 if ( !running ) {
00292 pthread_mutex_unlock( &mutexProduce);
00293 break;
00294 }
00295
00296 if ( threadData->accepting ) {
00297 if ( sink->canWrite( 0, 0) ) {
00298 try {
00299 sink->write( dataBuffer, dataSize);
00300 } catch ( Exception & e ) {
00301
00302
00303 threadData->accepting = false;
00304 }
00305 } else {
00306 reportEvent( 4,
00307 "MultiThreadedConnector :: sinkThread can't write ",
00308 ixSink);
00309
00310 }
00311 }
00312 threadData->isDone = true;
00313 pthread_cond_broadcast( &condProduce);
00314 pthread_mutex_unlock( &mutexProduce);
00315
00316 if ( !threadData->accepting ) {
00317 if ( reconnect ) {
00318 reportEvent( 4,
00319 "MultiThreadedConnector :: sinkThread reconnecting ",
00320 ixSink);
00321
00322 try {
00323 sink->close();
00324 sched_yield();
00325 sink->open();
00326 sched_yield();
00327 threadData->accepting = sink->isOpen();
00328 } catch ( Exception & e ) {
00329
00330 }
00331 } else {
00332
00333 running = false;
00334 }
00335 }
00336 }
00337 }
00338
00339
00340
00341
00342
00343
00344 void
00345 MultiThreadedConnector :: close ( void ) throw ( Exception )
00346 {
00347 unsigned int i;
00348
00349
00350 pthread_mutex_lock( &mutexProduce);
00351 running = false;
00352 pthread_cond_broadcast( &condProduce);
00353 pthread_mutex_unlock( &mutexProduce);
00354
00355
00356 for ( i = 0; i < numSinks; ++i ) {
00357 pthread_join( threads[i].thread, 0);
00358 }
00359 pthread_attr_destroy( &threadAttr);
00360
00361 Connector::close();
00362 }
00363
00364
00365
00366
00367
00368 void *
00369 MultiThreadedConnector :: ThreadData :: threadFunction( void * param )
00370 {
00371 struct sched_param sched;
00372 int sched_type;
00373 ThreadData * threadData = (ThreadData*) param;
00374
00375 pthread_getschedparam( threadData->thread, &sched_type, &sched );
00376
00377 reportEvent( 5,
00378 "MultiThreadedConnector :: ThreadData :: threadFunction, "
00379 "was (thread, priority, type): ",
00380 param,
00381 sched.sched_priority,
00382 sched_type == SCHED_FIFO ? "SCHED_FIFO" :
00383 sched_type == SCHED_RR ? "SCHED_RR" :
00384 sched_type == SCHED_OTHER ? "SCHED_OTHER" :
00385 "INVALID"
00386 );
00387
00388 sched.sched_priority = 1;
00389 pthread_setschedparam( threadData->thread, SCHED_FIFO, &sched);
00390
00391 pthread_getschedparam( threadData->thread, &sched_type, &sched );
00392 reportEvent( 5,
00393 "MultiThreadedConnector :: ThreadData :: threadFunction, "
00394 "now is (thread, priority, type): ",
00395 param,
00396 sched.sched_priority,
00397 sched_type == SCHED_FIFO ? "SCHED_FIFO" :
00398 sched_type == SCHED_RR ? "SCHED_RR" :
00399 sched_type == SCHED_OTHER ? "SCHED_OTHER" :
00400 "INVALID"
00401 );
00402
00403 threadData->connector->sinkThread( threadData->ixSink);
00404
00405 return 0;
00406 }
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443