summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@oath.com>2017-10-27 20:42:27 +0000
committerTor Egge <Tor.Egge@oath.com>2017-10-27 20:42:27 +0000
commit7e0ce424c29f5ee4a89c5c4a989fe2538cc9078d (patch)
tree480e467c684d1b6d9639d40ec589c73c0367e5f4
parent86730de554af723ffd5e4d57b7e3e4d54cb61c0f (diff)
Use std::mutex instead of FastOS_Mutex in slobrok and vespalog modules.
-rw-r--r--slobrok/src/tests/oldapi/mirror.cpp12
-rw-r--r--slobrok/src/tests/oldapi/mirror.h2
-rw-r--r--slobrok/src/vespa/slobrok/sbregister.cpp47
-rw-r--r--slobrok/src/vespa/slobrok/sbregister.h2
-rw-r--r--vespalog/src/vespa/log/bufferedlogger.cpp16
5 files changed, 36 insertions, 43 deletions
diff --git a/slobrok/src/tests/oldapi/mirror.cpp b/slobrok/src/tests/oldapi/mirror.cpp
index e0904fe3a00..2004620d57b 100644
--- a/slobrok/src/tests/oldapi/mirror.cpp
+++ b/slobrok/src/tests/oldapi/mirror.cpp
@@ -54,14 +54,13 @@ MirrorOld::SpecList
MirrorOld::lookup(const std::string & pattern) const
{
SpecList ret;
- _lock.Lock();
+ std::lock_guard<std::mutex> guard(_lock);
SpecList::const_iterator end = _specs.end();
for (SpecList::const_iterator it = _specs.begin(); it != end; ++it) {
if (match(it->first.c_str(), pattern.c_str())) {
ret.push_back(*it);
}
}
- _lock.Unlock();
return ret;
}
@@ -119,10 +118,11 @@ MirrorOld::PerformTask()
std::string(s[idx]._str)));
}
- _lock.Lock();
- std::swap(specs, _specs);
- _updates.add();
- _lock.Unlock();
+ {
+ std::lock_guard<std::mutex> guard(_lock);
+ std::swap(specs, _specs);
+ _updates.add();
+ }
_specsGen.setFromInt(answer[2]._intval32);
}
_backOff.reset();
diff --git a/slobrok/src/tests/oldapi/mirror.h b/slobrok/src/tests/oldapi/mirror.h
index de680641c89..fb150b5d64f 100644
--- a/slobrok/src/tests/oldapi/mirror.h
+++ b/slobrok/src/tests/oldapi/mirror.h
@@ -111,7 +111,7 @@ private:
void RequestDone(FRT_RPCRequest *req) override;
FRT_Supervisor &_orb;
- mutable FastOS_Mutex _lock;
+ mutable std::mutex _lock;
bool _reqDone;
SpecList _specs;
vespalib::GenCnt _specsGen;
diff --git a/slobrok/src/vespa/slobrok/sbregister.cpp b/slobrok/src/vespa/slobrok/sbregister.cpp
index 748ec198bc3..a2ec4a4a7b2 100644
--- a/slobrok/src/vespa/slobrok/sbregister.cpp
+++ b/slobrok/src/vespa/slobrok/sbregister.cpp
@@ -96,10 +96,9 @@ RegisterAPI::~RegisterAPI()
void
RegisterAPI::registerName(const vespalib::stringref & name)
{
- _lock.Lock();
+ std::lock_guard<std::mutex> guard(_lock);
for (uint32_t i = 0; i < _names.size(); ++i) {
if (_names[i] == name) {
- _lock.Unlock();
return;
}
}
@@ -108,20 +107,18 @@ RegisterAPI::registerName(const vespalib::stringref & name)
_pending.push_back(name);
discard(_unreg, name);
ScheduleNow();
- _lock.Unlock();
}
void
RegisterAPI::unregisterName(const vespalib::stringref & name)
{
- _lock.Lock();
+ std::lock_guard<std::mutex> guard(_lock);
_busy = true;
discard(_names, name);
discard(_pending, name);
_unreg.push_back(name);
ScheduleNow();
- _lock.Unlock();
}
// handle any request that completed
@@ -176,11 +173,12 @@ RegisterAPI::handleReconnect()
// try next possible server.
_target = _orb.GetTarget(_currSlobrok.c_str());
}
- _lock.Lock();
- // now that we have a new connection, we need to
- // immediately re-register everything.
- _pending = _names;
- _lock.Unlock();
+ {
+ std::lock_guard<std::mutex> guard(_lock);
+ // now that we have a new connection, we need to
+ // immediately re-register everything.
+ _pending = _names;
+ }
if (_target == 0) {
// we have tried all possible servers.
// start from the top after a delay,
@@ -206,18 +204,19 @@ RegisterAPI::handlePending()
bool unreg = false;
bool reg = false;
vespalib::string name;
- _lock.Lock();
- // pop off the todo stack, unregister has priority
- if (_unreg.size() > 0) {
- name = _unreg.back();
- _unreg.pop_back();
- unreg = true;
- } else if (_pending.size() > 0) {
- name = _pending.back();
- _pending.pop_back();
- reg = true;
+ {
+ std::lock_guard<std::mutex> guard(_lock);
+ // pop off the todo stack, unregister has priority
+ if (_unreg.size() > 0) {
+ name = _unreg.back();
+ _unreg.pop_back();
+ unreg = true;
+ } else if (_pending.size() > 0) {
+ name = _pending.back();
+ _pending.pop_back();
+ reg = true;
+ }
}
- _lock.Unlock();
if (unreg) {
// start a new unregister request
@@ -239,12 +238,11 @@ RegisterAPI::handlePending()
} else {
// nothing more to do right now; schedule to re-register all
// names after a long delay.
- _lock.Lock();
+ std::lock_guard<std::mutex> guard(_lock);
_pending = _names;
LOG(debug, "done, reschedule in 30s");
_busy = false;
Schedule(30.0);
- _lock.Unlock();
}
}
@@ -301,12 +299,11 @@ void
RegisterAPI::RPCHooks::rpc_listNamesServed(FRT_RPCRequest *req)
{
FRT_Values &dst = *req->GetReturn();
- _owner._lock.Lock();
+ std::lock_guard<std::mutex> guard(_owner._lock);
FRT_StringValue *names = dst.AddStringArray(_owner._names.size());
for (uint32_t i = 0; i < _owner._names.size(); ++i) {
dst.SetString(&names[i], _owner._names[i].c_str());
}
- _owner._lock.Unlock();
}
diff --git a/slobrok/src/vespa/slobrok/sbregister.h b/slobrok/src/vespa/slobrok/sbregister.h
index de9f8ed8923..d8812c0f3d0 100644
--- a/slobrok/src/vespa/slobrok/sbregister.h
+++ b/slobrok/src/vespa/slobrok/sbregister.h
@@ -84,7 +84,7 @@ private:
FRT_Supervisor &_orb;
RPCHooks _hooks;
- FastOS_Mutex _lock;
+ std::mutex _lock;
bool _reqDone;
bool _busy;
SlobrokList _slobrokSpecs;
diff --git a/vespalog/src/vespa/log/bufferedlogger.cpp b/vespalog/src/vespa/log/bufferedlogger.cpp
index 106a9bc1dba..5c243b86f86 100644
--- a/vespalog/src/vespa/log/bufferedlogger.cpp
+++ b/vespalog/src/vespa/log/bufferedlogger.cpp
@@ -1,7 +1,6 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "bufferedlogger.h"
-#include <vespa/fastos/mutex.h>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
@@ -13,6 +12,7 @@
#include <sstream>
#include <vector>
#include <cstdarg>
+#include <mutex>
namespace ns_log {
@@ -23,7 +23,7 @@ class BackingBuffer {
public:
std::unique_ptr<Timer> _timer;
/** Lock needed to access cache. */
- mutable FastOS_Mutex _mutex;
+ mutable std::mutex _mutex;
static uint64_t _countFactor;
@@ -100,9 +100,8 @@ public:
* need to empty buffer before new log messages arive.
*/
void trimCache() {
- _mutex.Lock();
+ std::lock_guard<std::mutex> guard(_mutex);
trimCache(_timer->getTimestamp());
- _mutex.Unlock();
}
/**
@@ -240,7 +239,7 @@ BackingBuffer::logImpl(Logger& l, Logger::LogLevel level,
{
Entry entry(level, file, line, token, message, _timer->getTimestamp(), l);
- _mutex.Lock();
+ std::lock_guard<std::mutex> guard(_mutex);
LogCacheFrontToken::iterator it1 = _cacheFront.get<1>().find(entry);
LogCacheBackToken::iterator it2 = _cacheBack.get<1>().find(entry);
if (it1 != _cacheFront.get<1>().end()) {
@@ -257,13 +256,12 @@ BackingBuffer::logImpl(Logger& l, Logger::LogLevel level,
_cacheFront.push_back(entry);
}
trimCache(entry._timestamp);
- _mutex.Unlock();
}
void
BackingBuffer::flush()
{
- _mutex.Lock();
+ std::lock_guard<std::mutex> guard(_mutex);
for (LogCacheBack::const_iterator it = _cacheBack.begin();
it != _cacheBack.end(); ++it)
{
@@ -276,7 +274,6 @@ BackingBuffer::flush()
log(*it);
}
_cacheFront.clear();
- _mutex.Unlock();
}
void
@@ -340,7 +337,7 @@ BackingBuffer::toString() const
{
std::ostringstream ost;
ost << "Front log cache content:\n";
- _mutex.Lock();
+ std::lock_guard<std::mutex> guard(_mutex);
for (LogCacheFront::const_iterator it = _cacheFront.begin();
it != _cacheFront.end(); ++it)
{
@@ -352,7 +349,6 @@ BackingBuffer::toString() const
{
ost << " " << it->toString() << "\n";
}
- _mutex.Unlock();
return ost.str();
}