Exemple #1
0
TEST(Flag, MultiThreaded)
{
	Flag f;
	int count = 0;
	const int ITERATIONS_COUNT = 100000;

	auto setter = [&]() {
		for (int i = 0; i < ITERATIONS_COUNT; ++i)
		{
			while (f.IsSet());
			f.Set();
		}
	};

	auto clearer = [&]() {
		for (int i = 0; i < ITERATIONS_COUNT; ++i)
		{
			while (!f.IsSet());
			count++;
			f.Clear();
		}
	};

	std::thread setter_thread(setter);
	std::thread clearer_thread(clearer);

	setter_thread.join();
	clearer_thread.join();

	EXPECT_EQ(ITERATIONS_COUNT, count);
}
void CommandLineFlagsEx::doubleAtPut(CommandLineFlagWithType flag, double value, Flag::Flags origin) {
  Flag* faddr = address_of_flag(flag);
  guarantee(faddr != NULL && faddr->is_double(), "wrong flag type");
  trace_flag_changed<EventDoubleFlagChanged, double>(faddr->_name, faddr->get_double(), value, origin);
  faddr->set_double(value);
  faddr->set_origin(origin);
}
Exemple #3
0
Flag* Flag::fuzzy_match(const char* name, size_t length, bool allow_locked) {
  float VMOptionsFuzzyMatchSimilarity = 0.7f;
  Flag* match = NULL;
  float score;
  float max_score = -1;

  for (Flag* current = &flagTable[0]; current->name != NULL; current++) {
    score = str_similar(current->name, name, length);
    if (score > max_score) {
      max_score = score;
      match = current;
    }
  }

  if (!(match->is_unlocked() || match->is_unlocker())) {
    if (!allow_locked) {
      return NULL;
    }
  }

  if (max_score < VMOptionsFuzzyMatchSimilarity) {
    return NULL;
  }

  return match;
}
Exemple #4
0
Flag::Error CommandLineFlags::uintxAt(const char* name, size_t len, uintx* value, bool allow_locked, bool return_flag) {
  Flag* result = Flag::find_flag(name, len, allow_locked, return_flag);
  if (result == NULL) return Flag::INVALID_FLAG;
  if (!result->is_uintx()) return Flag::WRONG_FORMAT;
  *value = result->get_uintx();
  return Flag::SUCCESS;
}
Exemple #5
0
TEST(Flag, SpinLock)
{
	// Uses a flag to implement basic spinlocking using TestAndSet.
	Flag f;
	int count = 0;
	const int ITERATIONS_COUNT = 5000;
	const int THREADS_COUNT = 50;

	auto adder_func = [&]() {
		for (int i = 0; i < ITERATIONS_COUNT; ++i)
		{
			// Acquire the spinlock.
			while (!f.TestAndSet());
			count++;
			f.Clear();
		}
	};

	std::array<std::thread, THREADS_COUNT> threads;
	for (auto& th : threads)
		th = std::thread(adder_func);
	for (auto& th : threads)
		th.join();

	EXPECT_EQ(ITERATIONS_COUNT * THREADS_COUNT, count);
}
Exemple #6
0
bool CommandLineFlags::doubleAt(char* name, size_t len, double* value) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_double()) return false;
  *value = result->get_double();
  return true;
}
Exemple #7
0
bool CommandLineFlags::ccstrAt(char* name, size_t len, ccstr* value) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_ccstr()) return false;
  *value = result->get_ccstr();
  return true;
}
void CommandLineFlagsEx::boolAtPut(CommandLineFlagWithType flag, bool value, Flag::Flags origin) {
  Flag* faddr = address_of_flag(flag);
  guarantee(faddr != NULL && faddr->is_bool(), "wrong flag type");
  trace_flag_changed<EventBooleanFlagChanged, bool>(faddr->_name, faddr->get_bool(), value, origin);
  faddr->set_bool(value);
  faddr->set_origin(origin);
}
Exemple #9
0
bool CommandLineFlags::uint64_tAt(char* name, size_t len, uint64_t* value) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_uint64_t()) return false;
  *value = result->get_uint64_t();
  return true;
}
void CommandLineFlagsEx::uint64_tAtPut(CommandLineFlagWithType flag, uint64_t value, Flag::Flags origin) {
  Flag* faddr = address_of_flag(flag);
  guarantee(faddr != NULL && faddr->is_uint64_t(), "wrong flag type");
  trace_flag_changed<EventUnsignedLongFlagChanged, u8>(faddr->_name, faddr->get_uint64_t(), value, origin);
  faddr->set_uint64_t(value);
  faddr->set_origin(origin);
}
void CommandLineFlagsEx::intxAtPut(CommandLineFlagWithType flag, intx value, Flag::Flags origin) {
  Flag* faddr = address_of_flag(flag);
  guarantee(faddr != NULL && faddr->is_intx(), "wrong flag type");
  trace_flag_changed<EventLongFlagChanged, s8>(faddr->_name, faddr->get_intx(), value, origin);
  faddr->set_intx(value);
  faddr->set_origin(origin);
}
Exemple #12
0
bool CommandLineFlags::uint64_tAtPut(char* name, size_t len, uint64_t* value, FlagValueOrigin origin) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_uint64_t()) return false;
  uint64_t old_value = result->get_uint64_t();
  result->set_uint64_t(*value);
  *value = old_value;
  result->origin = origin;
  return true;
}
Exemple #13
0
bool CommandLineFlags::doubleAtPut(char* name, size_t len, double* value, FlagValueOrigin origin) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_double()) return false;
  double old_value = result->get_double();
  result->set_double(*value);
  *value = old_value;
  result->origin = origin;
  return true;
}
Exemple #14
0
// Contract:  Flag will make private copy of the incoming value.
// Outgoing value is always malloc-ed, and caller MUST call free.
bool CommandLineFlags::ccstrAtPut(char* name, size_t len, ccstr* value, FlagValueOrigin origin) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_ccstr()) return false;
  ccstr old_value = result->get_ccstr();
  char* new_value = NULL;
  if (*value != NULL) {
    new_value = NEW_C_HEAP_ARRAY(char, strlen(*value)+1, mtInternal);
    strcpy(new_value, *value);
  }
