1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
5
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.
10
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.
15
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
19 */
20
21 #pragma once
22
23 #include "VMGlobals.h"
24 #include "SC_Export.h"
25 #include "SC_Lock.h"
26 #include <cerrno>
27
28 extern timed_mutex gLangMutex;
29
30 double elapsedTime();
31 double monotonicClockTime();
32 int64 OSCTime();
33
34 int64 ElapsedTimeToOSC(double elapsed);
35 double OSCToElapsedTime(int64 oscTime);
36
37 bool addheap(VMGlobals* g, PyrObject* heap, double schedtime, PyrSlot* task);
38 bool lookheap(PyrObject* heap, double* schedtime, PyrSlot* task);
39 bool getheap(VMGlobals* g, PyrObject* heap, double* schedtime, PyrSlot* task);
40 void offsetheap(VMGlobals* g, PyrObject* heap, double offset);
41 void dumpheap(PyrObject* heap);
42
43 const double kSecondsToOSC = 4294967296.; // pow(2,32)/1
44 const double kMicrosToOSC = 4294.967296; // pow(2,32)/1e6
45 const double kNanosToOSC = 4.294967296; // pow(2,32)/1e9
46 const double kOSCtoSecs = 2.328306436538696e-10; // 1/pow(2,32)
47 const double kOSCtoNanos = 0.2328306436538696; // 1e9/pow(2,32)
48
49 // lock language,
50 // if shouldBeRunning == false, return EINTR
51 // if language has been locked, return 0
lockLanguageOrQuit(bool shouldBeRunning)52 inline int lockLanguageOrQuit(bool shouldBeRunning) {
53 bool locked = gLangMutex.try_lock();
54 if (locked) {
55 if (shouldBeRunning == false) {
56 gLangMutex.unlock();
57 return EINTR;
58 }
59 } else {
60 for (;;) {
61 locked = gLangMutex.try_lock_for(std::chrono::seconds(1));
62 if (shouldBeRunning == false) {
63 if (locked)
64 gLangMutex.unlock();
65 return EINTR;
66 }
67 if (locked)
68 return 0;
69 }
70 }
71 return 0;
72 }
73