Exemple #1
0
void
OpenDDS::DCPS::DataLinkSet::remove_link(const DataLink_rch& link)
{
  DBG_ENTRY_LVL("DataLinkSet", "remove_link", 6);
  GuardType guard1(this->lock_);
  if (unbind(map_, link->id()) != 0) {
    // Just report to the log that we tried.
    VDBG((LM_DEBUG,
          ACE_TEXT("(%P|%t) DataLinkSet::remove_links: ")
          ACE_TEXT("link_id %d not found in map.\n"),
          link->id()));
  }
}
void deadLock(CriticalData& a, CriticalData& b){

  std::unique_lock<std::mutex>guard1(a.mut,std::defer_lock);
  std::cout << "Thread: " << std::this_thread::get_id() << " first mutex" <<  std::endl;

  std::this_thread::sleep_for(std::chrono::milliseconds(1));

  std::unique_lock<std::mutex>guard2(b.mut,std::defer_lock);
  std::cout << "    Thread: " << std::this_thread::get_id() << " second mutex" <<  std::endl;

  std::cout << "        Thread: " << std::this_thread::get_id() << " get both mutex" << std::endl;
  std::lock(guard1,guard2);
  // do something with a and b
}
 // acquires both locks, returns nullptr on failure
 pointer take_tail() {
   pointer result = nullptr;
   unique_node_ptr last;
   { // lifetime scope of guards
     lock_guard guard1(head_lock_);
     lock_guard guard2(tail_lock_);
     CAF_ASSERT(head_ != nullptr);
     last.reset(tail_.load());
     if (last.get() == head_.load()) {
       last.release();
       return nullptr;
     }
     result = last->value;
     tail_ = find_predecessor(last.get());
     CAF_ASSERT(tail_ != nullptr);
     tail_.load()->next = nullptr;
   }
   return result;
 }
Exemple #4
0
BACIComponent::~BACIComponent()
{
  ACS_TRACE("baci::BACIComponent::~BACIComponent");

  // set destruction flag
  inDestructionState_m = true;

  if (threadManager_mp!=0)
      {
      threadManager_mp->terminateAll();
      }

  // the threads are no more alive, so I can ...

  // remove all left actions (in case it was canceled)
  BACIAction *action_p;
  ThreadSyncGuard guard(&actionQueueMutex_m);
  while (getActionCount() > 0)
    {
      action_p = popAction();
      if (action_p)
	  {
	  delete action_p;
	  }
    }
  guard.release();

  // remove all left callbacks
  BACICallback* callback_p;
  ThreadSyncGuard guard1(&callbackTableMutex_m);
  int h = callbackTable_m.first();
  while (h != 0)
    {
      int nh = callbackTable_m.next(h);
      callback_p = callbackTable_m[h];
      callbackTable_m.deallocate(h);
      delete callback_p;
      h = nh;
    }
  guard1.release();

  ACS_DEBUG_PARAM("baci::BACIComponent::~BACIComponent", "Component '%s' destroyed.", name_m.c_str());
}//~BACIComponent
 // acquires both locks, returns nullptr on failure
 pointer take_tail() {
   pointer result = nullptr;
   unique_node_ptr last;
   { // lifetime scope of guards
     lock_guard guard1(m_head_lock);
     lock_guard guard2(m_tail_lock);
     CAF_REQUIRE(m_head != nullptr);
     last.reset(m_tail.load());
     if (last.get() == m_head.load()) {
       last.release();
       return nullptr;
     }
     result = last->value;
     m_tail = find_predecessor(last.get());
     CAF_REQUIRE(m_tail != nullptr);
     m_tail.load()->next = nullptr;
   }
   return result;
 }
 // acquires both locks
 void prepend(pointer value) {
   CAF_ASSERT(value != nullptr);
   node* tmp = new node(value);
   node* first = nullptr;
   // acquire both locks since we might touch last_ too
   lock_guard guard1(head_lock_);
   lock_guard guard2(tail_lock_);
   first = head_.load();
   CAF_ASSERT(first != nullptr);
   auto next = first->next.load();
   // first_ always points to a dummy with no value,
   // hence we put the new element second
   if (next) {
     CAF_ASSERT(first != tail_);
     tmp->next = next;
   } else {
     // queue is empty
     CAF_ASSERT(first == tail_);
     tail_ = tmp;
   }
   first->next = tmp;
 }
 // acquires both locks
 void prepend(pointer value) {
   CAF_REQUIRE(value != nullptr);
   node* tmp = new node(value);
   node* first = nullptr;
   // acquire both locks since we might touch m_last too
   lock_guard guard1(m_head_lock);
   lock_guard guard2(m_tail_lock);
   first = m_head.load();
   CAF_REQUIRE(first != nullptr);
   auto next = first->next.load();
   // m_first always points to a dummy with no value,
   // hence we put the new element second
   if (next == nullptr) {
     // queue is empty
     CAF_REQUIRE(first == m_tail);
     m_tail = tmp;
   } else {
     CAF_REQUIRE(first != m_tail);
     tmp->next = next;
   }
   first->next = tmp;
 }
