Statistics
| Branch: | Revision:

root / include / cthreadpool.h @ 6b81f4fa

History | View | Annotate | Download (4.49 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 __CTHREADPOOL_H
20
#define __CTHREADPOOL_H
21

    
22
#include "simkerneldefs.h"
23
#include "cobject.h"
24
#include "cmessageheap.h"
25
#include <pthread.h>
26

    
27
class cThreadPool;
28
class cThreadLocalData;
29
class cMessage;
30
class cComponent;
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 cThreadPool : public cObject
40
{
41
  protected:
42
    /**
43
     * flag indicating that the thread pool already shut down
44
     */
45
    bool shutdownDone;
46

    
47
    /**
48
     * helper method that encapsulates initialization and management
49
     */
50
    static cThreadLocalData* getLocalData();
51

    
52
    /**
53
         * IDs of the worker threads
54
         */
55
        pthread_t* workerIDs;
56

    
57
        /**
58
         * the number of worker threads
59
         */
60
    unsigned int numThreads;
61

    
62

    
63
  public:
64
    /** @name Constructors, destructors, initialization and shutdown management */
65
    //@{
66

    
67
    /**
68
     * Constructor
69
     */
70
    cThreadPool();
71

    
72
    /**
73
     * Destructor
74
     */
75
    virtual ~cThreadPool();
76

    
77
    /**
78
     * Creates worker threads.
79
     */
80
    virtual void activate();
81

    
82
    /**
83
     * Cancels worker threads and clears task queues.
84
     */
85
    virtual void shutdown();
86

    
87
    /**
88
     * Sets the defaultowner on a per thread basis.
89
     */
90
    static void setDefaultOwner(cDefaultList* list);
91

    
92
    /**
93
     * Gets the default owner of this thread
94
     */
95
    static cDefaultList* getDefaultOwner();
96

    
97
    /**
98
     * Sets the current simulation time on a per thread basis.
99
     */
100
    static void setSimTime(simtime_t t);
101

    
102
    /**
103
     * Gets the current simulation time of this thread
104
     */
105
    static simtime_t getSimTime();
106

    
107
    /**
108
     * get the current module
109
     */
110
    static cComponent* getContext();
111

    
112
    /**
113
     * set the current module
114
     */
115
    static void setContext(cComponent* mod);
116

    
117
    /**
118
     * get the number of threads
119
     */
120
    unsigned int getNumThreads() {return numThreads;}
121

    
122
        /**
123
         * Waits till there is no pseudo barrier message at the beginning of the message queue.
124
         * @param msgQueue pointer to the msgQueue where the messages to compare come from.
125
         */
126
        virtual void waitAtBarrier(cMessageHeap* msgQueue) = 0;
127

    
128
        /**
129
         * Returns true iff there is currently no event being processed (and therefore
130
         * no pseudo barrier message active.
131
         */
132
        virtual bool barrierEmpty() = 0;
133
    //}@
134

    
135
    /** @name Internal use only */
136
    //@{
137
    /**
138
     * Main routing of the worker threads. Internal use only.
139
     */
140
    virtual void worker() = 0;
141

    
142
    /**
143
     * Inserts a new task in the task queue. Internal use only.
144
     */
145
    virtual void insertTask(cMessage* msg, simtime_t duration) = 0;
146

    
147
    /**
148
     * Used by cSpinningThreadpool
149
     * Returns true if msg is scheduled before the minimal Barrier (barrierMin).
150
     * Returns false otherwise.
151
     */
152
    virtual bool isBeforeBarrier(cMessage* msg) = 0;
153
    //}@
154
};
155

    
156

    
157
/**
158
 * This class stores the local data of each thread
159
 */
160
class cThreadLocalData
161
{
162
    private:
163
        simtime_t time;               // the current simulation time per thread
164
        cDefaultList* defaultOwner;   // the current defaultOwner / module in context per thread
165
        cComponent* contextModule;       // the module currently in context
166

    
167
    public:
168
        cThreadLocalData();
169
        virtual ~cThreadLocalData();
170

    
171
        simtime_t getSimTime()                    {return time;}
172
        void setSimTime(simtime_t time)           {this->time = time;}
173

    
174
        cDefaultList* getDefaultOwner()           {return defaultOwner;}
175
        void setDefaultOwner(cDefaultList* owner) {this->defaultOwner = owner;}
176

    
177
        cComponent* getContextModule()               {return contextModule;}
178
        void setContextModule(cComponent* mod)       {this->contextModule = mod;}
179
};
180

    
181

    
182
#endif /* __CTHREADPOOL_H */