From 22a75b9a3e15a981910da337d103100c7fd790ac Mon Sep 17 00:00:00 2001
From: Terraneo Federico <fede.tft@miosix.org>
Date: Sun, 26 Mar 2023 14:35:54 +0200
Subject: [PATCH] Make CondData a class

---
 miosix/kernel/pthread.cpp | 16 +++++++---------
 miosix/kernel/sync.cpp    |  6 ++----
 miosix/kernel/sync.h      | 11 ++++++-----
 3 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/miosix/kernel/pthread.cpp b/miosix/kernel/pthread.cpp
index f91bfa80..99cf7613 100644
--- a/miosix/kernel/pthread.cpp
+++ b/miosix/kernel/pthread.cpp
@@ -313,11 +313,10 @@ int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
     auto *condList=reinterpret_cast<IntrusiveList<CondData>*>(cond);
 
     FastInterruptDisableLock dLock;
-    Thread *p=Thread::IRQgetCurrentThread();
-    CondData listItem;
-    listItem.thread=p;
+    Thread *t=Thread::IRQgetCurrentThread();
+    CondData listItem(t);
     condList->push_back(&listItem); //Putting this thread last on the list (lifo policy)
-    p->flags.IRQsetCondWait(true);
+    t->flags.IRQsetCondWait(true);
 
     unsigned int depth=IRQdoMutexUnlockAllDepthLevels(mutex);
     {
@@ -452,15 +451,14 @@ int pthreadCondTimedWaitImpl(pthread_cond_t *cond, pthread_mutex_t *mutex, long
     //side effect, very shor sleeps done very early at boot will be extended.
     absTime=std::max(absTime,100000LL);
     FastInterruptDisableLock dLock;
-    Thread *p=Thread::IRQgetCurrentThread();
-    CondData listItem;
-    listItem.thread=p;
+    Thread *t=Thread::IRQgetCurrentThread();
+    CondData listItem(t);
     condList->push_back(&listItem); //Putting this thread last on the list (lifo policy)
     SleepData sleepData;
-    sleepData.p=p;
+    sleepData.p=t;
     sleepData.wakeupTime=absTime;
     IRQaddToSleepingList(&sleepData); //Putting this thread on the sleeping list too
-    p->flags.IRQsetCondWait(true);
+    t->flags.IRQsetCondWait(true);
 
     unsigned int depth=IRQdoMutexUnlockAllDepthLevels(mutex);
     {
diff --git a/miosix/kernel/sync.cpp b/miosix/kernel/sync.cpp
index 8a7d4256..b6aef8c2 100644
--- a/miosix/kernel/sync.cpp
+++ b/miosix/kernel/sync.cpp
@@ -372,8 +372,7 @@ void ConditionVariable::wait(Mutex& m)
 {
     PauseKernelLock dLock;
     Thread *t=Thread::getCurrentThread();
-    CondData listItem;
-    listItem.thread=t;
+    CondData listItem(t);
     condList.push_back(&listItem); //Add entry to tail of list
 
     //Unlock mutex and wait
@@ -400,8 +399,7 @@ TimedWaitResult ConditionVariable::timedWait(Mutex& m, long long absTime)
 
     PauseKernelLock dLock;
     Thread *t=Thread::getCurrentThread();
-    CondData listItem;
-    listItem.thread=t;
+    CondData listItem(t);
     condList.push_back(&listItem); //Add entry to tail of list
     SleepData sleepData;
     sleepData.p=t;
diff --git a/miosix/kernel/sync.h b/miosix/kernel/sync.h
index 7bb97449..bc485215 100644
--- a/miosix/kernel/sync.h
+++ b/miosix/kernel/sync.h
@@ -386,18 +386,19 @@ private:
 
 /**
  * \internal
- * \struct CondData
- * This struct is used to make a list of threads that are waiting on a condition
+ * This class is used to make a list of threads that are waiting on a condition
  * variable. It is used by the kernel, and should not be used by end users.
  */
-struct CondData : public IntrusiveListItem
+class CondData : public IntrusiveListItem
 {
+public:
+    CondData(Thread *thread) : thread(thread) {}
     Thread *thread; ///<\internal Thread that is waiting
 };
 
 /**
-    * Possible return values of timedWait
-    */
+ * Possible return values of timedWait
+ */
 enum class TimedWaitResult
 {
     NoTimeout,
-- 
GitLab