Statistics
| Branch: | Revision:

root / include / clockedthreadpool.h @ 47c4b975

History | View | Annotate | Download (3.45 KB)

1
//==========================================================================
2
//   CLOCKEDTHREADPOOL.H  -  header for
3
//                     Horizon/OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//
7
//  Declaration of the following classes:
8
//    cLockedThreadpool: thread pool for asynchronous event handling based
9
//                       on the pthread library
10
//
11
//==========================================================================
12

    
13
/*--------------------------------------------------------------*
14
  Copyright (C) 2010 Georg Kunz
15

16
  This file is distributed WITHOUT ANY WARRANTY. See the file
17
  `license' for details on this and other legal matters.
18
*--------------------------------------------------------------*/
19

    
20
#ifndef __CLOCKEDTHREADPOOL_H
21
#define __CLOCKEDTHREADPOOL_H
22

    
23
#include <pthread.h>
24
#include <list>
25

    
26
#include "simkerneldefs.h"
27
#include "cobject.h"
28
#include "cthreadpool.h"
29

    
30
class cMessage;
31

    
32

    
33
/**
34
 * Simple thread pool implementation that maintains a work queue and a number of
35
 * worker threads. The number of threads is configurable in the omnetpp.ini file
36
 * via the thread-pool-size option in the General section (the default is 5 threads).
37
 * Synchronization is achieved by means of the pthread library.
38
 *
39
 * @ingroup Horizon
40
 */
41
class SIM_API cLockedThreadPool : public cThreadPool
42
{
43
  private:
44
    std::list<cMessage*> tasks;
45

    
46
    // the number of worker threads
47
    unsigned int numThreads;
48

    
49
    // IDs of the worker threads
50
    pthread_t* workerIDs;
51

    
52
    // protects the task queue
53
    pthread_mutex_t mutex;
54

    
55
    // used to suspend worker thread when no work is to be done
56
    pthread_cond_t nonEmpty;
57

    
58
    // mutex used to protect the condition variable for blocking the main thread
59
    pthread_mutex_t condMutex;
60

    
61
    // condition variable used to block the main thread until the threads are done
62
    pthread_cond_t threadsDone;
63

    
64
    // flag indicating that the thread pool already shut down
65
    bool shutdownDone;
66

    
67

    
68
  public:
69
    /** @name Constructors, destructors, initialization and shutdown management */
70
    //@{
71

    
72
    /**
73
     * Constructor
74
     */
75
    cLockedThreadPool();
76

    
77
    /**
78
     * Destructor
79
     */
80
    virtual ~cLockedThreadPool();
81

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

    
88

    
89
    /** @name Internal API */
90
    //@{
91
    /**
92
     * Waits till there is no pseudo barrier message at the beginning of the message queue.
93
     * @param barrierMin pointer to the first barrier currently active.
94
     *                   (set it to -1 for the first call, handover the returned value for
95
     *                   future calls)
96
     * @param msgQueue pointer to the msgQueue where the messages to compare come from.
97
     * @return updated barrierMin pointer
98
     */
99
    virtual void waitAtBarrier(cMessageHeap* msgQueue);
100

    
101
    /**
102
     * Returns true iff there is currently no event being processed (and therefore
103
     * no pseudo barrier message active.
104
     */
105
    virtual bool barrierEmpty();
106

    
107
    /**
108
     * Main routing of the worker threads. Internal use only.
109
     */
110
    void worker();
111

    
112
    /**
113
     * Inserts a new task in the task queue. Internal use only.
114
     */
115
    virtual void insertTask(cMessage* msg, simtime_t duration);
116

    
117
    /**
118
     * Used By cSpinningThreadpool
119
     * Returns true if msg is scheduled before the minimal Barrier (barrierMin).
120
     * Returns false otherwise.
121
     */
122
    virtual bool isBeforeBarrier(cMessage* msg);
123
    //}@
124
};
125

    
126
#endif /* __CLOCKEDTHREADPOOL_H */