bool CommandLineFlags::boolAtPut(char* name, size_t len, bool* value, Flag::Flags origin) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_bool()) return false;
  bool old_value = result->get_bool();
  trace_flag_changed<EventBooleanFlagChanged, bool>(name, old_value, *value, origin);
  result->set_bool(*value);
  *value = old_value;
  result->set_origin(origin);
  return true;
}
Exemple #16
0
Flag& Flag::get(const std::string& name,
                const std::string& type,
                const std::string& value,
                const std::string& desc,
                bool shell_only) {
  static Flag f;
  if (name != "") {
    f.add(name, type, value, desc, shell_only);
  }
  return f;
}
// Contract:  Flag will make private copy of the incoming value.
// Outgoing value is always malloc-ed, and caller MUST call free.
bool CommandLineFlags::ccstrAtPut(char* name, size_t len, ccstr* value, Flag::Flags origin) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_ccstr()) return false;
  ccstr old_value = result->get_ccstr();
  trace_flag_changed<EventStringFlagChanged, const char*>(name, old_value, *value, origin);
  char* new_value = NULL;
  if (*value != NULL) {
    new_value = NEW_C_HEAP_ARRAY(char, strlen(*value)+1, mtInternal);
    strcpy(new_value, *value);
  }
bool CommandLineFlags::doubleAtPut(char* name, size_t len, double* value, Flag::Flags origin) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_double()) return false;
  double old_value = result->get_double();
  trace_flag_changed<EventDoubleFlagChanged, double>(name, old_value, *value, origin);
  result->set_double(*value);
  *value = old_value;
  result->set_origin(origin);
  return true;
}
bool CommandLineFlags::uint64_tAtPut(char* name, size_t len, uint64_t* value, Flag::Flags origin) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_uint64_t()) return false;
  uint64_t old_value = result->get_uint64_t();
  trace_flag_changed<EventUnsignedLongFlagChanged, u8>(name, old_value, *value, origin);
  result->set_uint64_t(*value);
  *value = old_value;
  result->set_origin(origin);
  return true;
}
bool CommandLineFlags::intxAtPut(char* name, size_t len, intx* value, Flag::Flags origin) {
  Flag* result = Flag::find_flag(name, len);
  if (result == NULL) return false;
  if (!result->is_intx()) return false;
  intx old_value = result->get_intx();
  trace_flag_changed<EventLongFlagChanged, s8>(name, old_value, *value, origin);
  result->set_intx(*value);
  *value = old_value;
  result->set_origin(origin);
  return true;
}
Exemple #21
0
void QuoteTest::testObservable() {

    BOOST_MESSAGE("Testing observability of quotes...");

    boost::shared_ptr<SimpleQuote> me(new SimpleQuote(0.0));
    Flag f;
    f.registerWith(me);
    me->setValue(3.14);

    if (!f.isUp())
        BOOST_FAIL("Observer was not notified of quote change");

}
// Implementation of "setflag" command
static jint set_flag(AttachOperation* op, outputStream* out) {

  const char* name = NULL;
  if ((name = op->arg(0)) == NULL) {
    out->print_cr("flag name is missing");
    return JNI_ERR;
  }

  Flag* f = Flag::find_flag((char*)name, strlen(name));
  if (f && f->is_external() && f->is_writeable()) {
    if (f->is_bool()) {
      return set_bool_flag(name, op, out);
    } else if (f->is_intx()) {
      return set_intx_flag(name, op, out);
    } else if (f->is_uintx()) {
      return set_uintx_flag(name, op, out);
    } else if (f->is_uint64_t()) {
      return set_uint64_t_flag(name, op, out);
    } else if (f->is_ccstr()) {
      return set_ccstr_flag(name, op, out);
    } else {
      ShouldNotReachHere();
      return JNI_ERR;
    }
  } else {
    return AttachListener::pd_set_flag(op, out);
  }
}
Exemple #23
0
/**
 * Parses a config file with lines of the form
 *
 *     flag = value
 *
 * and sets flags as appropriate.
 */
