Project

General

Profile

Statistics
| Branch: | Revision:

root / include / cspinningthreadpool.h @ 47c4b975

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