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.
File:
1 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
Note: See TracChangeset for help on using the changeset viewer.