Esempio n. 1
0
int
main()
{
    try
    {
        WorkQueuePtr h = new WorkQueue();
        IceUtil::ThreadControl control = h->start();
        printf("Pushing work items");
        printf("."); fflush(stdout);
        h->add("item1");
        printf("."); fflush(stdout);
        h->add("item2");
        printf("."); fflush(stdout);
        h->add("item3");
        printf("."); fflush(stdout);
        h->add("item4");
        printf("."); fflush(stdout);
        h->add("item5");
        printf("."); fflush(stdout);
        h->add("destroy");
        printf(" ok\n");
        printf("Waiting for WorkQueue to terminate\n");
        control.join();
    }
    catch(const IceUtil::Exception& ex)
    {
        fprintf(stderr, "%s\n", ex.toString().c_str());
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
Esempio n. 2
0
void
TimeoutI::holdAdapter(Ice::Int to, const Ice::Current& current)
{
    current.adapter->hold();
    IceUtil::ThreadPtr thread = new ActivateAdapterThread(current.adapter, to);
    IceUtil::ThreadControl threadControl = thread->start();
    threadControl.detach();
}
Esempio n. 3
0
void
AliveTest::run()
{
    //
    // Check that calling isAlive() returns the correct result for alive and
    // and dead threads.
    //
    CondVar childCreated;
    CondVar parentReady;
    AliveTestThreadPtr t = new AliveTestThread(childCreated, parentReady);
    IceUtil::ThreadControl c = t->start();
    childCreated.waitForSignal();
    test(t->isAlive());
    parentReady.signal();
    c.join();
    test(!t->isAlive());
}
Esempio n. 4
0
void 
Test::AccountI::transfer3_async(const AMD_Account_transfer3Ptr& cb, int amount, const Test::AccountPrx& toAccount, const Current& current)
{
    //
    // Here the dispatch thread does the actual work, but a separate thread sends the response
    //

    ResponseThreadPtr thread = new ResponseThread(cb);
    IceUtil::ThreadControl tc = thread->start(33000);
    
    test(_evictor->getCurrentTransaction() != 0);

    try
    {
        toAccount->deposit(amount); // collocated call
        deposit(-amount, current); // direct call
    }
    catch(const Ice::UserException& e)
    {
        tc.detach();

        //
        // Need to rollback here -- "rollback on user exception" does not work
        // when the dispatch commits before it gets any response!
        //
        _evictor->getCurrentTransaction()->rollback();
        
        thread->exception(e);

        return;
    }
    catch(...)
    {
        thread->cancel();
        tc.join();
        throw;
    }

    tc.detach();
    thread->response();
}
TEST(ConcurrencyTest, testEvent )
{
        list<BaseThreadPtr> rs;
        list<IceUtil::ThreadControl> rcs;
        for (int i = 0; i < 10; i++) {
            BaseThreadPtr r = new ReaderThread();
            rs.push_back(r);
            rcs.push_back(r->start());
        }

        BaseThreadPtr w = new WriterThread();
        IceUtil::ThreadControl wc = w->start();
        wc.join();
        ASSERT_TRUE( (*w).passed );

        for (int i = 0; i < 10; i++) {
            BaseThreadPtr r = rs.front();
            IceUtil::ThreadControl tc = rcs.front();
            rs.pop_front();
            rcs.pop_front();
            tc.join();
            ASSERT_TRUE( (*r).passed );
        }
}
Esempio n. 6
0
int main(int argc, char** argv)
{
	int status;

	// ICE
	Ice::CommunicatorPtr ic;
	jderobot::cameraClient* camRGB = NULL;			// parallelIce RGB Image
	jderobot::Pose3DPrx pose3Dprx = 0;				// ICE Pose3D proxy

	IceUtil::ThreadControl rgbTc;	// RGB Image Thread Control

	pthread_t thr_gui;			// GUI thread
	pthread_t thr_camera;		// Update Camera thread
	pthread_t thr_speed;		//speed control

	std::string prefix("navigatorCamera");	// Component Prefix
	std::string gladeFile;					// Path to the glade file

	bool guiActivated;		// GUI activation flag
	bool controlActivated;	// Control activation flag
	bool speedActivated;

	int pose3dFps;		// Frequency to update the Pose3D.
	long cycle_pose3d;	// Time cycle for control the update of Pose3D.

	sharer = new navigatorCamera::Sharer();

	struct timeval a, b;
	long totala, totalb;
	long diff;

	//--------------------INPUT ARGUMENTS--------------------//
	if (argc != 2)
	{
		std::cerr << "\nUSE: ./" << prefix << " --Ice.Config=" << prefix << ".cfg\n" << std::endl;
		return 1;
	}
	//------------------END INPUT ARGUMENTS------------------//

	try
	{
		//--------------------ICE--------------------//
		ic = Ice::initialize(argc,argv);
		Ice::PropertiesPtr prop = ic->getProperties();

		gladeFile = prop->getPropertyWithDefault(prefix + ".gladeFile", "./" + prefix + ".glade");

		guiActivated = prop->getPropertyAsIntWithDefault(prefix + ".guiActivated",1);
		controlActivated = prop->getPropertyAsIntWithDefault(prefix + ".controlActivated",0);
		speedActivated = prop->getPropertyAsIntWithDefault(prefix + ".speedActivated",0);

		pose3dFps = prop->getPropertyAsIntWithDefault(prefix + ".Pose3D.Fps",25);
		cycle_pose3d = (long)( (1./(float)pose3dFps) * 1000000); //microseconds

		sharer->setTranslationStep(atof(prop->getPropertyWithDefault(prefix + ".TranslationStep","0.1").c_str()));
		sharer->setRotationStep(atof(prop->getPropertyWithDefault(prefix + ".RotationStep","0.1").c_str()));

		// Contact to RGB Image interface
		camRGB = new jderobot::cameraClient(ic, prefix + ".CameraRGB.");
		if (camRGB != NULL){
			rgbTc = camRGB->start();
		}
		else{
			throw prefix + ": failed to load RGB Camera";
		}

		// Contact to Pose3D interface
		Ice::ObjectPrx basePose3D = ic->propertyToProxy(prefix + ".Pose3D.Proxy");
		if ( basePose3D == 0 )
			throw "Could not create proxy with Pose3D.";
		// Cast to Pose3D
		pose3Dprx = jderobot::Pose3DPrx::checkedCast(basePose3D);
		if ( pose3Dprx == 0 )
			throw std::invalid_argument("Invalid proxy " + prefix + ".Pose3D.Proxy");

		//------------------END ICE------------------//

		sharer->setGuiVisible(guiActivated);
		sharer->setControlActive(controlActivated);
		if ( guiActivated )
			pthread_create(&thr_gui, NULL, &showGui, static_cast<void *>(&gladeFile));

		pthread_create(&thr_camera, NULL, &updateCamera, static_cast<void *>(camRGB));
		

		// Captures Pose3D ICE data.
		jderobot::Pose3DDataPtr p3dData = pose3Dprx->getPose3DData();
		sharer->setPose3D(p3dData);

		if ( speedActivated ) {
			pthread_create(&thr_speed, NULL, &updateSpeed, NULL);
		}

		while ( sharer->getControlActive() || sharer->getGuiVisible() )
		{
			gettimeofday(&a, NULL);
			totala = a.tv_sec * 1000000 + a.tv_usec;

			// Update the current Pose3D ICE data.
			pose3Dprx->setPose3DData(sharer->getPose3D());

			// Sleep Algorithm
			gettimeofday(&b, NULL);
			totalb = b.tv_sec * 1000000 + b.tv_usec;
			diff = (totalb - totala);

			cycleWait(cycle_pose3d, diff);
		}

		if ( guiActivated )
			pthread_join(thr_gui, NULL);

		pthread_join(thr_camera, NULL);
		camRGB->stop_thread();

	} catch (const Ice::Exception& ex) {
		std::cerr << ex << std::endl;
		status = 1;

	} catch (const std::exception& ex) {
		std::cerr << ex.what() << std::endl;
		status = 1;

	} catch (const char* msg) {
		std::cerr << "Error: " << msg << std::endl;
		status = 1;

	}

	rgbTc.join();

	if ( ic )
		ic->destroy();

	return status;
} //end main
Esempio n. 7
0
int main(int argc, char * argv[])
{
    cout<<"testing start() and stop()... ";
    {
        gbxiceutilacfr::Thread* t=0;
        try
        {
            t = new TestThread;
            t->start();
        }
        catch (...)
        {
            cout<<"failed"<<endl<<"should be able to create thread"<<endl;
            exit(EXIT_FAILURE);
        }
        IceUtil::ThreadControl tc = t->getThreadControl();
        t->stop();
        tc.join();
        // do not delete t! it's already self-destructed.
    }
    cout<<"ok"<<endl;

    cout<<"testing start() and stop() with smart pointer ... ";
    {
        gbxiceutilacfr::ThreadPtr t;
        try
        {
            t = new TestThread;
            t->start();
        }
        catch (...)
        {
            cout<<"failed"<<endl<<"should be able to create thread"<<endl;
            exit(EXIT_FAILURE);
        }
        IceUtil::ThreadControl tc = t->getThreadControl();
        t->stop();
        tc.join();
    }
    cout<<"ok"<<endl;

    // alexm: is this test still needed?
    cout<<"testing Thread() with exceptions... ";
    {
        TestThreadWithThrow* t=0;
        try
        {
            t = new TestThreadWithThrow( true );
            cout<<"failed"<<endl<<"should not be able to create thread"<<endl;
            exit(EXIT_FAILURE);
        }
        catch (...)
        {
            // ok
        }
    }
    cout<<"ok"<<endl;

    cout<<"testing state machine ... ";
    {
        // works only with smart pointers because with dumb ones the threads self-destruct
        // and we can't examine their final state.
        gbxiceutilacfr::ThreadPtr t = new TestThreadWithExit;

        if ( t->isStopping()!=false || t->isAlive()!=false || t->isStarted()!=false ) {
            cout<<"failed"<<endl
                <<"should be in Starting state but internal states do not match:"<<endl
                <<"isStopping="<<(int)t->isStopping()<<" isAlive="<<(int)t->isAlive()<<" isStarted()="<<(int)t->isStarted()<<endl;
            exit(EXIT_FAILURE);
        }

        t->start();
        if ( t->isStopping()!=false || t->isAlive()!=true || t->isStarted()!=true ) {
            cout<<"failed"<<endl
                <<"should be in Started state but internal states do not match:"<<endl
                <<"isStopping="<<(int)t->isStopping()<<" isAlive="<<(int)t->isAlive()<<" isStarted()="<<(int)t->isStarted()<<endl;
            exit(EXIT_FAILURE);
        }

        t->stop();
        if ( t->isStopping()!=true || t->isAlive()!=true || t->isStarted()!=true ) {
            cout<<"failed"<<endl
                <<"should be in Stopping state but internal states do not match:"<<endl
                <<"isStopping="<<(int)t->isStopping()<<" isAlive="<<(int)t->isAlive()<<" isStarted()="<<(int)t->isStarted()<<endl;
            exit(EXIT_FAILURE);
        }

        IceUtil::ThreadControl tc = t->getThreadControl();
        // this ugly shit is to call one special function
        TestThreadWithExit* dumb = (TestThreadWithExit*)&(*t);
        dumb->exit();
        IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(150));
        // alexm: I think it self-destructs here. what can we tell about it?
        if ( t->isStopping()!=true || t->isAlive()!=false || t->isStarted()!=true ) {
            cout<<"failed"<<endl
                <<"should be in Stopped state but internal states do not match:"<<endl
                <<"isStopping="<<(int)t->isStopping()<<" isAlive="<<(int)t->isAlive()<<" isStarted()="<<(int)t->isStarted()<<endl;
            exit(EXIT_FAILURE);
        }
        tc.join();
    }
    cout<<"ok"<<endl;
    
    cout<<"testing waitForStop() ... ";
    {
        gbxiceutilacfr::ThreadPtr t = new TestThreadWithWait;
        t->start();
        if ( t->isAlive()!=true ) {
            cout<<"failed"<<endl
                <<"should be in Started state but internal states do not match:"<<endl
                <<"isStopping="<<(int)t->isStopping()<<" isAlive="<<(int)t->isAlive()<<" isStarted()="<<(int)t->isStarted()<<endl;
            exit(EXIT_FAILURE);
        }

        t->stop();
        IceUtil::ThreadControl tc = t->getThreadControl();
        IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(150));
        if ( t->isAlive()!=false ) {
            cout<<"failed"<<endl
                <<"should be in Stopped state but internal states do not match:"<<endl
                <<"isStopping="<<(int)t->isStopping()<<" isAlive="<<(int)t->isAlive()<<" isStarted()="<<(int)t->isStarted()<<endl;
            exit(EXIT_FAILURE);
        }
        tc.join();
    }
    cout<<"ok"<<endl;

    // alexm: don't know how to test this, because of self-destruction
//     cout<<"testing stopAndJoin() ... ";
//     {
//     }
//     cout<<"ok"<<endl;

    cout<<"testing stopAndJoin() with smart pointer ... ";
    {
        gbxiceutilacfr::ThreadPtr t = new TestThread;
        t->start();
        if ( t->isAlive()!=true ) {
            cout<<"failed"<<endl
                <<"should be in Started state but internal states do not match:"<<endl
                <<"isStopping="<<(int)t->isStopping()<<" isAlive="<<(int)t->isAlive()<<" isStarted()="<<(int)t->isStarted()<<endl;
            exit(EXIT_FAILURE);
        }

        gbxiceutilacfr::stopAndJoin( t );
        IceUtil::ThreadControl::sleep(IceUtil::Time::milliSeconds(150));
        if ( t->isAlive()!=false ) {
            cout<<"failed"<<endl
                <<"should be in Stopped state but internal states do not match:"<<endl
                <<"isStopping="<<(int)t->isStopping()<<" isAlive="<<(int)t->isAlive()<<" isStarted()="<<(int)t->isStarted()<<endl;
            exit(EXIT_FAILURE);
        }
    }
    cout<<"ok"<<endl;

    cout<<"testing checkedSleep() ... ";
    {
        gbxiceutilacfr::ThreadPtr t = new TestThreadWithNap;
        t->start();

        IceUtil::Time stopTime = IceUtil::Time::now();
        gbxiceutilacfr::stopAndJoin( t );
        IceUtil::Time joinTime = IceUtil::Time::now();
        cout<<"time to stop = "<<(joinTime-stopTime).toDuration()<<endl;

        if ( joinTime-stopTime > IceUtil::Time::seconds(1) ) {
            cout<<"failed"<<endl
                <<"should stop faster than in 1 second, time to stop ="<<(joinTime-stopTime).toDuration()<<endl;
            exit(EXIT_FAILURE);
        }
    }
    cout<<"ok"<<endl;

    return EXIT_SUCCESS;
}