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

@@ -45,6 +45,8 @@ int main(int argc, char *argv[]) {
namespace CodeDweller {
std::mutex Service::objectMutex;
Service::Service() :
pauseReceived(false),
resumeReceived(false),
@@ -52,6 +54,12 @@ namespace CodeDweller {
stopReceived(false) {
}
Service &Service::getInstance() {
std::lock_guard<std::mutex> scopeMutex(objectMutex);
static Service service;
return service;
}
int Service::main(int argc, char *argv[]) {
// Load the arguments.
@@ -174,8 +182,24 @@ namespace CodeDweller {
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() {
@@ -230,14 +254,29 @@ namespace CodeDweller {
case SIGTSTP:
pauseReceived = true;
for (auto callback : pauseCallbacks) {
(*callback)();
}
break;
case SIGCONT:
resumeReceived = true;
for (auto callback : resumeCallbacks) {
(*callback)();
}
break;
case SIGHUP:
restartReceived = true;
for (auto callback : restartCallbacks) {
(*callback)();
}
break;
case SIGTERM:

+ 39
- 6
service.hpp View File

@@ -31,6 +31,7 @@

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

namespace CodeDweller {

@@ -88,11 +89,10 @@ namespace CodeDweller {
public:

/// 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.
class Callback {
@@ -116,12 +116,33 @@ namespace CodeDweller {
//
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.
//
// \param[in] stopFunctor is the function object to invoke when
// Stop is received.
//
void onStopCall(Callback *stopFunctor);
void onStopCall(Callback &stopFunctor);

/// Check whether Pause was received.
//
@@ -197,6 +218,9 @@ namespace CodeDweller {
//
int run();

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

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

@@ -224,6 +248,15 @@ namespace CodeDweller {
/// True if Stop message was received.
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.
std::vector<Callback *> stopCallbacks;


Loading…
Cancel
Save