inline bool interprocess_condition::do_timed_wait(bool tout_enabled,
                                     const boost::posix_time::ptime &abs_time, 
                                     interprocess_mutex &mut)
{
   boost::posix_time::ptime now = microsec_clock::universal_time();
   
   if(tout_enabled){
      if(now >= abs_time) return false;
   }

   typedef boost::interprocess::scoped_lock<interprocess_mutex> InternalLock;
   //The enter interprocess_mutex guarantees that while executing a notification, 
   //no other thread can execute the do_timed_wait method. 
   {
      //---------------------------------------------------------------
      InternalLock lock;
      if(tout_enabled){
         InternalLock dummy(m_enter_mut, abs_time);
         lock = detail::move_impl(dummy);
      }
      else{
         InternalLock dummy(m_enter_mut);
         lock = detail::move_impl(dummy);
      }

      if(!lock)
         return false;
      //---------------------------------------------------------------
      //We increment the waiting thread count protected so that it will be
      //always constant when another thread enters the notification logic.
      //The increment marks this thread as "waiting on interprocess_condition"
      detail::atomic_inc32(const_cast<boost::uint32_t*>(&m_num_waiters));

      //We unlock the external interprocess_mutex atomically with the increment
      mut.unlock();
   }

   //By default, we suppose that no timeout has happened
   bool timed_out  = false, unlock_enter_mut= false;
   
   //Loop until a notification indicates that the thread should 
   //exit or timeout occurs
   while(1){
      //The thread sleeps/spins until a interprocess_condition commands a notification
      //Notification occurred, we will lock the checking interprocess_mutex so that
      while(detail::atomic_read32(&m_command) == SLEEP){
         detail::thread_yield();

         //Check for timeout
         if(tout_enabled){
            now = microsec_clock::universal_time();

            if(now >= abs_time){
               //If we can lock the interprocess_mutex it means that no notification
               //is being executed in this interprocess_condition variable
               timed_out = m_enter_mut.try_lock();

               //If locking fails, indicates that another thread is executing
               //notification, so we play the notification game
               if(!timed_out){
                  //There is an ongoing notification, we will try again later
                  continue;
               }
               //No notification in execution, since enter interprocess_mutex is locked. 
               //We will execute time-out logic, so we will decrement count, 
               //release the enter interprocess_mutex and return false.
               break;
            }
         }
      }

      //If a timeout occurred, the interprocess_mutex will not execute checking logic
      if(tout_enabled && timed_out){
         //Decrement wait count
         detail::atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
         unlock_enter_mut = true;
         break;
      }
      else{
         //Notification occurred, we will lock the checking interprocess_mutex so that
         //if a notify_one notification occurs, only one thread can exit
        //---------------------------------------------------------------
         InternalLock lock;
         if(tout_enabled){
            InternalLock dummy(m_check_mut, abs_time);
            lock = detail::move_impl(dummy);
         }
         else{
            InternalLock dummy(m_check_mut);
            lock = detail::move_impl(dummy);
         }

         if(!lock)
            return false;
         //---------------------------------------------------------------
         boost::uint32_t result = detail::atomic_cas32
                        (const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ONE);
         if(result == SLEEP){
            //Other thread has been notified and since it was a NOTIFY one
            //command, this thread must sleep again
            continue;
         }
         else if(result == NOTIFY_ONE){
            //If it was a NOTIFY_ONE command, only this thread should  
            //exit. This thread has atomically marked command as sleep before
            //so no other thread will exit.
            //Decrement wait count.
            unlock_enter_mut = true;
            detail::atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
            break;
         }
         else{
            //If it is a NOTIFY_ALL command, all threads should return 
            //from do_timed_wait function. Decrement wait count. 
            unlock_enter_mut = 1 == detail::atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
            //Check if this is the last thread of notify_all waiters
            //Only the last thread will release the interprocess_mutex
            if(unlock_enter_mut){
               detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ALL);
            }
            break;
         }
      }
   }

   //Unlock the enter interprocess_mutex if it is a single notification, if this is 
   //the last notified thread in a notify_all or a timeout has occurred
   if(unlock_enter_mut){
      m_enter_mut.unlock();
   }

   //Lock external again before returning from the method
   mut.lock();
   return !timed_out;
}
Esempio n. 2
0
File: dane.c Progetto: Exim/exim
static void dummy(int x) { dummy(x-1); }
 void flatten(TreeNode *root) {
     TreeNode dummy(-1);
     TreeNode *tail = &dummy;
     flattenTree(root, tail);
     root = dummy.right;
 }
