[Proj] libproj4 thread safety

Gerald Evenden gerald.evenden at verizon.net
Sat Apr 23 19:59:34 EDT 2005


The problem of the thread safe version of pj_errno for
libproj4 seems to be taken care of with the following file:

#include <pthread.h>

         static pthread_once_t
key_once = PTHREAD_ONCE_INIT;
         static pthread_key_t
key;
         static void
destroy(void *buf) { free(buf); }
         static void
create(void) { pthread_key_create(&key, destroy); }
         int *
pj_errno_loc(void) {
         void *p;

         pthread_once(&key_once, create);
         if (!(p = pthread_getspecific(key))) {
                 p = (void *)malloc(sizeof(int));
                 pthread_setspecific(key, p);
         }
         return (int *)p;
}

and the following addition to a general header file:

extern int *pj_errno_loc();
#define pj_errno (*pj_errno_loc())

Note that some return error checks are not made but what
to do if they fail??  Can't set pj_errno. :-(  Maybe
should just call one of the exit procedures.

Next problem is how to handle this procedure for both those
interested in threads and those not caring a whit.  If it
is placed in libproj4.a, users have to link with '-l threads'
regardless of using threads.  Make two different libraries?
Do not include pj_errno.o library and force user to include
pj_errno.o on compile line?  Note: I will include a compile
switch to allow for compiling as either a vanilla or pthreads
version---it makes no difference to the macro inclusion nor
the remainder of the libproj4 system.

Comments, suggestions would be appreciated here.

I will run some more rigorous tests of the above with a
scaffold program before installing in the next release.

Lastly, thanks for the help on this problem.  Trying to
learn about threads from on-line manuals is not my preferred
method.  In this case I completely missed the use of
'pthread_once' which is critical to this solution.
_____________________________________
Jerry and the low riders: Daisy Mae and Joshua.
"The whole religious complexion of the modern world is due to the
absence from Jerusalem of a lunatic asylum." Havelock Ellis, 1914




More information about the Proj mailing list