Page 1 of 1

fix to walking thread list

Posted: Sun Aug 15, 2021 6:13 pm
by wild-boar
I recently upgraded a working project from 20.3.x to 21.6.x. I have custom code that uses chRegFirstThread() followed by chRegNextThread() to walk all threads. I noticed that chRegNextThread() always returns NULL for my application. This results in only seeing the first thread.

The following patch fixes chRegNextThread() when CH_CFG_USE_DYNAMIC == FALSE. It sets ntp to a value on the else statement.

Thanks!


Code: Select all

diff --git a/os/rt/src/chregistry.c b/os/rt/src/chregistry.c
index be0a40c6a..5318d9392 100644
--- a/os/rt/src/chregistry.c
+++ b/os/rt/src/chregistry.c
@@ -164,18 +164,18 @@ thread_t *chRegNextThread(thread_t *tp) {
   if (nqp == REG_HEADER(currcore)) {
     ntp = NULL;
   }
-#if CH_CFG_USE_DYNAMIC == TRUE
   else {
     uint8_t *p = (uint8_t *)nqp;
     /*lint -save -e413 [1.3] Safe to subtract a calculated offset.*/
     ntp = (thread_t *)(p - __CH_OFFSETOF(thread_t, rqueue));
     /*lint -restore*/
 
+#if CH_CFG_USE_DYNAMIC == TRUE
     chDbgAssert(ntp->refs < (trefs_t)255, "too many references");
 
     ntp->refs++;
-  }
 #endif
+  }
   chSysUnlock();
 #if CH_CFG_USE_DYNAMIC == TRUE
   chThdRelease(tp);

Re: fix to walking thread list

Posted: Mon Aug 16, 2021 7:22 am
by Giovanni
Hi,

Fixed as bug #1170.

Giovanni