]> git.puffer.fish Git - mirror/frr.git/commit
lib: safely check & set thread pointers
authorQuentin Young <qlyoung@cumulusnetworks.com>
Fri, 5 May 2017 17:30:21 +0000 (17:30 +0000)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Tue, 9 May 2017 20:44:22 +0000 (20:44 +0000)
commit32d86f8b7a327cfc6904d51d5c3ea8f5c6115e44
tree5b84bd582eb060d9eb9b5a852ca2a2a7115641da
parentffa2c8986d204f4a3e7204258fd6906af4a57c93
lib: safely check & set thread pointers

When scheduling a thread, the scheduling function returns a pointer to
the struct thread that was placed on one of the scheduling queues in the
associated thread master. This pointer is used to check whether or not
the thread is scheduled, and is passed to thread_cancel() should the
daemon need to cancel that particular task.

The thread_fetch() function is called to retrieve the next thread to
execute. However, when it returns, the aforementioned pointer is not
updated. As a result, in order for the above use cases to work, every
thread handler function must set the associated pointer to NULL. This is
bug prone, and moreover, not thread safe.

This patch changes the thread scheduling functions to return void. If
the caller needs a reference to the scheduled thread, it must pass in a
pointer to store the pointer to the thread struct in. Subsequent calls
to thread_cancel(), thread_cancel_event() or thread_fetch() will result
in that pointer being nulled before return. These operations occur
within the thread_master critical sections.

Overall this should avoid bugs introduced by thread handler funcs
forgetting to null the associated pointer, double-scheduling caused by
overwriting pointers to currently scheduled threads without performing a
nullity check, and the introduction of true kernel threads causing race
conditions within the userspace threading world.

Also removes the return value for thread_execute since it always returns
null...

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
lib/thread.c
lib/thread.h