From 4e9186cc14545b1dcc29dede0cc87d02a5a51f42 Mon Sep 17 00:00:00 2001
From: Terraneo Federico <fede.tft@miosix.org>
Date: Sun, 26 Mar 2023 14:50:32 +0200
Subject: [PATCH] Make SleepData a class

---
 miosix/kernel/kernel.cpp  | 14 ++++++--------
 miosix/kernel/kernel.h    | 13 ++++++++-----
 miosix/kernel/pthread.cpp |  4 +---
 miosix/kernel/sync.cpp    |  7 ++-----
 4 files changed, 17 insertions(+), 21 deletions(-)

diff --git a/miosix/kernel/kernel.cpp b/miosix/kernel/kernel.cpp
index cb888ee1..59d24649 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 7c8c054f..d689e82c 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 99cf7613..4dfb3260 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 b6aef8c2..456b3782 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);
-- 
GitLab