Note that this is not a criticism about pike, but I was just a bit surprises that low level pike didn't take advantage of some thread safe functions like is localtime_r()...
It's because there's no real need since Pike has its interpreter mutex. Nothing bad can happen by using the non-threadsafe variants unless you execute (or return to) pike code in the middle.
It's only, as Marcus pointed out, an issue if other non-pike threads are running, i.e. started by some library, that also uses the non-threadsafe functions. That's not very common, but it's of course better if we play safe anyway and use the threadsafe variants; we want to be able to interface Pike with other software without risking problems, e.g. making it embeddable.
Also, considering your example from FreeBSD, it looks like localtime_r would be more efficient than localtime, which has mutexes and stuff of its own.
/ Martin Stjernholm, Roxen IS
Previous text:
2003-06-17 23:39: Subject: Re: Pike localtime(), threads safe and low level localtime() / localtime_r() ?
Le mardi, 17 jun 2003, à 22:40 Europe/Paris, Martin Stjernholm, Roxen IS @ Pike developers forum a écrit :
And why can't they reason the same way? They might have their own mutex like the interpreter mutex. Is it perhaps because they are embedded while Pike is not? But we'd like to make Pike embeddable, so if this specific issue is solved beforehand there'll be one less worry.
you are right... FreeBSD libc_r function sets a mutex... and try to getit a bit thread safe :
struct tm * localtime_r(timep, p_tm) const time_t * const timep; struct tm *p_tm; { #ifdef _THREAD_SAFE pthread_mutex_lock(&lcl_mutex); #endif tzset(); localsub(timep, 0L, p_tm); #ifdef _THREAD_SAFE pthread_mutex_unlock(&lcl_mutex); #endif return(p_tm); }
struct tm * localtime(timep) const time_t * const timep; { #ifdef _THREAD_SAFE static struct pthread_mutex _localtime_mutex = PTHREAD_MUTEX_STATIC_INITIALIZER; static pthread_mutex_t localtime_mutex = &_localtime_mutex; static pthread_key_t localtime_key = -1; struct tm *p_tm;
pthread_mutex_lock(&localtime_mutex); if (localtime_key < 0) { if (pthread_key_create(&localtime_key, free) < 0) { pthread_mutex_unlock(&localtime_mutex); return(NULL); } } pthread_mutex_unlock(&localtime_mutex); p_tm = pthread_getspecific(localtime_key); if (p_tm == NULL) { if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) ==
NULL) return(NULL); pthread_setspecific(localtime_key, p_tm); } pthread_mutex_lock(&lcl_mutex); tzset(); localsub(timep, 0L, p_tm); pthread_mutex_unlock(&lcl_mutex); return p_tm; #else tzset(); localsub(timep, 0L, &tm); return &tm; #endif }
But it is not sure that some other OS has same protection options when calling localtime() in a threaded environment.
Note that this is not a criticism about pike, but I was just a bit surprises that low level pike didn't take advantage of some thread safe functions like is localtime_r()...
/Xavier
/ Brevbäraren