Reformat per new coding guidelines.

Change-Id: Ib0e5d3a6fabe6db414d72b334ca7a7f33bc5726b
This commit is contained in:
Peter Johnson
2015-06-25 22:57:43 -07:00
parent f5a82be9e5
commit cee77a3228
26 changed files with 1296 additions and 1576 deletions

View File

@@ -4,8 +4,8 @@
// - see < http://opensource.org/licenses/BSD-2-Clause>
//
#ifndef CONCURRENT_QUEUE_
#define CONCURRENT_QUEUE_
#ifndef NT_SUPPORT_CONCURRENT_QUEUE_H_
#define NT_SUPPORT_CONCURRENT_QUEUE_H_
#include <queue>
#include <thread>
@@ -13,56 +13,49 @@
#include <condition_variable>
template <typename T>
class ConcurrentQueue
{
public:
T pop()
{
std::unique_lock<std::mutex> mlock(mutex_);
while (queue_.empty())
{
cond_.wait(mlock);
}
auto item = std::move(queue_.front());
queue_.pop();
return item;
class ConcurrentQueue {
public:
T pop() {
std::unique_lock<std::mutex> mlock(mutex_);
while (queue_.empty()) {
cond_.wait(mlock);
}
auto item = std::move(queue_.front());
queue_.pop();
return item;
}
void pop(T& item)
{
std::unique_lock<std::mutex> mlock(mutex_);
while (queue_.empty())
{
cond_.wait(mlock);
}
item = queue_.front();
queue_.pop();
void pop(T& item) {
std::unique_lock<std::mutex> mlock(mutex_);
while (queue_.empty()) {
cond_.wait(mlock);
}
item = queue_.front();
queue_.pop();
}
void push(const T& item)
{
std::unique_lock<std::mutex> mlock(mutex_);
queue_.push(item);
mlock.unlock();
cond_.notify_one();
}
void push(const T& item) {
std::unique_lock<std::mutex> mlock(mutex_);
queue_.push(item);
mlock.unlock();
cond_.notify_one();
}
void push(T&& item)
{
std::unique_lock<std::mutex> mlock(mutex_);
queue_.push(std::move(item));
mlock.unlock();
cond_.notify_one();
}
void push(T&& item) {
std::unique_lock<std::mutex> mlock(mutex_);
queue_.push(std::move(item));
mlock.unlock();
cond_.notify_one();
}
ConcurrentQueue() = default;
ConcurrentQueue(const ConcurrentQueue&) = delete;
ConcurrentQueue& operator=(const ConcurrentQueue&) = delete;
private:
std::queue<T> queue_;
std::mutex mutex_;
std::condition_variable cond_;
ConcurrentQueue() = default;
ConcurrentQueue(const ConcurrentQueue&) = delete;
ConcurrentQueue& operator=(const ConcurrentQueue&) = delete;
private:
std::queue<T> queue_;
std::mutex mutex_;
std::condition_variable cond_;
};
#endif
#endif // NT_SUPPORT_CONCURRENT_QUEUE_H_

View File

@@ -1,3 +1,9 @@
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2015. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#include "timestamp.h"
#include <cassert>
@@ -10,82 +16,74 @@
#endif
// offset in microseconds
static unsigned long long
zerotime()
{
static unsigned long long zerotime() {
#ifdef _WIN32
FILETIME ft;
unsigned long long tmpres = 0;
// 100-nanosecond intervals since January 1, 1601 (UTC)
// which means 0.1 us
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
// January 1st, 1970 - January 1st, 1601 UTC ~ 369 years
// or 116444736000000000 us
static const unsigned long long deltaepoch = 116444736000000000ull;
tmpres -= deltaepoch;
return tmpres;
FILETIME ft;
unsigned long long tmpres = 0;
// 100-nanosecond intervals since January 1, 1601 (UTC)
// which means 0.1 us
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
// January 1st, 1970 - January 1st, 1601 UTC ~ 369 years
// or 116444736000000000 us
static const unsigned long long deltaepoch = 116444736000000000ull;
tmpres -= deltaepoch;
return tmpres;
#else
timespec ts;
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
clock_gettime(CLOCK_REALTIME, &ts);
// in 100-ns intervals
return static_cast<unsigned long long>(ts.tv_sec) * 10000000ull +
static_cast<unsigned long long>(ts.tv_nsec) / 100u;
// in 100-ns intervals
return static_cast<unsigned long long>(ts.tv_sec) * 10000000ull +
static_cast<unsigned long long>(ts.tv_nsec) / 100u;
#endif
}
static unsigned long long
timestamp()
{
static unsigned long long timestamp() {
#ifdef _WIN32
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
// there is an imprecision with the initial value,
// but what matters is that timestamps are monotonic and consistent
return static_cast<unsigned long long>(li.QuadPart);
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
// there is an imprecision with the initial value,
// but what matters is that timestamps are monotonic and consistent
return static_cast<unsigned long long>(li.QuadPart);
#else
timespec ts;
timespec ts;
// cannot fail, parameters are correct and we checked earlier we can
// access the clock
clock_gettime(CLOCK_MONOTONIC, &ts);
// in ns
return static_cast<unsigned long long>(ts.tv_sec) * 1000000000ull +
static_cast<unsigned long long>(ts.tv_nsec);
// cannot fail, parameters are correct and we checked earlier we can
// access the clock
clock_gettime(CLOCK_MONOTONIC, &ts);
// in ns
return static_cast<unsigned long long>(ts.tv_sec) * 1000000000ull +
static_cast<unsigned long long>(ts.tv_nsec);
#endif
}
static unsigned long long
update_frequency()
{
static unsigned long long update_frequency() {
#ifdef _WIN32
LARGE_INTEGER li;
if (!QueryPerformanceFrequency(&li) || !li.QuadPart)
{
// log something
std::terminate();
}
return static_cast<unsigned long long>(li.QuadPart);
LARGE_INTEGER li;
if (!QueryPerformanceFrequency(&li) || !li.QuadPart) {
// log something
std::terminate();
}
return static_cast<unsigned long long>(li.QuadPart);
#else
timespec ts;
timespec ts;
if (clock_getres(CLOCK_MONOTONIC, &ts) < 0)
{
// log error
std::terminate();
}
if (clock_getres(CLOCK_MONOTONIC, &ts) < 0) {
// log error
std::terminate();
}
assert(!ts.tv_sec);
assert(!ts.tv_sec);
// this is the precision of the clock, we want the number of updates per
// second, which is 1 / ts.tv_nsec * 1,000,000,000
static const unsigned long long billion = 1000000000ull;
// this is the precision of the clock, we want the number of updates per
// second, which is 1 / ts.tv_nsec * 1,000,000,000
static const unsigned long long billion = 1000000000ull;
return billion / static_cast<unsigned long long>(ts.tv_nsec);
return billion / static_cast<unsigned long long>(ts.tv_nsec);
#endif
}
@@ -93,14 +91,12 @@ static const unsigned long long zerotime_val = zerotime();
static const unsigned long long offset_val = timestamp();
static const unsigned long long frequency_val = update_frequency();
unsigned long long
NT_Now()
{
assert(offset_val > 0u);
assert(frequency_val > 0u);
unsigned long long delta = timestamp() - offset_val;
// because the frequency is in update per seconds, we have to multiply the
// delta by 10,000,000
unsigned long long delta_in_us = delta * 10000000ull / frequency_val;
return delta_in_us + zerotime_val;
unsigned long long NT_Now() {
assert(offset_val > 0u);
assert(frequency_val > 0u);
unsigned long long delta = timestamp() - offset_val;
// because the frequency is in update per seconds, we have to multiply the
// delta by 10,000,000
unsigned long long delta_in_us = delta * 10000000ull / frequency_val;
return delta_in_us + zerotime_val;
}

View File

@@ -1,12 +1,20 @@
#ifndef TIMESTAMP_H_
#define TIMESTAMP_H_
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2015. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
#ifndef NT_SUPPORT_TIMESTAMP_H_
#define NT_SUPPORT_TIMESTAMP_H_
#ifdef __cplusplus
extern "C" {
#endif
unsigned long long NT_Now(void);
#ifdef __cplusplus
}
#endif
#endif /* TIMESTAMP_H_ */
#endif // NT_SUPPORT_TIMESTAMP_H_