summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTor Egge <Tor.Egge@oath.com>2017-10-30 12:25:22 +0000
committerTor Egge <Tor.Egge@oath.com>2017-10-30 12:25:22 +0000
commitdc8cf61d81f44784620d0c5881b7e833e619fb1f (patch)
tree10ee3530290f7bbd79b237cc53b489d94cce2faf
parentc46c12fe27cbcb0f3ff8105e42cb490aab60921c (diff)
Use std::mutex instead of FastOS_Mutex to protect ring buffer.
-rw-r--r--fastos/src/vespa/fastos/ringbuffer.h12
-rw-r--r--fastos/src/vespa/fastos/unix_ipc.cpp18
2 files changed, 7 insertions, 23 deletions
diff --git a/fastos/src/vespa/fastos/ringbuffer.h b/fastos/src/vespa/fastos/ringbuffer.h
index 53ee003915e..41c0af7385b 100644
--- a/fastos/src/vespa/fastos/ringbuffer.h
+++ b/fastos/src/vespa/fastos/ringbuffer.h
@@ -32,7 +32,7 @@ private:
return (_dataIndex + offset) % _bufferSize;
}
- FastOS_Mutex _mutex;
+ std::mutex _mutex;
public:
void Reset ()
@@ -128,14 +128,6 @@ public:
return _closed;
}
- void Lock ()
- {
- _mutex.Lock();
- }
-
- void Unlock ()
- {
- _mutex.Unlock();
- }
+ std::unique_lock<std::mutex> getGuard() { return std::unique_lock<std::mutex>(_mutex); }
};
diff --git a/fastos/src/vespa/fastos/unix_ipc.cpp b/fastos/src/vespa/fastos/unix_ipc.cpp
index 2eec1ab93a4..e968f0a3509 100644
--- a/fastos/src/vespa/fastos/unix_ipc.cpp
+++ b/fastos/src/vespa/fastos/unix_ipc.cpp
@@ -55,7 +55,7 @@ DoWrite(FastOS_UNIX_Process::DescriptorHandle &desc)
bool rc = true;
FastOS_RingBuffer *buffer = desc._writeBuffer.get();
- buffer->Lock();
+ auto bufferGuard = buffer->getGuard();
int writeBytes = buffer->GetReadSpace();
if(writeBytes > 0)
{
@@ -78,8 +78,6 @@ DoWrite(FastOS_UNIX_Process::DescriptorHandle &desc)
else if(bytesWritten == 0)
desc.CloseHandle();
}
- buffer->Unlock();
-
return rc;
}
@@ -90,7 +88,7 @@ DoRead (FastOS_UNIX_Process::DescriptorHandle &desc)
FastOS_RingBuffer *buffer = desc._readBuffer.get();
- buffer->Lock();
+ auto bufferGuard = buffer->getGuard();
int readBytes = buffer->GetWriteSpace();
if(readBytes > 0) {
int bytesRead;
@@ -108,7 +106,6 @@ DoRead (FastOS_UNIX_Process::DescriptorHandle &desc)
desc.CloseHandle();
}
}
- buffer->Unlock();
return rc;
}
@@ -586,7 +583,7 @@ SendMessage (FastOS_UNIX_Process *xproc, const void *buffer,
ipcBuffer = desc._writeBuffer.get();
if(ipcBuffer != nullptr) {
- ipcBuffer->Lock();
+ auto ipcBufferGuard = ipcBuffer->getGuard();
if(ipcBuffer->GetWriteSpace() >= int((length + sizeof(int)))) {
memcpy(ipcBuffer->GetWritePtr(), &length, sizeof(int));
@@ -597,7 +594,6 @@ SendMessage (FastOS_UNIX_Process *xproc, const void *buffer,
NotifyProcessListChange();
rc = true;
}
- ipcBuffer->Unlock();
}
return rc;
}
@@ -658,7 +654,7 @@ void FastOS_UNIX_IPCHelper::DeliverMessages (FastOS_RingBuffer *buffer)
if(buffer == nullptr)
return;
- buffer->Lock();
+ auto bufferGuard = buffer->getGuard();
unsigned int readSpace;
while((readSpace = buffer->GetReadSpace()) > sizeof(int))
@@ -675,8 +671,6 @@ void FastOS_UNIX_IPCHelper::DeliverMessages (FastOS_RingBuffer *buffer)
else
break;
}
-
- buffer->Unlock();
}
void FastOS_UNIX_IPCHelper::
@@ -692,7 +686,7 @@ PipeData (FastOS_UNIX_Process *process,
if(listener == nullptr)
return;
- buffer->Lock();
+ auto bufferGuard = buffer->getGuard();
unsigned int readSpace;
while((readSpace = buffer->GetReadSpace()) > 0) {
@@ -702,6 +696,4 @@ PipeData (FastOS_UNIX_Process *process,
if(buffer->GetCloseFlag())
process->CloseListener(type);
-
- buffer->Unlock();
}