diff options
author | deischen <deischen@FreeBSD.org> | 2001-01-24 13:01:12 +0000 |
---|---|---|
committer | deischen <deischen@FreeBSD.org> | 2001-01-24 13:01:12 +0000 |
commit | 1635c221b7b2678beeeb2b5fa728edd7c8c3735b (patch) | |
tree | d7d4f61b9e319a781a335702fe846592726c22b9 /lib/libc/stdtime/localtime.c | |
parent | 08685e9e9fd9de1e8dc51cada55659344adaf0cc (diff) | |
download | FreeBSD-src-1635c221b7b2678beeeb2b5fa728edd7c8c3735b.zip FreeBSD-src-1635c221b7b2678beeeb2b5fa728edd7c8c3735b.tar.gz |
Remove _THREAD_SAFE and make libc thread-safe by default by
adding (weak definitions to) stubs for some of the pthread
functions. If the threads library is linked in, the real
pthread functions will pulled in.
Use the following convention for system calls wrapped by the
threads library:
__sys_foo - actual system call
_foo - weak definition to __sys_foo
foo - weak definition to __sys_foo
Change all libc uses of system calls wrapped by the threads
library from foo to _foo. In order to define the prototypes
for _foo(), we introduce namespace.h and un-namespace.h
(suggested by bde). All files that need to reference these
system calls, should include namespace.h before any standard
includes, then include un-namespace.h after the standard
includes and before any local includes. <db.h> is an exception
and shouldn't be included in between namespace.h and
un-namespace.h namespace.h will define foo to _foo, and
un-namespace.h will undefine foo.
Try to eliminate some of the recursive calls to MT-safe
functions in libc/stdio in preparation for adding a mutex
to FILE. We have recursive mutexes, but would like to avoid
using them if possible.
Remove uneeded includes of <errno.h> from a few files.
Add $FreeBSD$ to a few files in order to pass commitprep.
Approved by: -arch
Diffstat (limited to 'lib/libc/stdtime/localtime.c')
-rw-r--r-- | lib/libc/stdtime/localtime.c | 184 |
1 files changed, 76 insertions, 108 deletions
diff --git a/lib/libc/stdtime/localtime.c b/lib/libc/stdtime/localtime.c index 84a4ca9..e336414 100644 --- a/lib/libc/stdtime/localtime.c +++ b/lib/libc/stdtime/localtime.c @@ -19,16 +19,20 @@ static char elsieid[] = "@(#)localtime.c 7.57"; /*LINTLIBRARY*/ +#include "namespace.h" #include <sys/types.h> #include <sys/stat.h> - +#include <fcntl.h> +#include <pthread.h> #include "private.h" +#include "un-namespace.h" + #include "tzfile.h" -#include "fcntl.h" -#ifdef _THREAD_SAFE -#include <pthread.h> -#include "pthread_private.h" -#endif + +#include "libc_private.h" + +#define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x) +#define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x) /* ** SunOS 4.1.1 headers lack O_BINARY. @@ -172,12 +176,8 @@ static struct state gmtmem; static char lcl_TZname[TZ_STRLEN_MAX + 1]; static int lcl_is_set; static int gmt_is_set; -#ifdef _THREAD_SAFE -static struct pthread_mutex _lcl_mutexd = PTHREAD_MUTEX_STATIC_INITIALIZER; -static struct pthread_mutex _gmt_mutexd = PTHREAD_MUTEX_STATIC_INITIALIZER; -static pthread_mutex_t lcl_mutex = &_lcl_mutexd; -static pthread_mutex_t gmt_mutex = &_gmt_mutexd; -#endif +static pthread_mutex_t lcl_mutex = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t gmt_mutex = PTHREAD_MUTEX_INITIALIZER; char * tzname[2] = { wildabbr, @@ -316,7 +316,7 @@ register struct state * const sp; return -1; if ((fid = _open(name, OPEN_MODE)) == -1) return -1; - if ((fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) + if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) return -1; } { @@ -929,20 +929,8 @@ struct state * const sp; (void) tzparse(gmt, sp, TRUE); } -#ifndef STD_INSPIRED -/* -** A non-static declaration of tzsetwall in a system header file -** may cause a warning about this upcoming static declaration... -*/ -static -#endif /* !defined STD_INSPIRED */ -#ifdef _THREAD_SAFE -void -tzsetwall_basic P((void)) -#else -void -tzsetwall P((void)) -#endif +static void +tzsetwall_basic(void) { if (lcl_is_set < 0) return; @@ -962,23 +950,16 @@ tzsetwall P((void)) settzname(); } -#ifdef _THREAD_SAFE void tzsetwall P((void)) { - pthread_mutex_lock(&lcl_mutex); + _MUTEX_LOCK(&lcl_mutex); tzsetwall_basic(); - pthread_mutex_unlock(&lcl_mutex); + _MUTEX_UNLOCK(&lcl_mutex); } -#endif -#ifdef _THREAD_SAFE static void -tzset_basic P((void)) -#else -void -tzset P((void)) -#endif +tzset_basic(void) { register const char * name; @@ -1018,15 +999,13 @@ tzset P((void)) settzname(); } -#ifdef _THREAD_SAFE void tzset P((void)) { - pthread_mutex_lock(&lcl_mutex); + _MUTEX_LOCK(&lcl_mutex); tzset_basic(); - pthread_mutex_unlock(&lcl_mutex); + _MUTEX_UNLOCK(&lcl_mutex); } -#endif /* ** The easy way to behave "as if no library function calls" localtime @@ -1089,14 +1068,10 @@ localtime_r(timep, p_tm) const time_t * const timep; struct tm *p_tm; { -#ifdef _THREAD_SAFE - pthread_mutex_lock(&lcl_mutex); -#endif + _MUTEX_LOCK(&lcl_mutex); tzset(); localsub(timep, 0L, p_tm); -#ifdef _THREAD_SAFE - pthread_mutex_unlock(&lcl_mutex); -#endif + _MUTEX_UNLOCK(&lcl_mutex); return(p_tm); } @@ -1104,36 +1079,36 @@ 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_mutex_t localtime_mutex = PTHREAD_MUTEX_INITIALIZER; 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); + if (__isthreaded != 0) { + _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); } - 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 } /* @@ -1146,9 +1121,7 @@ const time_t * const timep; const long offset; struct tm * const tmp; { -#ifdef _THREAD_SAFE - pthread_mutex_lock(&gmt_mutex); -#endif + _MUTEX_LOCK(&gmt_mutex); if (!gmt_is_set) { gmt_is_set = TRUE; #ifdef ALL_STATE @@ -1157,9 +1130,7 @@ struct tm * const tmp; #endif /* defined ALL_STATE */ gmtload(gmtptr); } -#ifdef _THREAD_SAFE - pthread_mutex_unlock(&gmt_mutex); -#endif + _MUTEX_UNLOCK(&gmt_mutex); timesub(timep, offset, gmtptr, tmp); #ifdef TM_ZONE /* @@ -1186,36 +1157,37 @@ struct tm * gmtime(timep) const time_t * const timep; { -#ifdef _THREAD_SAFE - static struct pthread_mutex _gmtime_mutex = PTHREAD_MUTEX_STATIC_INITIALIZER; - static pthread_mutex_t gmtime_mutex = &_gmtime_mutex; + static pthread_mutex_t gmtime_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_key_t gmtime_key = -1; struct tm *p_tm; - pthread_mutex_lock(&gmtime_mutex); - if (gmtime_key < 0) { - if (pthread_key_create(&gmtime_key, free) < 0) { - pthread_mutex_unlock(&gmtime_mutex); - return(NULL); + if (__isthreaded != 0) { + _pthread_mutex_lock(&gmtime_mutex); + if (gmtime_key < 0) { + if (_pthread_key_create(&gmtime_key, free) < 0) { + _pthread_mutex_unlock(&gmtime_mutex); + return(NULL); + } } - } - pthread_mutex_unlock(&gmtime_mutex); - /* - * Changed to follow draft 4 pthreads standard, which - * is what BSD currently has. - */ - if ((p_tm = pthread_getspecific(gmtime_key)) == NULL) { - if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) == NULL) { - return(NULL); + _pthread_mutex_unlock(&gmtime_mutex); + /* + * Changed to follow POSIX.1 threads standard, which + * is what BSD currently has. + */ + if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) { + if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) + == NULL) { + return(NULL); + } + _pthread_setspecific(gmtime_key, p_tm); } - pthread_setspecific(gmtime_key, p_tm); + gmtsub(timep, 0L, p_tm); + return(p_tm); + } + else { + gmtsub(timep, 0L, &tm); + return(&tm); } - gmtsub(timep, 0L, p_tm); - return(p_tm); -#else - gmtsub(timep, 0L, &tm); - return &tm; -#endif } struct tm * @@ -1635,14 +1607,10 @@ mktime(tmp) struct tm * const tmp; { time_t mktime_return_value; -#ifdef _THREAD_SAFE - pthread_mutex_lock(&lcl_mutex); -#endif + _MUTEX_LOCK(&lcl_mutex); tzset(); mktime_return_value = time1(tmp, localsub, 0L); -#ifdef _THREAD_SAFE - pthread_mutex_unlock(&lcl_mutex); -#endif + _MUTEX_UNLOCK(&lcl_mutex); return(mktime_return_value); } |