Statistics
| Branch: | Revision:

root / include / cspinningthreadpool.h @ a3d116e3

History | View | Annotate | Download (3.45 KB)

1
//==========================================================================
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
public:
49
        /**
50
         * Stores the state of a thread. Using struct for padding to cache line.
51
         */
52
        typedef struct threadState_t
53
        {
54
                AO_t msg;
55
                simtime_t barrierTime;
56
                int priority;
57
#ifdef CACHE_LINE_SIZE
58
        } threadState_t __attribute__((aligned(CACHE_LINE_SIZE)));
59
#else
60
        } threadState_t;
61
#endif
62

    
63
        /**
64
         * array of states
65
         */
66
#ifdef CACHE_LINE_SIZE
67
        threadState_t* threadStates __attribute__((aligned(CACHE_LINE_SIZE)));
68
#else
69
        threadState_t* threadStates;
70
#endif
71

    
72
        /**
73
         * cancel flag
74
         */
75
        AO_t cancel;
76

    
77
        /**
78
         *
79
         */
80
        bool useMainThread;
81

    
82
        /**
83
         * Handle one event. Called either by a worker or the main thread.
84
         */
85
        void doOneEvent(cMessage* msg);
86

    
87
        /**
88
         *
89
         */
90
        int handleTaskWorkersOnly(cMessage* msg);
91

    
92
        /**
93
         *
94
         */
95
        int handleTaskWorkersAndMain(cMessage *msg);
96

    
97

    
98

    
99

    
100
public:
101
        /** @name Constructors, destructors, initialization and shutdown management */
102
        //@{
103

    
104
        /**
105
         * Constructor
106
         */
107
        cSpinningThreadPool();
108

    
109
        /**
110
         * Destructor
111
         */
112
        virtual ~cSpinningThreadPool();
113

    
114
        /**
115
         *
116
         */
117
        virtual void activate();
118

    
119
        /**
120
         * Cancels worker threads and clears task queues.
121
         */
122
        virtual void shutdown();
123

    
124
        /**
125
         * Waits till there is no pseudo barrier message at the beginning of the message queue.
126
         * @param barrierMin pointer to the first barrier currently active.
127
         *                   (set it to -1 for the first call, handover the returned value for
128
         *                   future calls)
129
         * @param msgQueue pointer to the msgQueue where the messages to compare come from.
130
         * @return updated barrierMin pointer
131
         */
132
        virtual int waitAtBarrier(int barrierMin,cMessageHeap* msgQueue);
133

    
134
        /**
135
         * Returns true if msg is scheduled before the minimal Barrier (barrierMin).
136
         * Returns false otherwise.
137
         */
138
        virtual bool isBeforeBarrier(int barrierMin, cMessage* msg);
139

    
140
        /**
141
         * Returns true iff there is currently no event being processed (and therefore
142
         * no pseudo barrier message active.
143
         */
144
        virtual bool barrierEmpty();
145
        //}@
146

    
147

    
148
        /** @name Internal use only */
149
        //@{
150
        /**
151
         * Main routing of the worker threads. Internal use only.
152
         */
153
        virtual void worker();
154

    
155
        /**
156
         * Inserts a new task in the task queue. Internal use only.
157
         */
158
        virtual int insertTask(cMessage* msg, simtime_t duration, int barrierMin);
159
        //}@
160
};
161

    
162
#endif /* __CSPINNINGTHREADPOOL_H */