Index: XIOS3/trunk/src/node/context.cpp
===================================================================
--- XIOS3/trunk/src/node/context.cpp	(revision 2507)
+++ XIOS3/trunk/src/node/context.cpp	(revision 2547)
@@ -34,4 +34,5 @@
 #include "services.hpp"
 #include "contexts_manager.hpp"
+#include "thread_manager.hpp"
 #include <chrono>
 #include <random>
@@ -488,8 +489,4 @@
       }
       contextId_ = getId() ;
-      
-      attached_mode=true ;
-      if (!CXios::isUsingServer()) attached_mode=false ;
-
 
       string contextRegistryId=getId() ;
@@ -544,5 +541,4 @@
     MPI_Comm_dup(intraComm_, &intraCommClient);
     comms.push_back(intraCommClient);
-    // attached_mode=parentServerContext_->isAttachedMode() ; //ym probably inherited from source context
 
     CContextServer* server ;
@@ -578,8 +574,10 @@
     if (commRank==0)
     {
-      CXios::getServicesManager()->getServiceNbPartitions(poolId, serverId, 0, nbPartitions, true) ;
-      for(int i=0 ; i<nbPartitions; i++) CXios::getContextsManager()->createServerContext(poolId, serverId, i, getContextId()) ;
-    }
-    setCurrent(getId()) ; // getCurrent/setCurrent may be supress, it can cause a lot of trouble (attached ???)
+      while (! CXios::getServicesManager()->getServiceNbPartitions(poolId, serverId, 0, nbPartitions)) yield() ;
+      for(int i=0 ; i<nbPartitions; i++)  
+        while (!CXios::getContextsManager()->createServerContext(poolId, serverId, i, getContextId())) yield() ;
+    }
+    synchronize() ;
+    setCurrent(getId()) ; // getCurrent/setCurrent may be supress, it can cause a lot of trouble 
     MPI_Bcast(&nbPartitions, 1, MPI_INT, 0, intraComm_) ;
       
