Statistics
| Branch: | Revision:

root / include / cbarriermessage.h @ fbe00e73

History | View | Annotate | Download (3.67 KB)

1
//==========================================================================
2
//   CBARRIERMESSAGE.H  -  header for
3
//                     Horizon/OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  Declaration of the following classes:
8
//    cBarrierMessage : message and event object for asynchronous event handling
9
//
10
//==========================================================================
11

    
12
/*--------------------------------------------------------------*
13
  Copyright (C) 2009 Georg Kunz
14

15
  This file is distributed WITHOUT ANY WARRANTY. See the file
16
  `license' for details on this and other legal matters.
17
*--------------------------------------------------------------*/
18

    
19
#ifndef __CBARRIERMESSAGE_H
20
#define __CBARRIERMESSAGE_H
21

    
22
#include <semaphore.h>
23

    
24
#include "cmessage.h"
25

    
26
#ifdef ATOMIC_OPS_DEBUG
27
    #include "catomicopsdebug.h"
28
#else
29
    #include <atomic_ops.h>
30
#endif
31

    
32
/**
33
 * This class implements a special barrier message type derived from cMessage.
34
 * Barrier messages are used to indicate the end of an expanded event and
35
 * block the event scheduler until the corresponding worker thread has finished
36
 * processing the expanded event.
37
 *
38
 * The current implementation uses a semaphore to block the event scheduler
39
 * thread. A faster implementation might by based on a spinlock, however
40
 * the use of barrier messages is discouraged for performance reasons anyway.
41
 * Instead, use the NOBARRIER synchronization algorithm.
42
 *
43
 * @see cMessage
44
 *
45
 * @ingroup Horizon
46
 */
47
class cBarrierMessage : public cMessage
48
{
49
  private:
50
    // the semaphore used to block the calling thread on calling wait()
51
    sem_t sem;
52
    AO_t valid; //set to false if barrier has already been signaled
53

    
54
  public:
55
    /** @name Constructor and destructor and stuff */
56
    //@{
57
    /**
58
     * Constructor
59
     */
60
    cBarrierMessage();
61

    
62
    /**
63
     * Copy constructor
64
     */
65
    cBarrierMessage(const cBarrierMessage& msg);
66

    
67
    /**
68
     * Destructor
69
     */
70
    virtual ~cBarrierMessage();
71

    
72
    /**
73
     * assignment operator
74
     */
75
    cBarrierMessage& operator=(const cBarrierMessage& msg);
76
    //@}
77

    
78

    
79
    /** @name Redefined cMessage functions. */
80
    //@{
81
    /**
82
     * Creates and returns an exact copy of this object.
83
     * See cObject for more details.
84
     */
85
    virtual cMessage *dup() const
86
    {
87
        return new cBarrierMessage(*this);
88
    }
89

    
90
    /**
91
     * Produces a one-line description of object contents into the buffer passed as argument.
92
     * See cObject for more details.
93
     */
94
    virtual std::string info() const
95
    {
96
        return "Barrier message: " + cMessage::info();
97
    }
98

    
99
    /**
100
     * Serializes the object into a PVM or MPI send buffer
101
     * Used by the simulation kernel for parallel execution.
102
     * See cObject for more details.
103
     */
104
    virtual void netPack(cCommBuffer *buffer)
105
    {
106
        throw cRuntimeError("Attempting to send a barrier message to another LP.");
107
    }
108

    
109
    /**
110
     * Deserializes the object from a PVM or MPI receive buffer
111
     * Used by the simulation kernel for parallel execution.
112
     * See cObject for more details.
113
     */
114
    virtual void netUnpack(cCommBuffer *buffer)
115
    {
116
        throw cRuntimeError("Received a barrier message from another LP.");
117
    }
118
    //@}
119

    
120
    /** @name Synchronization methods. */
121
    //@{
122
    /**
123
     * Blocks the calling thread as while the event is being processed.
124
     */
125
    inline void wait()
126
    {
127
        sem_wait(&sem);
128
    }
129

    
130
    /**
131
     * Signals the completion of the event processing.
132
     */
133
    inline void signal()
134
    {
135
        sem_post(&sem);
136
        AO_store(&valid, 0);
137
    }
138

    
139
    inline bool isValid() {
140
        return AO_load_read(&valid) !=0;
141
    }
142
    //@}
143
};
144

    
145
#endif /* __CBARRIERMESSAGE_H */