Back to index

lightning-sunbird  0.9+nobinonly
ncr.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * NCR 3.0  - cloned from UnixWare by ruslan
00040  */
00041 #include "primpl.h"
00042 
00043 #include <setjmp.h>
00044 
00045 void _MD_EarlyInit(void)
00046 {
00047 }
00048 
00049 PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np)
00050 {
00051     if (isCurrent) {
00052        (void) setjmp(CONTEXT(t));
00053     }
00054     *np = sizeof(CONTEXT(t)) / sizeof(PRWord);
00055     return (PRWord *) CONTEXT(t);
00056 }
00057 
00058 #ifdef ALARMS_BREAK_TCP /* I don't think they do */
00059 
00060 PRInt32 _MD_connect(PRInt32 osfd, const PRNetAddr *addr, PRInt32 addrlen,
00061                         PRIntervalTime timeout)
00062 {
00063     PRInt32 rv;
00064 
00065     _MD_BLOCK_CLOCK_INTERRUPTS();
00066     rv = _connect(osfd,addr,addrlen);
00067     _MD_UNBLOCK_CLOCK_INTERRUPTS();
00068 }
00069 
00070 PRInt32 _MD_accept(PRInt32 osfd, PRNetAddr *addr, PRInt32 addrlen,
00071                         PRIntervalTime timeout)
00072 {
00073     PRInt32 rv;
00074 
00075     _MD_BLOCK_CLOCK_INTERRUPTS();
00076     rv = _accept(osfd,addr,addrlen);
00077     _MD_UNBLOCK_CLOCK_INTERRUPTS();
00078     return(rv);
00079 }
00080 #endif
00081 
00082 /*
00083  * These are also implemented in pratom.c using NSPR locks.  Any reason
00084  * this might be better or worse?  If you like this better, define
00085  * _PR_HAVE_ATOMIC_OPS in include/md/unixware.h
00086  */
00087 #ifdef _PR_HAVE_ATOMIC_OPS
00088 /* Atomic operations */
00089 #include  <stdio.h>
00090 static FILE *_uw_semf;
00091 
00092 void
00093 _MD_INIT_ATOMIC(void)
00094 {
00095     /* Sigh.  Sure wish SYSV semaphores weren't such a pain to use */
00096     if ((_uw_semf = tmpfile()) == NULL)
00097         PR_ASSERT(0);
00098 
00099     return;
00100 }
00101 
00102 void
00103 _MD_ATOMIC_INCREMENT(PRInt32 *val)
00104 {
00105     flockfile(_uw_semf);
00106     (*val)++;
00107     unflockfile(_uw_semf);
00108 }
00109 
00110 void
00111 _MD_ATOMIC_ADD(PRInt32 *ptr, PRInt32 val)
00112 {
00113     flockfile(_uw_semf);
00114     (*ptr) += val;
00115     unflockfile(_uw_semf);
00116 }
00117 
00118 
00119 void
00120 _MD_ATOMIC_DECREMENT(PRInt32 *val)
00121 {
00122     flockfile(_uw_semf);
00123     (*val)--;
00124     unflockfile(_uw_semf);
00125 }
00126 
00127 void
00128 _MD_ATOMIC_SET(PRInt32 *val, PRInt32 newval)
00129 {
00130     flockfile(_uw_semf);
00131     *val = newval;
00132     unflockfile(_uw_semf);
00133 }
00134 #endif
00135 
00136 void
00137 _MD_SET_PRIORITY(_MDThread *thread, PRUintn newPri)
00138 {
00139     return;
00140 }
00141 
00142 PRStatus
00143 _MD_InitializeThread(PRThread *thread)
00144 {
00145        return PR_SUCCESS;
00146 }
00147 
00148 PRStatus
00149 _MD_WAIT(PRThread *thread, PRIntervalTime ticks)
00150 {
00151     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
00152     _PR_MD_SWITCH_CONTEXT(thread);
00153     return PR_SUCCESS;
00154 }
00155 
00156 PRStatus
00157 _MD_WAKEUP_WAITER(PRThread *thread)
00158 {
00159     if (thread) {
00160        PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
00161     }
00162     return PR_SUCCESS;
00163 }
00164 
00165 /* These functions should not be called for Unixware */
00166 void
00167 _MD_YIELD(void)
00168 {
00169     PR_NOT_REACHED("_MD_YIELD should not be called for Unixware.");
00170 }
00171 
00172 PRStatus
00173 _MD_CREATE_THREAD(
00174     PRThread *thread,
00175     void (*start) (void *),
00176     PRUintn priority,
00177     PRThreadScope scope,
00178     PRThreadState state,
00179     PRUint32 stackSize)
00180 {
00181     PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for Unixware.");
00182     return PR_FAILURE;
00183 }
00184 
00185 /*
00186  This is temp. replacement for localtime_r. Normally PR_ExplodeTime should
00187  be used as to my understanding
00188 */
00189 
00190 /*
00191 ** $$$$$ THEN WHY ARE WE DOING THIS? - AOF $$$$$
00192 */
00193 
00194 #define NEED_LOCALTIME_R
00195 #define NEED_GMTIME_R
00196 #define NEED_ASCTIME_R
00197 #define NEED_STRTOK_R
00198 #define NEED_CTIME_R
00199 
00200 #if defined (NEED_LOCALTIME_R) || defined (NEED_CTIME_R) || defined (NEED_ASCTIME_R) || defined (NEED_GMTIME_R) || defined (NEED_STRTOK_R)
00201 #include "prlock.h"
00202 #endif
00203 
00204 #if defined (NEED_LOCALTIME_R)
00205 
00206 static PRLock *localtime_r_monitor = NULL;
00207 
00208 struct tm *localtime_r (const time_t *clock, struct tm *result)
00209 {
00210     struct tm *tmPtr;
00211     int needLock = PR_Initialized();  /* We need to use a lock to protect
00212                                        * against NSPR threads only when the
00213                                        * NSPR thread system is activated. */
00214 
00215     if (needLock) {
00216         if (localtime_r_monitor == NULL) {
00217 
00218             localtime_r_monitor = PR_NewLock();
00219         }
00220         PR_Lock(localtime_r_monitor);
00221     }
00222 
00223     /*
00224      * On Windows, localtime() returns a NULL pointer if 'clock'
00225      * represents a time before midnight January 1, 1970.  In
00226      * that case, we also return a NULL pointer and the struct tm
00227      * object pointed to by 'result' is not modified.
00228      */
00229 
00230     tmPtr = localtime(clock);
00231     if (tmPtr) {
00232         *result = *tmPtr;
00233     } else {
00234         result = NULL;
00235     }
00236 
00237     if (needLock) PR_Unlock(localtime_r_monitor);
00238 
00239     return result;
00240 }
00241 
00242 #endif
00243 
00244 #if defined (NEED_GMTIME_R)
00245 
00246 static PRLock *gmtime_r_monitor = NULL;
00247 
00248 struct tm *gmtime_r (const time_t *clock, struct tm *result)
00249 {
00250     struct tm *tmPtr;
00251     int needLock = PR_Initialized();  /* We need to use a lock to protect
00252                                        * against NSPR threads only when the
00253                                        * NSPR thread system is activated. */
00254 
00255     if (needLock) {
00256         if (gmtime_r_monitor == NULL) {
00257             gmtime_r_monitor = PR_NewLock();
00258         }
00259         PR_Lock(gmtime_r_monitor);
00260     }
00261 
00262     tmPtr = gmtime(clock);
00263     if (tmPtr) {
00264         *result = *tmPtr;
00265     } else {
00266         result = NULL;
00267     }
00268 
00269     if (needLock) PR_Unlock(gmtime_r_monitor);
00270 
00271     return result;
00272 }
00273 
00274 #endif
00275 
00276 #if defined (NEED_CTIME_R)
00277 
00278 static PRLock *ctime_r_monitor = NULL;
00279 
00280 char  *ctime_r (const time_t *clock, char *buf, int buflen)
00281 {
00282     char *cbuf;
00283     int needLock = PR_Initialized();  /* We need to use a lock to protect
00284                                        * against NSPR threads only when the
00285                                        * NSPR thread system is activated. */
00286 
00287     if (needLock) {
00288 
00289         if (ctime_r_monitor == NULL) {
00290             ctime_r_monitor = PR_NewLock();
00291         }
00292         PR_Lock(ctime_r_monitor);
00293     }
00294 
00295     cbuf = ctime (clock);
00296     if (cbuf) {
00297         strncpy (buf, cbuf, buflen - 1);
00298         buf[buflen - 1] = 0;
00299     }
00300 
00301     if (needLock) PR_Unlock(ctime_r_monitor);
00302 
00303     return cbuf;
00304 }
00305 
00306 #endif
00307 
00308 #if defined (NEED_ASCTIME_R)
00309 
00310 static PRLock *asctime_r_monitor = NULL;
00311 
00312 
00313 char  *asctime_r (const struct tm  *tm, char *buf, int buflen)
00314 {
00315     char *cbuf;
00316     int needLock = PR_Initialized();  /* We need to use a lock to protect
00317                                        * against NSPR threads only when the
00318                                        * NSPR thread system is activated. */
00319 
00320     if (needLock) {
00321         if (asctime_r_monitor == NULL) {
00322             asctime_r_monitor = PR_NewLock();
00323         }
00324         PR_Lock(asctime_r_monitor);
00325     }
00326 
00327     cbuf = asctime (tm);
00328     if (cbuf) {
00329         strncpy (buf, cbuf, buflen - 1);
00330         buf[buflen - 1] = 0;
00331     }
00332 
00333     if (needLock) PR_Unlock(asctime_r_monitor);
00334 
00335     return cbuf;
00336 
00337 }
00338 #endif
00339 
00340 #if defined (NEED_STRTOK_R)
00341 
00342 char *
00343 strtok_r (s, delim, last)
00344         register char *s;
00345         register const char *delim;
00346         register char **last;
00347 {
00348         register char *spanp;
00349         register int c, sc;
00350         char *tok;
00351 
00352 
00353         if (s == NULL && (s = *last) == NULL)
00354                 return (NULL);
00355 
00356         /*
00357          * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
00358          */
00359 cont:
00360 
00361         c = *s++;
00362         for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
00363                 if (c == sc)
00364                         goto cont;
00365         }
00366 
00367         if (c == 0) {           /* no non-delimiter characters */
00368                 *last = NULL;
00369                 return (NULL);
00370         }
00371         tok = s - 1;
00372 
00373         /*
00374          * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
00375          * Note that delim must have one NUL; we stop if we see that, too.
00376          */
00377         for (;;) {
00378                 c = *s++;
00379                 spanp = (char *)delim;
00380                 do {
00381                         if ((sc = *spanp++) == c) {
00382                                 if (c == 0)
00383                                         s = NULL;
00384 
00385                                 else
00386                                         s[-1] = 0;
00387                                 *last = s;
00388                                 return (tok);
00389                         }
00390                 } while (sc != 0);
00391         }
00392         /* NOTREACHED */
00393 }
00394 
00395 #endif