Statistics
| Branch: | Revision:

root / include / cspinningthreadpool.h @ ffa9279f

History | View | Annotate | Download (3.61 KB)

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