Ejemplo n.º 1
0
static MojErr MojThreadTestFn(void* arg)
{
	static MojThreadLocalValue<int, MojThreadLocalValueZeroCtor<int> > s_localVal;
	MojThreadTestArgs* targs = (MojThreadTestArgs*) arg;
	MojTestAssert(targs);

	for (int i = 0; i < MojTestNumIterations; ++i) {
		targs->m_atomicCounter.increment();
		MojThreadGuard guard(targs->m_mutex, false);
		if (!guard.tryLock())
			guard.lock();
		++(targs->m_counter);
		if (targs->m_counter == (MojTestNumThreads * MojTestNumIterations)) {
			MojErr err = targs->m_countCond.signal();
			MojTestErrCheck(err);
		}
	}

	MojErr err = MojThreadYield();
	MojTestErrCheck(err);

	MojThreadGuard guard(targs->m_mutex);
	while (targs->m_wait) {
		err = targs->m_waitCond.wait(targs->m_mutex);
		MojTestErrCheck(err);
	}
	guard.unlock();

	int* localVal = NULL;
	err = s_localVal.get(localVal);
	MojTestErrCheck(err);
	MojTestAssert(localVal && *localVal == 0);

	for (int i = 0; i < MojTestNumIterations; ++i) {
		++(*localVal);
		targs->m_atomicCounter.decrement();
		{
			MojThreadReadGuard readGuard(targs->m_rwlock);
			MojTestAssert(targs->m_counter > 0);
		}
		MojThreadReadGuard readGuard(targs->m_rwlock, false);
		if (!readGuard.tryLock())
			readGuard.lock();
		MojTestAssert(targs->m_counter > 0);
		readGuard.unlock();

		{
			MojThreadWriteGuard writeGuard(targs->m_rwlock);
			++(targs->m_counter);
		}

		MojThreadWriteGuard writeGuard(targs->m_rwlock, false);
		if (!writeGuard.tryLock())
			writeGuard.lock();
		targs->m_counter -= 2;
	}
	MojTestAssert(*localVal == MojTestNumIterations);

	return MojErrNone;
}
void LinkAggregationManager::stateUpdated(const StateDelta& delta) {
  CHECK(sw_->getUpdateEvb()->inRunningEventBaseThread());

  folly::SharedMutexWritePriority::WriteHolder writeGuard(&controllersLock_);

  if (!initialized_) {
    bool inserted;
    for (const auto& port : *(delta.newState()->getPorts())) {
      // TODO(samank): use try_emplace once OSS build uses boost >1.63.0
      std::tie(std::ignore, inserted) = portToController_.insert(std::make_pair(
          port->getID(),
          std::make_shared<LacpController>(
              port->getID(), sw_->getLacpEvb(), this)));
      CHECK(inserted);
    }

    for (const auto& portAndController : portToController_) {
      portAndController.second->startMachines();
    }
    initialized_ = true;
  }

  DeltaFunctions::forEachChanged(
      delta.getAggregatePortsDelta(),
      &LinkAggregationManager::aggregatePortChanged,
      &LinkAggregationManager::aggregatePortAdded,
      &LinkAggregationManager::aggregatePortRemoved,
      this);

  // Downgrade to a reader lock
  folly::SharedMutexWritePriority::ReadHolder readGuard(std::move(writeGuard));

  DeltaFunctions::forEachChanged(
      delta.getPortsDelta(), &LinkAggregationManager::portChanged, this);
}
Ejemplo n.º 3
0
    const Signals::SemaphorePtr
    Signals::SignalTable::GetSemaphore(const ConnectionId & connection)
    {
        {
            boost::shared_lock<SignalsLock> guard(m_lock);

            Semaphores::iterator findIt = m_semaphores.find(connection.m_id);
            if (findIt != m_semaphores.end())
            {
                return findIt->second;
            }
        }

        //Did not find the semaphore, we need to get write access.

        //first get an upgrade lock, which is only a read lock, but stops new
        //readers from getting in, so we will never be starved.
        boost::upgrade_lock<SignalsLock> readGuard(m_lock);

        //someone else may have gotten in and added the semaphore, so we check.
        Semaphores::iterator findIt = m_semaphores.find(connection.m_id);
        if (findIt != m_semaphores.end())
        {
            return findIt->second;
        }

        //ok, now we upgrade and add.
        boost::upgrade_to_unique_lock<SignalsLock> writeGuard(readGuard);
        return AddSemaphore(connection);
    }
