Changeset 12765


Ignore:
Timestamp:
Mar 14, 2014, 8:26:33 PM (10 years ago)
Author:
hock@…
Message:

new functionality: SystemQueue::leave() and SystemQueue::join()

( + unit tests)

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • source/ariba/utility/system/SystemQueue.cpp

    r12764 r12765  
    7676void SystemQueue::scheduleEvent( const SystemEvent& event, uint32_t delay )
    7777{
    78     assert ( SysQ->running );  // should we really enforce this?
     78//     assert ( SysQ->running );  // should we really enforce this?
     79    if ( ! SysQ->running )
     80    {
     81        logging_debug("/// WARNING: The SystemQueue is NOT RUNNING!");
     82    }
    7983   
    8084    // copy
     
    140144    SysQ.reset( new QueueThread() );
    141145}
     146
     147
     148void SystemQueue::leave()
     149{
     150    // signal SysQ to quit (and abort queued events)
     151    SysQ->cancel();
     152}
     153
     154void SystemQueue::join()
     155{
     156    if ( sysq_thread )
     157    {
     158        logging_debug("/// ... joining SysQ thread");
     159        sysq_thread->join();
     160    }
     161}
     162
     163
    142164
    143165void SystemQueue::dropAll( const SystemEventListener* mlistener)
     
    303325        if ( timedEventsQ.empty() )
    304326        {
    305             logging_debug("/// SysQ is going to sleep.");
     327//             logging_debug("/// SysQ is going to sleep.");
    306328           
    307329            this->system_queue_idle.wait( lock );
     
    310332        else
    311333        {
    312             logging_debug( "/// SysQ is going to sleep for "
    313                         << ( timedEventsQ.top().deadline - get_clock() ).total_milliseconds()
    314                         << "ms. Deadline: "
    315                         << timedEventsQ.top().deadline
    316                         << ", Clock: "
    317                         << get_clock() );
     334//             logging_debug( "/// SysQ is going to sleep for "
     335//                         << ( timedEventsQ.top().deadline - get_clock() ).total_milliseconds()
     336//                         << "ms. Deadline: "
     337//                         << timedEventsQ.top().deadline
     338//                         << ", Clock: "
     339//                         << get_clock() );
    318340
    319341            this->system_queue_idle.timed_wait( lock, timedEventsQ.top().deadline );
  • source/ariba/utility/system/SystemQueue.h

    r12764 r12765  
    117117         * Cancels the system queue and ends the processing after the
    118118         * currently processed event is processed.
     119     *
     120     * NOTE: Do not call this function from within a SystemQueue-Event.
     121     *   Use SystemQueue::leave() instead.
    119122         *
    120123         * This method is thread-safe.
    121124         */
    122125        void cancel();
     126   
     127    /**
     128     * Like SystemQueue::cancel(), but may only be called from within a SystemQueue-Event.
     129     *
     130     * NOTE: In this case some cleanup can not be made. -- If the SystemQueue is
     131     *   restarted, SystemQueue::cancel() must be called before SystemQueue::run()
     132     *   can be called again.
     133     */
     134    void leave();
     135   
     136    /**
     137     * Join the SystemQueue thread -- the current thread is blocked until the
     138     * SystemQueue finishes.
     139     *
     140     * NOTE: Use this only in combination with SystemQueue::leave()
     141     *
     142     * [ There is a possible race condition with SystemQueue::cancel(), but
     143     *   SystemQueue::join() should not be used at the same time as
     144     *   SystemQueue::cancel() anyway. (SystemQueue::leave() is fine, though.)
     145     */
     146    void join();
    123147
    124148        /**
  • tests/SystemQueue-tests.cc

    r12764 r12765  
    102102       
    103103        checkmark = true;
     104    }
     105
     106    void Leave()
     107    {
     108        // let's do something before leaving..
     109        this->LongRunner();
     110        checkmark = false;
     111       
     112        cout << "### Leaving SysQ ### "<< endl;
     113        SystemQueue::instance().leave();
    104114    }
    105115
     
    223233
    224234/**
     235 *  leaves the SystemQueue from inside a scheduled event (in contrast to cancel)
     236 *
     237 *  NOTE: due to the SystemQueue singleton design, explicit cleanup is necessary!
     238 *
     239 *  [ NOTE: the old System Queue does not support this. ]
     240 */
     241TEST_F(SystemQueueTest, LeaveFromInsideEvent)
     242{
     243    SystemQueue& sysq = SystemQueue::instance();
     244   
     245    // start
     246    sysq.run();
     247   
     248    // scheduleCall
     249    sysq.scheduleCall(
     250        boost::bind(&SystemQueueTest::Leave, this)
     251    );
     252
     253    // schedule another which must NOT be performed
     254    sysq.scheduleCall(
     255        boost::bind(&SystemQueueTest::Check, this)
     256    );
     257   
     258    // sleep until the SystemQueue thread has finished
     259    sysq.join();
     260   
     261    EXPECT_FALSE( sysq.isRunning() );
     262   
     263    // ensure Check() was not perfomed
     264    EXPECT_FALSE( sysq.isEmpty() );
     265    EXPECT_FALSE( checkmark );
     266   
     267    // CLEANUP
     268    sysq.cancel();
     269   
     270    EXPECT_FALSE( sysq.isRunning() );
     271    EXPECT_TRUE( sysq.isEmpty() );
     272}
     273
     274
     275/**
    225276 *  schedule a call and test whether it is actually performed by the SystemQueue
    226277 */
Note: See TracChangeset for help on using the changeset viewer.