[Proj] libproj4 thread safety

Glynn Clements glynn at gclements.plus.com
Sun Apr 24 14:35:04 EDT 2005


I wrote:

> One possibility is to put all of the pthread code into a separate
> source file, and conditionalise the pj_errno definition, e.g.:
> 
> 	#ifdef PROJ_USES_PTHREADS
> 	#define pj_errno (*pj_errno_loc())
> 	#else
> 	extern int pj_static_errno;
> 	#define pj_errno pj_static_errno
> 	#endif
> 
> If the user compiles without PROJ_USES_PTHREADS, pj_errno_loc() will
> never be referenced, so the corresponding .o file from libproj.a won't
> get linked in, so there should be no references to the pthread
> functions in the final executable.

I overlooked one issue when suggesting the above: the libproj code
itself uses pj_errno.

One solution would be to:

1. Rename the thread-safe pj_errno_loc() function to e.g. 
pj_errno_loc_thr().

2. Add a single-threaded version in its own source file, i.e.:

	static int pj_errno_var;

	int *pj_errno_loc(void) { return &pj_errno_var; }

3. Unconditionally define pj_errno as:

	#define pj_errno (*pj_errno_loc())

If the user wants thread-safety, they would need to override the
default definition of pj_errno_loc() with:

	int *pj_errno_loc(void) { return pj_errno_loc_thr(); }

For a static libproj, this will prevent the single-threaded
implementation being used (by either libproj itself or the
application). Conversely, if the above isn't used, there will be no
references to pj_errno_loc_thr(), so the code won't be included in the
executable, and there will be no dependency upon libpthread.

For a shared library, the single-threaded pj_errno_loc would need to
be defined as a weak symbol, so that the application code can override
references to pj_errno_loc() within libproj itself. However, it would
still need to have libpthread as a dependency if it included the
thread-safe implementation.

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



More information about the Proj mailing list