diff options
author | Mike Buland <eichlan@xagasoft.com> | 2007-04-26 15:06:49 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2007-04-26 15:06:49 +0000 |
commit | 530014a3cce53e86dce8917e98a4e86d02f176aa (patch) | |
tree | c667c996fb91692b101f75296206b8420f19bf73 /src/itocondition.h | |
parent | 066282ae6de25cf92780dbdaa2fd70a033e95659 (diff) | |
download | libbu++-530014a3cce53e86dce8917e98a4e86d02f176aa.tar.gz libbu++-530014a3cce53e86dce8917e98a4e86d02f176aa.tar.bz2 libbu++-530014a3cce53e86dce8917e98a4e86d02f176aa.tar.xz libbu++-530014a3cce53e86dce8917e98a4e86d02f176aa.zip |
Merged Ito and put it in the BU namespace. I should probably clean up the
formatting on the comments, some of the lines wrap, but I'm not too worried
about it right now. I also fixed up the doxygen config and build.conf files
so that everything is building nice and smooth now.
Diffstat (limited to '')
-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..4771b22 --- /dev/null +++ b/src/itocondition.h | |||
@@ -0,0 +1,81 @@ | |||
1 | #ifndef ITO_CONDITION_H | ||
2 | #define 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 | ||