void OXMLi_ListenerState_Styles::startElement (OXMLi_StartElementRequest * rqst)
{
	UT_return_if_fail( _error_if_fail(rqst != NULL) );

	if (nameMatches(rqst->pName, NS_W_KEY, "docDefaults")) {
		m_pCurrentStyle = new OXML_Style("Normal", "Normal");
		m_pCurrentStyle->setAttribute(PT_TYPE_ATTRIBUTE_NAME, "P");
		m_pCurrentStyle->setAttribute(PT_FOLLOWEDBY_ATTRIBUTE_NAME, "Current Settings");

		rqst->handled = true;
	} else if (nameMatches(rqst->pName, NS_W_KEY, "rPr") || nameMatches(rqst->pName, NS_W_KEY, "pPr")) {
		//Push a dummy element onto the stack to collect the formatting for the current style.
		OXML_SharedElement dummy(new OXML_Element_Paragraph(""));
		rqst->stck->push(dummy);

		rqst->handled = true;

	} else if (nameMatches(rqst->pName, NS_W_KEY, "tblPr")) {
		//Push a dummy element onto the stack to collect the formatting for the current style.
		OXML_SharedElement dummy(new OXML_Element_Table(""));
		rqst->stck->push(dummy);
		//don't handle the request so that table listener state can adjust its internal state

	} else if (nameMatches(rqst->pName, NS_W_KEY, "trPr")) {
		//Push a dummy element onto the stack to collect the formatting for the current style.
		OXML_SharedElement dummy(new OXML_Element_Row("", NULL));
		rqst->stck->push(dummy);
		//don't handle the request so that table listener state can adjust its internal state

	} else if (nameMatches(rqst->pName, NS_W_KEY, "tcPr")) {
		//Push a dummy element onto the stack to collect the formatting for the current style.
		OXML_SharedElement dummy(new OXML_Element_Cell("", NULL, NULL, 0,0,0,0));
		rqst->stck->push(dummy);
		//don't handle the request so that table listener state can adjust its internal state

	} else if (nameMatches(rqst->pName, NS_W_KEY, "style")) {
		const gchar * id = attrMatches(NS_W_KEY, "styleId", rqst->ppAtts);
		const gchar * type = attrMatches(NS_W_KEY, "type", rqst->ppAtts);
		UT_return_if_fail( _error_if_fail( id != NULL ));
		if (!strcmp(id, "Normal")) id = "_Normal"; //Cannot interfere with document defaults
		m_pCurrentStyle = new OXML_Style(id, ""); //TODO: wrap this in try/catch

		if (!type || !*type) {
			// default to paragraph in the case of a missing/blank attribute
			// (as specified by the spec: 2.7.3.17)
			type = "P";
		}
		else if (!strcmp(type, "character")) {
			type = "C"; //Type is C for "character"
		} else {
			type = "P"; //Type is P for "paragraph", "numbering", and "table"
		}
		m_pCurrentStyle->setAttribute(PT_TYPE_ATTRIBUTE_NAME, type);

		rqst->handled = true;
	} else if (	nameMatches(rqst->pName, NS_W_KEY, "name") ||
				nameMatches(rqst->pName, NS_W_KEY, "basedOn") ||
				nameMatches(rqst->pName, NS_W_KEY, "next")) {
		const gchar * val = attrMatches(NS_W_KEY, "val", rqst->ppAtts);
		UT_return_if_fail( _error_if_fail( m_pCurrentStyle != NULL && val != NULL ));
		if (!strcmp(val, "Normal")) val = "_Normal"; //Cannot interfere with document defaults

		if (nameMatches(rqst->pName, NS_W_KEY, "name")) {
			m_pCurrentStyle->setName(val);
		} else if (nameMatches(rqst->pName, NS_W_KEY, "basedOn")) {
			//For now, we use the ID as reference, until all styles have been parsed
			m_pCurrentStyle->setAttribute(PT_BASEDON_ATTRIBUTE_NAME, val);
		} else if (nameMatches(rqst->pName, NS_W_KEY, "next")) {
			//For now, we use the ID as reference, until all styles have been parsed
			m_pCurrentStyle->setAttribute(PT_FOLLOWEDBY_ATTRIBUTE_NAME, val);
		}
		rqst->handled = true;
	}
}
Esempio n. 5
0
      void
      ForceBoundary::applyBoundary()
      {

        std::list<Molecule *> mol;
        std::list<Molecule *>::iterator mi;
        Molecule *m;
        double distanceVector[3];
        //double cornerA[3];
        //double cornerB[3];
        double f[3];
        f[0] = 0;

        /*	cornerA[X] = bBoxMin[X];
         cornerA[Y] = bBoxMin[Y];
         cornerA[Z] = bBoxMin[Z];

         cornerB[X] = bBoxMin[X]+_borderSize[X];
         cornerB[Y] = bBoxMax[Y];
         cornerB[Z] = bBoxMax[Z];


         _moleculeContainer->getRegion(cornerA, cornerB,mol);*/
        getLower(X, mol);
        for (mi = mol.begin(); mi != mol.end(); mi++)
          {
            m = (*mi);
            f[X] = calcFm(m->r(X) - bBoxMin[X]) * fmplus;
            f[Y] = 0;
            f[Z] = 0;
            /*	double r =m->r(X)-bBoxMin[X];
             if(r<0.5) {
             std::cout << m->F(0) << " "<<m->F(1) << " "<<m->F(2) << " X"<< std::endl;

             }*/

            m->Fadd(f);
            /*	if(r<0.5) {
             std::cout << r << " " <<m->F(0) << " "<<m->F(1) << " "<<m->F(2) << " X2"<< std::endl;

             }*/
            if (_RBinFB)
              {
                Molecule dummy(*m);
                dummy.move(X, -2 * m->r(X));
                double dd = dummy.dist2(*m, distanceVector);
                if (dd < 2.0)
                  {
                    std::cout << m->F(0) << " " << m->F(1) << " " << m->F(2)
                        << " X" << std::endl;
                    _particlePairsHandler->processPair(*m, dummy,
                        distanceVector, MOLECULE_HALOMOLECULE, dd, (dd
                            < _LJCutoffRadiusSquare));
                    m->calcFM();
                    std::cout << dd << " " << m->F(0) << " " << m->F(1) << " "
                        << m->F(2) << " X2" << std::endl;
                  }

              }

          }

        mol.clear();
        /*cornerA[X] = bBoxMax[X]-_borderSize[X];
         cornerB[X] = bBoxMax[X];

         _moleculeContainer->getRegion(cornerA, cornerB,mol);*/
        getUpper(X, mol);
        for (mi = mol.begin(); mi != mol.end(); mi++)
          {
            m = (*mi);
            f[X] = -calcFm(bBoxMax[X] - m->r(X)) * fmplus;
            if (_streamOutDir == X)
              f[X] *= _fStreamDamping;
            if (!_fStream || _streamOutDir != X)
              m->Fadd(f);
            if (_RBinFB && _streamOutDir != X)
              {
                Molecule dummy(*m);
                dummy.move(X, 2 * (m->r(X) - _domain->getGlobalLength(X)));
                double dd = dummy.dist2(*m, distanceVector);
                _particlePairsHandler->processPair(*m, dummy, distanceVector,
                    MOLECULE_HALOMOLECULE, dd, (dd < _LJCutoffRadiusSquare));
                //std::cout << dummy.F(0) << " "<<dummy.F(1) << " "<<dummy.F(2) << " -X"<< std::endl;

              }
          }

        mol.clear();
        /*	cornerA[X] = bBoxMin[X];
         cornerB[X] = bBoxMax[X];
         cornerB[Y] = bBoxMin[Y]+_borderSize[Y];

         _moleculeContainer->getRegion(cornerA, cornerB,mol);*/
        getLower(Y, mol);
        for (mi = mol.begin(); mi != mol.end(); mi++)
          {
            m = (*mi);
            f[X] = 0;
            f[Y] = calcFm(m->r(Y) - bBoxMin[Y]) * fmplus;
            m->Fadd(f);
            if (_RBinFB)
              {
                Molecule dummy(*m);
                dummy.move(Y, -2 * m->r(Y));
                double dd = dummy.dist2(*m, distanceVector);
                _particlePairsHandler->processPair(*m, dummy, distanceVector,
                    MOLECULE_HALOMOLECULE, dd, (dd < _LJCutoffRadiusSquare));
              }
          }

        mol.clear();
        /*	cornerA[Y] = bBoxMax[Y]-_borderSize[Y];
         cornerB[Y] = bBoxMax[Y];

         _moleculeContainer->getRegion(cornerA, cornerB,mol);*/
        getUpper(Y, mol);
        for (mi = mol.begin(); mi != mol.end(); mi++)
          {
            m = (*mi);
            f[Y] = -calcFm(bBoxMax[Y] - m->r(Y)) * fmplus;
            if (_streamOutDir == Y)
              f[Y] *= _fStreamDamping;
            if (!_fStream || _streamOutDir != Y)
              m->Fadd(f);
            if (_RBinFB && _streamOutDir != Y)
              {
                Molecule dummy(*m);
                dummy.move(Y, 2 * (m->r(Y) - _domain->getGlobalLength(Y)));
                double dd = dummy.dist2(*m, distanceVector);
                _particlePairsHandler->processPair(*m, dummy, distanceVector,
                    MOLECULE_HALOMOLECULE, dd, (dd < _LJCutoffRadiusSquare));
                //	 std::cout << dummy.F(0) << " "<<dummy.F(1) << " "<<dummy.F(2) << " -Y"<< std::endl;

              }
          }
        if (_dim == 3)
          {
            mol.clear();
            /*cornerA[Y] = bBoxMin[Y];
             cornerB[Y] = bBoxMax[Y];
             cornerB[Z] = bBoxMin[Z]+_borderSize[Z];

             _moleculeContainer->getRegion(cornerA, cornerB,mol);*/
            getLower(Z, mol);

            for (mi = mol.begin(); mi != mol.end(); mi++)
              {
                m = (*mi);
                f[Y] = 0;
                f[Z] = calcFm(m->r(Z) - bBoxMin[Z]) * fmplus;
                m->Fadd(f);
                if (_RBinFB)
                  {
                    Molecule dummy(*m);
                    dummy.move(Z, -2 * m->r(Z));
                    double dd = dummy.dist2(*m, distanceVector);
                    _particlePairsHandler->processPair(*m, dummy,
                        distanceVector, MOLECULE_HALOMOLECULE, dd, (dd
                            < _LJCutoffRadiusSquare));
                    // std::cout << dummy.F(0) << " "<<dummy.F(1) << " "<<dummy.F(2) << " "<< std::endl;
                  }
              }

            mol.clear();
            /*cornerA[Z] = bBoxMax[Z]-_borderSize[Z];
             cornerB[Z] = bBoxMax[Z];

             _moleculeContainer->getRegion(cornerA, cornerB,mol);*/
            getUpper(Z, mol);
            for (mi = mol.begin(); mi != mol.end(); mi++)
              {
                m = (*mi);
                f[Z] = -calcFm(bBoxMax[Z] - m->r(Z)) * fmplus;
                if (_streamOutDir == Z)
                  f[Z] *= _fStreamDamping;
                if (!_fStream || _streamOutDir != Z)
                  m->Fadd(f);
                if (_RBinFB && _streamOutDir != Z)
                  {
                    Molecule dummy(*m);
                    dummy.move(Z, 2 * (m->r(Z) - _domain->getGlobalLength(Z)));
                    double dd = dummy.dist2(*m, distanceVector);
                    _particlePairsHandler->processPair(*m, dummy,
                        distanceVector, MOLECULE_HALOMOLECULE, dd, (dd
                            < _LJCutoffRadiusSquare));

                  }
              }
          }

      }