/// add axioms corresponding to the String.compareTo java function
/// \par parameters: function application with two string arguments
/// \return a integer expression
exprt string_constraint_generatort::add_axioms_for_compare_to(
  const function_application_exprt &f)
{
  string_exprt s1=add_axioms_for_string_expr(args(f, 2)[0]);
  string_exprt s2=add_axioms_for_string_expr(args(f, 2)[1]);
  const typet &return_type=f.type();
  symbol_exprt res=fresh_symbol("compare_to", return_type);
  typet index_type=s1.length().type();

  // In the lexicographic comparison, x is the first point where the two
  // strings differ.
  // We add axioms:
  // a1 : res==0 => |s1|=|s2|
  // a2 : forall i<|s1|. s1[i]==s2[i]
  // a3 : exists x.
  // res!=0 ==> x> 0 &&
  //   ((|s1| <= |s2| &&x<|s1|) || (|s1| >= |s2| &&x<|s2|)
  //   &&res=s1[x]-s2[x] )
  // || cond2:
  //   (|s1|<|s2| &&x=|s1|) || (|s1| > |s2| &&x=|s2|) &&res=|s1|-|s2|)
  // a4 : forall i<x. res!=0 => s1[i]=s2[i]

  assert(return_type.id()==ID_signedbv);

  equal_exprt res_null=equal_exprt(res, from_integer(0, return_type));
  implies_exprt a1(res_null, s1.axiom_for_has_same_length_as(s2));
  axioms.push_back(a1);

  symbol_exprt i=fresh_univ_index("QA_compare_to", index_type);
  string_constraintt a2(i, s1.length(), res_null, equal_exprt(s1[i], s2[i]));
  axioms.push_back(a2);

  symbol_exprt x=fresh_exist_index("index_compare_to", index_type);
  equal_exprt ret_char_diff(
    res,
    minus_exprt(
      typecast_exprt(s1[x], return_type),
      typecast_exprt(s2[x], return_type)));
  equal_exprt ret_length_diff(
    res,
    minus_exprt(
      typecast_exprt(s1.length(), return_type),
      typecast_exprt(s2.length(), return_type)));
  or_exprt guard1(
    and_exprt(s1.axiom_for_is_shorter_than(s2),
              s1.axiom_for_is_strictly_longer_than(x)),
    and_exprt(s1.axiom_for_is_longer_than(s2),
              s2.axiom_for_is_strictly_longer_than(x)));
  and_exprt cond1(ret_char_diff, guard1);
  or_exprt guard2(
    and_exprt(s2.axiom_for_is_strictly_longer_than(s1),
              s1.axiom_for_has_length(x)),
    and_exprt(s1.axiom_for_is_strictly_longer_than(s2),
              s2.axiom_for_has_length(x)));
  and_exprt cond2(ret_length_diff, guard2);

  implies_exprt a3(
    not_exprt(res_null),
    and_exprt(
      binary_relation_exprt(x, ID_ge, from_integer(0, return_type)),
      or_exprt(cond1, cond2)));
  axioms.push_back(a3);

  string_constraintt a4(i, x, not_exprt(res_null), equal_exprt(s1[i], s2[i]));
  axioms.push_back(a4);

  return res;
}
Exemple #9
0
bool
MemoryManager::checkIfSameChunk(Location addr1, Location addr2, size_t typeSize, RuntimeViolation::Type violation) const
{
  RuntimeViolation::Type access_violation = violation;

  if (access_violation != RuntimeViolation::NONE) access_violation = RuntimeViolation::INVALID_READ;

  const MemoryType*      mem1 = checkLocation(addr1, typeSize, access_violation);
  const bool             sameChunk = (mem1 && mem1->containsMemArea(addr2, typeSize));

  // check that addr1 and addr2 point to the same base location
  if (! sameChunk)
  {
    const MemoryType*    mem2 = checkLocation(addr2, typeSize, access_violation);
    // the error is skipped, if a chunk is not available b/c pointer errors
    // should be recorded only when the out-of-bounds memory is accessed, but
    // not when the pointer is moved out of bounds.
    const bool           skiperr = (violation == RuntimeViolation::NONE && !(mem1 && mem2));

    if (!skiperr)
    {
      assert(mem1 && mem2 && mem1 != mem2);
      RuntimeSystem&       rs = RuntimeSystem::instance();
      std::stringstream    ss;

      ss << "Pointer changed allocation block from " << addr1 << " to " << addr2 << std::endl;

      rs.violationHandler( violation, ss.str() );
    }

    return false;
  }

  // so far we know that addr1, addr2 have been allocated within the same chunk
  // now, test for same array chunks
  const size_t                totalblocksize = mem1->blockSize();
  const Location              memloc = mem1->beginAddress();
  const std::ptrdiff_t        ofs1 = byte_offset(memloc, addr1, totalblocksize, 0);
  const std::ptrdiff_t        ofs2 = byte_offset(memloc, addr2, totalblocksize, 0);

  //~ std::cerr << *mem1 << std::endl;
  //~ std::cerr << "a = " << addr1 << std::endl;
  //~ std::cerr << "b = " << addr2 << std::endl;
  //~ std::cerr << "ts = " << typeSize << std::endl;
  //~ std::cerr << "tbs = " << totalblocksize << std::endl;

  // \pp as far as I understand, getTypeAt returns the innermost type
  //     that overlaps a certain memory region [addr1, typeSize).
  //     The current implementation stops at the highest level of array...
  const RsType*               type1 = mem1 -> getTypeAt( ofs1, typeSize );
  const RsType*               type2 = mem1 -> getTypeAt( ofs2, typeSize );
  std::auto_ptr<const RsType> guard1(NULL);
  std::auto_ptr<const RsType> guard2(NULL);

  if( !type1 ) {
      //~ std::cerr << "type1! " << std::endl;
      type1 = mem1->computeCompoundTypeAt( ofs1, typeSize );
      guard1.reset(type1);
  }

  if( !type2 ) {
      //~ std::cerr << "type2! " << std::endl;
      type2 = mem1->computeCompoundTypeAt( ofs2, typeSize );
      guard2.reset(type2);
  }

  assert( type1 && type2 );

  //~ std::cerr << "-- T1 = " << typeid(*type1).name() << std::endl;
  //~ std::cerr << "-- T2 = " << typeid(*type2).name() << std::endl;

  bool               accessOK = type1->isConsistentWith( *type2 );
  const RsArrayType* array = (accessOK ? dynamic_cast< const RsArrayType* >( type1 ) : 0);

  //~ std::cerr << "accOK: " << accessOK << std::endl;

  if (array)
  {
    // \pp in order to calculate the last memory location of elem, we need to
    //     know whether its type is an array type, and whether this is an
    //     immediate (there is no user defined type or pointer in between)
    //     subarray of the containing array.
    const size_t          blockofs = 0; /* \todo calculate offset from addr1 */

    // \pp \todo
    //     not sure why bounds checking is based on a relative address (addr1)
    //     and not on absolute boundaries of the chunk where this address
    //     is located...
    //     e.g., addr2 < array(addr).lb || array(addr).ub < (addr2+typeSize)
    //     - a reason is that we want to bounds check within a multi-dimensional
    //       array; a sub-array might not start at the same address as the
    //       allocated chunk (which is always at [0][0]...)
    // \bug  this works fine for arrays, but it does not seem to be OK for
    //       pointers; in which case addr1 might point in the middle of a chunk.
    const Location       arrlb = addr1;
    const std::ptrdiff_t arrlen = array->getByteSize();
    const Location       elemlo = addr2;
          Location       elemhi = addr2;
    const RsType*        memtype = mem1->getTypeAt( 0, mem1->getSize() );
    const size_t         basesize = basetypeSize(memtype);

    if (typeSize > basesize)
    {
      // elem is another array
      // assert( rs->testing() || dynamic_cast< const RsArrayType* >(type2) );

      const long   elemblockofs = 0; // \todo calculate the ofs of elem's address (is this needed?)

      elemhi = ::add(elemhi, typeSize-1, totalblocksize, elemblockofs);
    }
    else
    {
      elemhi.local += (typeSize-1);
    }

    // \pp \note [arrlb, arrub] and [elemlo, elemhi]  (therefore typeSize - 1)
    //           arrub < elemhi this holds for UPC
    //           in contrast, using a range [elemlb, elemub) causes problems
    //           for UPC where the elemub can be larger than arrub
    //           for last elements on threads other than 0.
    //           e.g., shared int x[THREADS];
    //                   arrub == &x@thread(0) + sizeof(int)
    //                 writing to x[1] has the following upper bound
    //                   elemub == &x@thread(1) + sizeof(int)
    //                 consequently arrub < elemub :(

#if EXTENDEDDBG
    std::cerr << "  arrlb = " << arrlb << "( lb + " << arrlen << " [" << totalblocksize << "] )" << std::endl;
    std::cerr << "  elmlb = " << elemlo << std::endl;
    std::cerr << "  elmub = " << elemhi << " ( lb + " << typeSize << " )" << std::endl;

    std::cerr << "  ofs(elemlb) >= 0 : " << byte_offset(arrlb, elemlo, totalblocksize, blockofs) << std::endl;
    std::cerr << "  ofs(elemub) < sz(arr) : " << (byte_offset(arrlb, elemhi, totalblocksize, blockofs)) << std::endl;
#endif /* EXTENDEDDBG */

    // the offset of the element in respect to the array has to be positive
    //   and the offset of the elements last byte has to be less than the
    //   array length.
    accessOK  = (byte_offset(arrlb, elemlo, totalblocksize, blockofs) >= 0);
    accessOK &= (byte_offset(arrlb, elemhi, totalblocksize, blockofs) < arrlen);

    // the array element might be before it [ -1 ]
    // ... or after the array [ N ]...
    // was: accessOK = ! ( (elemlo < arrlb) || (arrub < elemhi) )
    /* was:
    if  !(  addr1 + array -> getByteSize() >= addr2 + typeSize  // the array element might be after the array [ N ]...
         && addr1 <= addr2 // ... or before it [ -1 ]
         *   (12)     (8)
         )
    {
      // out of bounds error (e.g. int[2][3], ref [0][3])
      consistent = false;
    }
    */
  }

  if (!accessOK)
    failNotSameChunk( *type1, *type2, addr1, addr2, *mem1, violation );

  return accessOK;
}
  BaseGDL* rk4_fun(EnvT* e)
  {
    SizeT nParam = e->NParam();

    if( nParam != 5)    
      e->Throw(" Invalid Number of arguments in RK4 ");
    //-----------------------------  ACQUISITION DES PARAMETRES  ------------------//

    //"Y" input array a vector of values for Y at X value
    //DDoubleGDL* Yvals = new DDoubleGDL(e->GetParAs<DDoubleGDL>(0)->N_Elements(),BaseGDL::NOZERO);
    DDoubleGDL* Yvals = e->GetParAs<DDoubleGDL>(0);
    if(e->GetParDefined(0)->Type() == GDL_COMPLEX || e->GetParDefined(0)->Type() == GDL_COMPLEXDBL)
      cout<<" If RK4 is complex then only the real part is used for the computation "<< endl;

    //"dydx" input value or array 
    //DDoubleGDL* dydxvals = new DDoubleGDL(e->GetParAs<DDoubleGDL>(1)->N_Elements(),BaseGDL::NOZERO);
    DDoubleGDL* dydxvals = e->GetParAs<DDoubleGDL>(1);
    if(e->GetParDefined(1)->Type() == GDL_COMPLEX || e->GetParDefined(1)->Type() == GDL_COMPLEXDBL)
      cout<<" If RK4 is complex then only the real part is used for the computation "<< endl;

    if(dydxvals->N_Elements()!=Yvals->N_Elements())e->Throw(" Y and DYDX dimensions have to match "); 

    // "X" input value  
    DDoubleGDL* X = e->GetParAs<DDoubleGDL>(2);
    if(e->GetParDefined(2)->Type() == GDL_COMPLEX || e->GetParDefined(2)->Type() == GDL_COMPLEXDBL)
      cout<<" If RK4 is complex then only the real part is used for the computation "<< endl;

    // "H" input value  
    DDoubleGDL* H = e->GetParAs<DDoubleGDL>(3);
    if(e->GetParDefined(3)->Type() == GDL_COMPLEX || e->GetParDefined(3)->Type() == GDL_COMPLEXDBL)
      cout<<" If RK4 is complex then only the real part is used for the computation "<< endl;
	

    // Differentiate User's Function string name 
    DStringGDL* init = e->GetParAs<DStringGDL>(4);
    if(e->GetParDefined(4)->Type() != GDL_STRING )
      e->Throw(" Fifth value must be a function name string ");


    //-------------------------------- Allocation -----------------------------------//
    BaseGDL *Steptwo,*Stepthree,*Stepfour;
    SizeT i;
    DDoubleGDL *HH,*H6,*XplusH,*Ytampon,*XH,*Yout,* dym,* dyt;

    Ytampon = new DDoubleGDL(Yvals->Dim(),BaseGDL::NOZERO);
    Yout = new DDoubleGDL(Yvals->Dim(),BaseGDL::NOZERO);
    HH = new DDoubleGDL(H->Dim(),BaseGDL::NOZERO);
    H6 = new DDoubleGDL(H->Dim(),BaseGDL::NOZERO);
    XH = new DDoubleGDL(H->Dim(),BaseGDL::NOZERO);
    BaseGDL* XHO=static_cast<BaseGDL*>(XH);
    XplusH = new DDoubleGDL(H->Dim(),BaseGDL::NOZERO);
    BaseGDL* XplusHO=static_cast<BaseGDL*>(XplusH);
    dym= new DDoubleGDL(Yvals->Dim(),BaseGDL::NOZERO);
    dyt= new DDoubleGDL(Yvals->Dim(),BaseGDL::NOZERO);
    //-------------------------------- Init FIRST STEP -----------------------------------//
    (*HH)[0]=(*H)[0]*0.50000;
    (*H6)[0]=(*H)[0]/6.00000;
    (*XH)[0] = (*X)[0] + (*HH)[0];
// marc: probably an error
//     XplusH[0] = (*X)[0] +  (*H)[0];
    (*XplusH)[0] = (*X)[0] +  (*H)[0];
	
		
    //dym=static_cast<DDoubleGDL*>(dymO);
    //dyt=static_cast<DDoubleGDL*>(dytO);
    //---------------------------- Init Call function -------------------------------------//
    DString RK_Diff;
    e->AssureScalarPar<DStringGDL>( 4, RK_Diff);	

    // this is a function name -> convert to UPPERCASE
    RK_Diff = StrUpCase( RK_Diff);

    // first search library funcedures  
    int funIx=LibFunIx( RK_Diff);
    StackGuard<EnvStackT> guard( e->Interpreter()->CallStack());
		
    if( funIx != -1)
      {
	e->Throw(" String function name is intrinsic function name please change it  ");
      } 
    else
      {
	//  Search in user proc and function
	funIx = GDLInterpreter::GetFunIx(RK_Diff );
	
	//-----------------FIRST STEP-------------------//
	for (i=0;i<Yvals->N_Elements();++i)
	  (*Ytampon)[i]=(*Yvals)[i]+(*HH)[0]*(*dydxvals)[i]; 

	BaseGDL* Ytmp=static_cast<BaseGDL*>(Ytampon);
	  
	//  1st CALL to user function "differentiate"	
	PushNewEnvRK(e, funList[ funIx],&XHO,&Ytmp);
	EnvUDT* newEnv = static_cast<EnvUDT*>(e->Interpreter()->CallStack().back());	
	StackGuard<EnvStackT> guard1 ( e->Interpreter()->CallStack());

	BaseGDL* Steptwo = e->Interpreter()->call_fun(static_cast<DSubUD*>(newEnv->GetPro())->GetTree()); 
	  
	//Conversion BaseGDL*-> DDoubleGDL* in order to use the RK_Diff function result.
	dyt= static_cast<DDoubleGDL*>(Steptwo->Convert2(GDL_DOUBLE,BaseGDL::CONVERT));
	  
	  

	//-------------SECOND STEP-------------------//	
	for (i=0;i<Yvals->N_Elements();++i)
	  (*Ytampon)[i]=(*Yvals)[i]+(*HH)[0]*(*dyt)[i];

	  	  
	//  2nd CALL to user function "differentiate"
	PushNewEnvRK(e, funList[ funIx],&XHO,&Ytmp);	
	  	
	StackGuard<EnvStackT> guard2 ( newEnv->Interpreter()->CallStack());
	
	BaseGDL* Stepthree = e->Interpreter()->call_fun(static_cast<DSubUD*>(newEnv->GetPro())->GetTree());
	
	//Conversion BaseGDL*-> DDoubleGDL* in order to use the RK_Diff function result.
	dym = static_cast<DDoubleGDL*>(Stepthree->Convert2(GDL_DOUBLE,BaseGDL::CONVERT));
	  
	  
	//--------------THIRD STEP-------------------//
	for (i=0;i<Yvals->N_Elements();++i)
	  {
	    (*Ytampon)[i]=(*Yvals)[i]+ (*H)[0]*(*dym)[i];
	    (*dym)[i] += (*dyt)[i];
	  }

	  
	// 3rd CALL to user function "differentiate"
	PushNewEnvRK(e, funList[ funIx],&XplusHO,&Ytmp);
	  
	StackGuard<EnvStackT> guard3 ( newEnv->Interpreter()->CallStack());

	BaseGDL* Stepfour = e->Interpreter()->call_fun(static_cast<DSubUD*>(newEnv->GetPro())->GetTree());

	dyt= static_cast<DDoubleGDL*>(Stepfour->Convert2(GDL_DOUBLE,BaseGDL::CONVERT));
	  
	//--------------FOURTH STEP-------------------//
	for (i=0;i<Yvals->N_Elements();++i)
	  (*Yout)[i]= (*Yvals)[i] + (*H6)[0] * ( (*dydxvals)[i]+(*dyt)[i]+ 2.00000*(*dym)[i] );
	  
	static DInt doubleKWIx = e->KeywordIx("DOUBLE");

	//if need, convert things back
	if( !e->KeywordSet(doubleKWIx))
	  return Yout->Convert2(GDL_FLOAT,BaseGDL::CONVERT);
	else
	  return Yout;
      }
    assert( false);	
  }// RK4_fun