Skip to content

Commit

Permalink
for_each_process_thread() is better, from 6.6, than
Browse files Browse the repository at this point in the history
the older do_each|while_each_thread() macros
  • Loading branch information
kaiwan committed Jan 22, 2024
1 parent 320e1f1 commit f67c30b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
10 changes: 10 additions & 0 deletions ch13/3_lockfree/thrdshowall_rcu/thrd_showall_rcu.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ static int showthrds_rcu(void)
* the _rcu list-mutation primitives such as list_add_rcu() as long as it's
* guarded by rcu_read_lock(). ...'
*/

/* Commit # 5ffd2c37cb7a53d520 ... */
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
do_each_thread(g, t) { /* 'g' : process ptr; 't': thread ptr */
#else
for_each_process_thread(g, t) { /* 'g' : process ptr; 't': thread ptr */
#endif
g_rcu = rcu_dereference(g);
t_rcu = rcu_dereference(t);

Expand Down Expand Up @@ -121,7 +127,11 @@ static int showthrds_rcu(void)
memset(buf, 0, sizeof(buf));
memset(tmp, 0, sizeof(tmp));
put_task_struct(t_rcu); /* release reference to the task struct */
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
} while_each_thread(g, t);
#else
}
#endif
rcu_read_unlock(); /* This ends the RCU read-side critical section */
return total;
}
Expand Down
9 changes: 9 additions & 0 deletions ch13/4_lockdep/buggy_thrdshow_eg/thrd_showall_buggy.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ static int showthrds_buggy(void)
rcu_read_lock(); /* This triggers off an RCU read-side critical section; ensure
* you are non-blocking within it! */
#endif
/* Commit # 5ffd2c37cb7a53d520 ... */
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
do_each_thread(g, t) { /* 'g' : process ptr; 't': thread ptr */
#else
for_each_process_thread(g, t) { /* 'g' : process ptr; 't': thread ptr */
#endif
get_task_struct(t); /* take a reference to the task struct */
task_lock(t);

Expand Down Expand Up @@ -107,7 +112,11 @@ static int showthrds_buggy(void)
memset(tmp, 0, sizeof(tmp));
task_unlock(t);
put_task_struct(t); /* release reference to the task struct */
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
} while_each_thread(g, t);
#else
}
#endif
#if 0
/* <same as above, reg the RCU synchronization for the task list> */
rcu_read_unlock();
Expand Down
17 changes: 16 additions & 1 deletion ch6/foreach/thrd_showall/thrd_showall.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,18 @@ static int showthrds(void)
* Worry not, you'll learn several approaches to kernel synchronization in
* the book's last two chapters.
*/
do_each_thread(p, t) { /* 'p' : process ptr; 't': thread ptr */

/*
* FYI, from 6.6, the do_ach_thread()/while_each_thread() style macros have been
* removed in favor of the simpler and more readable for_each_process_thread()
* macro.
* Commit # 5ffd2c37cb7a53d520...
*/
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
do_each_thread(p, t) { /* 'p' : process ptr; 't': thread ptr */
#else
for_each_process_thread(p, t) { /* 'p' : process ptr; 't': thread ptr */
#endif
get_task_struct(t); /* take a reference to the task struct */
task_lock(t);

Expand Down Expand Up @@ -129,7 +140,11 @@ static int showthrds(void)

task_unlock(t);
put_task_struct(t); /* release reference to the task struct */
#if LINUX_VERSION_CODE < KERNEL_VERSION(6, 6, 0)
} while_each_thread(p, t);
#else
}
#endif

return total;
}
Expand Down

0 comments on commit f67c30b

Please sign in to comment.