Status ParseConfigFile() {
  Trace();
  if (!g_configFile.wasSet()) {
    Log(LL::Trace) << "No flag config file specified.";
    return Status::ok();
  }
  const std::string &filename = g_configFile.get();
  Log(LL::Trace) << "Begining parse of config file: " << filename;

  std::ifstream ifile(filename.c_str());
  if (!ifile.is_open()) {
    Log(LL::Trace) << "Config file " << filename << " not found.";
    return Status(Status::NOT_FOUND);
  }

  std::string line;
  int lineno = 0;
  while (std::getline(ifile, line)) {
    if (line.empty()) {
      lineno++;
      continue;
    }

    std::vector< std::string > argstr =
        Splitter().on('=').trimWhitespace().split(line, 2);

    RET_SM(
        argstr.size() == 2,
        Status::BAD_INPUT,
        "Error in config file \"" << filename << "\" "
                                  << "on line " << lineno
                                  << " is not a valid flag=value pair");

    tFlagIter itr = GetGlobalFlags().find(argstr[0]);
    RET_SM(
        itr != GetGlobalFlags().end(),
        Status::BAD_INPUT,
        "Unknown flag: " << argstr[0] << " @ line " << lineno);

    iFlagBase *pFlag = itr->second;
    RET_SM(
        pFlag->fromString(TrimQuotes(argstr[1])),
        Status::BAD_INPUT,
        "Invalid flag value for " << argstr[0] << " at line " << lineno
                                  << ". can't parse: " << argstr[1]);

    lineno++;
  }
  return Status::ok();
}
void InstrumentTest::testObservable() {

    BOOST_TEST_MESSAGE("Testing observability of instruments...");

    boost::shared_ptr<SimpleQuote> me1(new SimpleQuote(0.0));
    RelinkableHandle<Quote> h(me1);
    boost::shared_ptr<Instrument> s(new Stock(h));

    Flag f;
    f.registerWith(s);
    
    s->NPV();
    me1->setValue(3.14);
    if (!f.isUp())
        BOOST_FAIL("Observer was not notified of instrument change");
    
    s->NPV();
    f.lower();
    boost::shared_ptr<SimpleQuote> me2(new SimpleQuote(0.0));
    h.linkTo(me2);
    if (!f.isUp())
        BOOST_FAIL("Observer was not notified of instrument change");

    f.lower();
    s->freeze();
    s->NPV();
    me2->setValue(2.71);
    if (f.isUp())
        BOOST_FAIL("Observer was notified of frozen instrument change");
    s->NPV();
    s->unfreeze();
    if (!f.isUp())
        BOOST_FAIL("Observer was not notified of instrument change");
}
// Implementation of "printflag" command
// See also: PrintVMFlagsDCmd class
static jint print_flag(AttachOperation* op, outputStream* out) {
  const char* name = NULL;
  if ((name = op->arg(0)) == NULL) {
    out->print_cr("flag name is missing");
    return JNI_ERR;
  }
  Flag* f = Flag::find_flag((char*)name, strlen(name));
  if (f) {
    f->print_as_flag(out);
    out->print_cr("");
  } else {
    out->print_cr("no such flag '%s'", name);
  }
  return JNI_OK;
}
Exemple #26
0
	bool NE_DLL NEDebugManager::Flag::operator==(const Flag& source) const
	{
		if(this == &source) return true;
		if(getFlag() != source.getFlag()) return false;

		return true;
	}
