26 #include <sys/types.h>
27 #include <sys/syscall.h>
44 RtUtils::RtUtils() : mStarted(false), mEventEmitter(), mRemoteReady(true)
48 rtError RtUtils::initRt()
52 memset(&mThreadData,0,
sizeof(RtProcessThreadData));
53 pthread_cond_init(&mThreadData.mCond,NULL);
54 pthread_mutex_init(&mThreadData.mMutex,NULL);
55 mThreadData.mRunning =
true;
56 pthread_create(&mThreadData.mThread, NULL, RtUtils::RtMessageThread,
this);
60 rtRemoteRegisterQueueReadyHandler( rtEnvironmentGetGlobal(), rtRemoteCallback,
this);
65 printf(
"Failed to init rt!\n");
72 setDedicatedThreadRunning(
false);
75 pthread_mutex_lock(&mThreadData.mMutex);
76 pthread_cond_signal(&mThreadData.mCond);
77 pthread_mutex_unlock(&mThreadData.mMutex);
79 pthread_join(mThreadData.mThread, &returnValue);
80 pthread_cond_destroy(&mThreadData.mCond);
81 pthread_mutex_destroy(&mThreadData.mMutex);
82 printf(
"Dedicated RT message thread exited!\n");
85 printf(
"before rt remote shutdown\n"); fflush(stdout);
92 rc = rtRemoteShutdown();
95 printf(
"Failed to shutdown rt!\n");
96 printf(
"after rt remote shutdown\n"); fflush(stdout);
99 void RtUtils::ProcessRtItem()
102 while(err == RT_OK) {
103 err = rtRemoteProcessSingleItem();
104 if (err == RT_ERROR_QUEUE_EMPTY)
105 printf(
"queue was empty upon processing event\n");
106 else if (err != RT_OK)
107 printf(
"rtRemoteProcessSingleItem() returned %d\n", err);
111 void * RtUtils::RtMessageThread(
void * ctx)
115 pthread_mutex_lock(&rtUtils->mThreadData.mMutex);
116 while(rtUtils->mThreadData.mRunning)
118 pthread_cond_wait(&rtUtils->mThreadData.mCond, &rtUtils->mThreadData.mMutex);
120 printf(
"rtRemoteProcessSingleItem()\n");
121 rtUtils->ProcessRtItem();
123 pthread_mutex_unlock(&rtUtils->mThreadData.mMutex);
128 void RtUtils::setDedicatedThreadRunning(
bool running)
130 pthread_mutex_lock(&mThreadData.mMutex);
131 mThreadData.mRunning = running;
132 pthread_mutex_unlock(&mThreadData.mMutex);
135 bool RtUtils::isDedicatedThreadRunning()
137 bool isRunning =
false;
139 pthread_mutex_lock(&mThreadData.mMutex);
140 isRunning = mThreadData.mRunning;
141 pthread_mutex_unlock(&mThreadData.mMutex);
146 void RtUtils::rtRemoteCallback(
void* ctx)
150 pthread_mutex_lock(&rtUtils->mThreadData.mMutex);
151 pthread_cond_signal(&rtUtils->mThreadData.mCond);
152 pthread_mutex_unlock(&rtUtils->mThreadData.mMutex);
155 void RtUtils::setStarted(
bool started)
160 void RtUtils::send(
Event* event)
162 mEventEmitter.send(event);
165 void RtUtils::EventEmitter::processEvents()
167 pthread_mutex_lock(&mMutex);
168 while (!mEventQueue.empty())
170 rtObjectRef obj = mEventQueue.front();
173 rtError rc = m_emit.send(obj.get<rtString>(
"name"), obj);
175 printf(
"SENDING EVENT SUCCEDED!\n");
177 printf(
"SENDING EVENT FAILED!\n");
181 pthread_mutex_unlock(&mMutex);
184 rtError RtUtils::EventEmitter::send(
Event* event) {
185 pthread_mutex_lock(&mMutex);
186 mEventQueue.push(event->object());
187 pthread_mutex_unlock(&mMutex);
197 rtError RtUtils::setListener(rtString eventName,
const rtFunctionRef& f)
199 return mEventEmitter.setListener(eventName, f);
202 rtError RtUtils::delListener(rtString eventName,
const rtFunctionRef& f)
204 return mEventEmitter.delListener(eventName, f);
207 void RtUtils::remoteObjectReady()
210 mEventEmitter.remoteObjectReady();