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_
27 class SC_SyncCondition
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
);
42 pthread_mutex_destroy (&mutex
);
43 pthread_cond_destroy (&available
);
48 // waits if it has caught up.
49 // not very friendly, may be trying in vain to keep up.
50 pthread_mutex_lock (&mutex
);
52 pthread_cond_wait (&available
, &mutex
);
54 pthread_mutex_unlock (&mutex
);
59 // waits if not signaled since last time.
60 // if only a little late then can still go.
62 pthread_mutex_lock (&mutex
);
63 writeSnapshot
= write
;
64 while (read
== writeSnapshot
)
65 pthread_cond_wait (&available
, &mutex
);
67 pthread_mutex_unlock (&mutex
);
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
);
78 pthread_cond_wait (&available
, &mutex
);
79 pthread_mutex_unlock (&mutex
);
85 pthread_cond_signal (&available
);
89 pthread_cond_t available
;
90 pthread_mutex_t mutex
;