Project

General

Profile

Statistics
| Branch: | Revision:

root / include / cbarriermessage.h @ aeae20a1

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

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

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

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

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

    
73

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

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

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

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

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

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

    
135
#endif /* __CBARRIERMESSAGE_H */