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