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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1424 - (hide annotations) (download) (as text)
Sun Oct 14 22:00:17 2007 UTC (16 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 6589 byte(s)
* code cleanup:
- global.h now only covers global definitions that are needed for the C++
  API header files, all implementation internal global definitions are now
  in global_private.h
- atomic.h is not exposed to the C++ API anymore (replaced the references
  in SynchronizedConfig.h for this with local definitions)
- no need to include config.h anymore for using LS's API header files
- DB instruments classes are not exposed to the C++ API
- POSIX callback functions of Thread.h are hidden
- the (optional) gig Engine benchmark compiles again
- updated Doxyfile.in
- fixed warnings in API doc generation
* preparations for release 0.5.0

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 1424 * 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 __CONDITIONSERVER_H__
25     #define __CONDITIONSERVER_H__
26    
27     #include "Mutex.h"
28     #include "Condition.h"
29    
30 schoenebeck 880 namespace LinuxSampler {
31    
32 schoenebeck 53 /**
33     * Thread safe condition for semi real time operation
34     *
35     * Sense behind the ConditionServer is to sync a boolean condition between
36     * one real time thread (RTT) and n non real time threads (NRTT).
37     *
38     * 1 Real Time Thread <--> Condition Object <--> n Non Real Time Threads
39     *
40     * The non real time threads set the condition by calling Push(), this
41     * method will immediately return if the current condition already equals
42     * the desired condition given with Push(), if it's not equal then Push()
43     * will cause a request to change the condition and will block until the
44     * condition actually changed, which happens when the RTT calls the Pop()
45     * method, which will also return the new condition to the RTT.
46     *
47     * Advantage of this technique is that the RTT doesn't has to execute system
48     * calls whenever it wants to check for a condition change, the RTT only has
49     * to execute one system call when the condition changed. Disadvantage on
50     * the other hand is that the NRTTs might be blocked for a long time, but
51     * this is usually unproblematic for NRTTs.
52     */
53     class ConditionServer {
54     public:
55     ConditionServer();
56    
57    
58     // methods for non realtime threads (0..n NRTTs)
59    
60     /**
61     * Set condition to \a bCondition. If current condition already
62     * equals \a bCondition, then this method will immediately return,
63     * if not it will block the calling thread until the condition
64     * actually changed to the requested condition (which happens when
65     * Pop() is called by the real time thread). If there are multiple
66     * non real time threads calling Push() only one by one will be
67     * served, all others blocked meanwhile. When the calling thread
68     * returns from Push() the Push() method will still be blocked
69     * against other NRTTs, so the thread can safely enter a critical
70     * section and has to Unlock() right after it left it's critical
71     * section, so other NRTTs can pass Push().
72     *
73     * @param bCondition - condition to set
74     * @param TimeoutSeconds - optional: max. wait time in seconds
75     * (default: 0s)
76     * @param TimeoutNanoSeconds - optional: max wait time in nano
77     * seconds (default: 0ns)
78     * @returns bool pointer with condition before Push() call, NULL if
79     * timeout exceeded
80     * @see Unlock()
81     */
82     bool* Push(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L);
83    
84     /**
85     * Same as Push(), except that PushAndUnlock() will unlock the
86     * ConditionServer right after so that other NRTTs can follow to
87     * pass push. You should only call this method if you're sure that
88     * no other NRTT will change the condition, otherwise you should
89     * call Push() instead, execute the critical section and manually
90     * unlock at the end of the critical section.
91     *
92     * @param bCondition - condition to set
93     * @param TimeoutSeconds - optional: max. wait time in seconds
94     * (default: 0s)
95     * @param TimeoutNanoSeconds - optional: max wait time in nano
96     * seconds (default: 0ns)
97     * @returns bool pointer with condition before PushAndUnlock()
98     * call, NULL if timeout exceeded
99     */
100     bool* PushAndUnlock(bool bCondition, long TimeoutSeconds = 0L, long TimeoutNanoSeconds = 0L);
101    
102     /**
103     * Should be called by the NRTT after it left it's critical section
104     * to unlock the ConditionServer and give other NRTTs the chance to
105     * pass Push().
106     */
107     void Unlock();
108    
109     /**
110     * Returns unsafely the current condition. This method will not
111     * block and should only be used in a not thread critical context.
112     *
113     * @returns current condition (unsafe)
114     */
115     bool GetUnsafe();
116    
117    
118    
119     // method for real time thread (1 RTT)
120    
121     /**
122     * Should frequently be called by the real time thread (RTT) to
123     * check for a condition change. If a NRTT requested a condition
124     * change by calling Push() ot PushAndUnlock() the NRTT wil be
125     * blocked until the RTT calls Pop(), means until the RTT actually
126     * knows about the condition change.
127     *
128     * @returns current condition
129     */
130     bool Pop();
131    
132     protected:
133     bool bConditionQuick;
134     bool bChangeRequest;
135     bool bOldCondition;
136     Condition SyncCondition;
137     Mutex PushMutex;
138     };
139    
140 schoenebeck 880 } // namespace LinuxSampler
141    
142 schoenebeck 53 #endif // __CONDITIONSERVER_H__

  ViewVC Help
Powered by ViewVC