aboutsummaryrefslogtreecommitdiff
path: root/src/condition.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-10-27 04:44:46 +0000
committerMike Buland <eichlan@xagasoft.com>2011-10-27 04:44:46 +0000
commit9906ffe3c54875133448134c09ec12a0949d48cd (patch)
tree0542fef3d27e796700b87b44394a3ad31dd5b852 /src/condition.h
parent411f240da34bab53cd18aa8b7ba09834ede49b1c (diff)
parent029b5d159023f4dad607359dbfaa2479e21fe9e5 (diff)
downloadlibbu++-9906ffe3c54875133448134c09ec12a0949d48cd.tar.gz
libbu++-9906ffe3c54875133448134c09ec12a0949d48cd.tar.bz2
libbu++-9906ffe3c54875133448134c09ec12a0949d48cd.tar.xz
libbu++-9906ffe3c54875133448134c09ec12a0949d48cd.zip
Reorg'd! I merged in the release-fixup branch and fixed all random warnings.
I also cleaned up the build script, the symlink generation is faster and looks nicer, there's one think left to fix there, but it's not too bad.
Diffstat (limited to 'src/condition.h')
-rw-r--r--src/condition.h90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/condition.h b/src/condition.h
new file mode 100644
index 0000000..71634f5
--- /dev/null
+++ b/src/condition.h
@@ -0,0 +1,90 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#ifndef BU_CONDITION_H
9#define BU_CONDITION_H
10
11#include <pthread.h>
12
13#include "bu/mutex.h"
14
15namespace Bu
16{
17 /**
18 * Ito condition. This is a fairly simple condition mechanism. As you may
19 * notice this class inherits from the Mutex class, this is because all
20 * conditions must be within a locked block. The standard usage of a
21 * condition is to pause one thread, perhaps indefinately, until another
22 * thread signals that it is alright to procede.
23 * <br>
24 * Standard usage for the thread that wants to wait is as follows:
25 * <pre>
26 * Condition cond;
27 * ... // Perform setup and enter your run loop
28 * cond.lock();
29 * while( !isFinished() ) // Could be anything you're waiting for
30 * cond.wait();
31 * ... // Take care of what you have to.
32 * cond.unlock();
33 * </pre>
34 * The usage for the triggering thread is much simpler, when it needs to
35 * tell the others that it's time to grab some data it calls either signal
36 * or broadcast. See both of those functions for the difference.
37 *@ingroup Threading
38 */
39 class Condition : public Mutex
40 {
41 public:
42 /**
43 * Create a condition.
44 */
45 Condition();
46
47 /**
48 * Destroy a condition.
49 */
50 ~Condition();
51
52 /**
53 * Wait forever, or until signalled. This has to be called from within
54 * a locked section, i.e. before calling this this object's lock
55 * function should be called.
56 */
57 int wait();
58
59 /**
60 * Wait for a maximum of nSec seconds and nUSec micro-seconds or until
61 * signalled. This is a little more friendly function if you want to
62 * perform other operations in the thrad loop that calls this function.
63 * Like the other wait function, this must be inside a locked section.
64 *@param nSec The seconds to wait.
65 *@param nUSec the micro-seconds to wait.
66 */
67 int wait( int nSec, int nUSec );
68
69 /**
70 * Notify the next thread waiting on this condition that they can go
71 * ahead. This only signals one thread, the next one in the condition
72 * queue, that it is safe to procede with whatever operation was being
73 * waited on.
74 */
75 int signal();
76
77 /**
78 * Notify all threads waiting on this condition that they can go ahead
79 * now. This function is slower than signal, but more effective in
80 * certain situations where you may not know how many threads should be
81 * activated.
82 */
83 int broadcast();
84
85 private:
86 pthread_cond_t cond; /**< Internal condition reference. */
87 };
88}
89
90#endif