Statistics
| Branch: | Revision:

root / include / cthreadpool.h @ 96cc8d6f

History | View | Annotate | Download (4.47 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 cThreadLocalData;
28
class cMessage;
29
class cComponent;
30

    
31
/**
32
 * Simple thread pool implementation that maintains a work queue and a number of
33
 * worker threads. The number of thread is configurable in the omnetpp.ini file
34
 * via the thread-pool-size option in the General section (the default is 5 threads).
35
 *
36
 * @ingroup Horizon
37
 */
38
class SIM_API cThreadPool : public cObject
39
{
40
  protected:
41
    /**
42
     * flag indicating that the thread pool already shut down
43
     */
44
    bool shutdownDone;
45

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

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

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

    
61

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
155

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

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

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

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

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

    
180

    
181
#endif /* __CTHREADPOOL_H */