comparison core/ClockSyncThread.cpp @ 135:e77e2e712fbc ClockSync

To work with the ClockSync plugin
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 12 Sep 2015 20:05:55 +0100
parents
children 4e2dd3eb1d28
comparison
equal deleted inserted replaced
133:04b1678614c9 135:e77e2e712fbc
1 #include "ClockSyncThread.h"
2 #ifdef USE_JUCE
3 #else //declare static members TODO: rather refactor this similar to other threads so that only run and clockSyncTask are static
4 myClock_t ClockSyncThread::lastTime; // Used for clock synchronization
5 bool ClockSyncThread::listening;
6 ClockSync ClockSyncThread::clockSync;
7 VirtualClock* ClockSyncThread::virtualClock;
8 bool ClockSyncThread::threadIsExiting;
9 AuxiliaryTask ClockSyncThread::clockSyncTask;
10 #endif
11 #ifdef USE_JUCE
12 ClockSyncThread::ClockSyncThread(const String &threadName) :
13 Thread(threadName)
14 #else
15 ClockSyncThread::ClockSyncThread()
16 #endif /* USE_JUCE */
17 {
18 };
19 ClockSyncThread::~ClockSyncThread(){
20 #ifdef USE_JUCE
21 stopThread(1000);
22 #else
23 stopThread();
24 #endif /* USE_JUCE */
25 }
26 void ClockSyncThread::init(bool isSlave, int aPort, VirtualClock &aVirtualClock){
27 setVirtualClock(aVirtualClock);
28 listening=false;
29 clockSync.init(isSlave, aPort, *virtualClock);
30 #ifdef USE_JUCE
31 startThread(5);
32 #else
33 threadIsExiting=false;
34 clockSyncTask=BeagleRT_createAuxiliaryTask(&ClockSyncThread::run,98, "clockSyncTask");
35 //TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled
36 #endif /* USE_JUCE */
37 }
38
39 #ifdef USE_JUCE
40 #else
41 void ClockSyncThread::startThread(){
42 printf("starting\n");
43 BeagleRT_scheduleAuxiliaryTask(clockSyncTask);
44 printf("started\n");
45 }
46 void ClockSyncThread::stopThread(){
47 threadIsExiting=true;
48 }
49 bool ClockSyncThread::threadShouldExit(){
50 return(gShouldStop || threadIsExiting );
51 }
52 #endif /* USE_JUCE */
53
54 void ClockSyncThread::setVirtualClock(VirtualClock &aVirtualClock){
55 virtualClock=&aVirtualClock;
56 };
57
58 void ClockSyncThread::run(){
59 while(!threadShouldExit()){
60 clockSync.sendReceiveLoop();
61 usleep(5000);
62 // double now=virtualClock->getNow();
63 // printf("th(end+1)=%f;\n", now/44100.0f);
64 // printf("act(end+1)=%lld;\n", Clock::getTimeUs());
65 }
66 printf("Thread is not running \n");
67 }