예제 #1
0
		/**
		 * @brief Unlock of read
		 *
		 * @details
		 * <p> Decreases a reading count. </p>
		 *
		 * <p> When write_lock had done after read_lock, it continues by read_unlock
		 * if the reading count was 1 (read_unlock makes the count to be zero). </p>
		 */
		void readUnlock() const
		{
			std::unique_lock<std::mutex> uk((std::mutex&)read_mtx);

			//차감 전 이미 0 (과도한 readUnlock 수행) 은 안 됨
			if (reading != 0 && --((size_t&)reading) == 0)
				((std::condition_variable&)cv).notify_all();
		};
예제 #2
0
		/**
		 * @brief Lock on writing
		 *
		 * @details
		 * <p> Changes writing flag to true. </p>
		 *
		 * <p> If another write_lock or read_lock is on a progress, wait until them to be unlocked. </p>
		 *
		 *	\li Writing can be done by only a section at once.
		 *	\li Writing can't be done when reading.
		 *
		 * @note You've to call write_unlock when writing work was terminated.
		 */
		void writeLock()
		{
			std::unique_lock<std::mutex> uk(write_mtx_);

			while (writing_ == true || readers_count_ > 0)
				write_cv_.wait(uk);

			writing_ = true;
		};
예제 #3
0
	void Semaphore::lock()
	{
		unique_lock<mutex> uk(*referMutex);

		while (locked->load() >= size->load())
			cv->wait(uk);

		locked->operator++();
	}
예제 #4
0
		/**
		 * @brief Lock on read
		 *
		 * @details
		 * <p> Increases a reading count. </p>
		 * <p> When write_lock is on a progress, wait until write_unlock to be called. </p>
		 *
		 *	\li Reading can be done by multiple sections.
		 *	\li Reading can't be done when writing.
		 *
		 * @warning You've to call read_unlock when the reading work is terminated.
		 */
		void readLock() const
		{
			std::unique_lock<std::mutex> uk((std::mutex&)read_mtx);

			while (writing == true)
				((std::condition_variable&)cv).wait(uk);

			((size_t&)reading)++;
		};
예제 #5
0
		/**
		 * @brief Lock on writing
		 *
		 * @details
		 * <p> Changes writing flag to true. </p>
		 *
		 * <p> If another write_lock or read_lock is on a progress, wait until them to be unlocked. </p>
		 *
		 *	\li Writing can be done by only a section at once.
		 *	\li Writing can't be done when reading.
		 *
		 * @note You've to call write_unlock when writing work was terminated.
		 */
		void writeLock()
		{
			std::unique_lock<std::mutex> uk(read_mtx);
			while (reading > 0)
				cv.wait(uk);
			uk.unlock();

			write_mtx.lock();
			writing = true;
		};
		/**
		 * Add a newly connected remote client.
		 * 
		 * When a {@link ClientDriver remote client} connects to this *server* {@link ExternalClientArray} object, 
		 * then this {@link ExternalClientArray} creates a child {@link ExternalSystem external client} object through 
		 * the {@link createExternalClient createExternalClient()} method and {@link insert inserts} it.
		 * 
		 * @param driver A communicator for external client.
		 */
		virtual void addClient(std::shared_ptr<protocol::ClientDriver> driver) override final
		{
			std::shared_ptr<ExternalSystem> system(createExternalClient(driver));
			if (system == nullptr)
				return;

			system->communicator_ = driver;
			{
				library::UniqueWriteLock uk(getMutex());
				push_back(system);
			}
			driver->listen(system.get());

			for (size_t i = 0; i < size(); i++)
				if (at(i) == system)
				{
					library::UniqueWriteLock uk(getMutex());

					erase(begin() + i);
					break;
				}
		};
예제 #7
0
		/**
		 * @brief Unlock of read
		 *
		 * @details
		 * <p> Decreases a reading count. </p>
		 *
		 * <p> When write_lock had done after read_lock, it continues by read_unlock
		 * if the reading count was 1 (read_unlock makes the count to be zero). </p>
		 */
		void readUnlock() const
		{
			std::unique_lock<std::mutex> uk((std::mutex&)readers_count_mtx_);

			//차감 전 이미 0 (과도한 readUnlock 수행) 은 안 됨
			if (readers_count_ != 0 && --((size_t&)readers_count_) == 0)
			{
				uk.unlock();

				((std::condition_variable&)read_cv_).notify_all();
				((std::condition_variable&)write_cv_).notify_all();
			}
		};
