From f4e904b05e7c1fd9d4cda3ace693b41912ac6622 Mon Sep 17 00:00:00 2001 From: Tor Egge Date: Fri, 27 Oct 2017 12:50:07 +0000 Subject: Use std::mutex and std::condition_variable instead of FastOS_Cond in fnet unit tests. --- fnet/src/tests/frt/rpc/invoke.cpp | 46 +++++++++--------- fnet/src/tests/frt/rpc/session.cpp | 15 +++--- fnet/src/tests/info/info.cpp | 4 +- fnet/src/tests/locking/drainpackets.cpp | 43 ++++++++--------- fnet/src/tests/locking/lockspeed.cpp | 82 ++++++++++++++++----------------- 5 files changed, 95 insertions(+), 95 deletions(-) (limited to 'fnet/src') diff --git a/fnet/src/tests/frt/rpc/invoke.cpp b/fnet/src/tests/frt/rpc/invoke.cpp index 8f4f9dfca90..dfeb01f6542 100644 --- a/fnet/src/tests/frt/rpc/invoke.cpp +++ b/fnet/src/tests/frt/rpc/invoke.cpp @@ -1,10 +1,12 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include #include +#include +#include //------------------------------------------------------------- -FastOS_Mutex _delayedReturnCntLock; +std::mutex _delayedReturnCntLock; uint32_t _delayedReturnCnt = 0; uint32_t _phase_simple_cnt = 0; @@ -20,10 +22,11 @@ uint32_t _phase_echo_cnt = 0; struct LockedReqWait : public FRT_IRequestWait { - FastOS_Cond _cond; // cond used to signal req done + std::mutex _condLock; // cond used to signal req done + std::condition_variable _cond; // cond used to signal req done bool _done; // flag indicating req done - FastOS_Mutex _lockLock; // lock protecting virtual lock + std::mutex _lockLock; // lock protecting virtual lock bool _lock; // virtual lock bool _wasLocked; // was 'locked' when req done @@ -31,38 +34,33 @@ struct LockedReqWait : public FRT_IRequestWait ~LockedReqWait() {} void lock() { - _lockLock.Lock(); + std::lock_guard guard(_lockLock); _lock = true; - _lockLock.Unlock(); } void unlock() { - _lockLock.Lock(); + std::lock_guard guard(_lockLock); _lock = false; - _lockLock.Unlock(); } bool isLocked() { - _lockLock.Lock(); + std::lock_guard guard(_lockLock); bool ret = _lock; - _lockLock.Unlock(); return ret; } void RequestDone(FRT_RPCRequest *) override { _wasLocked = isLocked(); - _cond.Lock(); + std::lock_guard guard(_condLock); _done = true; - _cond.Signal(); - _cond.Unlock(); + _cond.notify_one(); } void waitReq() { - _cond.Lock(); + std::unique_lock guard(_condLock); while(!_done) { - _cond.Wait(); + _cond.wait(guard); } - _cond.Unlock(); } }; @@ -81,18 +79,18 @@ public: : FNET_Task(sched), _req(req) { - _delayedReturnCntLock.Lock(); - _delayedReturnCnt++; - _delayedReturnCntLock.Unlock(); + { + std::lock_guard guard(_delayedReturnCntLock); + _delayedReturnCnt++; + } Schedule(delay); } void PerformTask() override { _req->Return(); - _delayedReturnCntLock.Lock(); + std::lock_guard guard(_delayedReturnCntLock); _delayedReturnCnt--; - _delayedReturnCntLock.Unlock(); } }; @@ -547,9 +545,11 @@ State::WaitForDelayedReturnCount(uint32_t wantedCount, double timeout) FastOS_Time timer; timer.SetNow(); for (;;) { - _delayedReturnCntLock.Lock(); - uint32_t delayedReturnCnt = _delayedReturnCnt; - _delayedReturnCntLock.Unlock(); + uint32_t delayedReturnCnt; + { + std::lock_guard guard(_delayedReturnCntLock); + delayedReturnCnt = _delayedReturnCnt; + } if (delayedReturnCnt == wantedCount) { return true; } diff --git a/fnet/src/tests/frt/rpc/session.cpp b/fnet/src/tests/frt/rpc/session.cpp index 0316129a8b5..2e920ac9f98 100644 --- a/fnet/src/tests/frt/rpc/session.cpp +++ b/fnet/src/tests/frt/rpc/session.cpp @@ -2,28 +2,27 @@ #include #include +#include class Session { private: - static FastOS_Mutex _lock; + static std::mutex _lock; static int _cnt; int _val; public: Session() : _val(0) { - _lock.Lock(); - ++_cnt; - _lock.Unlock(); + std::lock_guard guard(_lock); + ++_cnt; } ~Session() { - _lock.Lock(); - --_cnt; - _lock.Unlock(); + std::lock_guard guard(_lock); + --_cnt; } void SetValue(int val) { _val = val; } @@ -31,7 +30,7 @@ public: static int GetCnt() { return _cnt; } }; -FastOS_Mutex Session::_lock; +std::mutex Session::_lock; int Session::_cnt(0); diff --git a/fnet/src/tests/info/info.cpp b/fnet/src/tests/info/info.cpp index f15c8f83d80..cd0364cad6f 100644 --- a/fnet/src/tests/info/info.cpp +++ b/fnet/src/tests/info/info.cpp @@ -74,12 +74,12 @@ TEST("size of important objects") EXPECT_EQUAL(32u, sizeof(FNET_Channel)); EXPECT_EQUAL(40u, sizeof(FNET_PacketQueue_NoLock)); EXPECT_EQUAL(488u, sizeof(FNET_Connection)); - EXPECT_EQUAL(96u, sizeof(FastOS_Cond)); + EXPECT_EQUAL(48u, sizeof(std::condition_variable)); EXPECT_EQUAL(56u, sizeof(FNET_DataBuffer)); EXPECT_EQUAL(24u, sizeof(FastOS_Time)); EXPECT_EQUAL(8u, sizeof(FNET_Context)); EXPECT_EQUAL(8u, sizeof(fastos::TimeStamp)); - EXPECT_EQUAL(48u, sizeof(FastOS_Mutex)); + EXPECT_EQUAL(40u, sizeof(std::mutex)); EXPECT_EQUAL(40u, sizeof(pthread_mutex_t)); EXPECT_EQUAL(48u, sizeof(pthread_cond_t)); EXPECT_EQUAL(40u, sizeof(std::mutex)); diff --git a/fnet/src/tests/locking/drainpackets.cpp b/fnet/src/tests/locking/drainpackets.cpp index 05ba923376d..a2999f0bddc 100644 --- a/fnet/src/tests/locking/drainpackets.cpp +++ b/fnet/src/tests/locking/drainpackets.cpp @@ -1,6 +1,7 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include #include +#include class MyPacket : public FNET_Packet { @@ -16,7 +17,7 @@ TEST("drain packets") { FastOS_Time start; FastOS_Time stop; - FastOS_Mutex lock; + std::mutex lock; FNET_PacketQueue q1(512); FNET_PacketQueue q2(512); @@ -39,25 +40,23 @@ TEST("drain packets") { FNET_Packet *packet; FNET_Context context; - lock.Lock(); - - while (!q1.IsEmpty_NoLock()) { - packet = q1.DequeuePacket_NoLock(&context); - q3.QueuePacket_NoLock(packet, context); + { + std::lock_guard guard(lock); + while (!q1.IsEmpty_NoLock()) { + packet = q1.DequeuePacket_NoLock(&context); + q3.QueuePacket_NoLock(packet, context); + } } - lock.Unlock(); - //------------------------ - lock.Lock(); - - while (!q3.IsEmpty_NoLock()) { - packet = q3.DequeuePacket_NoLock(&context); - q1.QueuePacket_NoLock(packet, context); + { + std::lock_guard guard(lock); + while (!q3.IsEmpty_NoLock()) { + packet = q3.DequeuePacket_NoLock(&context); + q1.QueuePacket_NoLock(packet, context); + } } - - lock.Unlock(); } stop.SetNow(); @@ -74,9 +73,10 @@ TEST("drain packets") { FNET_Packet *packet; FNET_Context context; - lock.Lock(); - q1.FlushPackets_NoLock(&q2); - lock.Unlock(); + { + std::lock_guard guard(lock); + q1.FlushPackets_NoLock(&q2); + } while (!q2.IsEmpty_NoLock()) { packet = q2.DequeuePacket_NoLock(&context); @@ -85,9 +85,10 @@ TEST("drain packets") { //------------------------ - lock.Lock(); - q3.FlushPackets_NoLock(&q2); - lock.Unlock(); + { + std::lock_guard guard(lock); + q3.FlushPackets_NoLock(&q2); + } while (!q2.IsEmpty_NoLock()) { packet = q2.DequeuePacket_NoLock(&context); diff --git a/fnet/src/tests/locking/lockspeed.cpp b/fnet/src/tests/locking/lockspeed.cpp index 9932f6aa316..b85777f264c 100644 --- a/fnet/src/tests/locking/lockspeed.cpp +++ b/fnet/src/tests/locking/lockspeed.cpp @@ -7,7 +7,7 @@ TEST("lock speed") { FastOS_Time start; FastOS_Time stop; DummyLock dummy; - FastOS_Mutex lock; + std::mutex lock; double dummyTime; double actualTime; double overhead; @@ -46,26 +46,26 @@ TEST("lock speed") { start.SetNow(); for (i = 0; i < 1000000; i++) { - lock.Lock(); - lock.Unlock(); - lock.Lock(); - lock.Unlock(); - lock.Lock(); - lock.Unlock(); - lock.Lock(); - lock.Unlock(); - lock.Lock(); - lock.Unlock(); - lock.Lock(); - lock.Unlock(); - lock.Lock(); - lock.Unlock(); - lock.Lock(); - lock.Unlock(); - lock.Lock(); - lock.Unlock(); - lock.Lock(); - lock.Unlock(); + lock.lock(); + lock.unlock(); + lock.lock(); + lock.unlock(); + lock.lock(); + lock.unlock(); + lock.lock(); + lock.unlock(); + lock.lock(); + lock.unlock(); + lock.lock(); + lock.unlock(); + lock.lock(); + lock.unlock(); + lock.lock(); + lock.unlock(); + lock.lock(); + lock.unlock(); + lock.lock(); + lock.unlock(); } stop.SetNow(); stop -= start; @@ -85,16 +85,16 @@ TEST("lock speed") { start.SetNow(); for (i = 0; i < 1000000; i++) { - FastOS_Mutex lock0; - FastOS_Mutex lock1; - FastOS_Mutex lock2; - FastOS_Mutex lock3; - FastOS_Mutex lock4; - FastOS_Mutex lock5; - FastOS_Mutex lock6; - FastOS_Mutex lock7; - FastOS_Mutex lock8; - FastOS_Mutex lock9; + std::mutex lock0; + std::mutex lock1; + std::mutex lock2; + std::mutex lock3; + std::mutex lock4; + std::mutex lock5; + std::mutex lock6; + std::mutex lock7; + std::mutex lock8; + std::mutex lock9; } stop.SetNow(); stop -= start; @@ -105,16 +105,16 @@ TEST("lock speed") { start.SetNow(); for (i = 0; i < 1000000; i++) { - FastOS_Cond cond0; - FastOS_Cond cond1; - FastOS_Cond cond2; - FastOS_Cond cond3; - FastOS_Cond cond4; - FastOS_Cond cond5; - FastOS_Cond cond6; - FastOS_Cond cond7; - FastOS_Cond cond8; - FastOS_Cond cond9; + std::condition_variable cond0; + std::condition_variable cond1; + std::condition_variable cond2; + std::condition_variable cond3; + std::condition_variable cond4; + std::condition_variable cond5; + std::condition_variable cond6; + std::condition_variable cond7; + std::condition_variable cond8; + std::condition_variable cond9; } stop.SetNow(); stop -= start; -- cgit v1.2.3