diff options
author | Tor Egge <Tor.Egge@oath.com> | 2017-10-27 12:50:07 +0000 |
---|---|---|
committer | Tor Egge <Tor.Egge@oath.com> | 2017-10-27 12:50:07 +0000 |
commit | f4e904b05e7c1fd9d4cda3ace693b41912ac6622 (patch) | |
tree | ba5425a875ed6bfaada08971d3bb9c7a06bc37c8 /fnet/src/tests/frt | |
parent | bfac5e28b274bf613728549c5dd4b597bd29a167 (diff) |
Use std::mutex and std::condition_variable instead of FastOS_Cond
in fnet unit tests.
Diffstat (limited to 'fnet/src/tests/frt')
-rw-r--r-- | fnet/src/tests/frt/rpc/invoke.cpp | 46 | ||||
-rw-r--r-- | fnet/src/tests/frt/rpc/session.cpp | 15 |
2 files changed, 30 insertions, 31 deletions
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 <vespa/vespalib/testkit/test_kit.h> #include <vespa/fnet/frt/frt.h> +#include <mutex> +#include <condition_variable> //------------------------------------------------------------- -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<std::mutex> guard(_lockLock); _lock = true; - _lockLock.Unlock(); } void unlock() { - _lockLock.Lock(); + std::lock_guard<std::mutex> guard(_lockLock); _lock = false; - _lockLock.Unlock(); } bool isLocked() { - _lockLock.Lock(); + std::lock_guard<std::mutex> guard(_lockLock); bool ret = _lock; - _lockLock.Unlock(); return ret; } void RequestDone(FRT_RPCRequest *) override { _wasLocked = isLocked(); - _cond.Lock(); + std::lock_guard<std::mutex> guard(_condLock); _done = true; - _cond.Signal(); - _cond.Unlock(); + _cond.notify_one(); } void waitReq() { - _cond.Lock(); + std::unique_lock<std::mutex> 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<std::mutex> guard(_delayedReturnCntLock); + _delayedReturnCnt++; + } Schedule(delay); } void PerformTask() override { _req->Return(); - _delayedReturnCntLock.Lock(); + std::lock_guard<std::mutex> 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<std::mutex> 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 <vespa/vespalib/testkit/test_kit.h> #include <vespa/fnet/frt/frt.h> +#include <mutex> 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<std::mutex> guard(_lock); + ++_cnt; } ~Session() { - _lock.Lock(); - --_cnt; - _lock.Unlock(); + std::lock_guard<std::mutex> 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); |