void TimeoutThread::checkForNewWorkers() {
  // If m_timeoutSeconds is not a positive number, then workers threads
  // are allowed to run forever, so don't bother creating timers for the
  // workers
  if (m_timeoutSeconds <= 0) {
    return;
  }

  Lock lock(this);
  for (; !m_pendingIds.empty(); m_pendingIds.pop()) {
    int id = m_pendingIds.front();
    ASSERT(mapContains(m_clients, id));
    ClientThread& ct = m_clients[id];

    if (ct.data != nullptr) {
      // This is a new thread and we have to register its timeout event

      struct timeval timeout;
      timeout.tv_usec = 0;
      // +2 to make sure when it times out, this equation always holds:
      //   time(0) - RequestInjection::s_reqInjectionData->started >=
      //     m_timeoutSeconds
      timeout.tv_sec = m_timeoutSeconds + 2;

      event_set(&ct.e, id, 0, on_timer, this);
      event_base_set(m_eventBase, &ct.e);
      event_add(&ct.e, &timeout);
    } else {
      // This was a deleted thread and we have to remove its timeout event
      event_del(&ct.e);
      m_clients.erase(id);
    }
  }
}
void TimeoutThread::registerRequestThread(RequestInjectionData* data) {
  ASSERT(data);
  data->timeoutSeconds = m_timeoutSeconds;

  {
    Lock l(this);
    int id = m_nextId++;
    ASSERT(!mapContains(m_clients, id));
    m_clients[id].data = data;
    m_pendingIds.push(id);
  }
  notifyPipe();
}
Beispiel #3
0
MapResult mapPut(Map map, MapKeyElement keyElement, MapDataElement dataElement){
    if (map == NULL)
        return MAP_NULL_ARGUMENT;
    NodeResult status_node = NODE_SUCCESS;
    int check_if_insert_node=0;
    MapDataElement new_data = map->copy_data(dataElement);
    MapKeyElement new_key = map->copy_key(keyElement);
    MapKeyElement key_in_node;                                                      //a variable that keep the key from a node
    MapResult status_map;
    bool check_if_key_in_map=mapContains(map,new_key);
    if(check_if_key_in_map==true){                                               //the key is in the map
        MapResult status_map=insertDataToNode(map,new_data, &status_node);
        map->free_key_map(new_key);
        return status_map;
    } else {                                               //the key is not in the map
        Node temp,temp1;
        Node new_node=newNodeAndUpdateItrator(map,new_key,new_data);
        if (new_node == NULL) {
            map->iterator = NULL;
            return MAP_OUT_OF_MEMORY;
        }
        key_in_node = nodeReturnKey(map->iterator, &status_node);
        if (status_node == NODE_NULL_ARGUMENT) {           //the map is clear. we will create the new node to the first pointer
            updataTheFirstPtr (map,new_node);
            return MAP_SUCCESS;
        } else {
            int different = map->compare_key(key_in_node, new_key);
            if (different > 0) {
                isnertNodeToFirstPlace(map,new_node, &status_node);
                return MAP_SUCCESS;
            }
            temp = map->iterator;                                           //save the address of this node, before we increae the iterator
            map->iterator = nodeGetNextIteration(map->iterator, &status_node);   //increase the iterator
            if (status_node == NODE_NULL_PTR) {                                //it is mean that map->iterator=NULL, we have only one node
                isnertNodeAfterTheFirst (map,new_node, &status_node);
                return MAP_SUCCESS;
            }
            temp1=insertNodeForLoop(map,new_key,temp,&status_map,
                                    &check_if_insert_node, new_node);
            if(status_map==MAP_SUCCESS){
                return MAP_SUCCESS;
            }
        }
        if(check_if_insert_node==0){                                 //we need to insert tne node the the end of the list
            isnertNodeToLastPlace (map,new_node,temp1, &status_node);
            return MAP_SUCCESS;
        }
        return MAP_SUCCESS;
    }
    return MAP_SUCCESS;
}
void TimeoutThread::onTimer(int index) {
  Lock l(this);
  ASSERT(mapContains(m_clients, index));
  ClientThread& ct = m_clients[index];
  if (ct.data == nullptr) {
    // The thread has been deleted but we haven't processed it
    // yet. This is ok: just do nothing.
    return;
  }

  event *e = &ct.e;
  event_del(e);

  RequestInjectionData *data = ct.data;
  ASSERT(data);
  struct timeval timeout;
  timeout.tv_usec = 0;
  if (data->started > 0) {
    time_t now = time(0);
    int delta = now - data->started;
    if (delta >= m_timeoutSeconds) {
      timeout.tv_sec = m_timeoutSeconds + 2;
      if (hhvm) {
        Lock l(data->surpriseLock);
        data->setTimedOutFlag();
        if (data->surprisePage) {
          mprotect(data->surprisePage, sizeof(void*), PROT_NONE);
        }
      } else {
        data->setTimedOutFlag();
      }
    } else {
      // Negative delta means start time was adjusted forward to give more time
      if (delta < 0) delta = 0;

      // otherwise, a new request started after we started the timer
      timeout.tv_sec = m_timeoutSeconds - delta + 2;
    }
  } else {
    // Another cycle of m_timeoutSeconds
    timeout.tv_sec = m_timeoutSeconds;
  }

  event_set(e, index, 0, on_timer, this);
  event_base_set(m_eventBase, e);
  event_add(e, &timeout);
}
SpriteSheetInfo* SpritesheetInfoReader::info()
{
	std::ifstream fileInfo(_infoPath);
	if (!fileInfo)
		return nullptr;

	std::map<std::string, int> loadedValues;

	std::string parameterName;
	int parameterValue;

	try
	{
		for (int i = 0; i < 6; ++i)
		{
			fileInfo >> parameterName >> parameterValue;
			loadedValues.insert(std::make_pair(parameterName, parameterValue));
		}

		int frameTime;
		while (fileInfo >> frameTime)
		{
			info_.frameTimes.push_back(frameTime);
		}
	}
	catch (...)
	{
		LOG_STREAM(std::cerr) << "Failed to read '" << _infoPath << '\'';
		return nullptr;
	}

	bool fail = false; //this is dumb 
	mapContains(loadedValues, "numSprites", fail);
	mapContains(loadedValues, "spriteWidth", fail);
	mapContains(loadedValues, "spriteHeight", fail);
	mapContains(loadedValues, "numRows", fail);
	mapContains(loadedValues, "numCols", fail);
	mapContains(loadedValues, "padding", fail);

	if (fail)
		return nullptr;
	else
	{
		info_.num = loadedValues["numSprites"];
		info_.width = loadedValues["spriteWidth"];
		info_.height = loadedValues["spriteHeight"];
		info_.numRows = loadedValues["numRows"];
		info_.numCols = loadedValues["numCols"];
		info_.padding = loadedValues["padding"];

		return &info_;
	}

}
SpritesheetInfo* SpritesheetInfoReader::info()
{
	std::ifstream fileInfo(_infoPath);
	if (!fileInfo)
		return nullptr;

	std::map<std::string, int> loadedValues;

	std::string parameterName;
	int parameterValue;

	try
	{
		while (fileInfo >> parameterName >> parameterValue)
		{
			loadedValues.insert(std::make_pair(parameterName, parameterValue));
		}
	}
	catch (...)
	{
		LOG_STREAM(std::cerr) << "Failed to read '" << _infoPath << '\'';
		return nullptr;
	}

	bool fail = false; //this is dumb 
	mapContains(loadedValues, "numSprites", fail);
	mapContains(loadedValues, "spriteWidth", fail);
	mapContains(loadedValues, "spriteHeight", fail);
	mapContains(loadedValues, "numRows", fail);
	mapContains(loadedValues, "numCols", fail);
	mapContains(loadedValues, "padding", fail);

	if (fail)
		return nullptr;
	else
	{
		info_ =	SpritesheetInfo{ loadedValues["numSprites"],
				loadedValues["spriteWidth"],
				loadedValues["spriteHeight"],
				loadedValues["numRows"],
				loadedValues["numCols"],
				loadedValues["padding"] };

		return &info_;
	}

}
Beispiel #7
0
bool RenamedFuncDict::isFunctionRenameable(const StringData* name) {
  return !m_restrictRenameableFunctions ||
    mapContains(m_renameableFunctions, name);
}
Beispiel #8
0
bool
RegAlloc::hasReg(const Location& loc) const {
  RegContent cont = RegContent(loc);
  return mapContains(m_contToRegMap, cont);
}
Beispiel #9
0
bool ProfData::optimized(FuncId funcId) const {
  return mapContains(m_optimizedFuncs, funcId);
}
Beispiel #10
0
bool ProfData::optimized(const SrcKey& sk) const {
  return mapContains(m_optimizedSKs, sk);
}
void Game::GameLauncher::launch()
{
	int input = 0;
	std::string nextEventTag = "Start";
	bool redirected = false;
	std::map<int, int> optionIndexMap;

	//main game loop
	do {
		//get next story event and display it to the player.
		//The story event message is displayed with effects only if it is 
		//encountered for the first time.
		StoryEvent storyEvent = storyEventFetcher->getNextStoryEvent(nextEventTag);
		consoleCommunicator->clear();
		if (!redirected)
			consoleCommunicator->writeToPlayer(storyEvent.getMessageToPlayer());
		else
			consoleCommunicator->writeToPlayerWithoutEffects(storyEvent.getMessageToPlayer());

		std::vector<StoryOption> optionsList = storyEvent.getOptions();
		if (optionsList.size() == 0) {
			consoleCommunicator->pause();
			nextEventTag = storyEvent.getRedirectTag();
			redirected = true;
		}
		else {
			//print out options to the player
			int i = 0;
			int j = 0;
			consoleCommunicator->writeToPlayerWithoutEffects(""); //newline
			for (std::vector<StoryOption>::iterator it = optionsList.begin(); it != optionsList.end(); it++) {
				//check if option should be enabled or disabled
				std::set<std::string> enableAfterSet = it->getEnableAfterSet();
				std::set<std::string> disableAfterSet = it->getDisableAfterSet();
				bool enabled = true;
				std::set<std::string>::const_iterator iterator;
				for (iterator = enableAfterSet.begin(); iterator != enableAfterSet.end(); ++iterator) {
					if (!mapContains(storyEvent.getTag(), *iterator)) {
						enabled = false;
						break;
					}
				}
				for (iterator = disableAfterSet.begin(); iterator != disableAfterSet.end(); ++iterator) {
					if (mapContains(storyEvent.getTag(), *iterator)) {
						enabled = false;
						break;
					}
				}
				
				//print out option to the player
				if (enabled) {
					consoleCommunicator->writeToPlayerWithoutEffects("--------------------------------------------------------------------------------");
					consoleCommunicator->writeToPlayerWithoutEffects(intToStr(++i) + ". " + it->getPlayersMessage());
					optionIndexMap[i] = j;
				}
				++j;
				
			}
			consoleCommunicator->writeToPlayerWithoutEffects("--------------------------------------------------------------------------------");
			
			//get player input to determine their choice of options and act accordingly
			do {
				input = consoleCommunicator->getInput();
			} while (input <= 0 || input > optionsList.size());
			int index = optionIndexMap[input];
			
			//book-keeping
			optionsList[index].getStoryAction()(optionsList[index].getStoryActionArgument()); //execute story action
			nextEventTag = optionsList[index].getNextStoryEventTag();
			redirected = false;
			playerHistoryMap[storyEvent.getTag()].insert(optionsList[index].getTag()); //mark that the option was taken
			optionIndexMap.clear();
		}
	} while (nextEventTag != "End");
}
bool BlockList::blockExists(QString name) const {
	return mapContains(m_blockNames, name);
}