Project

General

Profile

Statistics
| Branch: | Revision:

root / include / cbarriermessage.h @ 94cf1056

History | View | Annotate | Download (3.57 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

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

    
50
  public:
51
    /** @name Constructor and destructor and stuff */
52
    //@{
53
    /**
54
     * Constructor
55
     */
56
    cBarrierMessage();
57

    
58
    /**
59
     * Copy constructor
60
     */
61
    cBarrierMessage(const cBarrierMessage& msg);
62

    
63
    /**
64
     * Destructor
65
     */
66
    virtual ~cBarrierMessage();
67

    
68
    /**
69
     * assignment operator
70
     */
71
    cBarrierMessage& operator=(const cBarrierMessage& msg);
72
    //@}
73

    
74

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

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

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

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

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

    
126
    /**
127
     * Signals the completion of the event processing.
128
     */
129
    inline void signal()
130
    {
131
        sem_post(&sem);
132
        valid = false;
133
    }
134

    
135
    inline bool isValid() {
136
        return valid;
137
    }
138
    //@}
139
};
140

    
141
#endif /* __CBARRIERMESSAGE_H */