xiaoxiang781216 commented on code in PR #13129:
URL: https://github.com/apache/nuttx/pull/13129#discussion_r1736804792
##########
sched/task/task_setup.c:
##########
@@ -140,13 +140,32 @@ static int nxtask_assign_pid(FAR struct tcb_s *tcb)
* expand space.
*/
+ temp = g_pidhash;
+
+ /* Calling malloc in a critical section may cause thread switching.
+ * Here we check whether other threads have applied successfully,
+ * and if successful, return directly
+ */
+
+ leave_critical_section(flags);
pidhash = kmm_zalloc(g_npidhash * 2 * sizeof(*pidhash));
+ flags = enter_critical_section();
if (pidhash == NULL)
{
leave_critical_section(flags);
return -ENOMEM;
}
+ /* Handle conner case: context siwtch happened when kmm_malloc */
+
+ if (temp != g_pidhash)
+ {
+ leave_critical_section(flags);
+ kmm_free(pidhash);
+ flags = enter_critical_section();
Review Comment:
remove, let's move line 102 before line 94.
##########
sched/task/task_setup.c:
##########
@@ -155,16 +174,27 @@ static int nxtask_assign_pid(FAR struct tcb_s *tcb)
for (i = 0; i < g_npidhash / 2; i++)
{
+ if (g_pidhash[i] == NULL)
+ {
+ /* If the pid is not used, skip it.
+ * This may be triggered when a context switch occurs
+ * during zalloc and a thread is destroyed.
+ */
+
+ continue;
+ }
+
hash_ndx = PIDHASH(g_pidhash[i]->pid);
DEBUGASSERT(pidhash[hash_ndx] == NULL);
pidhash[hash_ndx] = g_pidhash[i];
}
/* Release resource for original g_pidhash, using new g_pidhash */
- temp = g_pidhash;
g_pidhash = pidhash;
+ leave_critical_section(flags);
kmm_free(temp);
+ flags = enter_critical_section();
Review Comment:
remove
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]