Skip to content

Commit

Permalink
use steady_clock (swoole#3329)
Browse files Browse the repository at this point in the history
* use steady_clock

* remove useless code

* use c++ template

* fix tests
  • Loading branch information
matyhtf authored May 19, 2020
1 parent bcf69e8 commit 9f9b96a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
2 changes: 1 addition & 1 deletion core-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 2.8.1)

project(core_tests)

Expand Down
26 changes: 26 additions & 0 deletions core-tests/src/core/time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "tests.h"

TEST(time, get_ms)
{
const int us = 3000;
long ms1 = swoole::time<std::chrono::milliseconds>();
usleep(us);
long ms2 = swoole::time<std::chrono::milliseconds>();
EXPECT_GE(ms2 - ms1, us / 1000);
}

TEST(time, get_ms_steady)
{
const int us = 3000;
long ms1 = swoole::time<std::chrono::milliseconds>(true);
usleep(us);
long ms2 = swoole::time<std::chrono::milliseconds>(true);
EXPECT_GE(ms2 - ms1, us / 1000);
}

TEST(time, get_seconds)
{
long sec1 = swoole::time<std::chrono::seconds>();
time_t sec2 = time(NULL);
EXPECT_EQ(sec1, sec2);
}
4 changes: 2 additions & 2 deletions include/lru_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class LRUCache
return nullptr;
}

if (iter->second->second.first < time(nullptr) && iter->second->second.first > 0)
if (iter->second->second.first < ::time(nullptr) && iter->second->second.first > 0)
{
return nullptr;
}
Expand All @@ -70,7 +70,7 @@ class LRUCache
}
else
{
expire_time = time(nullptr) + expire;
expire_time = ::time(nullptr) + expire;
}

auto iter = cache_map.find(key);
Expand Down
16 changes: 16 additions & 0 deletions include/swoole_cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <functional>
#include <vector>
#include <set>
#include <chrono>

namespace swoole {
//-------------------------------------------------------------------------------
Expand Down Expand Up @@ -118,6 +119,21 @@ static inline void hook_call(void **hooks, int type, void *arg)
}
}

template <typename T>
static inline long time(bool steady = false)
{
if (steady)
{
auto now = std::chrono::steady_clock::now();
return std::chrono::duration_cast<T>(now.time_since_epoch()).count();
}
else
{
auto now = std::chrono::system_clock::now();
return std::chrono::duration_cast<T>(now.time_since_epoch()).count();
}
}

typedef std::function<bool (char *, size_t)> StringExplodeHandler;
size_t string_split(swString *str, const char *delimiter, size_t delimiter_length, const StringExplodeHandler &handler);
std::string intersection(std::vector<std::string> &vec1, std::set<std::string> &vec2);
Expand Down
11 changes: 5 additions & 6 deletions include/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

#pragma once

#include "swoole_cxx.h"
#include "atomic.h"
#include "hash.h"
#include <string>
#include <vector>
#include <unordered_map>

typedef uint32_t swTable_string_length_t;
Expand Down Expand Up @@ -177,13 +176,13 @@ static sw_inline swTableColumn* swTableColumn_get(swTable *table, const std::str
}
}

#define SW_TABLE_FORCE_UNLOCK_TIME 2
#define SW_TABLE_FORCE_UNLOCK_TIME 2000 //milliseconds

static sw_inline void swTableRow_lock(swTableRow *row)
{
sw_atomic_t *lock = &row->lock;
uint32_t i, n;
time_t t = 0;
long t = 0;

while (1)
{
Expand Down Expand Up @@ -221,14 +220,14 @@ static sw_inline void swTableRow_lock(swTableRow *row)
*/
if (t == 0)
{
t = time(NULL);
t = swoole::time<std::chrono::milliseconds>(true);
}
/**
* The deadlock time exceeds 2 seconds (SW_TABLE_FORCE_UNLOCK_TIME),
* indicating that the lock process has OOM,
* and the PID has been reused, forcing the unlock
*/
else if (time(NULL) - t > SW_TABLE_FORCE_UNLOCK_TIME)
else if ((swoole::time<std::chrono::milliseconds>(true) - t) > SW_TABLE_FORCE_UNLOCK_TIME)
{
*lock = 1;
goto _success;
Expand Down

0 comments on commit 9f9b96a

Please sign in to comment.