Statistics
| Branch: | Revision:

root / include / cbarriermessage.h @ 463434d4

History | View | Annotate | Download (3.69 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
#include "casyncmodule.h"
26

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

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

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

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

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

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

    
79

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

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

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

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

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

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

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

    
146
#endif /* __CBARRIERMESSAGE_H */