예제 #8
0
		/**
		 * @brief Lock on read
		 *
		 * @details
		 * <p> Increases a reading count. </p>
		 * <p> When write_lock is on a progress, wait until write_unlock to be called. </p>
		 *
		 *	\li Reading can be done by multiple sections.
		 *	\li Reading can't be done when writing.
		 *
		 * @warning You've to call read_unlock when the reading work is terminated.
		 */
		void readLock() const
		{
			// WAIT WRITE_UNLOCK
			std::unique_lock<std::mutex> uk((std::mutex&)read_mtx_);

			while (writing_ == true)
				((std::condition_variable&)read_cv_).wait(uk);

			// PLUS NUMBER OF READERS
			((std::mutex&)readers_count_mtx_).lock();
			((size_t&)readers_count_)++;
			((std::mutex&)readers_count_mtx_).unlock();
		};
		/**
		 * Send an {@link Invoke} message.
		 * 
		 * @param invoke An {@link Invoke} message to send.
		 */
		virtual void sendData(std::shared_ptr<protocol::Invoke> invoke)
		{
			std::vector<std::thread> threads;
			library::UniqueReadLock uk(getMutex());
			
			threads.reserve(this->size());
			for (size_t i = 0; i < size(); i++)
				threads.emplace_back(&ExternalSystem::sendData, at(i).get(), invoke);
			
			uk.unlock();
			for (auto it = threads.begin(); it != threads.end(); it++)
				it->join();
		};
예제 #10
0
    /* ---------------------------------------------------------
    	INVOKE MESSAGE CHAIN
    --------------------------------------------------------- */
    void _Complete_history(size_t uid)
    {
        std::unique_lock<std::mutex> uk(mtx_);

        //--------
        // NEED TO REDEFINE START AND END TIME
        //--------
        // NO SUCH HISTORY; THE PROCESS HAD DONE ONLY IN THIS MEDIATOR LEVEL.
        if (progress_list_.has(uid) == false)
            return;

        // COMPLETE THE HISTORY
        std::shared_ptr<slave::InvokeHistory> history = progress_list_.get(uid);
        history->complete();

        // ERASE THE HISTORY ON PROGRESS LIST
        progress_list_.erase(uid);

        // REPORT THE HISTORY TO MASTER
        std::thread(&MediatorSystem::sendData, this, history->toInvoke()).detach();
    };
