diff options
Diffstat (limited to 'src/itocondition.h')
-rw-r--r-- | src/itocondition.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/itocondition.h b/src/itocondition.h new file mode 100644 index 0000000..1793f81 --- /dev/null +++ b/src/itocondition.h | |||
@@ -0,0 +1,81 @@ | |||
1 | #ifndef BU_ITO_CONDITION_H | ||
2 | #define BU_ITO_CONDITION_H | ||
3 | |||
4 | #include <pthread.h> | ||
5 | |||
6 | #include "itomutex.h" | ||
7 | |||
8 | namespace Bu | ||
9 | { | ||
10 | /** | ||
11 | * Ito condition. This is a fairly simple condition mechanism. As you may | ||
12 | * notice this class inherits from the ItoMutex class, this is because all | ||
13 | * conditions must be within a locked block. The standard usage of a condition | ||
14 | * is to pause one thread, perhaps indefinately, until another thread signals | ||
15 | * that it is alright to procede. | ||
16 | * <br> | ||
17 | * Standard usage for the thread that wants to wait is as follows: | ||
18 | * <pre> | ||
19 | * ItoCondition cond; | ||
20 | * ... // Perform setup and enter your run loop | ||
21 | * cond.lock(); | ||
22 | * while( !isFinished() ) // Could be anything you're waiting for | ||
23 | * cond.wait(); | ||
24 | * ... // Take care of what you have to. | ||
25 | * cond.unlock(); | ||
26 | * </pre> | ||
27 | * The usage for the triggering thread is much simpler, when it needs to tell | ||
28 | * the others that it's time to grab some data it calls either signal or | ||
29 | * broadcast. See both of those functions for the difference. | ||
30 | *@author Mike Buland | ||
31 | */ | ||
32 | class ItoCondition : public ItoMutex | ||
33 | { | ||
34 | public: | ||
35 | /** | ||
36 | * Create a condition. | ||
37 | */ | ||
38 | ItoCondition(); | ||
39 | |||
40 | /** | ||
41 | * Destroy a condition. | ||
42 | */ | ||
43 | ~ItoCondition(); | ||
44 | |||
45 | /** | ||
46 | * Wait forever, or until signalled. This has to be called from within a | ||
47 | * locked section, i.e. before calling this this object's lock function | ||
48 | * should be called. | ||
49 | */ | ||
50 | int wait(); | ||
51 | |||
52 | /** | ||
53 | * Wait for a maximum of nSec seconds and nUSec micro-seconds or until | ||
54 | * signalled. This is a little more friendly function if you want to | ||
55 | * perform other operations in the thrad loop that calls this function. | ||
56 | * Like the other wait function, this must be inside a locked section. | ||
57 | *@param nSec The seconds to wait. | ||
58 | *@param nUSec the micro-seconds to wait. | ||
59 | */ | ||
60 | int wait( int nSec, int nUSec ); | ||
61 | |||
62 | /** | ||
63 | * Notify the next thread waiting on this condition that they can go ahead. | ||
64 | * This only signals one thread, the next one in the condition queue, that | ||
65 | * it is safe to procede with whatever operation was being waited on. | ||
66 | */ | ||
67 | int signal(); | ||
68 | |||
69 | /** | ||
70 | * Notify all threads waiting on this condition that they can go ahead now. | ||
71 | * This function is slower than signal, but more effective in certain | ||
72 | * situations where you may not know how many threads should be activated. | ||
73 | */ | ||
74 | int broadcast(); | ||
75 | |||
76 | private: | ||
77 | pthread_cond_t cond; /**< Internal condition reference. */ | ||
78 | }; | ||
79 | } | ||
80 | |||
81 | #endif | ||