Statistics
| Branch: | Revision:

root / include / cspinningthreadpool.h @ 96cc8d6f

History | View | Annotate | Download (3.26 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

    
42
public:
43
        /**
44
         * Stores the state of a thread. Using struct for padding to cache line.
45
         */
46
        struct threadState_t
47
        {
48
                AO_t msg;
49
                simtime_t barrierTime;
50
                int priority;
51
#ifdef CACHE_LINE_SIZE
52
                char padding[CACHE_LINE_SIZE-sizeof(AO_t)-sizeof(simtime_t)-sizeof(int)];
53
#endif
54
        };
55

    
56

    
57
        /**
58
         * array of states
59
         */
60
        threadState_t* threadStates;
61

    
62
private:
63
        /**
64
         * cancel flag
65
         */
66
        AO_t cancel;
67

    
68
        /**
69
         *
70
         */
71
        bool useMainThread;
72

    
73
    int barrierMin;
74

    
75
public:
76
        /**
77
         * Handle one event. Called either by a worker or the main thread.
78
         */
79
        void doOneEvent(cMessage* msg);
80

    
81
        /**
82
         *
83
         */
84
        int handleTaskWorkersOnly(cMessage* msg);
85

    
86
        /**
87
         *
88
         */
89
        int handleTaskWorkersAndMain(cMessage *msg);
90

    
91

    
92

    
93

    
94
public:
95
        /** @name Constructors, destructors, initialization and shutdown management */
96
        //@{
97

    
98
        /**
99
         * Constructor
100
         */
101
        cSpinningThreadPool();
102

    
103
        /**
104
         * Destructor
105
         */
106
        virtual ~cSpinningThreadPool();
107

    
108
        /**
109
         *
110
         */
111
        virtual void activate();
112

    
113
        /**
114
         * Cancels worker threads and clears task queues.
115
         */
116
        virtual void shutdown();
117

    
118
        /**
119
         * Waits till there is no pseudo barrier message at the beginning of the message queue.
120
         * @param barrierMin pointer to the first barrier currently active.
121
         *                   (set it to -1 for the first call, handover the returned value for
122
         *                   future calls)
123
         * @param msgQueue pointer to the msgQueue where the messages to compare come from.
124
         * @return updated barrierMin pointer
125
         */
126
        virtual void waitAtBarrier(cMessageHeap* msgQueue);
127

    
128
        /**
129
         * Returns true if msg is scheduled before the minimal Barrier (barrierMin).
130
         * Returns false otherwise.
131
         */
132
        virtual bool isBeforeBarrier(cMessage* msg);
133

    
134
        /**
135
         * Returns true iff there is currently no event being processed (and therefore
136
         * no pseudo barrier message active.
137
         */
138
        virtual bool barrierEmpty();
139
        //}@
140

    
141

    
142
        /** @name Internal use only */
143
        //@{
144
        /**
145
         * Main routing of the worker threads. Internal use only.
146
         */
147
        virtual void worker();
148

    
149
        /**
150
         * Inserts a new task in the task queue. Internal use only.
151
         */
152
        virtual void insertTask(cMessage* msg, simtime_t duration);
153
        //}@
154
};
155

    
156
#endif /* __CSPINNINGTHREADPOOL_H */