Commit 8e691963 authored by Steven Murray's avatar Steven Murray
Browse files

castor::server::MutexLocker now has its own header file

parent e44321d3
......@@ -23,6 +23,7 @@
#include "castor/exception/Exception.hpp"
#include "castor/messages/ZmqSocketMT.hpp"
#include "castor/server/MutexLocker.hpp"
#include "h/serrno.h"
//------------------------------------------------------------------------------
......@@ -47,7 +48,7 @@ castor::messages::ZmqSocketMT::~ZmqSocketMT() throw() {
// close
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketMT::close() {
castor::server::MutexLocker lock(&m_mutex);
server::MutexLocker lock(&m_mutex);
m_socket.close();
}
......@@ -55,7 +56,7 @@ void castor::messages::ZmqSocketMT::close() {
// bind
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketMT::bind (const std::string &endpoint) {
castor::server::MutexLocker lock(&m_mutex);
server::MutexLocker lock(&m_mutex);
m_socket.bind(endpoint);
}
......@@ -63,7 +64,7 @@ void castor::messages::ZmqSocketMT::bind (const std::string &endpoint) {
// connect
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketMT::connect(const std::string &endpoint) {
castor::server::MutexLocker lock(&m_mutex);
server::MutexLocker lock(&m_mutex);
m_socket.connect(endpoint);
}
......@@ -71,7 +72,7 @@ void castor::messages::ZmqSocketMT::connect(const std::string &endpoint) {
// send
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketMT::send(ZmqMsg &msg, const int flags) {
castor::server::MutexLocker lock(&m_mutex);
server::MutexLocker lock(&m_mutex);
m_socket.send(msg, flags);
}
......@@ -80,7 +81,7 @@ void castor::messages::ZmqSocketMT::send(ZmqMsg &msg, const int flags) {
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketMT::send(zmq_msg_t *const msg,
const int flags) {
castor::server::MutexLocker lock(&m_mutex);
server::MutexLocker lock(&m_mutex);
m_socket.send(msg, flags);
}
......@@ -88,7 +89,7 @@ void castor::messages::ZmqSocketMT::send(zmq_msg_t *const msg,
// recv
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketMT::recv(ZmqMsg &msg, const int flags) {
castor::server::MutexLocker lock(&m_mutex);
server::MutexLocker lock(&m_mutex);
m_socket.recv(msg, flags);
}
......@@ -96,7 +97,7 @@ void castor::messages::ZmqSocketMT::recv(ZmqMsg &msg, const int flags) {
// recv
//------------------------------------------------------------------------------
void castor::messages::ZmqSocketMT::recv(zmq_msg_t *const msg, int flags) {
castor::server::MutexLocker lock(&m_mutex);
server::MutexLocker lock(&m_mutex);
m_socket.recv(msg, flags);
}
......
......@@ -24,6 +24,7 @@
#pragma once
#include "castor/server/MutexLocker.hpp"
#include "castor/server/Threading.hpp"
namespace castor {
......
......@@ -24,6 +24,7 @@
#pragma once
#include "castor/server/MutexLocker.hpp"
#include "castor/server/Threading.hpp"
namespace castor {
......@@ -45,4 +46,4 @@ struct AtomicFlag{
mutable Mutex m_mutex;
};
}}
\ No newline at end of file
}}
......@@ -31,6 +31,7 @@
#include <queue>
#include <exception>
#include "castor/server/MutexLocker.hpp"
#include "castor/server/Threading.hpp"
#include "castor/server/Semaphores.hpp"
......@@ -126,4 +127,4 @@ private:
};
}}
\ No newline at end of file
}}
......@@ -41,19 +41,4 @@ namespace server {
private:
pthread_mutex_t m_mutex;
};
/**
* A simple scoped locker for mutexes. Highly recommended as
* the mutex will be released in all cases (exception, mid-code return, etc...)
* To use, simply instanciate and forget.
* @param m pointer to a Mutex instance
*/
class MutexLocker {
public:
MutexLocker(Mutex * m) :m_mutex(m) {m->lock();}
~MutexLocker() { try { m_mutex->unlock(); } catch (...) {} }
private:
Mutex * m_mutex;
};
}}
\ No newline at end of file
}}
/******************************************************************************
* Payload.hpp
*
* This file is part of the Castor project.
* See http://castor.web.cern.ch/castor
*
* Copyright (C) 2003 CERN
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
*
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#pragma once
#include "castor/server/Mutex.hpp"
#include <pthread.h>
#include <semaphore.h>
namespace castor {
namespace server {
/**
* A simple scoped locker for mutexes. Highly recommended as
* the mutex will be released in all cases (exception, mid-code return, etc...)
* To use, simply instantiate and forget.
*/
class MutexLocker {
public:
/**
* Constructor.
*
* @param m pointer to Mutex to be owned.
*/
MutexLocker(Mutex *const m): m_mutex(m) {
m->lock();
}
/**
* Destructor.
*/
~MutexLocker() {
try {
m_mutex->unlock();
} catch (...) {
// Ignore any exceptions
}
}
private:
/**
* The mutex owened by this MutexLocker.
*/
Mutex *const m_mutex;
}; // class MutexLocker
} // namespace server
} // namespace castor
#include "castor/server/MutexLocker.hpp"
#include "castor/server/Semaphores.hpp"
#include "castor/server/Threading.hpp"
#include "castor/exception/Errnum.hpp"
......@@ -138,4 +139,4 @@ void castor::server::CondVarSemaphore::release(int n)
pthread_mutex_unlock(&m_mutex),
"Error from pthread_mutex_unlock in castor::server::CondVarSemaphore::release()");
}
}
\ No newline at end of file
}
......@@ -21,9 +21,12 @@
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#include <gtest/gtest.h>
#include "castor/server/MutexLocker.hpp"
#include "castor/server/Threading.hpp"
#include "castor/server/Semaphores.hpp"
#include <gtest/gtest.h>
/* This is a collection of multi threaded unit tests, which can (and should)
be passed through helgrind, as well as valgrind */
......
......@@ -21,10 +21,12 @@
* @author Castor Dev team, castor-dev@cern.ch
*****************************************************************************/
#include <gtest/gtest.h>
#include "castor/server/MutexLocker.hpp"
#include "castor/server/Threading.hpp"
#include "castor/server/ChildProcess.hpp"
#include "castor/server/Semaphores.hpp"
#include <gtest/gtest.h>
#include <time.h>
/* Note: those tests create multi threading errors on purpose and should not
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment