Browse Source

Made service thread-safe by serializing access.


git-svn-id: https://svn.microneil.com/svn/CodeDweller/branches/adeniz_1@37 d34b734f-a00e-4b39-a726-e4eeb87269ab
adeniz_1
adeniz 10 years ago
parent
commit
97756452ea
2 changed files with 80 additions and 8 deletions
  1. 41
    2
      service.cpp
  2. 39
    6
      service.hpp

+ 41
- 2
service.cpp View File

namespace CodeDweller { namespace CodeDweller {
std::mutex Service::objectMutex;
Service::Service() : Service::Service() :
pauseReceived(false), pauseReceived(false),
resumeReceived(false), resumeReceived(false),
stopReceived(false) { stopReceived(false) {
} }
Service &Service::getInstance() {
std::lock_guard<std::mutex> scopeMutex(objectMutex);
static Service service;
return service;
}
int Service::main(int argc, char *argv[]) { int Service::main(int argc, char *argv[]) {
// Load the arguments. // Load the arguments.
return cmdLineArgs; return cmdLineArgs;
} }
void Service::onStopCall(Callback *stopFunctor) {
stopCallbacks.push_back(stopFunctor);
void Service::onPauseCall(Callback &pauseFunctor) {
std::lock_guard<std::mutex> scopeMutex(objectMutex);
pauseCallbacks.push_back(&pauseFunctor);
}
void Service::onResumeCall(Callback &resumeFunctor) {
std::lock_guard<std::mutex> scopeMutex(objectMutex);
resumeCallbacks.push_back(&resumeFunctor);
}
void Service::onRestartCall(Callback &restartFunctor) {
std::lock_guard<std::mutex> scopeMutex(objectMutex);
restartCallbacks.push_back(&restartFunctor);
}
void Service::onStopCall(Callback &stopFunctor) {
std::lock_guard<std::mutex> scopeMutex(objectMutex);
stopCallbacks.push_back(&stopFunctor);
} }
bool Service::receivedPause() { bool Service::receivedPause() {
case SIGTSTP: case SIGTSTP:
pauseReceived = true; pauseReceived = true;
for (auto callback : pauseCallbacks) {
(*callback)();
}
break; break;
case SIGCONT: case SIGCONT:
resumeReceived = true; resumeReceived = true;
for (auto callback : resumeCallbacks) {
(*callback)();
}
break; break;
case SIGHUP: case SIGHUP:
restartReceived = true; restartReceived = true;
for (auto callback : restartCallbacks) {
(*callback)();
}
break; break;
case SIGTERM: case SIGTERM:

+ 39
- 6
service.hpp View File



#include <string> #include <string>
#include <vector> #include <vector>
#include <mutex>


namespace CodeDweller { namespace CodeDweller {


public: public:


/// Get the instance of the singleton. /// Get the instance of the singleton.
static Service& getInstance()
{
static Service service;
return service;
}
//
// \returns a reference to the singleton.
//
static Service& getInstance();


/// Callback functor interface. /// Callback functor interface.
class Callback { class Callback {
// //
int main(int argc, char *argv[]); int main(int argc, char *argv[]);


/// Register a callback for receipt of Pause.
//
// \param[in] pauseFunctor is the function object to invoke when
// Pause is received.
//
void onPauseCall(Callback &pauseFunctor);

/// Register a callback for receipt of Resume.
//
// \param[in] resumeFunctor is the function object to invoke when
// Resume is received.
//
void onResumeCall(Callback &resumeFunctor);

/// Register a callback for receipt of Restart.
//
// \param[in] restartFunctor is the function object to invoke when
// Restart is received.
//
void onRestartCall(Callback &restartFunctor);

/// Register a callback for receipt of Stop. /// Register a callback for receipt of Stop.
// //
// \param[in] stopFunctor is the function object to invoke when // \param[in] stopFunctor is the function object to invoke when
// Stop is received. // Stop is received.
// //
void onStopCall(Callback *stopFunctor);
void onStopCall(Callback &stopFunctor);


/// Check whether Pause was received. /// Check whether Pause was received.
// //
// //
int run(); int run();


/// Mutex to serialize access to the object.
static std::mutex objectMutex;

/// Thread start function to receive messages. /// Thread start function to receive messages.
void processMessages(); void processMessages();


/// True if Stop message was received. /// True if Stop message was received.
bool stopReceived; bool stopReceived;


/// Functions to invoke when the Pause is received.
std::vector<Callback *> pauseCallbacks;

/// Functions to invoke when the Resume is received.
std::vector<Callback *> resumeCallbacks;

/// Functions to invoke when the Restart is received.
std::vector<Callback *> restartCallbacks;

/// Functions to invoke when the Stop is received. /// Functions to invoke when the Stop is received.
std::vector<Callback *> stopCallbacks; std::vector<Callback *> stopCallbacks;



Loading…
Cancel
Save