/[svn]/linuxsampler/trunk/src/common/Condition.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/common/Condition.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1560 - (hide annotations) (download) (as text)
Thu Dec 6 17:19:16 2007 UTC (16 years, 4 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 6334 byte(s)
* minor code refactoring of win32 Condition code
* preparations for release 0.5.1

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 1221 * Copyright (C) 2005 - 2007 Christian Schoenebeck *
7 schoenebeck 53 * *
8     * This program is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This program is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #ifndef __CONDITION_H__
25     #define __CONDITION_H__
26    
27     #include "Mutex.h"
28    
29 senoner 1481 #if defined(WIN32)
30     #include <windows.h>
31 schoenebeck 1560 #endif
32 senoner 1481
33 schoenebeck 1560 namespace LinuxSampler {
34 senoner 1481
35 schoenebeck 1560 #if defined(WIN32)
36     class ConditionInternal;
37 senoner 1481 #endif
38    
39 schoenebeck 53 /**
40     * Thread safe boolean condition.
41     *
42     * This is not meant to be used for real time operation!
43     */
44     class Condition : public Mutex {
45     public:
46     /**
47     * Constructor
48     *
49     * @param bInitialCondition - optional: starting condition
50     * (default = false)
51     */
52     Condition(bool bInitialCondition = false);
53    
54     /**
55     * Destructor
56     */
57 letz 502 virtual ~Condition();
58 schoenebeck 53
59     /**
60     * Blocks the calling thread if current condition equals
61     * \a bCondition, in this case the calling thread will be blocked
62     * until condition turns. Upon successful return the Condition
63     * object is locked, so the calling thread can safely run it's
64     * critical section and has to explicitly call Unlock() right after
65     * it left it's critcal section.
66     *
67 schoenebeck 1221 * @e Note: If you don't provide a timeout value or if you provide a
68     * timeout value of exactly 0s and 0ns, then this call will block
69 schoenebeck 1319 * without any timeout, or in other words: @e infinity!
70 schoenebeck 1221 *
71 schoenebeck 53 * @param bCondition - block in case of this condition
72     * @param TimeoutSeconds - optional: max. wait time in seconds
73     * (default: 0s)
74     * @param TimeoutNanoSeconds - optional: max wait time in nano
75     * seconds (default: 0ns)
76     * @returns 0 on success, a value less than 0 if timeout exceeded
77     */
78     int WaitIf(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L);
79    
80     /**
81     * Same as WaitIf(), except that WaitAndUnlockIf() will unlock the
82     * Condition object, so only use this call if you don't need to
83     * enter a thread critical section, otherwise use WaitIf() instead!
84     *
85 schoenebeck 1221 * @e Note: If you don't provide a timeout value or if you provide a
86     * timeout value of exactly 0s and 0ns, then this call will block
87 schoenebeck 1319 * without any timeout, or in other words: @e infinity!
88 schoenebeck 1221 *
89 schoenebeck 53 * @param bCondition - block in case of this condition
90     * @param TimeoutSeconds - optional: max. wait time in seconds
91     * (default: 0s)
92     * @param TimeoutNanoSeconds - optional: max wait time in nano
93     * seconds (default: 0ns)
94     * @returns 0 on success, a value less than 0 if timeout exceeded
95     * @see WaitIf()
96     */
97     int WaitAndUnlockIf(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L);
98    
99     /**
100     * Set Condition object to \a bCondition. Upon change of the
101     * condition, other threads waiting for \a bCondition will be
102 schoenebeck 63 * awakened. (Note the condition will not be locked for the calling
103     * thread after this method returns!)
104 schoenebeck 53 *
105     * @param bCondition - new condition
106     */
107     void Set(bool bCondition);
108    
109 schoenebeck 1221 /**
110 schoenebeck 1319 * Returns the current boolean state of this condition object. This
111     * method never blocks, it returns immediately and doesn't use any
112     * system calls.
113     *
114 schoenebeck 1231 * @e Caution: this method is not thread safe! If you need to use
115     * the condition state in a thread critical context you must call
116     * @c Lock() and @c Unlock() respectively by yourself!
117     */
118 schoenebeck 1221 bool GetUnsafe();
119    
120 schoenebeck 53 protected:
121 senoner 1481 #if defined(WIN32)
122 schoenebeck 1560 friend class ConditionInternal;
123     struct win32thread_cond_t {
124     int waiters_count_; ///< Number of waiting threads.
125     CRITICAL_SECTION waiters_count_lock_; ///< Serialize access to <waiters_count_>.
126     HANDLE sema_; ///< Semaphore used to queue up threads waiting for the condition to become signaled.
127     HANDLE waiters_done_; ///< An auto-reset event used by the broadcast/signal thread to wait for all the waiting thread(s) to wake up and be released from the semaphore.
128     size_t was_broadcast_; ///< Keeps track of whether we were broadcasting or signaling. This allows us to optimize the code if we're just signaling.
129     } __win32_true_condition, __win32_false_condition;
130 senoner 1481 #else
131 schoenebeck 53 pthread_cond_t __posix_true_condition;
132     pthread_cond_t __posix_false_condition;
133 senoner 1481 #endif
134 schoenebeck 53 bool bCondition;
135     };
136    
137 schoenebeck 880 } // namespace LinuxSampler
138    
139 schoenebeck 53 #endif // __CONDITION_H__

  ViewVC Help
Powered by ViewVC