[libcamera-devel] [PATCH 1/4] libcamera: thread: Document dispatchMessages() thread-safety requirements

Laurent Pinchart laurent.pinchart at ideasonboard.com
Mon May 24 11:26:24 CEST 2021


Hi Jacopo,

On Mon, May 24, 2021 at 10:52:46AM +0200, Jacopo Mondi wrote:
> On Sun, May 23, 2021 at 05:31:52AM +0300, Laurent Pinchart wrote:
> > The Thread class is incorrectly documented as thread-safe, as the
> > dispatchMessages() function isn't thread-safe. Fix the documentation by
> > tagging individual functions as thread-safe as appropriate.
> >
> > Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
> > ---
> >  src/libcamera/thread.cpp | 17 +++++++++++++++--
> >  1 file changed, 15 insertions(+), 2 deletions(-)
> >
> > diff --git a/src/libcamera/thread.cpp b/src/libcamera/thread.cpp
> > index 198670de33a2..285b808332e8 100644
> > --- a/src/libcamera/thread.cpp
> > +++ b/src/libcamera/thread.cpp
> > @@ -229,8 +229,6 @@ ThreadData *ThreadData::current()
> >   * called. The event loop dispatches events (messages, notifiers and timers)
> >   * sent to the objects living in the thread. This behaviour can be modified by
> >   * overriding the run() function.
> > - *
> > - * \context This class is \threadsafe.
> >   */
> >
> >  /**
> > @@ -361,6 +359,8 @@ void Thread::finishThread()
> >   *
> >   * Calling exit() on a thread that reimplements the run() method and doesn't
> >   * call exec() will likely have no effect.
> > + *
> > + * \context This function is \threadsafe.
> >   */
> >  void Thread::exit(int code)
> >  {
> > @@ -383,6 +383,8 @@ void Thread::exit(int code)
> >   * utils::duration::max(), the wait never times out. If the thread is not
> >   * running the function returns immediately.
> >   *
> > + * \context This function is \threadsafe.
> > + *
> 
> Nit: we don't usually end this statements with '.'
> But \context seems a bit flaky as it has contradicting examples

I've had a look, and if I don't specify a period at the end, doxygen
doesn't add one, unlike for \brief. I'd thus prefer keeping it.

> Whatever feels best for you
> Reviewed-by: Jacopo Mondi <jacopo at jmondi.org>
> 
> >   * \return True if the thread has finished, or false if the wait timed out
> >   */
> >  bool Thread::wait(utils::duration duration)
> > @@ -412,6 +414,8 @@ bool Thread::wait(utils::duration duration)
> >   * started. This method guarantees that it returns true after the start()
> >   * method returns, and false after the wait() method returns.
> >   *
> > + * \context This function is \threadsafe.
> > + *
> >   * \return True if the thread is running, false otherwise
> >   */
> >  bool Thread::isRunning()
> > @@ -427,6 +431,7 @@ bool Thread::isRunning()
> >
> >  /**
> >   * \brief Retrieve the Thread instance for the current thread
> > + * \context This function is \threadsafe.
> >   * \return The Thread instance for the current thread
> >   */
> >  Thread *Thread::current()
> > @@ -441,6 +446,8 @@ Thread *Thread::current()
> >   * The thread ID corresponds to the Linux thread ID (TID) as returned by the
> >   * gettid system call.
> >   *
> > + * \context This function is \threadsafe.
> > + *
> >   * \return The ID of the current thread
> >   */
> >  pid_t Thread::currentId()
> > @@ -455,6 +462,8 @@ pid_t Thread::currentId()
> >   * This function retrieves the internal event dispatcher for the thread. The
> >   * returned event dispatcher is valid until the thread is destroyed.
> >   *
> > + * \context This function is \threadsafe.
> > + *
> >   * \return Pointer to the event dispatcher
> >   */
> >  EventDispatcher *Thread::eventDispatcher()
> > @@ -544,6 +553,10 @@ void Thread::removeMessages(Object *receiver)
> >   * This function immediately dispatches all the messages previously posted for
> >   * this thread with postMessage() that match the message \a type. If the \a type
> >   * is Message::Type::None, all messages are dispatched.
> > + *
> > + * Messages shall only be dispatched from the current thread, typically within
> > + * the thread from the run() function. Calling this function outside of the
> > + * thread results in undefined behaviour.
> >   */
> >  void Thread::dispatchMessages(Message::Type type)
> >  {

-- 
Regards,

Laurent Pinchart


More information about the libcamera-devel mailing list