Changeset 14124 for trunk/FACT++


Ignore:
Timestamp:
06/08/12 17:09:54 (12 years ago)
Author:
tbretz
Message:
Added locking to subscription and unsubscription of events. This doesn't happen often, so it shouldn't harm. The advantage is that subsccriptions can happen even if there are still afew events in the queue. Note that the constructor is not locked, so it must be ensured that no Subscriptions or Unssubscriptions happen during the construction.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/StateMachineImp.cc

    r14072 r14124  
    384384//!    The event names all have the SERVER/ pre-fix removed.
    385385//
    386 const vector<string> StateMachineImp::GetEventNames() const
     386const vector<string> StateMachineImp::GetEventNames()
    387387{
    388388    vector<string> v;
     
    391391    const int     len  = name.length();
    392392
     393    const lock_guard<mutex> guard(fMutexEvt);
     394
    393395    for (vector<EventImp*>::const_iterator i=fListOfEvents.begin();
    394396         i!=fListOfEvents.end(); i++)
     
    413415//!    if given only the given event is selected
    414416//
    415 void StateMachineImp::PrintListOfEvents(ostream &out, const string &evt) const
    416 {
     417void StateMachineImp::PrintListOfEvents(ostream &out, const string &evt)
     418{
     419    const lock_guard<mutex> guard(fMutexEvt);
     420
    417421    for (vector<EventImp*>::const_iterator c=fListOfEvents.begin(); c!=fListOfEvents.end(); c++)
    418422        if (evt.empty() || GetName()+'/'+evt==(*c)->GetName())
     
    429433//!
    430434//
    431 void StateMachineImp::PrintListOfAllowedEvents(ostream &out) const
    432 {
     435void StateMachineImp::PrintListOfAllowedEvents(ostream &out)
     436{
     437    const lock_guard<mutex> guard(fMutexEvt);
     438
    433439    for (vector<EventImp*>::const_iterator c=fListOfEvents.begin(); c!=fListOfEvents.end(); c++)
    434440        if ((*c)->IsStateAllowed(fCurrentState))
     
    444450//
    445451//
    446 void StateMachineImp::PrintListOfEvents(const string &str) const
     452void StateMachineImp::PrintListOfEvents(const string &str)
    447453{
    448454    PrintListOfEvents(Out(), str);
     
    485491//!    true if the event was found, false otherwise
    486492//
    487 bool StateMachineImp::HasEvent(const EventImp *cmd) const
     493bool StateMachineImp::HasEvent(const EventImp *cmd)
    488494{
    489495    // Find the event from the list of commands and queue it
     496    const lock_guard<mutex> guard(fMutexEvt);
    490497    return find(fListOfEvents.begin(), fListOfEvents.end(), cmd)!=fListOfEvents.end();
    491498}
     
    500507//!    true if the event was found, false otherwise
    501508//
    502 EventImp *StateMachineImp::FindEvent(const string &evt) const
     509EventImp *StateMachineImp::FindEvent(const string &evt)
    503510{
    504511    // Find the command from the list of commands and queue it
     512    const lock_guard<mutex> guard(fMutexEvt);
    505513    for (vector<EventImp*>::const_iterator c=fListOfEvents.begin(); c!=fListOfEvents.end(); c++)
    506514        if (evt == (*c)->GetName())
     
    546554#endif
    547555
     556    fMutexEvt.lock();
    548557    fListOfEvents.push_back(evt);
     558    fMutexEvt.unlock();
    549559
    550560    return *evt;
     
    611621{
    612622    EventImp *evt = CreateService(name);
     623
     624    fMutexEvt.lock();
    613625    fListOfEvents.push_back(evt);
     626    fMutexEvt.unlock();
     627
    614628    return *evt;
     629}
     630
     631void StateMachineImp::Unsubscribe(EventImp *evt)
     632{
     633    const lock_guard<mutex> guard(fMutexEvt);
     634
     635    auto it = find(fListOfEvents.begin(), fListOfEvents.end(), evt);
     636    if (it==fListOfEvents.end())
     637        return;
     638
     639    fListOfEvents.erase(it);
     640    delete evt;
    615641}
    616642
  • trunk/FACT++/src/StateMachineImp.h

    r14008 r14124  
    4141    std::queue<Event*>     fEventQueue;   /// Event queue (fifo) for the received commands
    4242
    43     std::mutex fMutex;   /// Mutex to ensure thread-safe access to the command fifo
     43    std::mutex fMutex;    /// Mutex to ensure thread-safe access to the command fifo
     44    std::mutex fMutexEvt; /// Mutex to ensure thread-safe access to the command fifo
    4445
    4546    bool fRunning;       /// Machine is in main-loop
     
    101102
    102103    EventImp &Subscribe(const std::string &name);
     104    void Unsubscribe(EventImp *evt);
    103105
    104106    /// return the current state of the machine
     
    128130
    129131    // Event handling
    130     bool HasEvent(const EventImp *cmd) const;
    131     EventImp *FindEvent(const std::string &evt) const;
     132    bool HasEvent(const EventImp *cmd);
     133    EventImp *FindEvent(const std::string &evt);
    132134
    133135    bool IsQueueEmpty() const { return fEventQueue.size()==0; }
    134136
    135     const std::vector<EventImp*> &GetListOfEvents() const { return fListOfEvents; }
    136     const std::vector<std::string> GetEventNames() const;
    137 
    138     void PrintListOfEvents(std::ostream &out, const std::string &evt="") const;
    139     void PrintListOfEvents(const std::string &str="") const;
    140 
    141     void PrintListOfAllowedEvents(std::ostream &out) const;
    142     void PrintListOfAllowedEvents() const;
     137    //const std::vector<EventImp*> &GetListOfEvents() const { return fListOfEvents; }
     138    const std::vector<std::string> GetEventNames();
     139
     140    void PrintListOfEvents(std::ostream &out, const std::string &evt="");
     141    void PrintListOfEvents(const std::string &str="");
     142
     143    void PrintListOfAllowedEvents(std::ostream &out);
     144    void PrintListOfAllowedEvents();
    143145
    144146    void PrintListOfStates(std::ostream &out) const;
Note: See TracChangeset for help on using the changeset viewer.