Project

General

Profile

Statistics
| Branch: | Revision:

root / include / cspinningthreadpool.h @ fbe00e73

History | View | Annotate | Download (3.66 KB)

1 01873262 Georg Kunz
//==========================================================================
2 fbe00e73 Mirko Stoffers
//   CSPINNINGTHREADPOOL.H  -  header for
3 01873262 Georg Kunz
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  Declaration of the following classes:
8 fbe00e73 Mirko Stoffers
//    cSpinningThreadPool : thread pool for asynchronous event handling
9 01873262 Georg Kunz
//
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 __CSPINNINGTHREADPOOL_H
20
#define __CSPINNINGTHREADPOOL_H
21
22
#include "cthreadpool.h"
23 fbe00e73 Mirko Stoffers
#include "csimplemodule.h"
24 01873262 Georg Kunz
25
#ifdef ATOMIC_OPS_DEBUG
26
        #include "catomicopsdebug.h"
27
#else
28
        #include <atomic_ops.h>
29
#endif
30
31 b9e9c37a Simon Tenbusch
#ifdef CACHE_LINE_SIZE
32
#   define ALIGN __attribute__((aligned(CACHE_LINE_SIZE)))
33
#else
34
#   define ALIGN
35
#endif
36
37
38 01873262 Georg Kunz
class cMessage;
39
40
/**
41
 * Simple thread pool implementation that maintains a work queue and a number of
42
 * worker threads. The number of thread is configurable in the omnetpp.ini file
43
 * via the thread-pool-size option in the General section (the default is 5 threads).
44
 *
45
 * @ingroup Horizon
46
 */
47
class SIM_API cSpinningThreadPool: public cThreadPool
48
{
49
50
public:
51
        /**
52
         * Stores the state of a thread. Using struct for padding to cache line.
53
         */
54 b9e9c37a Simon Tenbusch
    struct threadState_t
55
    {
56
        struct data
57
        {
58
            AO_t msg;
59
            simtime_t barrierTime;
60
            int priority;
61
            simtime_t parentStartTime;
62
            unsigned int parentExecutionOrderId;
63
            unsigned int schedulingOrderId;
64 006e5abb Simon Tenbusch
            unsigned long insertOrder;
65 b9e9c37a Simon Tenbusch
        } data;
66 01873262 Georg Kunz
#ifdef CACHE_LINE_SIZE
67 b9e9c37a Simon Tenbusch
        char padding[CACHE_LINE_SIZE-sizeof(struct data)];
68 01873262 Georg Kunz
#endif
69 b9e9c37a Simon Tenbusch
    } ALIGN;
70 01873262 Georg Kunz
71
        /**
72
         * array of states
73
         */
74
        threadState_t* threadStates;
75
76 8c63ddf5 Georg Kunz
private:
77 01873262 Georg Kunz
        /**
78
         * cancel flag
79
         */
80
        AO_t cancel;
81
82
        /**
83
         *
84
         */
85
        bool useMainThread;
86
87 2dd4eb12 Simon Tenbusch
    int barrierMin;
88
89 8c63ddf5 Georg Kunz
public:
90 01873262 Georg Kunz
        /**
91
         * Handle one event. Called either by a worker or the main thread.
92
         */
93
        void doOneEvent(cMessage* msg);
94
95
        /**
96
         *
97
         */
98
        int handleTaskWorkersOnly(cMessage* msg);
99
100
        /**
101
         *
102
         */
103
        int handleTaskWorkersAndMain(cMessage *msg);
104
105
106
107
108
public:
109
        /** @name Constructors, destructors, initialization and shutdown management */
110
        //@{
111
112
        /**
113
         * Constructor
114
         */
115
        cSpinningThreadPool();
116
117
        /**
118
         * Destructor
119
         */
120
        virtual ~cSpinningThreadPool();
121
122
        /**
123
         *
124
         */
125
        virtual void activate();
126
127
        /**
128
         * Cancels worker threads and clears task queues.
129
         */
130
        virtual void shutdown();
131
132
        /**
133
         * Waits till there is no pseudo barrier message at the beginning of the message queue.
134
         * @param barrierMin pointer to the first barrier currently active.
135
         *                   (set it to -1 for the first call, handover the returned value for
136
         *                   future calls)
137
         * @param msgQueue pointer to the msgQueue where the messages to compare come from.
138
         * @return updated barrierMin pointer
139
         */
140 2dd4eb12 Simon Tenbusch
        virtual void waitAtBarrier(cMessageHeap* msgQueue);
141 01873262 Georg Kunz
142
        /**
143 a3d116e3 Simon Tenbusch
         * Returns true if msg is scheduled before the minimal Barrier (barrierMin).
144
         * Returns false otherwise.
145
         */
146 2dd4eb12 Simon Tenbusch
        virtual bool isBeforeBarrier(cMessage* msg);
147 a3d116e3 Simon Tenbusch
148
        /**
149 01873262 Georg Kunz
         * Returns true iff there is currently no event being processed (and therefore
150
         * no pseudo barrier message active.
151
         */
152
        virtual bool barrierEmpty();
153
        //}@
154
155
156
        /** @name Internal use only */
157
        //@{
158
        /**
159
         * Main routing of the worker threads. Internal use only.
160
         */
161
        virtual void worker();
162
163
        /**
164
         * Inserts a new task in the task queue. Internal use only.
165
         */
166 2dd4eb12 Simon Tenbusch
        virtual void insertTask(cMessage* msg, simtime_t duration);
167 01873262 Georg Kunz
        //}@
168
};
169
170
#endif /* __CSPINNINGTHREADPOOL_H */