diff --git a/common/SC_Lock.h b/common/SC_Lock.h index e1072fa668a..08dabb265f7 100644 --- a/common/SC_Lock.h +++ b/common/SC_Lock.h @@ -25,13 +25,20 @@ #include #include +#ifdef __COBALT__ +# include +typedef XenomaiMutex SC_Lock; +#else // __COBALT__ typedef std::mutex SC_Lock; +#endif // __COBALT__ typedef std::thread SC_Thread; using std::cv_status; using std::lock_guard; -using std::mutex; using std::timed_mutex; using std::unique_lock; +#ifdef __COBALT__ +typedef XenomaiConditionVariable condition_variable_any; +#else // __COBALT__ typedef std::condition_variable_any condition_variable_any; - +#endif // __COBALT__ typedef SC_Lock mutex; diff --git a/common/SC_SyncCondition.h b/common/SC_SyncCondition.h index c00bc3ca8e3..1ca14c209ca 100644 --- a/common/SC_SyncCondition.h +++ b/common/SC_SyncCondition.h @@ -60,7 +60,14 @@ class SC_SyncCondition { void Signal() { ++write; - available.notify_one(); +#ifdef SC_CONDITION_VARIABLE_ANY_SHOULD_LOCK_BEFORE_NOTIFY + if (mutex.try_lock()) { +#endif // CONDITION_VARIABLE_ANY_SHOULD_LOCK_BEFORE_NOTIFY + available.notify_one(); +#ifdef SC_CONDITION_VARIABLE_ANY_SHOULD_LOCK_BEFORE_NOTIFY + mutex.unlock(); + } +#endif // CONDITION_VARIABLE_ANY_SHOULD_LOCK_BEFORE_NOTIFY } private: diff --git a/server/plugins/CMakeLists.txt b/server/plugins/CMakeLists.txt index 134c44ef268..0a44ea4d557 100644 --- a/server/plugins/CMakeLists.txt +++ b/server/plugins/CMakeLists.txt @@ -161,6 +161,9 @@ add_library(ML_UGens MODULE if(NOT NO_LIBSNDFILE) set(diskio_sources DiskIO_UGens.cpp) + if (XENOMAI_FOUND) + list(APPEND diskio_sources ../../common/XenomaiLock.cpp) + endif() add_library(DiskIO_UGens MODULE ${diskio_sources}) if(SNDFILE_FOUND) diff --git a/server/scsynth/CMakeLists.txt b/server/scsynth/CMakeLists.txt index 9e342f685d5..7fbcc8562cc 100644 --- a/server/scsynth/CMakeLists.txt +++ b/server/scsynth/CMakeLists.txt @@ -124,6 +124,9 @@ if (FFT_GREEN) list(APPEND scsynth_sources ../../common/fftlib.c) endif() +if (XENOMAI_FOUND) + list(APPEND scsynth_sources ../../common/XenomaiLock.cpp) +endif() include_directories(${CMAKE_SOURCE_DIR}/include/common ${CMAKE_SOURCE_DIR}/common diff --git a/server/scsynth/SC_Bela.cpp b/server/scsynth/SC_Bela.cpp index 209a5f82d4a..6385cbad06a 100644 --- a/server/scsynth/SC_Bela.cpp +++ b/server/scsynth/SC_Bela.cpp @@ -76,10 +76,7 @@ class SC_BelaDriver : public SC_AudioDriver { void BelaAudioCallback(BelaContext* belaContext); void SignalReceived(int); - static void staticMAudioSyncSignal(void*); - static AuxiliaryTask mAudioSyncSignalTask; static int countInstances; - static SC_SyncCondition* staticMAudioSync; Scope* mBelaScope; uint32 mBelaMaxScopeChannels; @@ -87,9 +84,7 @@ class SC_BelaDriver : public SC_AudioDriver { uint32 mSCBufLength; }; -AuxiliaryTask SC_BelaDriver::mAudioSyncSignalTask; int SC_BelaDriver::countInstances; -SC_SyncCondition* SC_BelaDriver::staticMAudioSync; SC_BelaDriver* mBelaDriverInstance = 0; SC_AudioDriver* SC_NewAudioDriver(struct World* inWorld) { @@ -105,7 +100,6 @@ SC_BelaDriver::SC_BelaDriver(struct World* inWorld): SC_AudioDriver(inWorld) { mStartHostSecs = 0; mSCBufLength = inWorld->mBufLength; - staticMAudioSync = &mAudioSync; ++countInstances; if (countInstances != 1) { fprintf(stderr, "Error: there are %d instances of SC_BelaDriver running at the same time. Exiting\n", @@ -302,15 +296,9 @@ void SC_BelaDriver::BelaAudioCallback(BelaContext* belaContext) { scprintf("SC_BelaDriver: unknown exception in real time\n"); } - // this avoids Xenomai mode switches in the audio thread ... - Bela_scheduleAuxiliaryTask(mAudioSyncSignalTask); + mAudioSync.Signal(); } -void SC_BelaDriver::staticMAudioSyncSignal(void*) { - // ... but mode switches are still happening here, in a lower priority thread. - // FIXME: this triggers a mode switch in Xenomai. - staticMAudioSync->Signal(); -} // ==================================================================== typedef struct _BelaHwConfig // HW_DETECT_HACK @@ -477,12 +465,6 @@ bool SC_BelaDriver::DriverSetup(int* outNumSamples, double* outSampleRate) { scprintf("Error in SC_BelaDriver::DriverSetup(): unable to initialise audio\n"); return false; } - mAudioSyncSignalTask = Bela_createAuxiliaryTask( - staticMAudioSyncSignal, 90, "mAudioSyncSignalTask"); // needs to be created after the call to Bela_initAudio() - if (!mAudioSyncSignalTask) { - fprintf(stderr, "Error: unable to create Bela auxiliary task\n"); - exit(1); - } *outNumSamples = settings->periodSize; *outSampleRate = gBelaSampleRate; diff --git a/server/scsynth/scsynth_main.cpp b/server/scsynth/scsynth_main.cpp index f1aa752d6d3..5d2c981c2d3 100644 --- a/server/scsynth/scsynth_main.cpp +++ b/server/scsynth/scsynth_main.cpp @@ -36,6 +36,10 @@ #else # include #endif +#ifdef __COBALT__ +# include "XenomaiLock.h" +XenomaiInitializer xenomaiInitializer; +#endif // __COBALT__ #ifdef _WIN32