diff --git a/miosix/kernel/kernel.cpp b/miosix/kernel/kernel.cpp index cb888ee178d8b3904a3472a95bcc638ea3ebc56a..59d2464964affa4666e5f96f3c6d7093c5303c90 100755 --- a/miosix/kernel/kernel.cpp +++ b/miosix/kernel/kernel.cpp @@ -266,7 +266,7 @@ bool isKernelRunning() */ void IRQaddToSleepingList(SleepData *x) { - x->p->flags.IRQsetSleep(true); + x->thread->flags.IRQsetSleep(true); if(sleepingList.empty() || sleepingList.front()->wakeupTime>=x->wakeupTime) { sleepingList.push_front(x); @@ -309,12 +309,12 @@ bool IRQwakeThreads(long long currentTime) for(auto it=sleepingList.begin();it!=sleepingList.end();) { if(currentTime<(*it)->wakeupTime) break; - if((*it)->p==nullptr) ++it; //Only csRecord has p==nullptr + if((*it)->thread==nullptr) ++it; //Only csRecord has p==nullptr else { - (*it)->p->flags.IRQsetSleep(false); //Wake thread + (*it)->thread->flags.IRQsetSleep(false); //Wake thread //Reset cond wait flag to wakeup threads in pthread_cond_timedwait() too - (*it)->p->flags.IRQsetCondWait(false); - if(const_cast<Thread*>(runningThread)->getPriority()<(*it)->p->getPriority()) + (*it)->thread->flags.IRQsetCondWait(false); + if(const_cast<Thread*>(runningThread)->getPriority()<(*it)->thread->getPriority()) result=true; it=sleepingList.erase(it); } @@ -402,13 +402,11 @@ void Thread::nanoSleepUntil(long long absoluteTimeNs) //The SleepData variable has to be in scope till Thread::yield() returns //as IRQaddToSleepingList() makes it part of a linked list till the //thread wakes up (i.e: after Thread::yield() returns) - SleepData d; + SleepData d(const_cast<Thread*>(runningThread),absoluteTimeNs); //pauseKernel() here is not enough since even if the kernel is stopped //the timer isr will wake threads, modifying the sleepingList { FastInterruptDisableLock lock; - d.p=const_cast<Thread*>(runningThread); - d.wakeupTime=absoluteTimeNs; IRQaddToSleepingList(&d);//Also sets SLEEP_FLAG } // NOTE: There is no need to synchronize the timer (calling IRQsetNextInterrupt) diff --git a/miosix/kernel/kernel.h b/miosix/kernel/kernel.h index 7c8c054f81fffea42c37a9b70e24560c167c4661..d689e82c75e14fdcc16e79a1a7cef6301a2bc3e9 100755 --- a/miosix/kernel/kernel.h +++ b/miosix/kernel/kernel.h @@ -416,7 +416,7 @@ long long getTime() noexcept; long long IRQgetTime() noexcept; //Forwrd declaration -struct SleepData; +class SleepData; class MemoryProfiling; class Mutex; class ConditionVariable; @@ -1102,14 +1102,17 @@ public: /** * \internal - * \struct Sleep_data - * This struct is used to make a list of sleeping threads. + * This class is used to make a list of sleeping threads. * It is used by the kernel, and should not be used by end users. */ -struct SleepData : public IntrusiveListItem +class SleepData : public IntrusiveListItem { +public: + SleepData(Thread *thread, long long wakeupTime) + : thread(thread), wakeupTime(wakeupTime) {} + ///\internal Thread that is sleeping - Thread *p; + Thread *thread; ///\internal When this number becomes equal to the kernel tick, ///the thread will wake diff --git a/miosix/kernel/pthread.cpp b/miosix/kernel/pthread.cpp index 99cf76131105591a935781cef4b5782d9d78f24d..4dfb3260130d0d92a12df48f88b766b2170bb572 100644 --- a/miosix/kernel/pthread.cpp +++ b/miosix/kernel/pthread.cpp @@ -454,9 +454,7 @@ int pthreadCondTimedWaitImpl(pthread_cond_t *cond, pthread_mutex_t *mutex, long Thread *t=Thread::IRQgetCurrentThread(); CondData listItem(t); condList->push_back(&listItem); //Putting this thread last on the list (lifo policy) - SleepData sleepData; - sleepData.p=t; - sleepData.wakeupTime=absTime; + SleepData sleepData(t,absTime); IRQaddToSleepingList(&sleepData); //Putting this thread on the sleeping list too t->flags.IRQsetCondWait(true); diff --git a/miosix/kernel/sync.cpp b/miosix/kernel/sync.cpp index b6aef8c2bb23b7d9a60bc8c02dabc82dcae16cbd..456b37824cbc021bb4970bd39b02c66cde6fec8c 100644 --- a/miosix/kernel/sync.cpp +++ b/miosix/kernel/sync.cpp @@ -401,17 +401,14 @@ TimedWaitResult ConditionVariable::timedWait(Mutex& m, long long absTime) Thread *t=Thread::getCurrentThread(); CondData listItem(t); condList.push_back(&listItem); //Add entry to tail of list - SleepData sleepData; - sleepData.p=t; - sleepData.wakeupTime=absTime; - - //Unlock mutex and wait + SleepData sleepData(t,absTime); { FastInterruptDisableLock l; IRQaddToSleepingList(&sleepData); //Putting this thread on the sleeping list too t->flags.IRQsetCondWait(true); } + //Unlock mutex and wait unsigned int depth=m.PKunlockAllDepthLevels(dLock); { RestartKernelLock eLock(dLock);