RockPhysicsInversion4D::RockPhysicsInversion4D(NRLib::Vector                      priorMean,
                                               NRLib::Matrix                      priorCov,
                                               NRLib::Matrix                      posteriorCov,
                                               std::vector<std::vector<double> >  mSamp)
{
  nf_.resize(4);
  nf_[0] = 60;
  nf_[1] = 60;
  nf_[2] = 60;
  nf_[3] = 60;
  nfp_= 135;

  fftplan1_ = rfftwnd_create_plan(1, &nfp_, FFTW_REAL_TO_COMPLEX, FFTW_ESTIMATE | FFTW_IN_PLACE);
  fftplan2_ = rfftwnd_create_plan(1, &nfp_, FFTW_COMPLEX_TO_REAL, FFTW_ESTIMATE | FFTW_IN_PLACE);

  v_.resize(4,6);
  SolveGEVProblem(priorCov,posteriorCov, v_);
  NRLib::Matrix tmp;
  NRLib::Matrix priorCovF;
  NRLib::Matrix posteriorCovF;

  meanf_        = priorMean*v_;
  tmp           = priorCov*v_;
  priorCovF     = transpose(v_)*tmp;
  tmp           = posteriorCov*v_;
  posteriorCovF = transpose(v_)*tmp;

  int nSamp = mSamp[0].size();

  NRLib::Vector m(6);
  for(int k=0;k<6;k++)
      m(k)=mSamp[k][0];

  maxf_=m*v_;
  minf_=m*v_;

  for(int i=0;i<nSamp;i++)
  {
    NRLib::Vector f;
    for(int k=0;k<6;k++)
      m(k)=mSamp[k][i];
    f=m*v_;
    for(int k=0;k<4;k++)
    {
      if(minf_(k) > f(k))
        minf_(k)=f(k);
      if(maxf_(k) < f(k))
        maxf_(k)=f(k);
    }
  }

  for(int k=0;k<4;k++)
  {
    minf_(k)-=3*sqrt(posteriorCovF(k,k));
    maxf_(k)+=3*sqrt(posteriorCovF(k,k));
  }

  smoothingFilter_.resize(4);
  priorDistribution_.resize(4);

  for(int i=0;i<4;i++)
  {
    double df=(maxf_(i)-minf_(i))/double(nf_[i]);
    priorDistribution_[i]=MakeGaussKernel(meanf_(i),priorCovF(i,i),minf_(i),df,nfp_);
    smoothingFilter_[i]=MakeSmoothingFilter(posteriorCovF(i,i),df);
  }

  allocatePredictionTables( );

  std::vector<double> dummy(nSamp);
  for(int i=0;i<nSamp;i++)
    dummy[i]=1.0;

  fillInTable( mSamp,dummy,0);
  DivideAndSmoothTable(0,priorDistribution_,smoothingFilter_);
}