Statistics
| Branch: | Revision:

root / include / cspinningthreadpool.h @ fbe00e73

History | View | Annotate | Download (3.66 KB)

1
//==========================================================================
2
//   CSPINNINGTHREADPOOL.H  -  header for
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  Declaration of the following classes:
8
//    cSpinningThreadPool : 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
#include "csimplemodule.h"
24

    
25
#ifdef ATOMIC_OPS_DEBUG
26
        #include "catomicopsdebug.h"
27
#else
28
        #include <atomic_ops.h>
29
#endif
30

    
31
#ifdef CACHE_LINE_SIZE
32
#   define ALIGN __attribute__((aligned(CACHE_LINE_SIZE)))
33
#else
34
#   define ALIGN
35
#endif
36

    
37

    
38
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
    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
            unsigned long insertOrder;
65
        } data;
66
#ifdef CACHE_LINE_SIZE
67
        char padding[CACHE_LINE_SIZE-sizeof(struct data)];
68
#endif
69
    } ALIGN;
70

    
71
        /**
72
         * array of states
73
         */
74
        threadState_t* threadStates;
75

    
76
private:
77
        /**
78
         * cancel flag
79
         */
80
        AO_t cancel;
81

    
82
        /**
83
         *
84
         */
85
        bool useMainThread;
86

    
87
    int barrierMin;
88

    
89
public:
90
        /**
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
        virtual void waitAtBarrier(cMessageHeap* msgQueue);
141

    
142
        /**
143
         * Returns true if msg is scheduled before the minimal Barrier (barrierMin).
144
         * Returns false otherwise.
145
         */
146
        virtual bool isBeforeBarrier(cMessage* msg);
147

    
148
        /**
149
         * 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
        virtual void insertTask(cMessage* msg, simtime_t duration);
167
        //}@
168
};
169

    
170
#endif /* __CSPINNINGTHREADPOOL_H */