WCondition.h 2.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WCONDITION_H
#define WCONDITION_H

28
#include <boost/shared_ptr.hpp>
29 30
#include <boost/function.hpp>
#include <boost/signals2/signal.hpp>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
31
#include <boost/thread.hpp>
32

33
#include "WExportCommon.h"
34

35 36 37 38
/**
 * Class to encapsulate boost::condition_variable_any. You may use it to efficiently wait for events (a condition comes true). It
 * is a very simple implementation. It might be extended easily. Timed wait functions and so on.
 */
39
class OWCOMMON_EXPORT WCondition // NOLINT
40 41 42
{
    friend class WCondition_test;
public:
43 44 45 46 47 48 49 50 51
    /**
     * Shared pointer type for WCondition.
     */
    typedef boost::shared_ptr< WCondition > SPtr;

    /**
     * Const shared pointer type for WCondition.
     */
    typedef boost::shared_ptr< const WCondition > ConstSPtr;
52 53 54 55 56 57 58 59 60 61 62 63 64 65

    /**
     * Default constructor.
     */
    WCondition();

    /**
     * Destructor.
     */
    virtual ~WCondition();

    /**
     * Wait for the condition. Sets the calling thread asleep.
     */
66
    virtual void wait() const;
67

68
    /**
69 70 71 72
     * Notifies all waiting threads.
     */
    virtual void notify();

73 74 75
    /**
     * Type used for signalling condition changes.
     */
Sebastian Eichelbaum's avatar
Sebastian Eichelbaum committed
76
    typedef boost::function0< void > t_ConditionNotifierType;
77 78 79 80 81 82 83 84 85 86

    /**
     * Subscribes a specified function to be notified on condition change.
     *
     * \param notifier the notifier function
     *
     * \return the connection.
     */
    boost::signals2::connection subscribeSignal( t_ConditionNotifierType notifier );

87 88
protected:

89 90 91
    /**
     * Type used for condition notification.
     */
92
    typedef boost::signals2::signal<void ( void )>  t_ConditionSignalType;
93 94 95 96 97 98

    /**
     * Signal getting fired whenever the condition fires.
     */
    t_ConditionSignalType signal_ConditionFired;

99 100 101
    /**
     * The condition.
     */
102
    mutable boost::condition_variable_any m_condition;
103 104 105 106

    /**
     * The mutex used for the condition.
     */
107
    mutable boost::shared_mutex m_mutex;
108 109 110 111 112 113

private:
};

#endif  // WCONDITION_H