Ejemplo n.º 4
0
    static bool HandleResetAllCommand(ChatHandler* handler, char const* args)
    {
        if (!*args)
            return false;

        std::string caseName = args;

        AtLoginFlags atLogin;

        // Command specially created as single command to prevent using short case names
        if (caseName == "spells")
        {
            atLogin = AT_LOGIN_RESET_SPELLS;
            sWorld->SendWorldText(LANG_RESETALL_SPELLS);
            if (!handler->GetSession())
                handler->SendSysMessage(LANG_RESETALL_SPELLS);
        }
        else if (caseName == "talents")
        {
            atLogin = AtLoginFlags(AT_LOGIN_RESET_TALENTS | AT_LOGIN_RESET_PET_TALENTS);
            sWorld->SendWorldText(LANG_RESETALL_TALENTS);
            if (!handler->GetSession())
               handler->SendSysMessage(LANG_RESETALL_TALENTS);
        }
        else
        {
            handler->PSendSysMessage(LANG_RESETALL_UNKNOWN_CASE, args);
            handler->SetSentErrorMessage(true);
            return false;
        }

        PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_UPD_ALL_AT_LOGIN_FLAGS);
        stmt->setUInt16(0, uint16(atLogin));
        CharacterDatabase.Execute(stmt);

        SF_SHARED_GUARD readGuard(*HashMapHolder<Player>::GetLock());
        HashMapHolder<Player>::MapType const& plist = sObjectAccessor->GetPlayers();
        for (HashMapHolder<Player>::MapType::const_iterator itr = plist.begin(); itr != plist.end(); ++itr)
            itr->second->SetAtLoginFlag(atLogin);

        return true;
    }
Ejemplo n.º 5
0
    static bool HandleGMListIngameCommand(ChatHandler* handler, char const* /*args*/)
    {
        bool first = true;
        bool footer = false;


        SF_SHARED_GUARD readGuard(*HashMapHolder<Player>::GetLock());
        HashMapHolder<Player>::MapType const& m = sObjectAccessor->GetPlayers();
        for (HashMapHolder<Player>::MapType::const_iterator itr = m.begin(); itr != m.end(); ++itr)
        {
            AccountTypes itrSec = itr->second->GetSession()->GetSecurity();
            if ((itr->second->IsGameMaster() ||
                (itr->second->GetSession()->HasPermission(rbac::RBAC_PERM_COMMANDS_APPEAR_IN_GM_LIST) &&
                 itrSec <= AccountTypes(sWorld->getIntConfig(CONFIG_GM_LEVEL_IN_GM_LIST)))) &&
                (!handler->GetSession() || itr->second->IsVisibleGloballyFor(handler->GetSession()->GetPlayer())))
            {
                if (first)
                {
                    first = false;
                    footer = true;
                    handler->SendSysMessage(LANG_GMS_ON_SRV);
                    handler->SendSysMessage("========================");
                }
                std::string const& name = itr->second->GetName();
                uint8 size = name.size();
                uint8 security = itrSec;
                uint8 max = ((16 - size) / 2);
                uint8 max2 = max;
                if ((max + max2 + size) == 16)
                    max2 = max - 1;
                if (handler->GetSession())
                    handler->PSendSysMessage("|    %s GMLevel %u", name.c_str(), security);
                else
                    handler->PSendSysMessage("|%*s%s%*s|   %u  |", max, " ", name.c_str(), max2, " ", security);
            }
        }
        if (footer)
            handler->SendSysMessage("========================");
        if (first)
            handler->SendSysMessage(LANG_GMS_NOT_LOGGED);
        return true;
    }