Skip to content

Commit

Permalink
适配vs2013未实现steady_clock的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
test committed May 27, 2016
1 parent ee5283b commit c67fb3d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
3 changes: 2 additions & 1 deletion libgo/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

// VS2013不支持thread_local
#if defined(_MSC_VER) && _MSC_VER < 1900
#define thread_local __declspec(thread)
# define thread_local __declspec(thread)
# define UNSUPPORT_STEADY_TIME
#endif

#if __linux__
Expand Down
6 changes: 6 additions & 0 deletions libgo/timer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "timer.h"
#include <mutex>
#include <limits>
#include <algorithm>

namespace co
{
Expand Down Expand Up @@ -65,6 +66,8 @@ CoTimerPtr CoTimerMgr::ExpireAt(SystemTimePoint const& time_point,
sptr->system_token_ = system_deadlines_.insert(std::make_pair(time_point, sptr));
return sptr;
}

#ifndef UNSUPPORT_STEADY_TIME
CoTimerPtr CoTimerMgr::ExpireAt(SteadyTimePoint const& time_point,
CoTimer::fn_t const& fn)
{
Expand All @@ -76,6 +79,7 @@ CoTimerPtr CoTimerMgr::ExpireAt(SteadyTimePoint const& time_point,
sptr->steady_token_ = steady_deadlines_.insert(std::make_pair(time_point, sptr));
return sptr;
}
#endif

bool CoTimerMgr::Cancel(CoTimerPtr co_timer_ptr)
{
Expand Down Expand Up @@ -188,9 +192,11 @@ void CoTimerMgr::SetNextTriggerTime(SystemTimePoint const& sys_tp)
system_next_trigger_time_ = std::chrono::time_point_cast<std::chrono::milliseconds>(sys_tp).time_since_epoch().count();
}

#ifndef UNSUPPORT_STEADY_TIME
void CoTimerMgr::SetNextTriggerTime(SteadyTimePoint const& sdy_tp)
{
steady_next_trigger_time_ = std::chrono::time_point_cast<std::chrono::milliseconds>(sdy_tp).time_since_epoch().count();
}
#endif

} //namespace co
10 changes: 8 additions & 2 deletions libgo/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
#include <vector>
#include <list>
#include "spinlock.h"
#include "config.h"

namespace co
{

class CoTimer;
typedef std::shared_ptr<CoTimer> CoTimerPtr;

typedef std::chrono::time_point<std::chrono::system_clock> SystemTimePoint;
typedef std::chrono::time_point<std::chrono::steady_clock> SteadyTimePoint;
typedef std::chrono::system_clock::time_point SystemTimePoint;
typedef std::chrono::steady_clock::time_point SteadyTimePoint;

class CoTimer
{
Expand Down Expand Up @@ -64,7 +65,9 @@ class CoTimerMgr

CoTimerPtr ExpireAt(SystemTimePoint const& time_point, CoTimer::fn_t const& fn);

#ifndef UNSUPPORT_STEADY_TIME
CoTimerPtr ExpireAt(SteadyTimePoint const& time_point, CoTimer::fn_t const& fn);
#endif

template <typename Duration>
CoTimerPtr ExpireAt(Duration const& duration, CoTimer::fn_t const& fn)
Expand All @@ -89,7 +92,10 @@ class CoTimerMgr
long long GetNextTriggerTime();

void SetNextTriggerTime(SystemTimePoint const& sys_tp);

#ifndef UNSUPPORT_STEADY_TIME
void SetNextTriggerTime(SteadyTimePoint const& sdy_tp);
#endif

private:
SystemDeadLines system_deadlines_;
Expand Down

0 comments on commit c67fb3d

Please sign in to comment.