예제 #11
0
void EHttpBasicAuthLogicHandler::doHandshake(EIoFilter::NextFilter* nextFilter) {
	logger->debug(__FILE__, __LINE__, " doHandshake()");

	if (step > 0) {
		throw EProxyAuthException(__FILE__, __LINE__, "Authentication request already sent");
	}

	// Send request
	sp<EHttpProxyRequest> req = dynamic_pointer_cast<EHttpProxyRequest>(request);
	EMap<EString*, EList<EString*>*>* headers = req->getHeaders();

	EString uk(EHttpProxyConstants_USER_PROPERTY);
	EString* username = req->getProperties()->get(&uk);
	EString pw(EHttpProxyConstants_PWD_PROPERTY);
	EString* password = req->getProperties()->get(&pw);

    EString value("Basic " + createAuthorization(username->c_str(), password->c_str()));
    EAbstractAuthLogicHandler::addValueToHeader(headers, "Proxy-Authorization",
                                     value.c_str(), true);
	addKeepAliveHeaders(headers);

	writeRequest(nextFilter, req);
	step++;
}
예제 #12
0
파일: ung_ssm.cpp 프로젝트: helske/bssm
arma::mat ung_ssm::sample_model(const unsigned int predict_type,
  const unsigned int nsim) {
  
  arma::cube alpha(m, n, nsim);
  std::normal_distribution<> normal(0.0, 1.0);
  
  for (unsigned int i = 0; i < nsim; i++) {
    
    alpha.slice(i).col(0) = a1;
    
    for (unsigned int t = 0; t < (n - 1); t++) {
      arma::vec uk(k);
      for(unsigned int j = 0; j < k; j++) {
        uk(j) = normal(engine);
      }
      alpha.slice(i).col(t + 1) =
        C.col(t * Ctv) + T.slice(t * Ttv) * alpha.slice(i).col(t) +
        R.slice(t * Rtv) * uk;
    }
  }
  if (predict_type < 3) {
    
    arma::cube y(1, n, nsim);
    
    switch(distribution) {
    case 0:
      y.zeros();
      break;
    case 1:
      for (unsigned int i = 0; i < nsim; i++) {
        for (unsigned int t = 0; t < n; t++) {
          y(0, t, i) = std::exp(xbeta(t) + D(t * Dtv) +
            arma::as_scalar(Z.col(t * Ztv).t() * alpha.slice(i).col(t)));
        }
      }
      break;
    case 2:
      for (unsigned int i = 0; i < nsim; i++) {
        for (unsigned int t = 0; t < n; t++) {
          double tmp = std::exp(xbeta(t) + D(t * Dtv) +
            arma::as_scalar(Z.col(t * Ztv).t() * alpha.slice(i).col(t)));
          y(0, t, i) = tmp / (1.0 + tmp);
        }
      }
      break;
    case 3:
      for (unsigned int i = 0; i < nsim; i++) {
        for (unsigned int t = 0; t < n; t++) {
          y(0, t, i) = std::exp(xbeta(t) + D(t * Dtv) +
            arma::as_scalar(Z.col(t * Ztv).t() * alpha.slice(i).col(t)));
        }
      }
      break;
    }
    
    if (predict_type == 1) {
      
      switch(distribution) {
      case 0:
        break;
      case 1:
        for (unsigned int i = 0; i < nsim; i++) {
          for (unsigned int t = 0; t < n; t++) {
            std::poisson_distribution<> poisson(u(t) * y(0, t, i));
            if ((u(t) * y(0, t, i)) < poisson.max()) {
              y(0, t, i) = poisson(engine);
            } else {
              y(0, t, i) = std::numeric_limits<double>::quiet_NaN();
            }
          }
        }
        break;
      case 2:
        for (unsigned int i = 0; i < nsim; i++) {
          for (unsigned int t = 0; t < n; t++) {
            std::binomial_distribution<> binomial(u(t), y(0, t, i));
            y(0, t, i) = binomial(engine);
          }
        }
        break;
      case 3:
        for (unsigned int i = 0; i < nsim; i++) {
          for (unsigned int t = 0; t < n; t++) {
            std::negative_binomial_distribution<>
            negative_binomial(phi, phi / (phi + u(t) * y(0, t, i)));
            y(0, t, i) = negative_binomial(engine);
          }
        }
        break;
      }
    }
    return y;
  }
  return alpha;
}
예제 #13
0
파일: ung_ssm.cpp 프로젝트: helske/bssm
double ung_ssm::bsf_filter(const unsigned int nsim, arma::cube& alpha,
  arma::mat& weights, arma::umat& indices) {
  
  arma::uvec nonzero = arma::find(P1.diag() > 0);
  arma::mat L_P1(m, m, arma::fill::zeros);
  if (nonzero.n_elem > 0) {
    L_P1.submat(nonzero, nonzero) =
      arma::chol(P1.submat(nonzero, nonzero), "lower");
  }
  std::normal_distribution<> normal(0.0, 1.0);
  for (unsigned int i = 0; i < nsim; i++) {
    arma::vec um(m);
    for(unsigned int j = 0; j < m; j++) {
      um(j) = normal(engine);
    }
    alpha.slice(i).col(0) = a1 + L_P1 * um;
  }
  
  std::uniform_real_distribution<> unif(0.0, 1.0);
  arma::vec normalized_weights(nsim);
  double loglik = 0.0;
  
  if(arma::is_finite(y(0))) {
    weights.col(0) = log_obs_density(0, alpha);
    double max_weight = weights.col(0).max();
    weights.col(0) = arma::exp(weights.col(0) - max_weight);
    double sum_weights = arma::accu(weights.col(0));
    if(sum_weights > 0.0){
      normalized_weights = weights.col(0) / sum_weights;
    } else {
      return -std::numeric_limits<double>::infinity();
    }
    loglik = max_weight + std::log(sum_weights / nsim);
  } else {
    weights.col(0).ones();
    normalized_weights.fill(1.0 / nsim);
  }
  for (unsigned int t = 0; t < n; t++) {
    
    arma::vec r(nsim);
    for (unsigned int i = 0; i < nsim; i++) {
      r(i) = unif(engine);
    }
    
    indices.col(t) = stratified_sample(normalized_weights, r, nsim);
    
    arma::mat alphatmp(m, nsim);
    
    for (unsigned int i = 0; i < nsim; i++) {
      alphatmp.col(i) = alpha.slice(indices(i, t)).col(t);
    }
    
    for (unsigned int i = 0; i < nsim; i++) {
      arma::vec uk(k);
      for(unsigned int j = 0; j < k; j++) {
        uk(j) = normal(engine);
      }
      alpha.slice(i).col(t + 1) = C.col(t * Ctv) +
        T.slice(t * Ttv) * alphatmp.col(i) + R.slice(t * Rtv) * uk;
    }
    
    if ((t < (n - 1)) && arma::is_finite(y(t + 1))) {
      weights.col(t + 1) = log_obs_density(t + 1, alpha);
      
      double max_weight = weights.col(t + 1).max();
      weights.col(t + 1) = arma::exp(weights.col(t + 1) - max_weight);
      double sum_weights = arma::accu(weights.col(t + 1));
      if(sum_weights > 0.0){
        normalized_weights = weights.col(t + 1) / sum_weights;
      } else {
        return -std::numeric_limits<double>::infinity();
      }
      loglik += max_weight + std::log(sum_weights / nsim);
    } else {
      weights.col(t + 1).ones();
      normalized_weights.fill(1.0/nsim);
    }
  }
  // constant part of the log-likelihood
  switch(distribution) {
  case 0 :
    loglik += arma::uvec(arma::find_finite(y)).n_elem * norm_log_const(phi);
    break;
  case 1 : {
      arma::uvec finite_y(find_finite(y));
      loglik += poisson_log_const(y(finite_y), u(finite_y));
    } break;
  case 2 : {
    arma::uvec finite_y(find_finite(y));
    loglik += binomial_log_const(y(finite_y), u(finite_y));
  } break;
  case 3 : {
    arma::uvec finite_y(find_finite(y));
    loglik += negbin_log_const(y(finite_y), u(finite_y), phi);
  } break;
  }
  return loglik;
}