Explicitly include a boost "windows" folder even on linux
[supercollider.git] / include / common / SC_SyncCondition.h
blob1571cac2636f42eb5b8879bc73d190ab5386b433
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef _SC_SyncCondition_
23 #define _SC_SyncCondition_
25 #include <pthread.h>
27 class SC_SyncCondition
29 public:
30 SC_SyncCondition()
31 : read(0), write(0)
33 // the mutex is only for pthread_cond_wait, which requires it.
34 // since there is only supposed to be one signaller and one waiter
35 // there is nothing to mutually exclude.
36 pthread_mutex_init (&mutex, NULL);
37 pthread_cond_init (&available, NULL);
40 ~SC_SyncCondition()
42 pthread_mutex_destroy (&mutex);
43 pthread_cond_destroy (&available);
46 void WaitEach()
48 // waits if it has caught up.
49 // not very friendly, may be trying in vain to keep up.
50 pthread_mutex_lock (&mutex);
51 while (read == write)
52 pthread_cond_wait (&available, &mutex);
53 ++read;
54 pthread_mutex_unlock (&mutex);
57 void WaitOnce()
59 // waits if not signaled since last time.
60 // if only a little late then can still go.
61 int writeSnapshot;
62 pthread_mutex_lock (&mutex);
63 writeSnapshot = write;
64 while (read == writeSnapshot)
65 pthread_cond_wait (&available, &mutex);
66 read = writeSnapshot;
67 pthread_mutex_unlock (&mutex);
70 void WaitNext()
72 // will wait for the next signal after the read = write statement
73 // this is the friendliest to other tasks, because if it is
74 // late upon entry, then it has to lose a turn.
75 pthread_mutex_lock (&mutex);
76 read = write;
77 while (read == write)
78 pthread_cond_wait (&available, &mutex);
79 pthread_mutex_unlock (&mutex);
82 void Signal()
84 ++write;
85 pthread_cond_signal (&available);
88 private:
89 pthread_cond_t available;
90 pthread_mutex_t mutex;
91 int read, write;
94 #endif