[Proj] libproj4 thread safety

Glynn Clements glynn at gclements.plus.com
Sun Apr 24 23:28:32 EDT 2005


Gerald Evenden wrote:

> A couple of solutions have been offered to solve the problem of make
> either a thread safe or unsafe library but all of them force a 
> recompilation of the entire library.
> 
> As libproj4 now sits on my machine the *only* module affected with
> pthread references is pj_errno.c.  *All* other procedures do not have
> any <phread.h> or  other reference to threads including lib_proj.h.
> 
> What would be a clean, but somewhat complicated, solution at compile
> time would be two supplementary libraries libpj_errno.a and 
> libpj_errno_p.a
> so that two compile/link lines are possible and solve all cases:
> 
> gcc myprog -lproj4 -lpj_errno              #for non threads
> gcc myprog -lproj4 -lpj_errno_p -lpthread  # for threads

You only need an additional library for the multi-threaded case. You
can supply a default implementation for the single-threaded case then
allow the user to override it for the the multi-threaded case.

Given that no existing applications which use proj are multi-threaded
(they wouldn't work without the pj_errno changes), you only need to
maintain backwards compatibility with single-threaded code. You can
force the application to take additional steps for multi-threaded use.

> For some reason, a single module library seems a bit -- er  -- 
> rediculous.
> Of course, pj_errno.o is not is the above libproj4.a.
> 
> In the case of the system's errno I suspect that a thread version of the
> equivalent of pj_errno.o is in libpthread.a which is loaded prior to the
> linker seeing the version in libc.a.  Just a guess.

Yep. Both libraries define __errno_location and __h_errno_location
(the latter is for h_errno, which isused by the <netdb.h> functions).

IOW, libc gets privileged treatment from libpthread.

> What would happen if I did -lc -lpthread?

I suspect that it will fail.

> The only changes I made in libproj4 was to pj_errno.c and dropped the
> reference to the global 'extern int pj_errno' in lib_proj.h and added 
> the
> two lines referring to *pj_errno_loc() and defining pj_errno.
> 
> The bottom line is that I would strongly prefer *not* to make any 
> conditional
> compile statements in lib_proj.h and keep this whole process identical 
> to
> C library custom of handling errno.

You can't make it quite identical (unless you can persuade the
libpthread authors to grant pj_errno the same treatment they give to
errno and h_errno).

The best that you can do is to make the single-threaded case work as
it always has done while requiring minimal additional work to enable
the multi-threaded case.

AFAICT, you can't enable the multi-threaded case simply through a
compilation switch; it would have to be done through either a linker
switch or an additional line of code in the application.

-- 
Glynn Clements <glynn at gclements.plus.com>



More information about the Proj mailing list