home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / src / md / unix / ncr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.6 KB  |  368 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * NCR 3.0  - cloned from UnixWare by ruslan
  21.  */
  22. #include "primpl.h"
  23.  
  24. #include <setjmp.h>
  25.  
  26. void _MD_EarlyInit(void)
  27. {
  28. }
  29.  
  30. PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np)
  31. {
  32.     if (isCurrent) {
  33.     (void) setjmp(CONTEXT(t));
  34.     }
  35.     *np = sizeof(CONTEXT(t)) / sizeof(PRWord);
  36.     return (PRWord *) CONTEXT(t);
  37. }
  38.  
  39. #ifdef ALARMS_BREAK_TCP /* I don't think they do */
  40.  
  41. PRInt32 _MD_connect(PRInt32 osfd, const PRNetAddr *addr, PRInt32 addrlen,
  42.                         PRIntervalTime timeout)
  43. {
  44.     PRInt32 rv;
  45.  
  46.     _MD_BLOCK_CLOCK_INTERRUPTS();
  47.     rv = _connect(osfd,addr,addrlen);
  48.     _MD_UNBLOCK_CLOCK_INTERRUPTS();
  49. }
  50.  
  51. PRInt32 _MD_accept(PRInt32 osfd, PRNetAddr *addr, PRInt32 addrlen,
  52.                         PRIntervalTime timeout)
  53. {
  54.     PRInt32 rv;
  55.  
  56.     _MD_BLOCK_CLOCK_INTERRUPTS();
  57.     rv = _accept(osfd,addr,addrlen);
  58.     _MD_UNBLOCK_CLOCK_INTERRUPTS();
  59.     return(rv);
  60. }
  61. #endif
  62.  
  63. /*
  64.  * These are also implemented in pratom.c using NSPR locks.  Any reason
  65.  * this might be better or worse?  If you like this better, define
  66.  * _PR_HAVE_ATOMIC_OPS in include/md/unixware.h
  67.  */
  68. #ifdef _PR_HAVE_ATOMIC_OPS
  69. /* Atomic operations */
  70. #include  <stdio.h>
  71. static FILE *_uw_semf;
  72.  
  73. void
  74. _MD_INIT_ATOMIC(void)
  75. {
  76.     /* Sigh.  Sure wish SYSV semaphores weren't such a pain to use */
  77.     if ((_uw_semf = tmpfile()) == NULL)
  78.         PR_ASSERT(0);
  79.  
  80.     return;
  81. }
  82.  
  83. void
  84. _MD_ATOMIC_INCREMENT(PRInt32 *val)
  85. {
  86.     flockfile(_uw_semf);
  87.     (*val)++;
  88.     unflockfile(_uw_semf);
  89. }
  90.  
  91. void
  92. _MD_ATOMIC_DECREMENT(PRInt32 *val)
  93. {
  94.     flockfile(_uw_semf);
  95.     (*val)--;
  96.     unflockfile(_uw_semf);
  97. }
  98.  
  99. void
  100. _MD_ATOMIC_SET(PRInt32 *val, PRInt32 newval)
  101. {
  102.     flockfile(_uw_semf);
  103.     *val = newval;
  104.     unflockfile(_uw_semf);
  105. }
  106. #endif
  107.  
  108. void
  109. _MD_SET_PRIORITY(_MDThread *thread, PRUintn newPri)
  110. {
  111.     return;
  112. }
  113.  
  114. PRStatus
  115. _MD_InitializeThread(PRThread *thread)
  116. {
  117.     return PR_SUCCESS;
  118. }
  119.  
  120. PRStatus
  121. _MD_WAIT(PRThread *thread, PRIntervalTime ticks)
  122. {
  123.     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
  124.     _PR_MD_SWITCH_CONTEXT(thread);
  125.     return PR_SUCCESS;
  126. }
  127.  
  128. PRStatus
  129. _MD_WAKEUP_WAITER(PRThread *thread)
  130. {
  131.     if (thread) {
  132.     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
  133.     }
  134.     return PR_SUCCESS;
  135. }
  136.  
  137. /* These functions should not be called for Unixware */
  138. void
  139. _MD_YIELD(void)
  140. {
  141.     PR_NOT_REACHED("_MD_YIELD should not be called for Unixware.");
  142. }
  143.  
  144. PRStatus
  145. _MD_CREATE_THREAD(
  146.     PRThread *thread,
  147.     void (*start) (void *),
  148.     PRUintn priority,
  149.     PRThreadScope scope,
  150.     PRThreadState state,
  151.     PRUint32 stackSize)
  152. {
  153.     PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for Unixware.");
  154.     return PR_FAILURE;
  155. }
  156.  
  157. /*
  158.  This is temp. replacement for localtime_r. Normally PR_ExplodeTime should
  159.  be used as to my understanding
  160. */
  161.  
  162. /*
  163. ** $$$$$ THEN WHY ARE WE DOING THIS? - AOF $$$$$
  164. */
  165.  
  166. #define NEED_LOCALTIME_R
  167. #define NEED_GMTIME_R
  168. #define NEED_ASCTIME_R
  169. #define NEED_STRTOK_R
  170. #define NEED_CTIME_R
  171.  
  172. #if defined (NEED_LOCALTIME_R) || defined (NEED_CTIME_R) || defined (NEED_ASCTIME_R) || defined (NEED_GMTIME_R) || defined (NEED_STRTOK_R)
  173. #include "prlock.h"
  174. #endif
  175.  
  176. #if defined (NEED_LOCALTIME_R)
  177.  
  178. static PRLock *localtime_r_monitor = NULL;
  179.  
  180. struct tm *localtime_r (const time_t *clock, struct tm *result)
  181. {
  182.     struct tm *tmPtr;
  183.     int needLock = PR_Initialized();  /* We need to use a lock to protect
  184.                                        * against NSPR threads only when the
  185.                                        * NSPR thread system is activated. */
  186.  
  187.     if (needLock) {
  188.         if (localtime_r_monitor == NULL) {
  189.  
  190.             localtime_r_monitor = PR_NewLock();
  191.         }
  192.         PR_Lock(localtime_r_monitor);
  193.     }
  194.  
  195.     /*
  196.      * On Windows, localtime() returns a NULL pointer if 'clock'
  197.      * represents a time before midnight January 1, 1970.  In
  198.      * that case, we also return a NULL pointer and the struct tm
  199.      * object pointed to by 'result' is not modified.
  200.      */
  201.  
  202.     tmPtr = localtime(clock);
  203.     if (tmPtr) {
  204.         *result = *tmPtr;
  205.     } else {
  206.         result = NULL;
  207.     }
  208.  
  209.     if (needLock) PR_Unlock(localtime_r_monitor);
  210.  
  211.     return result;
  212. }
  213.  
  214. #endif
  215.  
  216. #if defined (NEED_GMTIME_R)
  217.  
  218. static PRLock *gmtime_r_monitor = NULL;
  219.  
  220. struct tm *gmtime_r (const time_t *clock, struct tm *result)
  221. {
  222.     struct tm *tmPtr;
  223.     int needLock = PR_Initialized();  /* We need to use a lock to protect
  224.                                        * against NSPR threads only when the
  225.                                        * NSPR thread system is activated. */
  226.  
  227.     if (needLock) {
  228.         if (gmtime_r_monitor == NULL) {
  229.             gmtime_r_monitor = PR_NewLock();
  230.         }
  231.         PR_Lock(gmtime_r_monitor);
  232.     }
  233.  
  234.     tmPtr = gmtime(clock);
  235.     if (tmPtr) {
  236.         *result = *tmPtr;
  237.     } else {
  238.         result = NULL;
  239.     }
  240.  
  241.     if (needLock) PR_Unlock(gmtime_r_monitor);
  242.  
  243.     return result;
  244. }
  245.  
  246. #endif
  247.  
  248. #if defined (NEED_CTIME_R)
  249.  
  250. static PRLock *ctime_r_monitor = NULL;
  251.  
  252. char  *ctime_r (const time_t *clock, char *buf, int buflen)
  253. {
  254.     char *cbuf;
  255.     int needLock = PR_Initialized();  /* We need to use a lock to protect
  256.                                        * against NSPR threads only when the
  257.                                        * NSPR thread system is activated. */
  258.  
  259.     if (needLock) {
  260.  
  261.         if (ctime_r_monitor == NULL) {
  262.             ctime_r_monitor = PR_NewLock();
  263.         }
  264.         PR_Lock(ctime_r_monitor);
  265.     }
  266.  
  267.     cbuf = ctime (clock);
  268.     if (cbuf) {
  269.         strncpy (buf, cbuf, buflen - 1);
  270.         buf[buflen - 1] = 0;
  271.     }
  272.  
  273.     if (needLock) PR_Unlock(ctime_r_monitor);
  274.  
  275.     return cbuf;
  276. }
  277.  
  278. #endif
  279.  
  280. #if defined (NEED_ASCTIME_R)
  281.  
  282. static PRLock *asctime_r_monitor = NULL;
  283.  
  284.  
  285. char  *asctime_r (const struct tm  *tm, char *buf, int buflen)
  286. {
  287.     char *cbuf;
  288.     int needLock = PR_Initialized();  /* We need to use a lock to protect
  289.                                        * against NSPR threads only when the
  290.                                        * NSPR thread system is activated. */
  291.  
  292.     if (needLock) {
  293.         if (asctime_r_monitor == NULL) {
  294.             asctime_r_monitor = PR_NewLock();
  295.         }
  296.         PR_Lock(asctime_r_monitor);
  297.     }
  298.  
  299.     cbuf = asctime (tm);
  300.     if (cbuf) {
  301.         strncpy (buf, cbuf, buflen - 1);
  302.         buf[buflen - 1] = 0;
  303.     }
  304.  
  305.     if (needLock) PR_Unlock(asctime_r_monitor);
  306.  
  307.     return cbuf;
  308.  
  309. }
  310. #endif
  311.  
  312. #if defined (NEED_STRTOK_R)
  313.  
  314. char *
  315. strtok_r (s, delim, last)
  316.         register char *s;
  317.         register const char *delim;
  318.         register char **last;
  319. {
  320.         register char *spanp;
  321.         register int c, sc;
  322.         char *tok;
  323.  
  324.  
  325.         if (s == NULL && (s = *last) == NULL)
  326.                 return (NULL);
  327.  
  328.         /*
  329.          * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
  330.          */
  331. cont:
  332.  
  333.         c = *s++;
  334.         for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
  335.                 if (c == sc)
  336.                         goto cont;
  337.         }
  338.  
  339.         if (c == 0) {           /* no non-delimiter characters */
  340.                 *last = NULL;
  341.                 return (NULL);
  342.         }
  343.         tok = s - 1;
  344.  
  345.         /*
  346.          * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  347.          * Note that delim must have one NUL; we stop if we see that, too.
  348.          */
  349.         for (;;) {
  350.                 c = *s++;
  351.                 spanp = (char *)delim;
  352.                 do {
  353.                         if ((sc = *spanp++) == c) {
  354.                                 if (c == 0)
  355.                                         s = NULL;
  356.  
  357.                                 else
  358.                                         s[-1] = 0;
  359.                                 *last = s;
  360.                                 return (tok);
  361.                         }
  362.                 } while (sc != 0);
  363.         }
  364.         /* NOTREACHED */
  365. }
  366.  
  367. #endif
  368.