Motion - Thread Nr Tls Patch

Threadnr in TLS patch


The last days or so, we discussed TLS on IRC (sorry, logger was down so no reference to the discussion). Basically, TLS (thread-local storage) allows us to associate data with a thread, in a way that the data is accessible to that thread only.

My initial idea was to store a pointer to the thread's context struct in TLS, so that we wouldn't have to pass along it to almost every function. An issue that was raised, however, is that functions then could modify data that is not among the function parameters. This is not a technical problem, but more a matter of coding style. It could possibly also degrade Motion performance, if it is more expensive to access TLS than to simply pass a function parameter. Don't know for sure about that.

This patch does not put the context struct in TLS, but only the thread number. The point of doing this is that functions that previously needed the context struct only to call motion_log, now won't need it.

It is a problem if TLS through pthreads isn't available on all platforms. We have to check that!

Description of Patch

To avoid a patch that affects almost all source files (due to the fact that motion_log is called from everywhere), the patch is applied in three steps. Only the first step involves the patch file. See below for instructions.

Installation of Patch

Note: There are two patch files, one that contains step 1 only, and one that contains all three steps. Read on for instructions how to apply the patch in separate steps.

Step 1

Apply the attached patch file with zcat motion-3.2.2_snap11-tls-step1.patch.gz | patch -p1. This introduces TLS initialization/destruction, makes the motion and netcam threads store thread number in TLS, and changes motion_log to use TLS.

For threads for which there is no thread number in TLS, motion_log will print 0 as the thread number. This behaviour is the same as before, since those threads (only the control thread, I think) used to pass cnt_list[0] anyway.

Motion is fully functional after this step.

Step 2

Remove the first parameter of every call to motion_log, by issuing the following command (in the Motion source directory):

perl -pi.bak -e 's/motion_log *\(.*?, */motion_log\(/g' *.c *.h

Remove .bak (keep -pi) if you don't want backup files. Note that a bonus is that the above command also modifies the function prototype of motion_log (both in motion.c and motion.h).

Motion is fully functional after this step.

Step 3

Finally remove parameters/variables in functions that previously used the context struct only for calls to motion_log. According to make (complaining about unused variables), the relevant functions are send_template_ini_client, send_template_ini_client_raw and send_template, all in webhttpd.c.

Passing the struct context as user data to the above functions is of course no longer necessary.

Change History of Patch

Version history:

  • Version 1 (10 Aug 2005 -- 15 Aug 2005):
    • Puts thread number in TLS for motion_log to use.
    • Modifies all source files to not pass the context struct to motion_log.
    • Cleans up webhttpd.c where the context struct was passed only for logging purposes. (Big patch only.)

Discussion and Comments

I have updated the patch to be against a clean 3.2.2 now. There are two patch files, one for step 1 and one for all steps (I cleaned up webhttpd.c in the same way as Angel did when he collapsed the three steps into one patch).

-- PerJonsson - 15 Aug 2005

Patch uploaded for motion-20050821-223339 (daily snap). It contains all changes, including those in webhttpd.c.

-- PerJonsson - 21 Aug 2005

Integrated in

Also cleaned up all warnings about unused cnt everywhere.

-- KennethLavrsen - 21 Aug 2005
Topic revision: r9 - 20 Dec 2005, KennethLavrsen
Copyright © 1999-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Please do not email Kenneth for support questions (read why). Use the Support Requests page or join the Mailing List.
This website only use harmless session cookies. See Cookie Policy for details. By using this website you accept the use of these cookies.