File: StopSignalThread.h

package info (click to toggle)
passage 4%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 2,692 kB
  • ctags: 2,203
  • sloc: cpp: 18,864; objc: 272; sh: 134; makefile: 120; perl: 67; ansic: 44
file content (104 lines) | stat: -rw-r--r-- 1,934 bytes parent folder | download | duplicates (30)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Modification History
 *
 * 2002-April-4    Jason Rohrer
 * Created.
 * Changed to reflect the fact that the base class
 * destructor is called *after* the derived class destructor.
 *
 * 2003-September-5   Jason Rohrer
 * Moved into minorGems.
 *
 * 2004-November-19   Jason Rohrer
 * Changed to virtual inheritance from Thread class.
 *
 * 2005-January-9   Jason Rohrer
 * Changed to sleep on a semaphore to make sleep interruptable by stop.
 */



#ifndef STOP_SIGNAL_THREAD_INCLUDED
#define STOP_SIGNAL_THREAD_INCLUDED



#include "minorGems/system/Thread.h"
#include "minorGems/system/MutexLock.h"
#include "minorGems/system/BinarySemaphore.h"



/**
 * Abstract subclass of thread that has a stop signal.
 *
 * Note that subclasses MUST check the isStopped() function
 * periodically in their run() function for this class to work
 * properly.
 *
 * @author Jason Rohrer
 */
class StopSignalThread : public virtual Thread {



    public:


        
        /**
         * Only destroys this thread.
         * Does not stop or join.
         */
        virtual ~StopSignalThread();



    protected:


        
        StopSignalThread();


        
        // overrides Thread::sleep to make it interruptable by our stop call
        virtual void sleep( unsigned long inTimeInMilliseconds );
        
        

        /**
         * Signals this thread to stop, interrupting it if it is sleeping.
         *
         * Thread safe.
         *
         * Thread must be joined after this call returns.
         */
        void stop();

        
        
        /**
         * Gets whether this thread has been signaled to stop.
         *
         * Thread safe.
         *
         * @return true if this thread should stop.
         */
        char isStopped();


        
    private:

        MutexLock *mStopLock;
        char mStopped;

        BinarySemaphore *mSleepSemaphore;
        
        
    };



#endif