@@ -587,8 +585,10 @@
     for(int i=0 ; i<nbPartitions; i++)
     {
-      parentServerContext_->createIntercomm(poolId, serverId, i, getContextId(), intraComm_, interCommClient, interCommServer) ;
+      while (!parentServerContext_->createIntercomm(poolId, serverId, i, getContextId(), intraComm_, interCommClient, interCommServer)) yield() ;
       int type ; 
-      if (commRank==0) CXios::getServicesManager()->getServiceType(poolId, serverId, 0, type, true) ;
+      if (commRank==0) while (!CXios::getServicesManager()->getServiceType(poolId, serverId, 0, type)) yield();
+      synchronize() ;
       MPI_Bcast(&type,1,MPI_INT,0,intraComm_) ;
+
       string fullServerId=CXios::getContextsManager()->getServerContextName(poolId, serverId, i, type, getContextId()) ;
 
@@ -620,6 +620,5 @@
     if (serviceType_ == CServicesManager::CLIENT)
     {
-      if (attached_mode) createServerInterComm(CClient::getPoolRessource()->getId(), getContextId()+"_"+CXios::defaultWriterId, clientServers) ;
-      else if (CXios::usingServer2) createServerInterComm(CXios::defaultPoolId, CXios::defaultGathererId, clientServers) ;
+      if (CXios::usingServer2) createServerInterComm(CXios::defaultPoolId, CXios::defaultGathererId, clientServers) ;
       else createServerInterComm(CXios::defaultPoolId, CXios::defaultWriterId, clientServers) ;
       
@@ -629,6 +628,5 @@
       clientServers.clear() ;
    
-      if (attached_mode) createServerInterComm(CClient::getPoolRessource()->getId(), getContextId()+"_"+CXios::defaultReaderId, clientServers) ;
-      else createServerInterComm(CXios::defaultPoolId, CXios::defaultReaderId, clientServers) ;
+      createServerInterComm(CXios::defaultPoolId, CXios::defaultReaderId, clientServers) ;
       readerClientOut_.push_back(clientServers[0].second.first) ; 
       readerServerOut_.push_back(clientServers[0].second.second) ;
@@ -658,10 +656,10 @@
     else
     {
-      if (attached_mode) createServerInterComm(CClient::getPoolRessource()->getId(), getContextId()+"_"+serviceId, retClientServers) ;
-      else createServerInterComm(poolId, serviceId, retClientServers) ;
+      createServerInterComm(poolId, serviceId, retClientServers) ;
       for(auto& retClientServer : retClientServers)  clientServers.push_back(retClientServer.second) ;
       
       int serviceType ;
-      if (intraCommRank_==0) CXios::getServicesManager()->getServiceType(poolId, serviceId, 0, serviceType, true) ;
+      if (intraCommRank_==0) while(!CXios::getServicesManager()->getServiceType(poolId, serviceId, 0, serviceType)) yield(); 
+      synchronize() ;
       MPI_Bcast(&serviceType,1,MPI_INT,0,intraComm_) ;
       
@@ -694,10 +692,48 @@
   void CContext::globalEventLoop(void)
   {
-    lockContext() ;
-    CXios::getDaemonsManager()->eventLoop() ;
-    unlockContext() ;
-    setCurrent(getId()) ;
+    if (CThreadManager::isUsingThreads()) CThreadManager::yield();
+    else
+    {
+      lockContext() ;
+      CXios::getDaemonsManager()->eventLoop() ;
+      unlockContext() ;
+      setCurrent(getId()) ;
+    }
   }
 
+  void CContext::yield(void)
+  {
+    if (CThreadManager::isUsingThreads()) 
+    {
+      CThreadManager::yield();
+      setCurrent(getId()) ;
+    }
+    else
+    {
+      lockContext() ;
+      CXios::getDaemonsManager()->eventLoop() ;
+      unlockContext() ;
+      setCurrent(getId()) ;
+    }
+  }
+
+  void CContext::synchronize(void)
+  {
+    bool out, finished; 
+    size_t timeLine=timeLine_ ;
+      
+    timeLine_++ ;
+    eventScheduler_->registerEvent(timeLine, hashId_) ;
+      
+    out = eventScheduler_->queryEvent(timeLine,hashId_) ;
+    if (out) eventScheduler_->popEvent() ;
+    while (!out)
+    {
+      yield() ;
+      out = eventScheduler_->queryEvent(timeLine,hashId_) ;
+      if (out) eventScheduler_->popEvent() ;
+    }
+  }
+  
   bool CContext::scheduledEventLoop(bool enableEventsProcessing) 
   {
@@ -761,10 +797,11 @@
        if (couplerOutClient_.find(fullContextId)==couplerOutClient_.end()) 
        {
-         bool ok=CXios::getContextsManager()->getContextLeader(fullContextId, contextLeader, getIntraComm()) ;
+         while(!CXios::getContextsManager()->getContextLeader(fullContextId, contextLeader, getIntraComm())) yield();
+         synchronize() ;
      
          MPI_Comm interComm, interCommClient, interCommServer  ;
          MPI_Comm intraCommClient, intraCommServer ;
 
-         if (ok) MPI_Intercomm_create(getIntraComm(), 0, CXios::getXiosComm(), contextLeader, 0, &interComm) ;
+         MPI_Intercomm_create(getIntraComm(), 0, CXios::getXiosComm(), contextLeader, 0, &interComm) ;
 
         MPI_Comm_dup(intraComm_, &intraCommClient) ;
@@ -783,10 +820,11 @@
     else if (couplerInClient_.find(fullContextId)==couplerInClient_.end())
     {
-      bool ok=CXios::getContextsManager()->getContextLeader(fullContextId, contextLeader, getIntraComm()) ;
+      while(!CXios::getContextsManager()->getContextLeader(fullContextId, contextLeader, getIntraComm())) yield() ;
+      synchronize() ;
      
        MPI_Comm interComm, interCommClient, interCommServer  ;
        MPI_Comm intraCommClient, intraCommServer ;
 
-       if (ok) MPI_Intercomm_create(getIntraComm(), 0, CXios::getXiosComm(), contextLeader, 0, &interComm) ;
+       MPI_Intercomm_create(getIntraComm(), 0, CXios::getXiosComm(), contextLeader, 0, &interComm) ;
 
        MPI_Comm_dup(intraComm_, &intraCommClient) ;
@@ -824,5 +862,6 @@
           couplersInFinalized=true ;
           for(auto& couplerOutClient : couplerOutClient_) couplersInFinalized &= isCouplerInContextFinalized(couplerOutClient.second) ; 
-          globalEventLoop() ;
+          if (CThreadManager::isUsingThreads()) yield() ;
+          else globalEventLoop() ;
         } while (!couplersInFinalized) ;
 
@@ -900,25 +939,35 @@
           info(100)<<"DEBUG: context "<<getId()<<" release client reader ok"<<endl ;
         }
+        closeAllFile() ;
       }
       else if (serviceType_==CServicesManager::GATHERER)
       {
-         for(auto& client : writerClientOut_)
-         {
-           client->finalize();
-           bool bufferReleased;
-           do
-           {
-             client->eventLoop();
-             bufferReleased = !client->havePendingRequests();
-           } while (!bufferReleased);
+        CContextClient* client ;
+        CContextServer* server ;
+        
+        for(int n=0; n<writerClientOut_.size() ; n++)
+        {
+          client=writerClientOut_[n] ;
+          server=writerServerOut_[n] ;
+        
+          client->finalize();
+          bool bufferReleased;
+          do
+          {
+            client->eventLoop();
+            bufferReleased = !client->havePendingRequests();
+          } while (!bufferReleased);
            
-           bool notifiedFinalized=false ;
-           do
-           {
-             notifiedFinalized=client->isNotifiedFinalized() ;
-           } while (!notifiedFinalized) ;
-           client->releaseBuffers();
+          bool notifiedFinalized=false ;
+          do
+          {
+            notifiedFinalized=client->isNotifiedFinalized() ;
+          } while (!notifiedFinalized) ;
+          server->releaseBuffers();
+          client->releaseBuffers();
          }
          closeAllFile();
+         writerClientIn_[0]->releaseBuffers();
+         writerServerIn_[0]->releaseBuffers();         
          //ym writerClientIn & writerServerIn not released here ==> to check !!
       }
@@ -957,20 +1006,33 @@
    void CContext::setDefaultServices(void)
    {
-     defaultPoolWriterId_ = CXios::defaultPoolId ;
-     defaultPoolReaderId_ = CXios::defaultPoolId ;
-     defaultPoolGathererId_ = CXios::defaultPoolId ;
-     defaultWriterId_ = CXios::defaultWriterId ;
-     defaultReaderId_ = CXios::defaultReaderId ;
-     defaultGathererId_ = CXios::defaultGathererId ;
-     defaultUsingServer2_ = CXios::usingServer2 ;
+     if (!CXios::isUsingServer())
+     {
+       defaultPoolWriterId_ = CXios::defaultPoolId ;
+       defaultPoolReaderId_ = CXios::defaultPoolId ;
+       defaultPoolGathererId_ = CXios::defaultPoolId ;
+       defaultWriterId_ = "attached" ;
+       defaultReaderId_ = "attached" ;
+       defaultGathererId_ =  "attached" ;
+       defaultUsingServer2_ = false;
+     }
+     else
+     {
+       defaultPoolWriterId_ = CXios::defaultPoolId ;
+       defaultPoolReaderId_ = CXios::defaultPoolId ;
+       defaultPoolGathererId_ = CXios::defaultPoolId ;
+       defaultWriterId_ = CXios::defaultWriterId ;
+       defaultReaderId_ = CXios::defaultReaderId ;
+       defaultGathererId_ = CXios::defaultGathererId ;
+       defaultUsingServer2_ = CXios::usingServer2 ;
      
-     if (!default_pool.isEmpty())  defaultPoolWriterId_ = defaultPoolReaderId_= defaultPoolGathererId_= default_pool ;
-     if (!default_pool_writer.isEmpty()) defaultPoolWriterId_ = default_pool_writer ;
-     if (!default_pool_reader.isEmpty()) defaultPoolReaderId_ = default_pool_reader ;
-     if (!default_pool_gatherer.isEmpty()) defaultPoolGathererId_ = default_pool_gatherer ;
-     if (!default_writer.isEmpty()) defaultWriterId_ = default_writer ;
-     if (!default_reader.isEmpty()) defaultWriterId_ = default_reader ;
-     if (!default_gatherer.isEmpty()) defaultGathererId_ = default_gatherer ;
-     if (!default_using_server2.isEmpty()) defaultUsingServer2_ = default_using_server2 ;
+       if (!default_pool.isEmpty())  defaultPoolWriterId_ = defaultPoolReaderId_= defaultPoolGathererId_= default_pool ;
+       if (!default_pool_writer.isEmpty()) defaultPoolWriterId_ = default_pool_writer ;
+       if (!default_pool_reader.isEmpty()) defaultPoolReaderId_ = default_pool_reader ;
+       if (!default_pool_gatherer.isEmpty()) defaultPoolGathererId_ = default_pool_gatherer ;
+       if (!default_writer.isEmpty()) defaultWriterId_ = default_writer ;
+       if (!default_reader.isEmpty()) defaultWriterId_ = default_reader ;
+       if (!default_gatherer.isEmpty()) defaultGathererId_ = default_gatherer ;
+       if (!default_using_server2.isEmpty()) defaultUsingServer2_ = default_using_server2 ;
+     }
    }
 
