Logo Search packages:      
Sourcecode: darkice version File versions

bool MultiThreadedConnector::open ( void   )  throw ( Exception ) [virtual]

Open the connector. Opens the Source and the Sinks if necessary.

Returns:
true if opening was successful, false otherwise.
Exceptions:
Exception 

Reimplemented from Connector.

Definition at line 143 of file MultiThreadedConnector.cpp.

References MultiThreadedConnector::ThreadData::accepting, condProduce, MultiThreadedConnector::ThreadData::connector, MultiThreadedConnector::ThreadData::isDone, MultiThreadedConnector::ThreadData::ixSink, mutexProduce, Connector::numSinks, Connector::open(), Reporter::reportEvent(), running, MultiThreadedConnector::ThreadData::thread, threadAttr, MultiThreadedConnector::ThreadData::threadFunction(), and threads.

{
    unsigned int        i;
    size_t              st;

    if ( !Connector::open() ) {
        return false;
    }

    running = true;

    pthread_attr_init( &threadAttr);
    pthread_attr_getstacksize(&threadAttr, &st);
    if (st < 128 * 1024) {
        reportEvent( 5, "MultiThreadedConnector :: open, stack size ",
                        (long)st);
        st = 128 * 1024;
        pthread_attr_setstacksize(&threadAttr, st);
    }
    pthread_attr_setdetachstate( &threadAttr, PTHREAD_CREATE_JOINABLE);

    threads = new ThreadData[numSinks];
    for ( i = 0; i < numSinks; ++i ) {
        ThreadData    * threadData = threads + i;

        threadData->connector = this;
        threadData->ixSink    = i;
        threadData->accepting = true;
        threadData->isDone    = true;
        if ( pthread_create( &(threadData->thread),
                             &threadAttr,
                             ThreadData::threadFunction,
                             threadData ) ) {
            break;
        }
    }

    // if could not create all, delete the ones created
    if ( i < numSinks ) {
        unsigned int    j;

        // signal to stop for all running threads
        pthread_mutex_lock( &mutexProduce);
        running = false;
        pthread_cond_broadcast( &condProduce);
        pthread_mutex_unlock( &mutexProduce);

        for ( j = 0; j < i; ++j ) {
            pthread_join( threads[j].thread, 0);
        }

        delete[] threads;
        threads = 0;

        return false;
    }

    return true;
}


Generated by  Doxygen 1.6.0   Back to index