Exemple #27
0
int main(int argc, const char *argv[])
{
    Flag flag;

    flag.addOption("h", "127.0.0.1", "server address");
    flag.addOption("p", "10001", "server port");
    flag.addOption("c", "1", "number of client to run");

    if (!flag.parse(argc, argv))
    {
        std::cout << flag.help() << std::endl;
        return -1;
    }

    std::string host;
    int port;
    int clientNum;

    flag.getString("h", host);
    flag.getInt("p", port);
    flag.getInt("c", clientNum);

    io_service ios;

    std::list<boost::shared_ptr<Client> > clients;
    boost::thread_group threads;
//    for (int i = 0; i < clientNum; ++i)
//    {
//        boost::shared_ptr<Client> c(new Client(ios, host, port));
//        clients.push_back(c);

//        threads.create_thread(boost::bind(&Client::startConnect, c));
//    }

    for (int i = 0; i < clientNum; ++i)
    {
        boost::shared_ptr<Client> c(new Client(ios, host, port));
        clients.push_back(c);
        c->startConnect();
    }

    int coreNum = boost::thread::hardware_concurrency();
    for (int i = 0; i < coreNum; ++i)
    {
        threads.create_thread(boost::bind(&boost::asio::io_service::run, &ios));
    }

    threads.join_all();

    return 0;
}
Exemple #28
0
// Search the flag table for a named flag
Flag* Flag::find_flag(const char* name, size_t length, bool allow_locked) {
  for (Flag* current = &flagTable[0]; current->name != NULL; current++) {
    if (str_equal(current->name, name, length)) {
      // Found a matching entry.  Report locked flags only if allowed.
      if (!(current->is_unlocked() || current->is_unlocker())) {
        if (!allow_locked) {
          // disable use of locked flags, e.g. diagnostic, experimental,
          // commercial... until they are explicitly unlocked
          return NULL;
        }
      }
      return current;
    }
  }
  // Flag name is not in the flag table
  return NULL;
}
Exemple #29
0
void LazyObjectTest::testDiscardingNotifications() {

    BOOST_TEST_MESSAGE(
        "Testing that lazy object discards notifications after the first...");

    boost::shared_ptr<SimpleQuote> q(new SimpleQuote(0.0));
    boost::shared_ptr<Instrument> s(new Stock(Handle<Quote>(q)));

    Flag f;
    f.registerWith(s);
    
    s->NPV();
    q->setValue(1.0);
    if (!f.isUp())
        BOOST_FAIL("Observer was not notified of change");
    
    f.lower();
    q->setValue(2.0);
    if (f.isUp())
        BOOST_FAIL("Observer was notified of second change");

    f.lower();
    s->NPV();
    q->setValue(3.0);
    if (!f.isUp())
        BOOST_FAIL("Observer was not notified of change after recalculation");
}
Exemple #30
0
bool DataStore::doAppendItemsFlag(const PimItem::List &items, const Flag &flag,
                                  const QSet<Entity::Id> &existing, const Collection &col_,
                                  bool silent)
{
    Collection col = col_;
    QVariantList flagIds;
    QVariantList appendIds;
    PimItem::List appendItems;
    Q_FOREACH (const PimItem &item, items) {
        if (existing.contains(item.id())) {
            continue;
        }

        flagIds << flag.id();
        appendIds << item.id();
        appendItems << item;

        if (col.id() == -1) {
            col.setId(item.collectionId());
        } else if (col.id() != item.collectionId()) {
            col.setId(-2);
        }
    }

    if (appendItems.isEmpty()) {
        return true; // all items have the desired flags already
    }

    QueryBuilder qb2(PimItemFlagRelation::tableName(), QueryBuilder::Insert);
    qb2.setColumnValue(PimItemFlagRelation::leftColumn(), appendIds);
    qb2.setColumnValue(PimItemFlagRelation::rightColumn(), flagIds);
    qb2.setIdentificationColumn(QString());
    if (!qb2.exec()) {
        qCDebug(AKONADISERVER_LOG) << "Failed to execute query:" << qb2.query().lastError();
        return false;
    }

    if (!silent) {
        mNotificationCollector->itemsFlagsChanged(appendItems, QSet<QByteArray>() << flag.name().toLatin1(),
                                                  QSet<QByteArray>(), col);
    }

    return true;
}