[omniORB] Run an ORB in a thread but keep its reference

Laurent JOIGNY ljoigny@houston.westerngeco.slb.com
Wed Oct 30 18:05:02 2002


Hi,

I have some problems running an ORB in a thread and keep the ORB reference
for future use.

I think, it is maybe a problem of reference shared with my thread,
but give me your idea.

Due to the existing project I use mixed C and C++ so my server class use 
pthread for AIX and Linux portability.


I tried 2 things:

1)  - The current main process make the ORB_init call and activate the POA
      - Then the thread use the ORB instance in the class to call the run 
method

Result:   Segment fault in the thread at run()
Idea:       I am dealing with a wrong ORB reference reference

2)  - The current process do nothing but launching the thread
      - Then the thread use the ORB object of the class and call ORB_init 
and activate the POA

Result:   Segment fault in the thread at init()
Idea:       Something wrong with C++ object instance and C function


Do you have an idea? What will be the best to do to launch this server?

I beg you pardon if this is only a question of C/C++ use. I think it is a 
problem of orb reference
and c/c++ mixed code.

Thank you anyway for reading me.


------------------------------------- Base server class, use startServer 
and runServer for the thread (extract) ---------------------
....
#include <pthread.h>

class atDataSourceServer
{
public:
     atDataSourceServer(int argc, char *argv[]);
     virtual ~atDataSourceServer();

     /* Two different try to start the server */
     int startServer();
     int startServer2();

     int stopServer();
     bool isStarted();

     char *rebindSource(char *sourceName);
     char *getSourceBinded();

private:
     void setSourceName(char *name);

     /* Two function to execute in a thread using pthread */
     void *runServer(void* orb);
     void *runServer2(void* orb);

private:	
     char*  _source;

     int    _argc;
     char** _argv;

     /* ---- ORB reference for further use like bind a new object ---- */
     CORBA::ORB_var _orb;

     pthread_t     _thread;
     volatile bool _started;
};
------------------------------------- Base server class implemantation 
(extract) ------------------------------

/* The function use in the main C program */
extern "C" char* startupCorbaServer(int argc, char **argv) {

         char *iorStr = NULL;

         atDataSourceServer* aServer = new atDataSourceServer(argc, argv);

         /* Use one of them */
         aServer->startServer();
         //aServer->startServer2();

         iorStr = aServer->rebindSource("source44");
         printf("iorString: %s\n",iorStr);

         return iorStr;
}

atDataSourceServer::atDataSourceServer(int argc, char **argv) {
	...
	_orb = NULL;
}

int atDataSourceServer::startServer() {

	int res = 0;

	puts("startServer in");

	if (!isStarted())
	{
		try {
			// Initialize ORB
			printf("ORB init\n");
			_orb = CORBA::ORB_init(_argc, _argv, "omniORB3");

			// Get POA reference
			printf("POA init\n");
			CORBA::Object_var obj = _orb->resolve_initial_references("RootPOA");
			PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);

			// Activate POA manager
			printf("POA activate\n");
			PortableServer::POAManager_var pman = poa->the_POAManager();
			pman->activate();

		} catch (const CORBA::Exception & e) {
			printf("ERROR: startServer()\n");
			return 1;
		}
		
		printf("Create Thread\n");
                       printf("ORB run: %d\n", _orb);
		res = pthread_create(&_thread, NULL, atDataSourceServer::runServer, 
(void*)(_orb));
		_started = true;
	}	
	puts("startServer out");
	return res;
}

int atDataSourceServer::startServer2() {

	int res = 0;

	puts("startServer2 in");
			
	if (!isStarted())
	{
		printf("Create Thread\n");
		res = pthread_create(&_thread, NULL, atDataSourceServer::runServer2, NULL);
		_started = true;
	}
	puts("startServer2 out");
	return res;
}

char* atDataSourceServer::rebindSource(char *sourceName)
{
	.......
	/* Use the _orb to rebind a new object and get the IOR string */
	.......
}

void* atDataSourceServer::runServer(void* orb)
{
     puts("runServer in");

     try {

         // Run the ORB
         printf("ORB run: %ld\n", _orb);
         ((CORBA::ORB_ptr)_orb)->run();

     } catch (const CORBA::Exception & e1) {
          // Destroy the ORB
          puts("ORB destroy");
          ((CORBA::ORB_ptr)_orb)->destroy();
     }

     puts("Thread exit");
     pthread_exit(NULL);
}

void* atDataSourceServer::runServer2(void* nothing)
{
	puts("runServer2 in");

	try {
		// Initialize ORB
		printf("ORB init\n");
		_orb = CORBA::ORB_init(_argc, _argv, "omniORB3");

		// Get POA reference
		printf("POA init\n");
		CORBA::Object_var obj = _orb->resolve_initial_references("RootPOA");
		PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);

		// Activate POA manager
		printf("POA activate\n");
		PortableServer::POAManager_var pman = poa->the_POAManager();
		pman->activate();
	

		// Run the ORB
		printf("ORB run: %ld\n", _orb);
		((CORBA::ORB_ptr)_orb)->run();
	
	} catch (const CORBA::Exception & e1) {
		// Destroy the ORB
		puts("ORB destroy");
		((CORBA::ORB_ptr)_orb)->destroy();
	}

	puts("Thread exit");
	pthread_exit(NULL);
}



--
Laurent JOIGNY
WesternGeco / Schlumberger / Houston
phone : (+1) 713 689 6646