Skip to content

Commit 88cb1e7

Browse files
committed
fixes around setTimeout() and its internals
1 parent 0f54a87 commit 88cb1e7

File tree

3 files changed

+14
-25
lines changed

3 files changed

+14
-25
lines changed

include/infrastructure/q_based_infrastructure.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class NodeProcessor
253253
uint64_t nextTimeoutAt = nextTimeout();
254254
now = infraGetCurrentTime();
255255
// int timeoutToUse = getPollTimeout(nextTimeoutAt, now);
256-
uint64_t timeoutToUse = nextTimeoutAt == TimeOutNever ? TimeOutNever : (nextTimeoutAt < now ? 0 : nextTimeoutAt - now);
256+
uint64_t timeoutToUse = nextTimeoutAt == TimeOutNever ? TimeOutNever : (nextTimeoutAt > now ? nextTimeoutAt - now : 0);
257257

258258
timeoutManager = nullptr;
259259
inmediateQueue = nullptr;

src/interthread_comm_impl.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,18 @@ class MWSRFixedSizeQueueWithFlowControl {
183183
}
184184

185185
std::pair<bool, size_t> pop_front( T* messages, size_t count, uint64_t timeout ) {
186+
::std::chrono::steady_clock::time_point now = ::std::chrono::steady_clock::now();
186187
std::unique_lock<std::mutex> lock(mx);
187188
bool expired = false;
188189
while (coll.size() == 0 && !expired && !killflag) {
189-
expired = waitrd.wait_for(lock, std::chrono::milliseconds(timeout)) == std::cv_status::timeout;
190+
expired = waitrd.wait_for(lock, std::chrono::microseconds(timeout)) == std::cv_status::timeout;
191+
if ( !expired ) {
192+
uint64_t d = std::chrono::duration_cast<std::chrono::microseconds>( ::std::chrono::steady_clock::now() - now ).count();
193+
if ( d < timeout )
194+
timeout -= d;
195+
else
196+
timeout = 0;
197+
}
190198
}
191199
if (killflag)
192200
{

src/q_based_infrastructure.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#endif
5454
#include <sys/resource.h>
5555
#endif
56+
#include <chrono>
5657

5758

5859
namespace nodecpp {
@@ -69,7 +70,9 @@ thread_local TimeoutManager* timeoutManager;
6970
uint64_t infraGetCurrentTime()
7071
{
7172
#ifdef _MSC_VER
72-
return GetTickCount64() * 1000; // mks
73+
// return GetTickCount64() * 1000; // mks
74+
auto now = ::std::chrono::steady_clock::now();
75+
return std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count();
7376
#else
7477
struct timespec ts;
7578
// timespec_get(&ts, TIME_UTC);
@@ -126,28 +129,6 @@ namespace nodecpp {
126129
inmediateQueue->add(std::move(cb));
127130
}
128131

129-
#if 0
130-
namespace time
131-
{
132-
size_t now()
133-
{
134-
#if defined NODECPP_MSVC || ( (defined NODECPP_WINDOWS) && (defined NODECPP_CLANG) )
135-
#if defined(NODECPP_X64) || defined(NODECPP_ARM64)
136-
return GetTickCount64();
137-
#else
138-
return GetTickCount();
139-
#endif // NODECPP_X86 or NODECPP_X64
140-
#elif (defined NODECPP_CLANG) || (defined NODECPP_GCC)
141-
struct timespec ts;
142-
clock_gettime(CLOCK_MONOTONIC, &ts);
143-
return (size_t)(ts.tv_nsec / 1000000) + ((uint64_t)ts.tv_sec * 1000ull);
144-
#else
145-
#error not implemented for this compiler
146-
#endif
147-
}
148-
} // namespace time
149-
#endif // 0
150-
151132
} // namespace nodecpp
152133

153134

0 commit comments

Comments
 (0)