summaryrefslogtreecommitdiff
path: root/pr/src/md
diff options
context:
space:
mode:
Diffstat (limited to 'pr/src/md')
-rw-r--r--pr/src/md/beos/bfile.c422
-rw-r--r--pr/src/md/beos/bmisc.c7
-rw-r--r--pr/src/md/beos/bmmap.c3
-rw-r--r--pr/src/md/beos/bnet.c811
-rw-r--r--pr/src/md/beos/bproc.c184
-rw-r--r--pr/src/md/mac/macdll.c106
-rw-r--r--pr/src/md/mac/macio.c22
-rw-r--r--pr/src/md/mac/macsocket.h1
-rw-r--r--pr/src/md/mac/macsockotpt.c346
-rw-r--r--pr/src/md/mac/macthr.c170
-rw-r--r--pr/src/md/mac/mdcriticalregion.c1
-rw-r--r--pr/src/md/mac/mdmac.c5
-rw-r--r--pr/src/md/os2/Makefile.in4
-rw-r--r--pr/src/md/os2/objs.mk6
-rw-r--r--pr/src/md/os2/os2_errors.c149
-rw-r--r--pr/src/md/os2/os2cv.c112
-rw-r--r--pr/src/md/os2/os2emx.s111
-rw-r--r--pr/src/md/os2/os2inrval.c73
-rw-r--r--pr/src/md/os2/os2io.c55
-rw-r--r--pr/src/md/os2/os2misc.c398
-rw-r--r--pr/src/md/os2/os2poll.c417
-rw-r--r--pr/src/md/os2/os2sock.c1036
-rw-r--r--pr/src/md/os2/os2thred.c152
-rw-r--r--pr/src/md/os2/os2vaclegacy.s74
-rw-r--r--pr/src/md/os2/os2vacpp.asm199
-rw-r--r--pr/src/md/unix/Makefile.in22
-rw-r--r--pr/src/md/unix/darwin.c33
-rw-r--r--pr/src/md/unix/irix.c8
-rw-r--r--pr/src/md/unix/openvms.c76
-rw-r--r--pr/src/md/unix/os_Darwin_ppc.s92
-rw-r--r--pr/src/md/unix/os_Linux_x86.s3
-rw-r--r--pr/src/md/unix/os_Linux_x86_64.s95
-rw-r--r--pr/src/md/unix/solaris.c38
-rw-r--r--pr/src/md/unix/unix.c53
-rw-r--r--pr/src/md/unix/unix_errors.c10
-rw-r--r--pr/src/md/unix/uxpoll.c9
-rw-r--r--pr/src/md/unix/uxproces.c2
-rw-r--r--pr/src/md/unix/uxrng.c19
-rw-r--r--pr/src/md/unix/uxshm.c7
-rw-r--r--pr/src/md/windows/Makefile.in10
-rw-r--r--pr/src/md/windows/ntinrval.c47
-rw-r--r--pr/src/md/windows/ntio.c35
-rw-r--r--pr/src/md/windows/ntmisc.c24
-rw-r--r--pr/src/md/windows/ntthread.c43
-rw-r--r--pr/src/md/windows/w32poll.c40
-rw-r--r--pr/src/md/windows/w95io.c420
-rw-r--r--pr/src/md/windows/w95sock.c26
-rw-r--r--pr/src/md/windows/w95thred.c23
48 files changed, 3995 insertions, 2004 deletions
diff --git a/pr/src/md/beos/bfile.c b/pr/src/md/beos/bfile.c
index e80181cc..95206f30 100644
--- a/pr/src/md/beos/bfile.c
+++ b/pr/src/md/beos/bfile.c
@@ -118,18 +118,45 @@ _MD_make_nonblock (PRFileDesc *fd)
}
+PRStatus
+_MD_set_fd_inheritable (PRFileDesc *fd, PRBool inheritable)
+{
+ int rv;
+
+ rv = fcntl(fd->secret->md.osfd, F_SETFD, inheritable ? 0 : FD_CLOEXEC);
+ if (-1 == rv) {
+ PR_SetError(PR_UNKNOWN_ERROR, _MD_ERRNO());
+ return PR_FAILURE;
+ }
+ return PR_SUCCESS;
+}
+
void
_MD_init_fd_inheritable (PRFileDesc *fd, PRBool imported)
{
- /* XXX this function needs to be implemented */
- fd->secret->inheritable = _PR_TRI_UNKNOWN;
+ if (imported) {
+ fd->secret->inheritable = _PR_TRI_UNKNOWN;
+ } else {
+ int flags = fcntl(fd->secret->md.osfd, F_GETFD, 0);
+ if (flags == -1) {
+ PR_SetError(PR_UNKNOWN_ERROR, _MD_ERRNO());
+ return;
+ }
+ fd->secret->inheritable = (flags & FD_CLOEXEC) ?
+ _PR_TRI_TRUE : _PR_TRI_FALSE;
+ }
}
void
_MD_query_fd_inheritable (PRFileDesc *fd)
{
- /* XXX this function needs to be implemented */
- PR_ASSERT(0);
+ int flags;
+
+ PR_ASSERT(_PR_TRI_UNKNOWN == fd->secret->inheritable);
+ flags = fcntl(fd->secret->md.osfd, F_GETFD, 0);
+ PR_ASSERT(-1 != flags);
+ fd->secret->inheritable = (flags & FD_CLOEXEC) ?
+ _PR_TRI_FALSE : _PR_TRI_TRUE;
}
PRInt32
@@ -223,12 +250,14 @@ _MD_write (PRFileDesc *fd, const void *buf, PRInt32 amount)
return( rv );
}
+#ifndef BONE_VERSION /* Writev moves to bnet.c with BONE */
PRInt32
-_MD_writev (PRFileDesc *fd, struct PRIOVec *iov, PRInt32 iov_size,
+_MD_writev (PRFileDesc *fd, const PRIOVec *iov, PRInt32 iov_size,
PRIntervalTime timeout)
{
return PR_NOT_IMPLEMENTED_ERROR;
}
+#endif
PRInt32
_MD_lseek (PRFileDesc *fd, PRInt32 offset, int whence)
@@ -524,191 +553,280 @@ int rv, err;
}
PRInt32
-_MD_pr_poll (PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
+_MD_pr_poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
{
- PRInt32 rc = 0;
- fd_set rd, wr;
- struct timeval tv, *tvp = NULL;
+ PRInt32 rv = 0;
+ PRThread *me = _PR_MD_CURRENT_THREAD();
+ /*
+ * This code is almost a duplicate of w32poll.c's _PR_MD_PR_POLL().
+ */
+ fd_set rd, wt, ex;
+ PRFileDesc *bottom;
PRPollDesc *pd, *epd;
- int i = 0, j = 0;
- int maxfd = -1;
- PRInt32 osfd;
- PRInt16 in_flags;
- PRFileDesc *bottom;
+ PRInt32 maxfd = -1, ready, err;
+ PRIntervalTime remaining, elapsed, start;
- /*printf("POLL: entering _MD_pr_poll\n");*/
-
- /*
- * Is it an empty set? If so, just sleep for the timeout and return
- */
- if (npds < 1)
- {
- /*printf("POLL: empty set. exiting _MD_pr_poll\n");*/
- PR_Sleep(timeout);
- return rc;
- }
+ struct timeval tv, *tvp = NULL;
+
+ if (_PR_PENDING_INTERRUPT(me))
+ {
+ me->flags &= ~_PR_INTERRUPT;
+ PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
+ return -1;
+ }
+
+ if (0 == npds) {
+ PR_Sleep(timeout);
+ return rv;
+ }
- FD_ZERO(&rd);
- FD_ZERO(&wr);
+ FD_ZERO(&rd);
+ FD_ZERO(&wt);
+ FD_ZERO(&ex);
- /*
- * first, sort out the new connects, the reads, and the writes
- */
- epd = pds + npds;
- for(pd = pds; pd < epd; pd++)
+ ready = 0;
+ for (pd = pds, epd = pd + npds; pd < epd; pd++)
{
- in_flags = pd->in_flags;
- bottom = pd->fd;
+ PRInt16 in_flags_read = 0, in_flags_write = 0;
+ PRInt16 out_flags_read = 0, out_flags_write = 0;
- if(bottom != 0 && in_flags != 0)
+ if ((NULL != pd->fd) && (0 != pd->in_flags))
{
- while(bottom->lower != 0)
+ if (pd->in_flags & PR_POLL_READ)
{
- bottom = bottom->lower;
+ in_flags_read = (pd->fd->methods->poll)(pd->fd, pd->in_flags & ~PR_POLL_WRITE, &out_flags_read);
}
- osfd = bottom->secret->md.osfd;
-
- if(in_flags & PR_POLL_WRITE || in_flags & PR_POLL_EXCEPT)
+ if (pd->in_flags & PR_POLL_WRITE)
{
- /*printf("POLL: adding to write\n");*/
- FD_SET(osfd, &wr);
- if( osfd > maxfd ) maxfd = osfd;
+ in_flags_write = (pd->fd->methods->poll)(pd->fd, pd->in_flags & ~PR_POLL_READ, &out_flags_write);
+ }
+ if ((0 != (in_flags_read & out_flags_read))
+ || (0 != (in_flags_write & out_flags_write)))
+ {
+ /* this one's ready right now */
+ if (0 == ready)
+ {
+ /*
+ * We will have to return without calling the
+ * system poll/select function. So zero the
+ * out_flags fields of all the poll descriptors
+ * before this one.
+ */
+ PRPollDesc *prev;
+ for (prev = pds; prev < pd; prev++)
+ {
+ prev->out_flags = 0;
+ }
+ }
+ ready += 1;
+ pd->out_flags = out_flags_read | out_flags_write;
}
- if(in_flags & PR_POLL_READ || in_flags & PR_POLL_EXCEPT)
+ else
{
- /*printf("POLL: adding to read\n");*/
- FD_SET(osfd, &rd);
- if( osfd > maxfd ) maxfd = osfd;
+ pd->out_flags = 0; /* pre-condition */
+
+ /* make sure this is an NSPR supported stack */
+ bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
+ PR_ASSERT(NULL != bottom); /* what to do about that? */
+ if ((NULL != bottom)
+ && (_PR_FILEDESC_OPEN == bottom->secret->state))
+ {
+ if (0 == ready)
+ {
+ PRInt32 osfd = bottom->secret->md.osfd;
+ if (osfd > maxfd) maxfd = osfd;
+ if (in_flags_read & PR_POLL_READ)
+ {
+ pd->out_flags |= _PR_POLL_READ_SYS_READ;
+ FD_SET(osfd, &rd);
+ }
+ if (in_flags_read & PR_POLL_WRITE)
+ {
+ pd->out_flags |= _PR_POLL_READ_SYS_WRITE;
+ FD_SET(osfd, &wt);
+ }
+ if (in_flags_write & PR_POLL_READ)
+ {
+ pd->out_flags |= _PR_POLL_WRITE_SYS_READ;
+ FD_SET(osfd, &rd);
+ }
+ if (in_flags_write & PR_POLL_WRITE)
+ {
+ pd->out_flags |= _PR_POLL_WRITE_SYS_WRITE;
+ FD_SET(osfd, &wt);
+ }
+ if (pd->in_flags & PR_POLL_EXCEPT) FD_SET(osfd, &ex);
+ }
+ }
+ else
+ {
+ if (0 == ready)
+ {
+ PRPollDesc *prev;
+ for (prev = pds; prev < pd; prev++)
+ {
+ prev->out_flags = 0;
+ }
+ }
+ ready += 1; /* this will cause an abrupt return */
+ pd->out_flags = PR_POLL_NVAL; /* bogii */
+ }
}
}
-
-
- }
+ else
+ {
+ pd->out_flags = 0;
+ }
+ }
+
+ if (0 != ready) return ready; /* no need to block */
- if(maxfd >= 0)
+ remaining = timeout;
+ start = PR_IntervalNow();
+
+ retry:
+ if (timeout != PR_INTERVAL_NO_TIMEOUT)
+ {
+ PRInt32 ticksPerSecond = PR_TicksPerSecond();
+ tv.tv_sec = remaining / ticksPerSecond;
+ tv.tv_usec = PR_IntervalToMicroseconds( remaining % ticksPerSecond );
+ tvp = &tv;
+ }
+
+ ready = _MD_SELECT(maxfd + 1, &rd, &wt, &ex, tvp);
+
+ if (ready == -1 && errno == EINTR)
{
- PRInt32 n;
- do {
- PRIntervalTime start = PR_IntervalNow();
- if (timeout != PR_INTERVAL_NO_TIMEOUT)
+ if (timeout == PR_INTERVAL_NO_TIMEOUT) goto retry;
+ else
+ {
+ elapsed = (PRIntervalTime) (PR_IntervalNow() - start);
+ if (elapsed > timeout) ready = 0; /* timed out */
+ else
{
- /*printf("POLL: timeout = %ld\n", (long) PR_IntervalToMicroseconds(timeout));*/
- tv.tv_sec = PR_IntervalToSeconds(timeout);
- tv.tv_usec = PR_IntervalToMicroseconds(timeout) % PR_USEC_PER_SEC;
- tvp = &tv;
+ remaining = timeout - elapsed;
+ goto retry;
}
+ }
+ }
-
- n = select(maxfd + 1, &rd, &wr, 0, tvp);
- /*printf("POLL: maxfd = %d, select returns %d, errno = %d %s\n", maxfd, n, errno, strerror(errno));*/
- if (n == 0 || (n < 0 && errno == EINTR))
+ /*
+ ** Now to unravel the select sets back into the client's poll
+ ** descriptor list. Is this possibly an area for pissing away
+ ** a few cycles or what?
+ */
+ if (ready > 0)
+ {
+ ready = 0;
+ for (pd = pds, epd = pd + npds; pd < epd; pd++)
+ {
+ PRInt16 out_flags = 0;
+ if ((NULL != pd->fd) && (0 != pd->in_flags))
{
- if (timeout != PR_INTERVAL_NO_TIMEOUT)
- {
- timeout -= PR_IntervalNow() - start;
- if(timeout <= 0)
+ PRInt32 osfd;
+ bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
+ PR_ASSERT(NULL != bottom);
+
+ osfd = bottom->secret->md.osfd;
+
+ if (FD_ISSET(osfd, &rd))
{
- /* timed out */
- n = 0;
+ if (pd->out_flags & _PR_POLL_READ_SYS_READ)
+ out_flags |= PR_POLL_READ;
+ if (pd->out_flags & _PR_POLL_WRITE_SYS_READ)
+ out_flags |= PR_POLL_WRITE;
}
- }
- }
-
- } while(n < 0 && errno == EINTR);
-
- if(n > 0)
- {
- epd = pds + npds;
- for(pd = pds; pd < epd; pd++)
- {
- int selected;
- in_flags = pd->in_flags;
- bottom = pd->fd;
- selected = 0;
-
- if(bottom != 0 && in_flags != 0)
+ if (FD_ISSET(osfd, &wt))
{
- while(bottom->lower != 0)
- {
- bottom = bottom->lower;
- }
- osfd = bottom->secret->md.osfd;
- if (FD_ISSET(osfd, &rd))
- {
- pd->out_flags |= PR_POLL_READ;
- selected++;
- }
- if (FD_ISSET(osfd, &wr))
- {
- pd->out_flags |= PR_POLL_WRITE;
- selected++;
- }
-
- if(selected > 0)
+ if (pd->out_flags & _PR_POLL_READ_SYS_WRITE)
+ out_flags |= PR_POLL_READ;
+ if (pd->out_flags & _PR_POLL_WRITE_SYS_WRITE)
+ out_flags |= PR_POLL_WRITE;
+ }
+ if (FD_ISSET(osfd, &ex)) out_flags |= PR_POLL_EXCEPT;
+
+/* Workaround for nonblocking connects under net_server */
+#ifndef BONE_VERSION
+ if (out_flags)
+ {
+ /* check if it is a pending connect */
+ int i = 0, j = 0;
+ PR_Lock( _connectLock );
+ for( i = 0; i < connectCount; i++ )
{
- rc++;
- /*
- * check if it is a pending connect
- */
- PR_Lock( _connectLock );
- for( i = 0; i < connectCount; i++ )
+ if(connectList[i].osfd == osfd)
{
- if(connectList[i].osfd == osfd)
+ int connectError;
+ int connectResult;
+
+ connectResult = connect(connectList[i].osfd,
+ &connectList[i].addr,
+ connectList[i].addrlen);
+ connectError = errno;
+
+ if(connectResult < 0 )
{
- int connectError;
- int connectResult;
-
- connectResult = connect(connectList[i].osfd,
- &connectList[i].addr,
- connectList[i].addrlen);
- connectError = errno;
-
- if(connectResult < 0 )
+ if(connectError == EINTR || connectError == EWOULDBLOCK ||
+ connectError == EINPROGRESS || connectError == EALREADY)
{
- if(connectError == EINTR || connectError == EWOULDBLOCK
- || connectError == EINPROGRESS || connectError == EALREADY)
- {
- break;
- }
+ break;
}
-
- if(i == (connectCount - 1))
+ }
+
+ if(i == (connectCount - 1))
+ {
+ connectList[i].osfd = -1;
+ } else {
+ for(j = i; j < connectCount; j++ )
{
- connectList[i].osfd = -1;
- } else {
- for(j = i; j < connectCount; j++ )
- {
- memcpy( &connectList[j], &connectList[j+1],
- sizeof(connectList[j]));
- }
+ memcpy( &connectList[j], &connectList[j+1],
+ sizeof(connectList[j]));
}
- connectCount--;
-
- bottom->secret->md.connectReturnValue = connectResult;
- bottom->secret->md.connectReturnError = connectError;
- bottom->secret->md.connectValueValid = PR_TRUE;
- break;
}
+ connectCount--;
+
+ bottom->secret->md.connectReturnValue = connectResult;
+ bottom->secret->md.connectReturnError = connectError;
+ bottom->secret->md.connectValueValid = PR_TRUE;
+ break;
}
-
-
- PR_Unlock( _connectLock );
}
- } else {
- pd->out_flags = 0;
- continue;
+ PR_Unlock( _connectLock );
}
-
+#endif
}
- } else if (n < 0) {
- /* hit error that's not EINTR. */
- rc = -1;
+ pd->out_flags = out_flags;
+ if (out_flags) ready++;
}
+ PR_ASSERT(ready > 0);
}
-
- /*printf("POLL: exiting _MD_pr_poll with %d\n", rc);*/
- return rc;
-}
+ else if (ready < 0)
+ {
+ err = _MD_ERRNO();
+ if (err == EBADF)
+ {
+ /* Find the bad fds */
+ ready = 0;
+ for (pd = pds, epd = pd + npds; pd < epd; pd++)
+ {
+ pd->out_flags = 0;
+ if ((NULL != pd->fd) && (0 != pd->in_flags))
+ {
+ bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
+ if (fcntl(bottom->secret->md.osfd, F_GETFL, 0) == -1)
+ {
+ pd->out_flags = PR_POLL_NVAL;
+ ready++;
+ }
+ }
+ }
+ PR_ASSERT(ready > 0);
+ }
+ else _PR_MD_MAP_SELECT_ERROR(err);
+ }
+
+ return ready;
+} /* _MD_pr_poll */
/*
* File locking.
diff --git a/pr/src/md/beos/bmisc.c b/pr/src/md/beos/bmisc.c
index 634bea11..056d26a9 100644
--- a/pr/src/md/beos/bmisc.c
+++ b/pr/src/md/beos/bmisc.c
@@ -37,8 +37,12 @@
#include <stdlib.h>
PRLock *_connectLock = NULL;
+
+#ifndef BONE_VERSION
+/* Workaround for nonblocking connects under net_server */
PRUint32 connectCount = 0;
ConnectListNode connectList[64];
+#endif
void
_MD_cleanup_before_exit (void)
@@ -63,7 +67,10 @@ _MD_final_init (void)
{
_connectLock = PR_NewLock();
PR_ASSERT(NULL != _connectLock);
+#ifndef BONE_VERSION
+ /* Workaround for nonblocking connects under net_server */
connectCount = 0;
+#endif
}
void
diff --git a/pr/src/md/beos/bmmap.c b/pr/src/md/beos/bmmap.c
index 2af102cc..27759693 100644
--- a/pr/src/md/beos/bmmap.c
+++ b/pr/src/md/beos/bmmap.c
@@ -37,6 +37,7 @@
PR_EXTERN(PRStatus)
_PR_MD_CREATE_FILE_MAP(PRFileMap *fmap, PRInt64 size)
{
+ PR_SetError( PR_NOT_IMPLEMENTED_ERROR, 0 );
return PR_FAILURE;
}
@@ -57,11 +58,13 @@ _PR_MD_MEM_MAP(PRFileMap *fmap, PRInt64 offset, PRUint32 len)
PR_EXTERN(PRStatus)
_PR_MD_MEM_UNMAP(void *addr, PRUint32 size)
{
+ PR_SetError( PR_NOT_IMPLEMENTED_ERROR, 0 );
return PR_FAILURE;
}
PR_EXTERN(PRStatus)
_PR_MD_CLOSE_FILE_MAP(PRFileMap *fmap)
{
+ PR_SetError( PR_NOT_IMPLEMENTED_ERROR, 0 );
return PR_FAILURE;
}
diff --git a/pr/src/md/beos/bnet.c b/pr/src/md/beos/bnet.c
index 86943b3b..0b4fa149 100644
--- a/pr/src/md/beos/bnet.c
+++ b/pr/src/md/beos/bnet.c
@@ -1,5 +1,5 @@
/* -*- Mode: C++; c-basic-offset: 4 -*- */
-/*
+/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
@@ -49,6 +49,7 @@
*/
#define _PRSockLen_t int
+
/*
** Global lock variable used to bracket calls into rusty libraries that
** aren't thread safe (like libc, libX, etc).
@@ -56,135 +57,154 @@
static PRLock *_pr_rename_lock = NULL;
static PRMonitor *_pr_Xfe_mon = NULL;
-/*
+#define READ_FD 1
+#define WRITE_FD 2
+
+/*
** This is a support routine to handle "deferred" i/o on sockets.
** It uses "select", so it is subject to all of the BeOS limitations
** (only READ notification, only sockets)
*/
-#define READ_FD 1
-#define WRITE_FD 2
+
+/*
+ * socket_io_wait --
+ *
+ * wait for socket i/o, periodically checking for interrupt
+ *
+ */
static PRInt32 socket_io_wait(PRInt32 osfd, PRInt32 fd_type,
- PRIntervalTime timeout)
+ PRIntervalTime timeout)
{
- PRInt32 rv = -1;
- struct timeval tv, *tvp;
- PRThread *me = _PR_MD_CURRENT_THREAD();
- PRIntervalTime epoch, now, elapsed, remaining;
- PRInt32 syserror;
- fd_set rd_wr;
-
- switch (timeout) {
- case PR_INTERVAL_NO_WAIT:
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
- break;
- case PR_INTERVAL_NO_TIMEOUT:
- /*
- * This is a special case of the 'default' case below.
- * Please see the comments there.
- */
- tv.tv_sec = _PR_INTERRUPT_CHECK_INTERVAL_SECS;
- tv.tv_usec = 0;
- tvp = &tv;
- FD_ZERO(&rd_wr);
- do {
- FD_SET(osfd, &rd_wr);
- if (fd_type == READ_FD)
- rv = _MD_SELECT(osfd + 1, &rd_wr, NULL, NULL, tvp);
- else
- rv = _MD_SELECT(osfd + 1, NULL, &rd_wr, NULL, tvp);
- if (rv == -1 && (syserror = _MD_ERRNO()) != EINTR) {
- if (syserror == EBADF) {
- PR_SetError(PR_BAD_DESCRIPTOR_ERROR, EBADF);
- } else {
- PR_SetError(PR_UNKNOWN_ERROR, syserror);
- }
- if( _PR_PENDING_INTERRUPT(me)) {
- me->flags &= ~_PR_INTERRUPT;
- PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
- rv = -1;
- break;
- }
- break;
- }
- } while (rv == 0 || (rv == -1 && syserror == EINTR));
- break;
- default:
- now = epoch = PR_IntervalNow();
- remaining = timeout;
- tvp = &tv;
- FD_ZERO(&rd_wr);
- do {
- /*
- * We block in _MD_SELECT for at most
- * _PR_INTERRUPT_CHECK_INTERVAL_SECS seconds,
- * so that there is an upper limit on the delay
- * before the interrupt bit is checked.
- */
- tv.tv_sec = PR_IntervalToSeconds(remaining);
- if (tv.tv_sec > _PR_INTERRUPT_CHECK_INTERVAL_SECS) {
- tv.tv_sec = _PR_INTERRUPT_CHECK_INTERVAL_SECS;
- tv.tv_usec = 0;
- } else {
- tv.tv_usec = PR_IntervalToMicroseconds(
- remaining -
- PR_SecondsToInterval(tv.tv_sec));
- }
- FD_SET(osfd, &rd_wr);
- if (fd_type == READ_FD)
- rv = _MD_SELECT(osfd + 1, &rd_wr, NULL, NULL, tvp);
- else
- rv = _MD_SELECT(osfd + 1, NULL, &rd_wr, NULL, tvp);
- /*
- * we don't consider EINTR a real error
- */
- if (rv == -1 && (syserror = _MD_ERRNO()) != EINTR) {
- if (syserror == EBADF) {
- PR_SetError(PR_BAD_DESCRIPTOR_ERROR, EBADF);
- } else {
- PR_SetError(PR_UNKNOWN_ERROR, syserror);
- }
- break;
- }
- if (_PR_PENDING_INTERRUPT(me)) {
- me->flags &= ~_PR_INTERRUPT;
- PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
- rv = -1;
- break;
- }
- /*
- * We loop again if _MD_SELECT timed out or got interrupted
- * by a signal, and the timeout deadline has not passed yet.
- */
- if (rv == 0 || (rv == -1 && syserror == EINTR)) {
- /*
- * If _MD_SELECT timed out, we know how much time
- * we spent in blocking, so we can avoid a
- * PR_IntervalNow() call.
- */
- if (rv == 0) {
- now += PR_SecondsToInterval(tv.tv_sec)
- + PR_MicrosecondsToInterval(tv.tv_usec);
- } else {
- now = PR_IntervalNow();
- }
- elapsed = (PRIntervalTime) (now - epoch);
- if (elapsed >= timeout) {
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
- rv = -1;
- break;
- } else {
- remaining = timeout - elapsed;
- }
- }
- } while (rv == 0 || (rv == -1 && syserror == EINTR));
- break;
- }
- return(rv);
+ PRInt32 rv = -1;
+ struct timeval tv;
+ PRThread *me = _PR_MD_CURRENT_THREAD();
+ PRIntervalTime epoch, now, elapsed, remaining;
+ PRBool wait_for_remaining;
+ PRInt32 syserror;
+ fd_set rd_wr;
+
+ switch (timeout) {
+ case PR_INTERVAL_NO_WAIT:
+ PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+ break;
+ case PR_INTERVAL_NO_TIMEOUT:
+ /*
+ * This is a special case of the 'default' case below.
+ * Please see the comments there.
+ */
+ tv.tv_sec = _PR_INTERRUPT_CHECK_INTERVAL_SECS;
+ tv.tv_usec = 0;
+ FD_ZERO(&rd_wr);
+ do {
+ FD_SET(osfd, &rd_wr);
+ if (fd_type == READ_FD)
+ rv = _MD_SELECT(osfd + 1, &rd_wr, NULL, NULL, &tv);
+ else
+ rv = _MD_SELECT(osfd + 1, NULL, &rd_wr, NULL, &tv);
+ if (rv == -1 && (syserror = _MD_ERRNO()) != EINTR) {
+#ifdef BONE_VERSION
+ _PR_MD_MAP_SELECT_ERROR(syserror);
+#else
+ if (syserror == EBADF) {
+ PR_SetError(PR_BAD_DESCRIPTOR_ERROR, EBADF);
+ } else {
+ PR_SetError(PR_UNKNOWN_ERROR, syserror);
+ }
+#endif
+ break;
+ }
+ if (_PR_PENDING_INTERRUPT(me)) {
+ me->flags &= ~_PR_INTERRUPT;
+ PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
+ rv = -1;
+ break;
+ }
+ } while (rv == 0 || (rv == -1 && syserror == EINTR));
+ break;
+ default:
+ now = epoch = PR_IntervalNow();
+ remaining = timeout;
+ FD_ZERO(&rd_wr);
+ do {
+ /*
+ * We block in _MD_SELECT for at most
+ * _PR_INTERRUPT_CHECK_INTERVAL_SECS seconds,
+ * so that there is an upper limit on the delay
+ * before the interrupt bit is checked.
+ */
+ wait_for_remaining = PR_TRUE;
+ tv.tv_sec = PR_IntervalToSeconds(remaining);
+ if (tv.tv_sec > _PR_INTERRUPT_CHECK_INTERVAL_SECS) {
+ wait_for_remaining = PR_FALSE;
+ tv.tv_sec = _PR_INTERRUPT_CHECK_INTERVAL_SECS;
+ tv.tv_usec = 0;
+ } else {
+ tv.tv_usec = PR_IntervalToMicroseconds(
+ remaining -
+ PR_SecondsToInterval(tv.tv_sec));
+ }
+ FD_SET(osfd, &rd_wr);
+ if (fd_type == READ_FD)
+ rv = _MD_SELECT(osfd + 1, &rd_wr, NULL, NULL, &tv);
+ else
+ rv = _MD_SELECT(osfd + 1, NULL, &rd_wr, NULL, &tv);
+ /*
+ * we don't consider EINTR a real error
+ */
+ if (rv == -1 && (syserror = _MD_ERRNO()) != EINTR) {
+#ifdef BONE_VERSION
+ _PR_MD_MAP_SELECT_ERROR(syserror);
+#else
+ if (syserror == EBADF) {
+ PR_SetError(PR_BAD_DESCRIPTOR_ERROR, EBADF);
+ } else {
+ PR_SetError(PR_UNKNOWN_ERROR, syserror);
+ }
+#endif
+ break;
+ }
+ if (_PR_PENDING_INTERRUPT(me)) {
+ me->flags &= ~_PR_INTERRUPT;
+ PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
+ rv = -1;
+ break;
+ }
+ /*
+ * We loop again if _MD_SELECT timed out or got interrupted
+ * by a signal, and the timeout deadline has not passed yet.
+ */
+ if (rv == 0 || (rv == -1 && syserror == EINTR)) {
+ /*
+ * If _MD_SELECT timed out, we know how much time
+ * we spent in blocking, so we can avoid a
+ * PR_IntervalNow() call.
+ */
+ if (rv == 0) {
+ if (wait_for_remaining) {
+ now += remaining;
+ } else {
+ now += PR_SecondsToInterval(tv.tv_sec)
+ + PR_MicrosecondsToInterval(tv.tv_usec);
+ }
+ } else {
+ now = PR_IntervalNow();
+ }
+ elapsed = (PRIntervalTime) (now - epoch);
+ if (elapsed >= timeout) {
+ PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+ rv = -1;
+ break;
+ } else {
+ remaining = timeout - elapsed;
+ }
+ }
+ } while (rv == 0 || (rv == -1 && syserror == EINTR));
+ break;
+ }
+ return(rv);
}
-
-
PRInt32
_MD_recv (PRFileDesc *fd, void *buf, PRInt32 amount, PRInt32 flags,
PRIntervalTime timeout)
@@ -193,33 +213,44 @@ _MD_recv (PRFileDesc *fd, void *buf, PRInt32 amount, PRInt32 flags,
PRInt32 rv, err;
PRThread *me = _PR_MD_CURRENT_THREAD();
+#ifndef BONE_VERSION
if (fd->secret->md.sock_state & BE_SOCK_SHUTDOWN_READ) {
- _PR_MD_MAP_RECV_ERROR(EPIPE);
- return -1;
+ _PR_MD_MAP_RECV_ERROR(EPIPE);
+ return -1;
}
+#endif
+
+#ifdef BONE_VERSION
+ /*
+ ** Gah, stupid hack. If reading a zero amount, instantly return success.
+ ** BONE beta 6 returns EINVAL for reads of zero bytes, which parts of
+ ** mozilla use to check for socket availability.
+ */
+
+ if( 0 == amount ) return(0);
+#endif
while ((rv = recv(osfd, buf, amount, flags)) == -1) {
- err = _MD_ERRNO();
+ err = _MD_ERRNO();
- if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
- if (fd->secret->nonblocking) {
- break;
- }
- /* If socket was supposed to be blocking,
- wait a while for the condition to be
- satisfied. */
- if ((rv = socket_io_wait(osfd, READ_FD, timeout)) < 0)
- goto done;
- } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))){
- continue;
+ if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
+ if (fd->secret->nonblocking) {
+ break;
+ }
+ /* If socket was supposed to be blocking,
+ wait a while for the condition to be
+ satisfied. */
+ if ((rv = socket_io_wait(osfd, READ_FD, timeout)) < 0)
+ goto done;
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))){
+ continue;
- } else {
- break;
- }
+ } else
+ break;
}
if (rv < 0) {
- _PR_MD_MAP_RECV_ERROR(err);
+ _PR_MD_MAP_RECV_ERROR(err);
}
done:
@@ -235,31 +266,38 @@ _MD_recvfrom (PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,
PRThread *me = _PR_MD_CURRENT_THREAD();
while ((*addrlen = PR_NETADDR_SIZE(addr)),
- ((rv = recvfrom(osfd, buf, amount, flags,
- (struct sockaddr *) addr,
- (_PRSockLen_t *)addrlen)) == -1)) {
- err = _MD_ERRNO();
-
- if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
- if (fd->secret->nonblocking) {
- break;
- }
+ ((rv = recvfrom(osfd, buf, amount, flags,
+ (struct sockaddr *) addr,
+ (_PRSockLen_t *)addrlen)) == -1)) {
+ err = _MD_ERRNO();
+
+ if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
+ if (fd->secret->nonblocking) {
+ break;
+ }
if ((rv = socket_io_wait(osfd, READ_FD, timeout)) < 0)
goto done;
- } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))) {
- continue;
-
- } else {
- break;
- }
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))) {
+ continue;
+ } else {
+ break;
+ }
}
if (rv < 0) {
- _PR_MD_MAP_RECVFROM_ERROR(err);
+ _PR_MD_MAP_RECVFROM_ERROR(err);
}
done:
+#ifdef _PR_HAVE_SOCKADDR_LEN
+ if (rv != -1) {
+ /* ignore the sa_len field of struct sockaddr */
+ if (addr) {
+ addr->raw.family = ((struct sockaddr *) addr)->sa_family;
+ }
+ }
+#endif /* _PR_HAVE_SOCKADDR_LEN */
return(rv);
}
@@ -271,46 +309,71 @@ _MD_send (PRFileDesc *fd, const void *buf, PRInt32 amount, PRInt32 flags,
PRInt32 rv, err;
PRThread *me = _PR_MD_CURRENT_THREAD();
+#ifndef BONE_VERSION
if (fd->secret->md.sock_state & BE_SOCK_SHUTDOWN_WRITE)
{
- _PR_MD_MAP_SEND_ERROR(EPIPE);
- return -1;
+ _PR_MD_MAP_SEND_ERROR(EPIPE);
+ return -1;
}
+#endif
while ((rv = send(osfd, buf, amount, flags)) == -1) {
- err = _MD_ERRNO();
+ err = _MD_ERRNO();
- if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
- if (fd->secret->nonblocking) {
- break;
- }
+ if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
+ if (fd->secret->nonblocking) {
+ break;
+ }
- if( _PR_PENDING_INTERRUPT(me)) {
+#ifndef BONE_VERSION
+ if( _PR_PENDING_INTERRUPT(me)) {
me->flags &= ~_PR_INTERRUPT;
PR_SetError( PR_PENDING_INTERRUPT_ERROR, 0);
return -1;
}
- /* in UNIX implementations, you could do a socket_io_wait here.
- * but since BeOS doesn't yet support WRITE notification in select,
- * you're spanked.
- */
- snooze( 10000L );
- continue;
-
- } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))) {
- continue;
+ /* in UNIX implementations, you could do a socket_io_wait here.
+ * but since BeOS doesn't yet support WRITE notification in select,
+ * you're spanked.
+ */
+ snooze( 10000L );
+ continue;
+#else /* BONE_VERSION */
+ if ((rv = socket_io_wait(osfd, WRITE_FD, timeout))< 0)
+ goto done;
+#endif
+
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))) {
+ continue;
- } else {
- break;
- }
+ } else {
+ break;
+ }
}
+#ifdef BONE_VERSION
+ /*
+ * optimization; if bytes sent is less than "amount" call
+ * select before returning. This is because it is likely that
+ * the next writev() call will return EWOULDBLOCK.
+ */
+ if ((!fd->secret->nonblocking) && (rv > 0) && (rv < amount)
+ && (timeout != PR_INTERVAL_NO_WAIT)) {
+ if (socket_io_wait(osfd, WRITE_FD, timeout) < 0) {
+ rv = -1;
+ goto done;
+ }
+ }
+#endif /* BONE_VERSION */
+
if (rv < 0) {
- _PR_MD_MAP_SEND_ERROR(err);
+ _PR_MD_MAP_SEND_ERROR(err);
}
+#ifdef BONE_VERSION
+done:
+#endif
return(rv);
}
@@ -321,32 +384,111 @@ _MD_sendto (PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,
PRInt32 osfd = fd->secret->md.osfd;
PRInt32 rv, err;
PRThread *me = _PR_MD_CURRENT_THREAD();
+#ifdef _PR_HAVE_SOCKADDR_LEN
+ PRNetAddr addrCopy;
+ addrCopy = *addr;
+ ((struct sockaddr *) &addrCopy)->sa_len = addrlen;
+ ((struct sockaddr *) &addrCopy)->sa_family = addr->raw.family;
+
+ while ((rv = sendto(osfd, buf, amount, flags,
+ (struct sockaddr *) &addrCopy, addrlen)) == -1) {
+#else
while ((rv = sendto(osfd, buf, amount, flags,
- (struct sockaddr *) addr, addrlen)) == -1) {
- err = _MD_ERRNO();
+ (struct sockaddr *) addr, addrlen)) == -1) {
+#endif
+ err = _MD_ERRNO();
- if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
- if (fd->secret->nonblocking) {
- break;
- }
+ if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
+ if (fd->secret->nonblocking) {
+ break;
+ }
- printf( "This should be a blocking sendto call!!!\n" );
- } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))) {
- continue;
+#ifdef BONE_VERSION
+ if ((rv = socket_io_wait(osfd, WRITE_FD, timeout))< 0)
+ goto done;
+#endif
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))) {
+ continue;
- } else {
- break;
- }
+ } else {
+ break;
+ }
}
if (rv < 0) {
- _PR_MD_MAP_SENDTO_ERROR(err);
+ _PR_MD_MAP_SENDTO_ERROR(err);
}
+#ifdef BONE_VERSION
+done:
+#endif
return(rv);
}
+#ifdef BONE_VERSION
+
+PRInt32 _MD_writev(
+ PRFileDesc *fd, const PRIOVec *iov,
+ PRInt32 iov_size, PRIntervalTime timeout)
+{
+ PRInt32 rv, err;
+ PRThread *me = _PR_MD_CURRENT_THREAD();
+ PRInt32 index, amount = 0;
+ PRInt32 osfd = fd->secret->md.osfd;
+
+ /*
+ * Calculate the total number of bytes to be sent; needed for
+ * optimization later.
+ * We could avoid this if this number was passed in; but it is
+ * probably not a big deal because iov_size is usually small (less than
+ * 3)
+ */
+ if (!fd->secret->nonblocking) {
+ for (index=0; index<iov_size; index++) {
+ amount += iov[index].iov_len;
+ }
+ }
+
+ while ((rv = writev(osfd, (const struct iovec*)iov, iov_size)) == -1) {
+ err = _MD_ERRNO();
+ if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
+ if (fd->secret->nonblocking) {
+ break;
+ }
+ if ((rv = socket_io_wait(osfd, WRITE_FD, timeout))<0)
+ goto done;
+
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))){
+ continue;
+ } else {
+ break;
+ }
+ }
+
+ /*
+ * optimization; if bytes sent is less than "amount" call
+ * select before returning. This is because it is likely that
+ * the next writev() call will return EWOULDBLOCK.
+ */
+ if ((!fd->secret->nonblocking) && (rv > 0) && (rv < amount)
+ && (timeout != PR_INTERVAL_NO_WAIT)) {
+ if (socket_io_wait(osfd, WRITE_FD, timeout) < 0) {
+ rv = -1;
+ goto done;
+ }
+ }
+
+
+ if (rv < 0) {
+ _PR_MD_MAP_WRITEV_ERROR(err);
+ }
+done:
+ return(rv);
+}
+
+#endif /* BONE_VERSION */
+
PRInt32
_MD_accept (PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen,
PRIntervalTime timeout)
@@ -356,42 +498,39 @@ _MD_accept (PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen,
PRThread *me = _PR_MD_CURRENT_THREAD();
while ((rv = accept(osfd, (struct sockaddr *) addr,
- (_PRSockLen_t *)addrlen)) == -1) {
- err = _MD_ERRNO();
-
- if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
- if (fd->secret->nonblocking) {
- break;
- }
- /* If it's SUPPOSED to be a blocking thread, wait
- * a while to see if the triggering condition gets
- * satisfied.
- */
- /* Assume that we're always using a native thread */
- if ((rv = socket_io_wait(osfd, READ_FD, timeout)) < 0)
- goto done;
- } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))) {
- continue;
- } else {
- break;
- }
- }
-
- if (addr) addr->raw.family = AF_INET;
+ (_PRSockLen_t *)addrlen)) == -1) {
+ err = _MD_ERRNO();
+ if ((err == EAGAIN) || (err == EWOULDBLOCK)) {
+ if (fd->secret->nonblocking) {
+ break;
+ }
+ /* If it's SUPPOSED to be a blocking thread, wait
+ * a while to see if the triggering condition gets
+ * satisfied.
+ */
+ /* Assume that we're always using a native thread */
+ if ((rv = socket_io_wait(osfd, READ_FD, timeout)) < 0)
+ goto done;
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))) {
+ continue;
+ } else {
+ break;
+ }
+ }
if (rv < 0) {
- _PR_MD_MAP_ACCEPT_ERROR(err);
+ _PR_MD_MAP_ACCEPT_ERROR(err);
+ } else if (addr != NULL) {
+ /* bug 134099 */
+ err = getpeername(rv, (struct sockaddr *) addr, (_PRSockLen_t *)addrlen);
}
done:
#ifdef _PR_HAVE_SOCKADDR_LEN
if (rv != -1) {
- /* Mask off the first byte of struct sockaddr (the length field) */
- if (addr) {
- *((unsigned char *) addr) = 0;
-#ifdef IS_LITTLE_ENDIAN
- addr->raw.family = ntohs(addr->raw.family);
-#endif
- }
+ /* Mask off the first byte of struct sockaddr (the length field) */
+ if (addr) {
+ addr->raw.family = ((struct sockaddr *) addr)->sa_family;
+ }
}
#endif /* _PR_HAVE_SOCKADDR_LEN */
return(rv);
@@ -401,62 +540,118 @@ PRInt32
_MD_connect (PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen,
PRIntervalTime timeout)
{
- PRInt32 osfd = fd->secret->md.osfd;
PRInt32 rv, err;
PRThread *me = _PR_MD_CURRENT_THREAD();
+ PRInt32 osfd = fd->secret->md.osfd;
+#ifndef BONE_VERSION
fd->secret->md.connectValueValid = PR_FALSE;
+#endif
+#ifdef _PR_HAVE_SOCKADDR_LEN
+ PRNetAddr addrCopy;
+
+ addrCopy = *addr;
+ ((struct sockaddr *) &addrCopy)->sa_len = addrlen;
+ ((struct sockaddr *) &addrCopy)->sa_family = addr->raw.family;
+#endif
+
+ /* (Copied from unix.c)
+ * We initiate the connection setup by making a nonblocking connect()
+ * call. If the connect() call fails, there are two cases we handle
+ * specially:
+ * 1. The connect() call was interrupted by a signal. In this case
+ * we simply retry connect().
+ * 2. The NSPR socket is nonblocking and connect() fails with
+ * EINPROGRESS. We first wait until the socket becomes writable.
+ * Then we try to find out whether the connection setup succeeded
+ * or failed.
+ */
retry:
+#ifdef _PR_HAVE_SOCKADDR_LEN
+ if ((rv = connect(osfd, (struct sockaddr *)&addrCopy, addrlen)) == -1) {
+#else
if ((rv = connect(osfd, (struct sockaddr *)addr, addrlen)) == -1) {
+#endif
err = _MD_ERRNO();
- fd->secret->md.connectReturnValue = rv;
- fd->secret->md.connectReturnError = err;
- fd->secret->md.connectValueValid = PR_TRUE;
-
- if( err == EINTR ) {
+#ifndef BONE_VERSION
+ fd->secret->md.connectReturnValue = rv;
+ fd->secret->md.connectReturnError = err;
+ fd->secret->md.connectValueValid = PR_TRUE;
+#endif
+ if( err == EINTR ) {
- if( _PR_PENDING_INTERRUPT(me)) {
+ if( _PR_PENDING_INTERRUPT(me)) {
- me->flags &= ~_PR_INTERRUPT;
+ me->flags &= ~_PR_INTERRUPT;
PR_SetError( PR_PENDING_INTERRUPT_ERROR, 0);
return -1;
}
- snooze( 100000L );
+#ifndef BONE_VERSION
+ snooze( 100000L );
+#endif
goto retry;
}
+#ifndef BONE_VERSION
if(!fd->secret->nonblocking && ((err == EINPROGRESS) || (err==EAGAIN) || (err==EALREADY))) {
- /*
- ** There's no timeout on this connect, but that's not
- ** a big deal, since the connect times out anyways
- ** after 30 seconds. Just sleep for 1/10th of a second
- ** and retry until we go through or die.
- */
+ /*
+ ** There's no timeout on this connect, but that's not
+ ** a big deal, since the connect times out anyways
+ ** after 30 seconds. Just sleep for 1/10th of a second
+ ** and retry until we go through or die.
+ */
- if( _PR_PENDING_INTERRUPT(me)) {
+ if( _PR_PENDING_INTERRUPT(me)) {
me->flags &= ~_PR_INTERRUPT;
PR_SetError( PR_PENDING_INTERRUPT_ERROR, 0);
return -1;
- }
+ }
- goto retry;
- }
+ goto retry;
+ }
- if( fd->secret->nonblocking && ((err == EAGAIN) || (err == EINPROGRESS))) {
- PR_Lock(_connectLock);
- connectList[connectCount].osfd = osfd;
- memcpy(&connectList[connectCount].addr, addr, addrlen);
- connectList[connectCount].addrlen = addrlen;
- connectList[connectCount].timeout = timeout;
- connectCount++;
- PR_Unlock(_connectLock);
- _PR_MD_MAP_CONNECT_ERROR(err);
- return rv;
- }
+ if( fd->secret->nonblocking && ((err == EAGAIN) || (err == EINPROGRESS))) {
+ PR_Lock(_connectLock);
+ if (connectCount < sizeof(connectList)/sizeof(connectList[0])) {
+ connectList[connectCount].osfd = osfd;
+ memcpy(&connectList[connectCount].addr, addr, addrlen);
+ connectList[connectCount].addrlen = addrlen;
+ connectList[connectCount].timeout = timeout;
+ connectCount++;
+ PR_Unlock(_connectLock);
+ _PR_MD_MAP_CONNECT_ERROR(err);
+ } else {
+ PR_Unlock(_connectLock);
+ PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, 0);
+ }
+ return rv;
+ }
+#else /* BONE_VERSION */
+ if(!fd->secret->nonblocking && (err == EINTR)) {
+
+ rv = socket_io_wait(osfd, WRITE_FD, timeout);
+ if (rv == -1) {
+ return -1;
+ }
+
+ PR_ASSERT(rv == 1);
+ if (_PR_PENDING_INTERRUPT(me)) {
+ me->flags &= ~_PR_INTERRUPT;
+ PR_SetError( PR_PENDING_INTERRUPT_ERROR, 0);
+ return -1;
+ }
+ err = _MD_beos_get_nonblocking_connect_error(osfd);
+ if (err != 0) {
+ _PR_MD_MAP_CONNECT_ERROR(err);
+ return -1;
+ }
+ return 0;
+ }
+#endif
- _PR_MD_MAP_CONNECT_ERROR(err);
+ _PR_MD_MAP_CONNECT_ERROR(err);
}
return rv;
@@ -466,9 +661,16 @@ PRInt32
_MD_bind (PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen)
{
PRInt32 rv, err;
+#ifdef _PR_HAVE_SOCKADDR_LEN
+ PRNetAddr addrCopy;
+ addrCopy = *addr;
+ ((struct sockaddr *) &addrCopy)->sa_len = addrlen;
+ ((struct sockaddr *) &addrCopy)->sa_family = addr->raw.family;
+ rv = bind(fd->secret->md.osfd, (struct sockaddr *) &addrCopy, (int )addrlen);
+#else
rv = bind(fd->secret->md.osfd, (struct sockaddr *) addr, (int )addrlen);
-
+#endif
if (rv < 0) {
err = _MD_ERRNO();
_PR_MD_MAP_BIND_ERROR(err);
@@ -482,12 +684,14 @@ _MD_listen (PRFileDesc *fd, PRIntn backlog)
{
PRInt32 rv, err;
+#ifndef BONE_VERSION
/* Bug workaround! Setting listen to 0 on Be accepts no connections.
** On most UN*Xes this sets the default.
*/
if( backlog == 0 ) backlog = 5;
-
+#endif
+
rv = listen(fd->secret->md.osfd, backlog);
if (rv < 0) {
err = _MD_ERRNO();
@@ -502,15 +706,24 @@ _MD_shutdown (PRFileDesc *fd, PRIntn how)
{
PRInt32 rv, err;
+#ifndef BONE_VERSION
if (how == PR_SHUTDOWN_SEND)
- fd->secret->md.sock_state = BE_SOCK_SHUTDOWN_WRITE;
+ fd->secret->md.sock_state = BE_SOCK_SHUTDOWN_WRITE;
else if (how == PR_SHUTDOWN_RCV)
- fd->secret->md.sock_state = BE_SOCK_SHUTDOWN_READ;
+ fd->secret->md.sock_state = BE_SOCK_SHUTDOWN_READ;
else if (how == PR_SHUTDOWN_BOTH) {
- fd->secret->md.sock_state = (BE_SOCK_SHUTDOWN_WRITE | BE_SOCK_SHUTDOWN_READ);
+ fd->secret->md.sock_state = (BE_SOCK_SHUTDOWN_WRITE | BE_SOCK_SHUTDOWN_READ);
}
return 0;
+#else /* BONE_VERSION */
+ rv = shutdown(fd->secret->md.osfd, how);
+ if (rv < 0) {
+ err = _MD_ERRNO();
+ _PR_MD_MAP_SHUTDOWN_ERROR(err);
+ }
+ return(rv);
+#endif
}
PRInt32
@@ -522,7 +735,11 @@ _MD_socketpair (int af, int type, int flags, PRInt32 *osfd)
PRInt32
_MD_close_socket (PRInt32 osfd)
{
+#ifdef BONE_VERSION
+ close( osfd );
+#else
closesocket( osfd );
+#endif
}
PRStatus
@@ -532,7 +749,14 @@ _MD_getsockname (PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen)
rv = getsockname(fd->secret->md.osfd,
(struct sockaddr *) addr, (_PRSockLen_t *)addrlen);
-
+#ifdef _PR_HAVE_SOCKADDR_LEN
+ if (rv == 0) {
+ /* ignore the sa_len field of struct sockaddr */
+ if (addr) {
+ addr->raw.family = ((struct sockaddr *) addr)->sa_family;
+ }
+ }
+#endif /* _PR_HAVE_SOCKADDR_LEN */
if (rv < 0) {
err = _MD_ERRNO();
_PR_MD_MAP_GETSOCKNAME_ERROR(err);
@@ -549,6 +773,15 @@ _MD_getpeername (PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen)
rv = getpeername(fd->secret->md.osfd,
(struct sockaddr *) addr, (_PRSockLen_t *)addrlen);
+#ifdef _PR_HAVE_SOCKADDR_LEN
+ if (rv == 0) {
+ /* ignore the sa_len field of struct sockaddr */
+ if (addr) {
+ addr->raw.family = ((struct sockaddr *) addr)->sa_family;
+ }
+ }
+#endif /* _PR_HAVE_SOCKADDR_LEN */
+
if (rv < 0) {
err = _MD_ERRNO();
_PR_MD_MAP_GETPEERNAME_ERROR(err);
@@ -560,9 +793,6 @@ PRStatus
_MD_getsockopt (PRFileDesc *fd, PRInt32 level,
PRInt32 optname, char* optval, PRInt32* optlen)
{
- return PR_NOT_IMPLEMENTED_ERROR;
-
-#if 0
PRInt32 rv, err;
rv = getsockopt(fd->secret->md.osfd, level, optname,
@@ -573,7 +803,6 @@ _MD_getsockopt (PRFileDesc *fd, PRInt32 level,
}
return rv==0?PR_SUCCESS:PR_FAILURE;
-#endif
}
PRStatus
@@ -597,6 +826,7 @@ _MD_accept_read (PRFileDesc *sd, PRInt32 *newSock, PRNetAddr **raddr,
return PR_NOT_IMPLEMENTED_ERROR;
}
+#ifndef BONE_VERSION
PRInt32
_MD_socket (int af, int type, int flags)
{
@@ -606,17 +836,43 @@ _MD_socket (int af, int type, int flags)
if( -1 == osfd ) {
- err = _MD_ERRNO();
- _PR_MD_MAP_SOCKET_ERROR( err );
+ err = _MD_ERRNO();
+ _PR_MD_MAP_SOCKET_ERROR( err );
}
return( osfd );
}
+#else
+PRInt32
+_MD_socket(PRInt32 domain, PRInt32 type, PRInt32 proto)
+{
+ PRInt32 osfd, err;
+
+ osfd = socket(domain, type, proto);
+
+ if (osfd == -1) {
+ err = _MD_ERRNO();
+ _PR_MD_MAP_SOCKET_ERROR(err);
+ }
+
+ return(osfd);
+}
+#endif
PRInt32
_MD_socketavailable (PRFileDesc *fd)
{
+#ifdef BONE_VERSION
+ PRInt32 result;
+
+ if (ioctl(fd->secret->md.osfd, FIONREAD, &result) < 0) {
+ _PR_MD_MAP_SOCKETAVAILABLE_ERROR(_MD_ERRNO());
+ return -1;
+ }
+ return result;
+#else
return PR_NOT_IMPLEMENTED_ERROR;
+#endif
}
PRInt32
@@ -628,32 +884,25 @@ _MD_get_socket_error (void)
PRStatus
_MD_gethostname (char *name, PRUint32 namelen)
{
-PRInt32 rv, err;
+ PRInt32 rv, err;
rv = gethostname(name, namelen);
if (rv == 0)
{
- err = _MD_ERRNO();
- _PR_MD_MAP_GETHOSTNAME_ERROR(err);
- return PR_FAILURE;
+ err = _MD_ERRNO();
+ _PR_MD_MAP_GETHOSTNAME_ERROR(err);
+ return PR_FAILURE;
}
return PR_SUCCESS;
}
+#ifndef BONE_VERSION
PRInt32
_MD_beos_get_nonblocking_connect_error(PRFileDesc *fd)
{
int rv;
int flags = 0;
- if( fd->secret->md.connectValueValid == PR_TRUE )
-
- if( fd->secret->md.connectReturnValue == -1 )
-
- return fd->secret->md.connectReturnError;
- else
- return 0; /* No error */
-
rv = recv(fd->secret->md.osfd, NULL, 0, flags);
PR_ASSERT(-1 == rv || 0 == rv);
if (-1 == rv && errno != EAGAIN && errno != EWOULDBLOCK) {
@@ -661,3 +910,17 @@ _MD_beos_get_nonblocking_connect_error(PRFileDesc *fd)
}
return 0; /* no error */
}
+#else
+PRInt32
+_MD_beos_get_nonblocking_connect_error(int osfd)
+{
+ return PR_NOT_IMPLEMENTED_ERROR;
+ // int err;
+ // _PRSockLen_t optlen = sizeof(err);
+ // if (getsockopt(osfd, SOL_SOCKET, SO_ERROR, (char *) &err, &optlen) == -1) {
+ // return errno;
+ // } else {
+ // return err;
+ // }
+}
+#endif /* BONE_VERSION */
diff --git a/pr/src/md/beos/bproc.c b/pr/src/md/beos/bproc.c
index 5d9ac0a5..54ccaa0f 100644
--- a/pr/src/md/beos/bproc.c
+++ b/pr/src/md/beos/bproc.c
@@ -1,4 +1,4 @@
-/* -*- Mode: C++; c-basic-offset: 4 -*- */
+/* -*- Mode: C++; tab-width: 8; c-basic-offset: 8 -*- */
/*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
@@ -33,28 +33,202 @@
*/
#include "primpl.h"
+#include <stdio.h>
+#include <signal.h>
+
+#define _PR_SIGNALED_EXITSTATUS 256
PRProcess*
_MD_create_process (const char *path, char *const *argv,
char *const *envp, const PRProcessAttr *attr)
{
- return NULL;
+ PRProcess *process;
+ int nEnv, idx;
+ char *const *childEnvp;
+ char **newEnvp = NULL;
+ int flags;
+
+ process = PR_NEW(PRProcess);
+ if (!process) {
+ PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
+ return NULL;
+ }
+
+ childEnvp = envp;
+ if (attr && attr->fdInheritBuffer) {
+ if (NULL == childEnvp) {
+ childEnvp = environ;
+ }
+ for (nEnv = 0; childEnvp[nEnv]; nEnv++) {
+ }
+ newEnvp = (char **) PR_MALLOC((nEnv + 2) * sizeof(char *));
+ if (NULL == newEnvp) {
+ PR_DELETE(process);
+ PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
+ return NULL;
+ }
+ for (idx = 0; idx < nEnv; idx++) {
+ newEnvp[idx] = childEnvp[idx];
+ }
+ newEnvp[idx++] = attr->fdInheritBuffer;
+ newEnvp[idx] = NULL;
+ childEnvp = newEnvp;
+ }
+
+ process->md.pid = fork();
+
+ if ((pid_t) -1 == process->md.pid) {
+ PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, errno);
+ PR_DELETE(process);
+ if (newEnvp) {
+ PR_DELETE(newEnvp);
+ }
+ return NULL;
+ } else if (0 == process->md.pid) { /* the child process */
+ /*
+ * If the child process needs to exit, it must call _exit().
+ * Do not call exit(), because exit() will flush and close
+ * the standard I/O file descriptors, and hence corrupt
+ * the parent process's standard I/O data structures.
+ */
+
+ if (attr) {
+ /* the osfd's to redirect stdin, stdout, and stderr to */
+ int in_osfd = -1, out_osfd = -1, err_osfd = -1;
+
+ if (attr->stdinFd
+ && attr->stdinFd->secret->md.osfd != 0) {
+ in_osfd = attr->stdinFd->secret->md.osfd;
+ if (dup2(in_osfd, 0) != 0) {
+ _exit(1); /* failed */
+ }
+ flags = fcntl(0, F_GETFL, 0);
+ if (flags & O_NONBLOCK) {
+ fcntl(0, F_SETFL, flags & ~O_NONBLOCK);
+ }
+ }
+ if (attr->stdoutFd
+ && attr->stdoutFd->secret->md.osfd != 1) {
+ out_osfd = attr->stdoutFd->secret->md.osfd;
+ if (dup2(out_osfd, 1) != 1) {
+ _exit(1); /* failed */
+ }
+ flags = fcntl(1, F_GETFL, 0);
+ if (flags & O_NONBLOCK) {
+ fcntl(1, F_SETFL, flags & ~O_NONBLOCK);
+ }
+ }
+ if (attr->stderrFd
+ && attr->stderrFd->secret->md.osfd != 2) {
+ err_osfd = attr->stderrFd->secret->md.osfd;
+ if (dup2(err_osfd, 2) != 2) {
+ _exit(1); /* failed */
+ }
+ flags = fcntl(2, F_GETFL, 0);
+ if (flags & O_NONBLOCK) {
+ fcntl(2, F_SETFL, flags & ~O_NONBLOCK);
+ }
+ }
+ if (in_osfd != -1) {
+ close(in_osfd);
+ }
+ if (out_osfd != -1 && out_osfd != in_osfd) {
+ close(out_osfd);
+ }
+ if (err_osfd != -1 && err_osfd != in_osfd
+ && err_osfd != out_osfd) {
+ close(err_osfd);
+ }
+ if (attr->currentDirectory) {
+ if (chdir(attr->currentDirectory) < 0) {
+ _exit(1); /* failed */
+ }
+ }
+ }
+
+ if (childEnvp) {
+ (void)execve(path, argv, childEnvp);
+ } else {
+ /* Inherit the environment of the parent. */
+ (void)execv(path, argv);
+ }
+ /* Whoops! It returned. That's a bad sign. */
+ _exit(1);
+ }
+
+ if (newEnvp) {
+ PR_DELETE(newEnvp);
+ }
+
+ return process;
}
PRStatus
_MD_detach_process (PRProcess *process)
{
- return PR_NOT_IMPLEMENTED_ERROR;
+ /* If we kept a process table like unix does,
+ * we'd remove the entry here.
+ * Since we dont', just delete the process variable
+ */
+ PR_DELETE(process);
+ return PR_SUCCESS;
}
PRStatus
_MD_wait_process (PRProcess *process, PRInt32 *exitCode)
{
- return PR_NOT_IMPLEMENTED_ERROR;
+ PRStatus retVal = PR_SUCCESS;
+ int ret, status;
+
+ /* Ignore interruptions */
+ do {
+ ret = waitpid(process->md.pid, &status, 0);
+ } while (ret == -1 && errno == EINTR);
+
+ /*
+ * waitpid() cannot return 0 because we did not invoke it
+ * with the WNOHANG option.
+ */
+ PR_ASSERT(0 != ret);
+
+ if (ret < 0) {
+ PR_SetError(PR_UNKNOWN_ERROR, _MD_ERRNO());
+ return PR_FAILURE;
+ }
+
+ /* If child process exited normally, return child exit code */
+ if (WIFEXITED(status)) {
+ *exitCode = WEXITSTATUS(status);
+ } else {
+ PR_ASSERT(WIFSIGNALED(status));
+ *exitCode = _PR_SIGNALED_EXITSTATUS;
+ }
+
+ PR_DELETE(process);
+ return PR_SUCCESS;
}
PRStatus
_MD_kill_process (PRProcess *process)
{
- return PR_NOT_IMPLEMENTED_ERROR;
+ PRErrorCode prerror;
+ PRInt32 oserror;
+
+ if (kill(process->md.pid, SIGKILL) == 0) {
+ return PR_SUCCESS;
+ }
+ oserror = errno;
+ switch (oserror) {
+ case EPERM:
+ prerror = PR_NO_ACCESS_RIGHTS_ERROR;
+ break;
+ case ESRCH:
+ prerror = PR_INVALID_ARGUMENT_ERROR;
+ break;
+ default:
+ prerror = PR_UNKNOWN_ERROR;
+ break;
+ }
+ PR_SetError(prerror, oserror);
+ return PR_FAILURE;
}
diff --git a/pr/src/md/mac/macdll.c b/pr/src/md/mac/macdll.c
index 2cfe79db..23b32759 100644
--- a/pr/src/md/mac/macdll.c
+++ b/pr/src/md/mac/macdll.c
@@ -218,7 +218,7 @@ GetSharedLibraryFilterProc(const CInfoPBRec* const inCpb, Boolean* inWantQuit, v
// see if this symbol is in this fragment
if (LibInPefContainer(&fragSpec, pFilterData->inName, &codeOffset, &codeLength))
- tempErr = GetDiskFragment(&fragSpec, codeOffset, codeLength, pFilterData->inName, kLoadCFrag, &pFilterData->outID, &pFilterData->outAddress, errName);
+ tempErr = GetDiskFragment(&fragSpec, codeOffset, codeLength, fragSpec.name, kLoadCFrag, &pFilterData->outID, &pFilterData->outAddress, errName);
else
return;
@@ -503,19 +503,30 @@ done:
OSErr NSLoadNamedFragment(const FSSpec *fileSpec, const char* fragmentName, CFragConnectionID *outConnectionID)
{
- UInt32 fragOffset, fragLength;
- Ptr main;
- Str255 fragName = "\p";
- Str255 errName;
- OSErr err;
-
- err = GetNamedFragmentOffsets(fileSpec, fragmentName, &fragOffset, &fragLength);
- if (err != noErr) return err;
-
- err = GetDiskFragment(fileSpec, fragOffset, fragLength, fragName,
- kLoadCFrag, outConnectionID, &main, errName);
-
- return err;
+ UInt32 fragOffset, fragLength;
+ short fragNameLength;
+ Ptr main;
+ Str255 fragName;
+ Str255 errName;
+ OSErr err;
+
+ err = GetNamedFragmentOffsets(fileSpec, fragmentName, &fragOffset, &fragLength);
+ if (err != noErr) return err;
+
+ // convert fragment name to pascal string
+ fragNameLength = strlen(fragmentName);
+ if (fragNameLength > 255)
+ fragNameLength = 255;
+ BlockMoveData(fragmentName, &fragName[1], fragNameLength);
+ fragName[0] = fragNameLength;
+
+ // Note that we pass the fragment name as the 4th param to GetDiskFragment.
+ // This value affects the ability of debuggers, and the Talkback system,
+ // to match code fragments with symbol files
+ err = GetDiskFragment(fileSpec, fragOffset, fragLength, fragName,
+ kLoadCFrag, outConnectionID, &main, errName);
+
+ return err;
}
@@ -532,39 +543,42 @@ OSErr NSLoadNamedFragment(const FSSpec *fileSpec, const char* fragmentName, CFra
-----------------------------------------------------------------*/
OSErr NSLoadIndexedFragment(const FSSpec *fileSpec, PRUint32 fragmentIndex,
- char** outFragName, CFragConnectionID *outConnectionID)
+ char** outFragName, CFragConnectionID *outConnectionID)
{
- UInt32 fragOffset, fragLength;
- char *fragNameBlock = NULL;
- Ptr main;
- Str255 fragName = "\p";
- Str255 errName;
- OSErr err;
-
- *outFragName = NULL;
-
- err = GetIndexedFragmentOffsets(fileSpec, fragmentIndex, &fragOffset, &fragLength, &fragNameBlock);
- if (err != noErr) return err;
-
- if (fragNameBlock)
- {
- UInt32 nameLen = strlen(fragNameBlock);
- if (nameLen > 63)
- nameLen = 63;
- BlockMoveData(fragNameBlock, &fragName[1], nameLen);
- fragName[0] = nameLen;
- }
-
- err = GetDiskFragment(fileSpec, fragOffset, fragLength, fragName,
- kLoadCFrag, outConnectionID, &main, errName);
- if (err != noErr)
- {
- free(fragNameBlock);
- return err;
- }
-
- *outFragName = fragNameBlock;
- return noErr;
+ UInt32 fragOffset, fragLength;
+ char *fragNameBlock = NULL;
+ Ptr main;
+ Str255 fragName = "\p";
+ Str255 errName;
+ OSErr err;
+
+ *outFragName = NULL;
+
+ err = GetIndexedFragmentOffsets(fileSpec, fragmentIndex, &fragOffset, &fragLength, &fragNameBlock);
+ if (err != noErr) return err;
+
+ if (fragNameBlock)
+ {
+ UInt32 nameLen = strlen(fragNameBlock);
+ if (nameLen > 63)
+ nameLen = 63;
+ BlockMoveData(fragNameBlock, &fragName[1], nameLen);
+ fragName[0] = nameLen;
+ }
+
+ // Note that we pass the fragment name as the 4th param to GetDiskFragment.
+ // This value affects the ability of debuggers, and the Talkback system,
+ // to match code fragments with symbol files
+ err = GetDiskFragment(fileSpec, fragOffset, fragLength, fragName,
+ kLoadCFrag, outConnectionID, &main, errName);
+ if (err != noErr)
+ {
+ free(fragNameBlock);
+ return err;
+ }
+
+ *outFragName = fragNameBlock;
+ return noErr;
}
diff --git a/pr/src/md/mac/macio.c b/pr/src/md/mac/macio.c
index 0a546635..f4bb60a4 100644
--- a/pr/src/md/mac/macio.c
+++ b/pr/src/md/mac/macio.c
@@ -81,15 +81,16 @@ static void AsyncIOCompletion (ExtendedParamBlock *pbAsyncPtr)
if (_PR_MD_GET_INTSOFF()) {
thread->md.missedIONotify = PR_TRUE;
cpu->u.missed[cpu->where] |= _PR_MISSED_IO;
- return;
- }
+ } else {
+ _PR_INTSOFF(is);
- _PR_INTSOFF(is);
+ thread->md.osErrCode = noErr;
+ DoneWaitingOnThisThread(thread);
- thread->md.osErrCode = noErr;
- DoneWaitingOnThisThread(thread);
+ _PR_FAST_INTSON(is);
+ }
- _PR_FAST_INTSON(is);
+ SignalIdleSemaphore();
}
void _MD_SetError(OSErr oserror)
@@ -266,7 +267,7 @@ PRInt32 ReadWriteProc(PRFileDesc *fd, void *buf, PRUint32 bytes, IOOperation op)
a 32 byte Ptr in the heap, so only do this once
*/
if (!sCompletionUPP)
- sCompletionUPP = NewIOCompletionProc((IOCompletionProcPtr)&AsyncIOCompletion);
+ sCompletionUPP = NewIOCompletionUPP((IOCompletionProcPtr)&AsyncIOCompletion);
/* grab the thread so we know which one to post to at completion */
pbAsync.thread = me;
@@ -1295,7 +1296,9 @@ ConvertUnixPathToMacPath(const char *unixPath, char **macPath)
FSSpec foundSpec;
short pathBufferSize;
+#if DEBUG
char *temp;
+#endif
int tempLen;
// Are we dealing with the temp folder?
@@ -1905,14 +1908,12 @@ PRStatus _MD_CreateFileMap(PRFileMap *fmap, PRInt64 size)
{
#pragma unused (fmap, size)
- PR_ASSERT(!"Not implemented");
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return PR_FAILURE;
}
PRInt32 _MD_GetMemMapAlignment(void)
{
- PR_ASSERT(!"Not implemented");
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return -1;
}
@@ -1924,7 +1925,6 @@ void * _MD_MemMap(
{
#pragma unused (fmap, offset, len)
- PR_ASSERT(!"Not implemented");
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return NULL;
}
@@ -1933,7 +1933,6 @@ PRStatus _MD_MemUnmap(void *addr, PRUint32 len)
{
#pragma unused (addr, len)
- PR_ASSERT(!"Not implemented");
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return PR_FAILURE;
}
@@ -1942,7 +1941,6 @@ PRStatus _MD_CloseFileMap(PRFileMap *fmap)
{
#pragma unused (fmap)
- PR_ASSERT(!"Not implemented");
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return PR_FAILURE;
}
diff --git a/pr/src/md/mac/macsocket.h b/pr/src/md/mac/macsocket.h
index b1045e36..b9194c52 100644
--- a/pr/src/md/mac/macsocket.h
+++ b/pr/src/md/mac/macsocket.h
@@ -39,6 +39,7 @@
// Interface visible to xp code
// C socket type definitions and routines
// from sys/socket.h
+#include <Files.h>
#include <OpenTptInternet.h> // All the internet typedefs
#include <utime.h> // For timeval
/*
diff --git a/pr/src/md/mac/macsockotpt.c b/pr/src/md/mac/macsockotpt.c
index ef15a56b..6c54de4c 100644
--- a/pr/src/md/mac/macsockotpt.c
+++ b/pr/src/md/mac/macsockotpt.c
@@ -38,6 +38,7 @@
#include <string.h>
#include <Gestalt.h>
+#include <Files.h>
#include <OpenTransport.h>
#include <OSUtils.h>
@@ -173,9 +174,9 @@ static pascal void DNSNotifierRoutine(void * contextPtr, OTEventCode otEvent, O
if (_PR_MD_GET_INTSOFF()) {
dnsContext.thread->md.missedIONotify = PR_TRUE;
cpu->u.missed[cpu->where] |= _PR_MISSED_IO;
- return;
+ } else {
+ DoneWaitingOnThisThread(dnsContext.thread);
}
- DoneWaitingOnThisThread(dnsContext.thread);
break;
case kOTProviderWillClose:
@@ -189,9 +190,9 @@ static pascal void DNSNotifierRoutine(void * contextPtr, OTEventCode otEvent, O
if (_PR_MD_GET_INTSOFF()) {
dnsContext.thread->md.missedIONotify = PR_TRUE;
cpu->u.missed[cpu->where] |= _PR_MISSED_IO;
- return;
+ } else {
+ DoneWaitingOnThisThread(dnsContext.thread);
}
- DoneWaitingOnThisThread(dnsContext.thread);
break;
default: // or else we don't handle the event
@@ -199,6 +200,8 @@ static pascal void DNSNotifierRoutine(void * contextPtr, OTEventCode otEvent, O
}
// or else we don't handle the event
+
+ SignalIdleSemaphore();
}
@@ -296,10 +299,12 @@ WakeUpNotifiedThread(PRThread *thread, OTResult result)
if (_PR_MD_GET_INTSOFF()) {
thread->md.missedIONotify = PR_TRUE;
cpu->u.missed[cpu->where] |= _PR_MISSED_IO;
- return;
+ } else {
+ DoneWaitingOnThisThread(thread);
}
- DoneWaitingOnThisThread(thread);
}
+
+ SignalIdleSemaphore();
}
// Notification routine
@@ -369,6 +374,8 @@ static pascal void NotifierRoutine(void * contextPtr, OTEventCode code, OTResul
PR_ASSERT(err == kOTNoError);
secret->md.exceptReady = PR_TRUE; // XXX Check this
+ md->disconnectError = discon.reason; // save for _MD_mac_get_nonblocking_connect_error
+
// wake up waiting threads, if any
result = -3199 - discon.reason; // obtain the negative error code
if ((readThread = secret->md.read.thread) != NULL) {
@@ -1080,12 +1087,12 @@ typedef struct RawEndpointAndThread
// A5 is OK. Cannot allocate memory here
static pascal void RawEndpointNotifierRoutine(void * contextPtr, OTEventCode code, OTResult result, void * cookie)
{
- RawEndpointAndThread *endthr = (RawEndpointAndThread *) contextPtr;
+ RawEndpointAndThread *endthr = (RawEndpointAndThread *) contextPtr;
PRThread * thread = endthr->thread;
EndpointRef * endpoint = endthr->endpoint;
_PRCPU * cpu = _PR_MD_CURRENT_CPU();
- OSStatus err;
- OTResult resultOT;
+ OSStatus err;
+ OTResult resultOT;
switch (code)
{
@@ -1169,10 +1176,12 @@ static pascal void RawEndpointNotifierRoutine(void * contextPtr, OTEventCode co
if (_PR_MD_GET_INTSOFF()) {
thread->md.asyncNotifyPending = PR_TRUE;
cpu->u.missed[cpu->where] |= _PR_MISSED_IO;
- return;
+ } else {
+ DoneWaitingOnThisThread(thread);
}
- DoneWaitingOnThisThread(thread);
}
+
+ SignalIdleSemaphore();
}
PRInt32 _MD_accept(PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen, PRIntervalTime timeout)
@@ -1309,7 +1318,7 @@ PRInt32 _MD_connect(PRFileDesc *fd, PRNetAddr *addr, PRUint32 addrlen, PRInterva
err = kEFAULTErr;
goto ErrorExit;
}
-
+
// Bind to a local port; let the system assign it.
bindAddr.inet.family = AF_INET;
@@ -1320,19 +1329,19 @@ PRInt32 _MD_connect(PRFileDesc *fd, PRNetAddr *addr, PRUint32 addrlen, PRInterva
bindReq.addr.buf = (UInt8*) &bindAddr;
bindReq.qlen = 0;
- PR_Lock(fd->secret->md.miscLock);
+ PR_Lock(fd->secret->md.miscLock);
PrepareForAsyncCompletion(me, fd->secret->md.osfd);
- fd->secret->md.misc.thread = me;
+ fd->secret->md.misc.thread = me;
err = OTBind(endpoint, &bindReq, NULL);
if (err != kOTNoError) {
- me->io_pending = PR_FALSE;
- PR_Unlock(fd->secret->md.miscLock);
- goto ErrorExit;
- }
+ me->io_pending = PR_FALSE;
+ PR_Unlock(fd->secret->md.miscLock);
+ goto ErrorExit;
+ }
WaitOnThisThread(me, PR_INTERVAL_NO_TIMEOUT);
- PR_Unlock(fd->secret->md.miscLock);
+ PR_Unlock(fd->secret->md.miscLock);
err = me->md.osErrCode;
if (err != kOTNoError)
@@ -1344,26 +1353,26 @@ PRInt32 _MD_connect(PRFileDesc *fd, PRNetAddr *addr, PRUint32 addrlen, PRInterva
sndCall.addr.len = addrlen;
sndCall.addr.buf = (UInt8*) addr;
- if (!fd->secret->nonblocking) {
- PrepareForAsyncCompletion(me, fd->secret->md.osfd);
- PR_ASSERT(fd->secret->md.write.thread == NULL);
- fd->secret->md.write.thread = me;
+ if (!fd->secret->nonblocking) {
+ PrepareForAsyncCompletion(me, fd->secret->md.osfd);
+ PR_ASSERT(fd->secret->md.write.thread == NULL);
+ fd->secret->md.write.thread = me;
}
-
+
err = OTConnect (endpoint, &sndCall, NULL);
- if (err == kOTNoError) {
- PR_ASSERT(!"OTConnect returned kOTNoError in async mode!?!");
- }
- if (fd->secret->nonblocking) {
- if (err == kOTNoDataErr)
- err = EINPROGRESS;
- goto ErrorExit;
- } else {
- if (err != kOTNoError && err != kOTNoDataErr) {
- me->io_pending = PR_FALSE;
- goto ErrorExit;
- }
- }
+ if (err == kOTNoError) {
+ PR_ASSERT(!"OTConnect returned kOTNoError in async mode!?!");
+ }
+ if (fd->secret->nonblocking) {
+ if (err == kOTNoDataErr)
+ err = EINPROGRESS;
+ goto ErrorExit;
+ } else {
+ if (err != kOTNoError && err != kOTNoDataErr) {
+ me->io_pending = PR_FALSE;
+ goto ErrorExit;
+ }
+ }
WaitOnThisThread(me, timeout);
@@ -1583,7 +1592,6 @@ static PRInt32 SendReceiveDgram(PRFileDesc *fd, void *buf, PRInt32 amount,
PRThread *me = _PR_MD_CURRENT_THREAD();
PRInt32 bytesLeft = amount;
TUnitData dgram;
- OTResult result;
PR_ASSERT(flags == 0);
@@ -1618,13 +1626,13 @@ static PRInt32 SendReceiveDgram(PRFileDesc *fd, void *buf, PRInt32 amount,
fd->secret->md.write.thread = me;
fd->secret->md.writeReady = PR_FALSE; // expect the worst
err = OTSndUData(endpoint, &dgram);
- if (result != kOTFlowErr) // hope for the best
+ if (err != kOTFlowErr) // hope for the best
fd->secret->md.writeReady = PR_TRUE;
} else {
fd->secret->md.read.thread = me;
fd->secret->md.readReady = PR_FALSE; // expect the worst
err = OTRcvUData(endpoint, &dgram, NULL);
- if (result != kOTNoDataErr) // hope for the best
+ if (err != kOTNoDataErr) // hope for the best
fd->secret->md.readReady = PR_TRUE;
}
@@ -1754,83 +1762,131 @@ static PRBool GetState(PRFileDesc *fd, PRBool *readReady, PRBool *writeReady, PR
}
// check to see if any of the poll descriptors have data available
+// for reading or writing, by calling their poll methods (layered IO).
+static PRInt32 CheckPollDescMethods(PRPollDesc *pds, PRIntn npds, PRInt16 *outReadFlags, PRInt16 *outWriteFlags)
+{
+ PRInt32 ready = 0;
+ PRPollDesc *pd, *epd;
+ PRInt16 *readFlag, *writeFlag;
+
+ for (pd = pds, epd = pd + npds, readFlag = outReadFlags, writeFlag = outWriteFlags;
+ pd < epd;
+ pd++, readFlag++, writeFlag++)
+ {
+ PRInt16 in_flags_read = 0, in_flags_write = 0;
+ PRInt16 out_flags_read = 0, out_flags_write = 0;
+
+ pd->out_flags = 0;
+
+ if (NULL == pd->fd || pd->in_flags == 0) continue;
+
+ if (pd->in_flags & PR_POLL_READ)
+ {
+ in_flags_read = (pd->fd->methods->poll)(
+ pd->fd, pd->in_flags & ~PR_POLL_WRITE, &out_flags_read);
+ }
+
+ if (pd->in_flags & PR_POLL_WRITE)
+ {
+ in_flags_write = (pd->fd->methods->poll)(
+ pd->fd, pd->in_flags & ~PR_POLL_READ, &out_flags_write);
+ }
+
+ if ((0 != (in_flags_read & out_flags_read)) ||
+ (0 != (in_flags_write & out_flags_write)))
+ {
+ ready += 1; /* some layer has buffer input */
+ pd->out_flags = out_flags_read | out_flags_write;
+ }
+
+ *readFlag = in_flags_read;
+ *writeFlag = in_flags_write;
+ }
+
+ return ready;
+}
+
+// check to see if any of OT endpoints of the poll descriptors have data available
// for reading or writing.
-static PRInt32 CheckPollDescs(PRPollDesc *pds, PRIntn npds)
+static PRInt32 CheckPollDescEndpoints(PRPollDesc *pds, PRIntn npds, const PRInt16 *inReadFlags, const PRInt16 *inWriteFlags)
{
PRInt32 ready = 0;
PRPollDesc *pd, *epd;
+ const PRInt16 *readFlag, *writeFlag;
- for (pd = pds, epd = pd + npds; pd < epd; pd++)
- {
- PRInt16 in_flags_read = 0, in_flags_write = 0;
- PRInt16 out_flags_read = 0, out_flags_write = 0;
+ for (pd = pds, epd = pd + npds, readFlag = inReadFlags, writeFlag = inWriteFlags;
+ pd < epd;
+ pd++, readFlag++, writeFlag++)
+ {
+ PRFileDesc *bottomFD;
+ PRBool readReady, writeReady, exceptReady;
+ PRInt16 in_flags_read = *readFlag;
+ PRInt16 in_flags_write = *writeFlag;
- if (NULL == pd->fd || pd->in_flags == 0) continue;
+ if (NULL == pd->fd || pd->in_flags == 0) continue;
- if (pd->in_flags & PR_POLL_READ)
- {
- in_flags_read = (pd->fd->methods->poll)(
- pd->fd, pd->in_flags & ~PR_POLL_WRITE, &out_flags_read);
- }
- if (pd->in_flags & PR_POLL_WRITE)
- {
- in_flags_write = (pd->fd->methods->poll)(
- pd->fd, pd->in_flags & ~PR_POLL_READ, &out_flags_write);
- }
- if ((0 != (in_flags_read & out_flags_read))
- || (0 != (in_flags_write & out_flags_write)))
- {
- ready += 1; /* some layer has buffer input */
- pd->out_flags = out_flags_read | out_flags_write;
- }
- else
- {
- PRFileDesc *bottomFD;
- PRBool readReady, writeReady, exceptReady;
+ if ((pd->in_flags & ~pd->out_flags) == 0) {
+ ready++;
+ continue;
+ }
- pd->out_flags = 0; /* pre-condition */
- bottomFD = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
- /* bottomFD can be NULL for pollable sockets */
- if (bottomFD)
+ bottomFD = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
+ /* bottomFD can be NULL for pollable sockets */
+ if (bottomFD)
+ {
+ if (_PR_FILEDESC_OPEN == bottomFD->secret->state)
+ {
+ if (GetState(bottomFD, &readReady, &writeReady, &exceptReady))
{
- if (_PR_FILEDESC_OPEN == bottomFD->secret->state)
+ if (readReady)
+ {
+ if (in_flags_read & PR_POLL_READ)
+ pd->out_flags |= PR_POLL_READ;
+ if (in_flags_write & PR_POLL_READ)
+ pd->out_flags |= PR_POLL_WRITE;
+ }
+ if (writeReady)
{
- if (GetState(bottomFD, &readReady, &writeReady, &exceptReady))
- {
- if (readReady)
- {
- if (in_flags_read & PR_POLL_READ)
- pd->out_flags |= PR_POLL_READ;
- if (in_flags_write & PR_POLL_READ)
- pd->out_flags |= PR_POLL_WRITE;
- }
- if (writeReady)
- {
- if (in_flags_read & PR_POLL_WRITE)
- pd->out_flags |= PR_POLL_READ;
- if (in_flags_write & PR_POLL_WRITE)
- pd->out_flags |= PR_POLL_WRITE;
- }
- if (exceptReady && (pd->in_flags & PR_POLL_EXCEPT))
- {
- pd->out_flags |= PR_POLL_EXCEPT;
- }
- if (0 != pd->out_flags) ready++;
- }
+ if (in_flags_read & PR_POLL_WRITE)
+ pd->out_flags |= PR_POLL_READ;
+ if (in_flags_write & PR_POLL_WRITE)
+ pd->out_flags |= PR_POLL_WRITE;
}
- else /* bad state */
+ if (exceptReady && (pd->in_flags & PR_POLL_EXCEPT))
{
- ready += 1; /* this will cause an abrupt return */
- pd->out_flags = PR_POLL_NVAL; /* bogii */
+ pd->out_flags |= PR_POLL_EXCEPT;
}
}
+ if (0 != pd->out_flags) ready++;
+ }
+ else /* bad state */
+ {
+ ready += 1; /* this will cause an abrupt return */
+ pd->out_flags = PR_POLL_NVAL; /* bogii */
}
}
+ }
return ready;
}
-// set or clear md.poll.thread on the poll descriptors
+
+// see how many of the poll descriptors are ready
+static PRInt32 CountReadyPollDescs(PRPollDesc *pds, PRIntn npds)
+{
+ PRInt32 ready = 0;
+ PRPollDesc *pd, *epd;
+
+ for (pd = pds, epd = pd + npds; pd < epd; pd++)
+ {
+ if (pd->out_flags)
+ ready ++;
+ }
+
+ return ready;
+}
+
+// set or clear the poll thread on the poll descriptors
static void SetDescPollThread(PRPollDesc *pds, PRIntn npds, PRThread* thread)
{
PRInt32 ready = 0;
@@ -1862,43 +1918,82 @@ static void SetDescPollThread(PRPollDesc *pds, PRIntn npds, PRThread* thread)
bottomFD->secret->md.write.thread = thread;
}
}
- }
+ }
}
}
+
+#define DESCRIPTOR_FLAGS_ARRAY_SIZE 32
+
PRInt32 _MD_poll(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
{
+ PRInt16 readFlagsArray[DESCRIPTOR_FLAGS_ARRAY_SIZE];
+ PRInt16 writeFlagsArray[DESCRIPTOR_FLAGS_ARRAY_SIZE];
+
+ PRInt16 *readFlags = readFlagsArray;
+ PRInt16 *writeFlags = writeFlagsArray;
+
+ PRInt16 *ioFlags = NULL;
+
PRThread *thread = _PR_MD_CURRENT_THREAD();
- intn is;
- PRInt32 ready;
+ PRInt32 ready;
- if (timeout == PR_INTERVAL_NO_WAIT) {
- return CheckPollDescs(pds, npds);
+ if (npds > DESCRIPTOR_FLAGS_ARRAY_SIZE)
+ {
+ // we allocate a single double-size array. The first half is used
+ // for read flags, and the second half for write flags.
+ ioFlags = (PRInt16*)PR_Malloc(sizeof(PRInt16) * npds * 2);
+ if (!ioFlags)
+ {
+ PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
+ return -1;
+ }
+
+ readFlags = ioFlags;
+ writeFlags = &ioFlags[npds];
}
-
- _PR_INTSOFF(is);
- PR_Lock(thread->md.asyncIOLock);
- // ensure that we don't miss the firing of the notifier while checking socket status
- // need to set up the thread
- PrepareForAsyncCompletion(thread, 0);
+ // we have to be outside the lock when calling this, since
+ // it can call arbitrary user code (including other socket
+ // entry points)
+ ready = CheckPollDescMethods(pds, npds, readFlags, writeFlags);
- SetDescPollThread(pds, npds, thread);
- ready = CheckPollDescs(pds, npds);
+ if (!ready && timeout != PR_INTERVAL_NO_WAIT) {
+ intn is;
+
- PR_Unlock(thread->md.asyncIOLock);
- _PR_FAST_INTSON(is);
+ _PR_INTSOFF(is);
+ PR_Lock(thread->md.asyncIOLock);
+ PrepareForAsyncCompletion(thread, 0);
- if (ready == 0) {
- WaitOnThisThread(thread, timeout);
- ready = CheckPollDescs(pds, npds);
+ SetDescPollThread(pds, npds, thread);
- } else {
+ (void)CheckPollDescEndpoints(pds, npds, readFlags, writeFlags);
+
+ PR_Unlock(thread->md.asyncIOLock);
+ _PR_FAST_INTSON(is);
+
+ ready = CountReadyPollDescs(pds, npds);
+
+ if (ready == 0) {
+ WaitOnThisThread(thread, timeout);
+
+ // since we may have been woken by a pollable event firing,
+ // we have to check both poll methods and endpoints.
+ (void)CheckPollDescMethods(pds, npds, readFlags, writeFlags);
+ ready = CheckPollDescEndpoints(pds, npds, readFlags, writeFlags);
+ }
+
thread->io_pending = PR_FALSE;
+ SetDescPollThread(pds, npds, NULL);
+ }
+ else {
+ ready = CheckPollDescEndpoints(pds, npds, readFlags, writeFlags);
}
- SetDescPollThread(pds, npds, NULL);
-
+ if (readFlags != readFlagsArray)
+ PR_Free(ioFlags);
+
return ready;
}
@@ -2192,25 +2287,32 @@ ErrorExit:
}
-int _MD_mac_get_nonblocking_connect_error(PRInt32 osfd)
+int _MD_mac_get_nonblocking_connect_error(PRFileDesc* fd)
{
- OTResult resultOT;
- EndpointRef endpoint = (EndpointRef) osfd;
+ EndpointRef endpoint = (EndpointRef)fd->secret->md.osfd;
+ OTResult resultOT = OTGetEndpointState(endpoint);
- resultOT = OTGetEndpointState(endpoint);
switch (resultOT) {
case T_OUTCON:
macsock_map_error(EINPROGRESS);
return -1;
+
case T_DATAXFER:
return 0;
+
case T_IDLE:
+ macsock_map_error(fd->secret->md.disconnectError);
+ fd->secret->md.disconnectError = 0;
return -1;
+
case T_INREL:
macsock_map_error(ENOTCONN);
return -1;
+
default:
PR_ASSERT(0);
return -1;
}
+
+ return -1; // not reached
}
diff --git a/pr/src/md/mac/macthr.c b/pr/src/md/mac/macthr.c
index 0df8a590..fd3b489d 100644
--- a/pr/src/md/mac/macthr.c
+++ b/pr/src/md/mac/macthr.c
@@ -39,7 +39,7 @@
#include <MacTypes.h>
#include <Timer.h>
#include <OSUtils.h>
-
+#include <Math64.h>
#include <LowMem.h>
#include <Multiprocessing.h>
#include <Gestalt.h>
@@ -49,6 +49,8 @@
TimerUPP gTimerCallbackUPP = NULL;
PRThread * gPrimaryThread = NULL;
+ProcessSerialNumber gApplicationProcess;
+
PR_IMPLEMENT(PRThread *) PR_GetPrimaryThread()
{
return gPrimaryThread;
@@ -159,7 +161,21 @@ extern void _MD_ClearStack(PRThreadStack *ts)
#pragma mark -
#pragma mark TIME MANAGER-BASED CLOCK
-TMTask gTimeManagerTaskElem;
+// On Mac OS X, it's possible for the application to spend lots of time
+// in WaitNextEvent, yielding to other applications. Since NSPR threads are
+// cooperative here, this means that NSPR threads will also get very little
+// time to run. To kick ourselves out of a WaitNextEvent call when we have
+// determined that it's time to schedule another thread, the Timer Task
+// (which fires every 8ms, even when other apps have the CPU) calls WakeUpProcess.
+// We only want to do this on Mac OS X; the gTimeManagerTaskDoesWUP variable
+// indicates when we're running on that OS.
+//
+// Note that the TimerCallback makes use of gApplicationProcess. We need to
+// have set this up before the first possible run of the timer task; we do
+// so in _MD_EarlyInit().
+static Boolean gTimeManagerTaskDoesWUP;
+
+static TMTask gTimeManagerTaskElem;
extern void _MD_IOInterrupt(void);
_PRInterruptTable _pr_interruptTable[] = {
@@ -168,6 +184,8 @@ _PRInterruptTable _pr_interruptTable[] = {
{ 0 }
};
+#define kMacTimerInMiliSecs 8L
+
pascal void TimerCallback(TMTaskPtr tmTaskPtr)
{
_PRCPU *cpu = _PR_MD_CURRENT_CPU();
@@ -184,8 +202,20 @@ pascal void TimerCallback(TMTaskPtr tmTaskPtr)
// And tell nspr that a clock interrupt occured.
_PR_ClockInterrupt();
- if ((_PR_RUNQREADYMASK(cpu)) >> ((_PR_MD_CURRENT_THREAD()->priority)))
+ if ((_PR_RUNQREADYMASK(cpu)) >> ((_PR_MD_CURRENT_THREAD()->priority))) {
+ if (gTimeManagerTaskDoesWUP) {
+ // We only want to call WakeUpProcess if we know that NSPR has managed to switch threads
+ // since the last call, otherwise we end up spewing out WakeUpProcess() calls while the
+ // application is blocking somewhere. This can interfere with events loops other than
+ // our own (see bug 158927).
+ if (UnsignedWideToUInt64(cpu->md.lastThreadSwitch) > UnsignedWideToUInt64(cpu->md.lastWakeUpProcess))
+ {
+ WakeUpProcess(&gApplicationProcess);
+ cpu->md.lastWakeUpProcess = UpTime();
+ }
+ }
_PR_SET_RESCHED_FLAG();
+ }
_PR_FAST_INTSON(is);
@@ -198,8 +228,10 @@ void _MD_StartInterrupts(void)
{
gPrimaryThread = _PR_MD_CURRENT_THREAD();
+ gTimeManagerTaskDoesWUP = RunningOnOSX();
+
if ( !gTimerCallbackUPP )
- gTimerCallbackUPP = NewTimerProc(TimerCallback);
+ gTimerCallbackUPP = NewTimerUPP(TimerCallback);
// Fill in the Time Manager queue element
@@ -222,26 +254,36 @@ void _MD_StopInterrupts(void)
}
}
+
+#define MAX_PAUSE_TIMEOUT_MS 500
+
void _MD_PauseCPU(PRIntervalTime timeout)
{
if (timeout != PR_INTERVAL_NO_WAIT)
{
- EventRecord theEvent;
-
- /*
- ** Calling WaitNextEvent() here is suboptimal. This routine should
- ** pause the process until IO or the timeout occur, yielding time to
- ** other processes on operating systems that require this (Mac OS classic).
- ** WaitNextEvent() may incur too much latency, and has other problems,
- ** such as the potential to drop suspend/resume events, and to handle
- ** AppleEvents at a time at which we're not prepared to handle them.
- */
- (void) WaitNextEvent(nullEvent, &theEvent, 1, NULL);
-
+ // There is a race condition entering the critical section
+ // in AsyncIOCompletion (and probably elsewhere) that can
+ // causes deadlock for the duration of this timeout. To
+ // work around this, use a max 500ms timeout for now.
+ // See bug 99561 for details.
+ if (PR_IntervalToMilliseconds(timeout) > MAX_PAUSE_TIMEOUT_MS)
+ timeout = PR_MillisecondsToInterval(MAX_PAUSE_TIMEOUT_MS);
+
+ WaitOnIdleSemaphore(timeout);
(void) _MD_IOInterrupt();
}
}
+void _MD_InitRunningCPU(_PRCPU* cpu)
+{
+ cpu->md.trackScheduling = RunningOnOSX();
+ if (cpu->md.trackScheduling) {
+ AbsoluteTime zeroTime = {0, 0};
+ cpu->md.lastThreadSwitch = UpTime();
+ cpu->md.lastWakeUpProcess = zeroTime;
+ }
+}
+
//##############################################################################
//##############################################################################
@@ -528,19 +570,25 @@ void _MD_SetIntsOff(PRInt32 ints)
#pragma mark -
#pragma mark CRITICAL REGION SUPPORT
+
+static PRBool RunningOnOSX()
+{
+ long systemVersion;
+ OSErr err = Gestalt(gestaltSystemVersion, &systemVersion);
+ return (err == noErr) && (systemVersion >= 0x00001000);
+}
+
+
#if MAC_CRITICAL_REGIONS
MDCriticalRegionID gCriticalRegion;
void InitCriticalRegion()
{
- long systemVersion;
OSStatus err;
// we only need to do critical region stuff on Mac OS X
- err = Gestalt(gestaltSystemVersion, &systemVersion);
- gUseCriticalRegions = (err == noErr) && (systemVersion >= 0x00001000);
-
+ gUseCriticalRegions = RunningOnOSX();
if (!gUseCriticalRegions) return;
err = MD_CriticalRegionCreate(&gCriticalRegion);
@@ -586,3 +634,85 @@ void LeaveCritialRegion()
#endif // MAC_CRITICAL_REGIONS
+//##############################################################################
+//##############################################################################
+#pragma mark -
+#pragma mark IDLE SEMAPHORE SUPPORT
+
+/*
+ Since the WaitNextEvent() in _MD_PauseCPU() is causing all sorts of
+ headache under Mac OS X we're going to switch to MPWaitOnSemaphore()
+ which should do what we want
+*/
+
+#if TARGET_CARBON
+PRBool gUseIdleSemaphore = PR_FALSE;
+MPSemaphoreID gIdleSemaphore = NULL;
+#endif
+
+void InitIdleSemaphore()
+{
+ // we only need to do idle semaphore stuff on Mac OS X
+#if TARGET_CARBON
+ gUseIdleSemaphore = RunningOnOSX();
+ if (gUseIdleSemaphore)
+ {
+ OSStatus err = MPCreateSemaphore(1 /* max value */, 0 /* initial value */, &gIdleSemaphore);
+ PR_ASSERT(err == noErr);
+ }
+#endif
+}
+
+void TermIdleSemaphore()
+{
+#if TARGET_CARBON
+ if (gUseIdleSemaphore)
+ {
+ OSStatus err = MPDeleteSemaphore(gIdleSemaphore);
+ PR_ASSERT(err == noErr);
+ gUseIdleSemaphore = NULL;
+ }
+#endif
+}
+
+
+void WaitOnIdleSemaphore(PRIntervalTime timeout)
+{
+#if TARGET_CARBON
+ if (gUseIdleSemaphore)
+ {
+ OSStatus err = MPWaitOnSemaphore(gIdleSemaphore, kDurationMillisecond * PR_IntervalToMilliseconds(timeout));
+ PR_ASSERT(err == noErr);
+ }
+ else
+#endif
+ {
+ EventRecord theEvent;
+ /*
+ ** Calling WaitNextEvent() here is suboptimal. This routine should
+ ** pause the process until IO or the timeout occur, yielding time to
+ ** other processes on operating systems that require this (Mac OS classic).
+ ** WaitNextEvent() may incur too much latency, and has other problems,
+ ** such as the potential to drop suspend/resume events.
+ */
+ (void)WaitNextEvent(nullEvent, &theEvent, 1, NULL);
+ }
+}
+
+
+void SignalIdleSemaphore()
+{
+#if TARGET_CARBON
+ if (gUseIdleSemaphore)
+ {
+ // often we won't be waiting on the semaphore here, so ignore any errors
+ (void)MPSignalSemaphore(gIdleSemaphore);
+ }
+ else
+#endif
+ {
+ WakeUpProcess(&gApplicationProcess);
+ }
+}
+
+
diff --git a/pr/src/md/mac/mdcriticalregion.c b/pr/src/md/mac/mdcriticalregion.c
index 927b9246..ff035d0d 100644
--- a/pr/src/md/mac/mdcriticalregion.c
+++ b/pr/src/md/mac/mdcriticalregion.c
@@ -35,6 +35,7 @@
*/
#include "mdcriticalregion.h"
+#include <MacErrors.h>
/*
This code is a replacement for MPEnterCriticalRegion/MPLeaveCriticalRegion,
diff --git a/pr/src/md/mac/mdmac.c b/pr/src/md/mac/mdmac.c
index f7e9a4fa..6d65e578 100644
--- a/pr/src/md/mac/mdmac.c
+++ b/pr/src/md/mac/mdmac.c
@@ -70,6 +70,7 @@
unsigned char GarbageCollectorCacheFlusher(PRUint32 size);
extern PRThread *gPrimaryThread;
+extern ProcessSerialNumber gApplicationProcess; // in macthr.c
//##############################################################################
@@ -288,7 +289,10 @@ void _MD_EarlyInit()
{
Handle environmentVariables;
+ GetCurrentProcess(&gApplicationProcess);
+
INIT_CRITICAL_REGION();
+ InitIdleSemaphore();
#if !defined(MAC_NSPR_STANDALONE)
// MacintoshInitializeMemory(); Moved to mdmacmem.c: AllocateRawMemory(Size blockSize)
@@ -376,6 +380,7 @@ void CleanupTermProc(void)
_MD_StopInterrupts(); // deactive Time Manager task
CLOSE_OPEN_TRANSPORT();
+ TermIdleSemaphore();
TERM_CRITICAL_REGION();
__NSTerminate();
diff --git a/pr/src/md/os2/Makefile.in b/pr/src/md/os2/Makefile.in
index 9898c88f..7f56eb23 100644
--- a/pr/src/md/os2/Makefile.in
+++ b/pr/src/md/os2/Makefile.in
@@ -62,6 +62,10 @@ ifeq ($(MOZ_OS2_TOOLS),VACPP)
ASFILES = os2vacpp.asm
endif
+ifeq ($(MOZ_OS2_TOOLS),EMX)
+ASFILES = os2emx.s os2vaclegacy.s
+endif
+
TARGETS = $(OBJS)
INCLUDES = -I$(dist_includedir) -I$(topsrcdir)/pr/include -I$(topsrcdir)/pr/include/private
diff --git a/pr/src/md/os2/objs.mk b/pr/src/md/os2/objs.mk
index 7c0bb943..05fe4658 100644
--- a/pr/src/md/os2/objs.mk
+++ b/pr/src/md/os2/objs.mk
@@ -52,6 +52,10 @@ ifeq ($(MOZ_OS2_TOOLS),VACPP)
ASFILES = os2vacpp.asm
endif
+ifeq ($(MOZ_OS2_TOOLS),EMX)
+ASFILES = os2emx.s os2vaclegacy.s
+endif
+
OBJS += $(addprefix md/os2/$(OBJDIR)/,$(CSRCS:.c=.$(OBJ_SUFFIX))) \
- $(addprefix md/os2/$(OBJDIR)/,$(ASFILES:.asm=.$(OBJ_SUFFIX)))
+ $(addprefix md/os2/$(OBJDIR)/,$(ASFILES:.$(ASM_SUFFIX)=.$(OBJ_SUFFIX)))
diff --git a/pr/src/md/os2/os2_errors.c b/pr/src/md/os2/os2_errors.c
index f5be8391..7c270c4d 100644
--- a/pr/src/md/os2/os2_errors.c
+++ b/pr/src/md/os2/os2_errors.c
@@ -35,6 +35,44 @@
#include "prerror.h"
#include "primpl.h"
+void _MD_os2_map_default_error(PRInt32 err)
+{
+ switch (err) {
+ case EWOULDBLOCK:
+ PR_SetError(PR_WOULD_BLOCK_ERROR, err);
+ break;
+ case EBADF:
+ PR_SetError(PR_BAD_DESCRIPTOR_ERROR, err);
+ break;
+ case ENOTSOCK:
+ PR_SetError(PR_NOT_SOCKET_ERROR, err);
+ break;
+ case EMSGSIZE:
+ case EINVAL:
+ PR_SetError(PR_INVALID_ARGUMENT_ERROR, err);
+ break;
+ case ENOBUFS:
+ PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, err);
+ break;
+ case ECONNREFUSED:
+ PR_SetError(PR_CONNECT_REFUSED_ERROR, err);
+ break;
+ case EISCONN:
+ PR_SetError(PR_IS_CONNECTED_ERROR, err);
+ break;
+#ifdef SOCEFAULT
+ case SOCEFAULT:
+ PR_SetError(PR_ACCESS_FAULT_ERROR, err);
+ break;
+#endif
+ case ERROR_NETNAME_DELETED:
+ PR_SetError(PR_CONNECT_RESET_ERROR, err);
+ break;
+ default:
+ PR_SetError(PR_UNKNOWN_ERROR, err);
+ break;
+ }
+}
void _MD_os2_map_opendir_error(PRInt32 err)
{
switch (err) {
@@ -92,7 +130,7 @@ void _MD_os2_map_closedir_error(PRInt32 err)
}
}
-void _MD_unix_readdir_error(PRInt32 err)
+void _MD_os2_readdir_error(PRInt32 err)
{
switch (err) {
@@ -671,73 +709,17 @@ void _MD_os2_map_send_error(PRInt32 err)
void _MD_os2_map_sendto_error(PRInt32 err)
{
- switch (err) {
- case EWOULDBLOCK:
- PR_SetError(PR_WOULD_BLOCK_ERROR, err);
- break;
- case EBADF:
- PR_SetError(PR_BAD_DESCRIPTOR_ERROR, err);
- break;
- case ENOTSOCK:
- PR_SetError(PR_NOT_SOCKET_ERROR, err);
- break;
- case EMSGSIZE:
- case EINVAL:
- PR_SetError(PR_INVALID_ARGUMENT_ERROR, err);
- break;
- case ENOBUFS:
- PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, err);
- break;
- case ECONNREFUSED:
- PR_SetError(PR_CONNECT_REFUSED_ERROR, err);
- break;
- case EISCONN:
- PR_SetError(PR_IS_CONNECTED_ERROR, err);
- break;
-#ifdef SOCEFAULT
- case SOCEFAULT:
- PR_SetError(PR_ACCESS_FAULT_ERROR, err);
- break;
-#endif
- case ERROR_NETNAME_DELETED:
- PR_SetError(PR_CONNECT_RESET_ERROR, err);
- break;
- default:
- PR_SetError(PR_UNKNOWN_ERROR, err);
- break;
- }
+ _MD_os2_map_default_error(err);
+}
+
+void _MD_os2_map_writev_error(int err)
+{
+ _MD_os2_map_default_error(err);
}
void _MD_os2_map_accept_error(PRInt32 err)
{
- switch (err) {
- case EWOULDBLOCK:
- PR_SetError(PR_WOULD_BLOCK_ERROR, err);
- break;
- case EBADF:
- PR_SetError(PR_BAD_DESCRIPTOR_ERROR, err);
- break;
- case ENOTSOCK:
- PR_SetError(PR_NOT_SOCKET_ERROR, err);
- break;
- case EOPNOTSUPP:
- PR_SetError(PR_NOT_TCP_SOCKET_ERROR, err);
- break;
-#ifdef SOCEFAULT
- case SOCEFAULT:
- PR_SetError(PR_ACCESS_FAULT_ERROR, err);
- break;
-#endif
- case EMFILE:
- PR_SetError(PR_PROC_DESC_TABLE_FULL_ERROR, err);
- break;
- case ENOBUFS:
- PR_SetError(PR_OUT_OF_MEMORY_ERROR, err);
- break;
- default:
- PR_SetError(PR_UNKNOWN_ERROR, err);
- break;
- }
+ _MD_os2_map_default_error(err);
}
void _MD_os2_map_acceptex_error(PRInt32 err)
@@ -759,6 +741,21 @@ void _MD_os2_map_acceptex_error(PRInt32 err)
}
}
+/*
+ * An error code of 0 means that the nonblocking connect succeeded.
+ */
+
+int _MD_os2_get_nonblocking_connect_error(int osfd)
+{
+ int err;
+ int len = sizeof(err);
+ if (getsockopt(osfd, SOL_SOCKET, SO_ERROR, (char *) &err, &len) == -1) {
+ return sock_errno();
+ } else {
+ return err;
+ }
+}
+
void _MD_os2_map_connect_error(PRInt32 err)
{
switch (err) {
@@ -878,6 +875,30 @@ void _MD_os2_map_shutdown_error(PRInt32 err)
}
}
+void _MD_os2_map_socketpair_error(PRInt32 err)
+{
+ switch (err) {
+ case ENOMEM:
+ PR_SetError(PR_INSUFFICIENT_RESOURCES_ERROR, err);
+ break;
+ case EAFNOSUPPORT:
+ PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, err);
+ break;
+ case EPROTONOSUPPORT:
+ PR_SetError(PR_PROTOCOL_NOT_SUPPORTED_ERROR, err);
+ break;
+ case EOPNOTSUPP:
+ PR_SetError(PR_NOT_TCP_SOCKET_ERROR, err);
+ break;
+ case EPROTOTYPE:
+ PR_SetError(PR_ADDRESS_NOT_SUPPORTED_ERROR, err);
+ break;
+ default:
+ _MD_os2_map_default_error(err);
+ return;
+ }
+}
+
void _MD_os2_map_getsockname_error(PRInt32 err)
{
switch (err) {
diff --git a/pr/src/md/os2/os2cv.c b/pr/src/md/os2/os2cv.c
index 1a2d9399..44e1e68b 100644
--- a/pr/src/md/os2/os2cv.c
+++ b/pr/src/md/os2/os2cv.c
@@ -48,6 +48,42 @@
#include "primpl.h"
+#ifdef USE_RAMSEM
+ULONG _Far16 _Pascal Dos16GetInfoSeg(PSEL pselGlobal, PSEL pselLocal);
+
+#ifdef XP_OS2_EMX
+typedef unsigned short BOOL16;
+#endif
+
+typedef struct _LINFOSEG
+{
+ USHORT pidCurrent;
+ USHORT pidParent;
+ USHORT prtyCurrent;
+ USHORT tidCurrent;
+ USHORT sgCurrent;
+ UCHAR rfProcStatus;
+ UCHAR dummy1;
+ BOOL16 fForeground;
+ UCHAR typProcess;
+ UCHAR dummy2;
+ SEL selEnvironment;
+ USHORT offCmdLine;
+ USHORT cbDataSegment;
+ USHORT cbStack;
+ USHORT cbHeap;
+ USHORT hmod;
+ SEL selDS;
+ SEL selPack;
+ SEL selPackShr;
+ SEL selPackPck;
+ ULONG ulReserved;
+} LINFOSEG;
+typedef LINFOSEG FAR *PLINFOSEG;
+
+PLINFOSEG plisCurrent = NULL;
+#endif
+
/*
* AddThreadToCVWaitQueueInternal --
*
@@ -82,7 +118,7 @@ AddThreadToCVWaitQueueInternal(PRThread *thred, struct _MDCVar *cv)
* This function is called by _PR_MD_WAIT_CV and _PR_MD_UNLOCK,
* the two places where a lock is unlocked.
*/
-static void
+void
md_UnlockAndPostNotifies(
_MDLock *lock,
PRThread *waitThred,
@@ -163,7 +199,11 @@ md_UnlockAndPostNotifies(
}
/* Release the lock before notifying */
- DosReleaseMutexSem(lock->mutex);
+#ifdef USE_RAMSEM
+ SemReleasex86(&lock->mutex, 0);
+#else
+ DosReleaseMutexSem(lock->mutex);
+#endif
notified = &post; /* this is where we start */
do {
@@ -270,7 +310,11 @@ _PR_MD_WAIT_CV(_MDCVar *cv, _MDLock *lock, PRIntervalTime timeout )
md_UnlockAndPostNotifies(lock, thred, cv);
} else {
AddThreadToCVWaitQueueInternal(thred, cv);
+#ifdef USE_RAMSEM
+ SemReleasex86( &lock->mutex, 0 );
+#else
DosReleaseMutexSem(lock->mutex);
+#endif
}
/* Wait for notification or timeout; don't really care which */
@@ -279,7 +323,11 @@ _PR_MD_WAIT_CV(_MDCVar *cv, _MDLock *lock, PRIntervalTime timeout )
DosResetEventSem(thred->md.blocked_sema, &count);
}
+#ifdef USE_RAMSEM
+ SemRequest486(&(lock->mutex), -1);
+#else
DosRequestMutexSem((lock->mutex), SEM_INDEFINITE_WAIT);
+#endif
PR_ASSERT(rv == NO_ERROR || rv == ERROR_TIMEOUT);
@@ -336,28 +384,41 @@ _PR_MD_NOTIFY_CV(_MDCVar *cv, _MDLock *lock)
PRStatus
_PR_MD_NEW_LOCK(_MDLock *lock)
{
+#ifdef USE_RAMSEM
+ // It's better if this API traps when pCriticalSect is not a valid
+ // pointer, because we can't return an error code and if we just return
+ // the API caller will have nasty bugs that are hard to find.
+
+ PRAMSEM pramsem = (PRAMSEM)(&(lock->mutex));
+ /* First time, set up addresses of processor specific functions
+ */
+ if (plisCurrent == NULL)
+ {
+ SEL selGlobal = 0, selLocal = 0;
+
+ /* Convert 16 bit global information segment to 32 bit address
+ * by performing CRMA on the 16 bit address: "shift" operation
+ * to convert sel to flat, "and" operation to mask the address
+ * to 32-bit
+ */
+ Dos16GetInfoSeg(&selGlobal, &selLocal);
+ plisCurrent = (PLINFOSEG)(((ULONG)selLocal << 13) &
+ (ULONG)0x1fff0000);
+
+ }
+
+ memset(pramsem, 0, sizeof(pramsem));
+ DosCreateEventSem(0, &pramsem->hevSem, DC_SEM_SHARED, 0);
+
+ lock->notified.length=0;
+ lock->notified.link=NULL;
+ return PR_SUCCESS;
+#else
DosCreateMutexSem(0, &(lock->mutex), 0, 0);
(lock)->notified.length=0;
(lock)->notified.link=NULL;
return PR_SUCCESS;
-}
-
-void
-_PR_MD_FREE_LOCK(_MDLock *lock)
-{
- DosCloseMutexSem(lock->mutex);
-}
-
-void _PR_MD_LOCK(_MDLock *lock)
-{
- DosRequestMutexSem(lock->mutex, SEM_INDEFINITE_WAIT);
-}
-
-PRIntn
-_PR_MD_TEST_AND_LOCK(_MDLock *lock)
-{
- DosRequestMutexSem(lock->mutex, SEM_INDEFINITE_WAIT);
- return 0;
+#endif
}
void
@@ -366,14 +427,3 @@ _PR_MD_NOTIFYALL_CV(_MDCVar *cv, _MDLock *lock)
md_PostNotifyToCvar(cv, lock, PR_TRUE);
return;
}
-
-void
-_PR_MD_UNLOCK(_MDLock *lock)
-{
- if (0 != lock->notified.length) {
- md_UnlockAndPostNotifies(lock, NULL, NULL);
- } else {
- DosReleaseMutexSem(lock->mutex);
- }
- return;
-}
diff --git a/pr/src/md/os2/os2emx.s b/pr/src/md/os2/os2emx.s
new file mode 100644
index 00000000..4dd81e39
--- /dev/null
+++ b/pr/src/md/os2/os2emx.s
@@ -0,0 +1,111 @@
+/ -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+/
+/ The contents of this file are subject to the Mozilla Public
+/ License Version 1.1 (the "License"); you may not use this file
+/ except in compliance with the License. You may obtain a copy of
+/ the License at http://www.mozilla.org/MPL/
+/
+/ Software distributed under the License is distributed on an "AS
+/ IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+/ implied. See the License for the specific language governing
+/ rights and limitations under the License.
+/
+/ The Original Code is the Netscape Portable Runtime (NSPR).
+/
+/ The Initial Developer of the Original Code is Netscape
+/ Communications Corporation. Portions created by Netscape are
+/ Copyright (C) 2000 Netscape Communications Corporation. All
+/ Rights Reserved.
+/
+/ Contributor(s):
+/
+/ Alternatively, the contents of this file may be used under the
+/ terms of the GNU General Public License Version 2 or later (the
+/ "GPL"), in which case the provisions of the GPL are applicable
+/ instead of those above. If you wish to allow use of your
+/ version of this file only under the terms of the GPL and not to
+/ allow others to use your version of this file under the MPL,
+/ indicate your decision by deleting the provisions above and
+/ replace them with the notice and other provisions required by
+/ the GPL. If you do not delete the provisions above, a recipient
+/ may use your version of this file under either the MPL or the
+/ GPL.
+/
+
+/ PRInt32 __PR_MD_ATOMIC_INCREMENT(PRInt32 *val)
+/
+/ Atomically increment the integer pointed to by 'val' and return
+/ the result of the increment.
+/
+ .text
+ .globl __PR_MD_ATOMIC_INCREMENT
+ .align 4
+__PR_MD_ATOMIC_INCREMENT:
+ movl 4(%esp), %ecx
+ movl $1, %eax
+ lock
+ xaddl %eax, (%ecx)
+ incl %eax
+ ret
+
+/ PRInt32 __PR_MD_ATOMIC_DECREMENT(PRInt32 *val)
+/
+/ Atomically decrement the integer pointed to by 'val' and return
+/ the result of the decrement.
+/
+ .text
+ .globl __PR_MD_ATOMIC_DECREMENT
+ .align 4
+__PR_MD_ATOMIC_DECREMENT:
+ movl 4(%esp), %ecx
+ movl $-1, %eax
+ lock
+ xaddl %eax, (%ecx)
+ decl %eax
+ ret
+
+/ PRInt32 __PR_MD_ATOMIC_SET(PRInt32 *val, PRInt32 newval)
+/
+/ Atomically set the integer pointed to by 'val' to the new
+/ value 'newval' and return the old value.
+/
+/ An alternative implementation:
+/ .text
+/ .globl __PR_MD_ATOMIC_SET
+/ .align 4
+/__PR_MD_ATOMIC_SET:
+/ movl 4(%esp), %ecx
+/ movl 8(%esp), %edx
+/ movl (%ecx), %eax
+/retry:
+/ lock
+/ cmpxchgl %edx, (%ecx)
+/ jne retry
+/ ret
+/
+ .text
+ .globl __PR_MD_ATOMIC_SET
+ .align 4
+__PR_MD_ATOMIC_SET:
+ movl 4(%esp), %ecx
+ movl 8(%esp), %eax
+ lock
+ xchgl %eax, (%ecx)
+ ret
+
+/ PRInt32 __PR_MD_ATOMIC_ADD(PRInt32 *ptr, PRInt32 val)
+/
+/ Atomically add 'val' to the integer pointed to by 'ptr'
+/ and return the result of the addition.
+/
+ .text
+ .globl __PR_MD_ATOMIC_ADD
+ .align 4
+__PR_MD_ATOMIC_ADD:
+ movl 4(%esp), %ecx
+ movl 8(%esp), %eax
+ movl %eax, %edx
+ lock
+ xaddl %eax, (%ecx)
+ addl %edx, %eax
+ ret
diff --git a/pr/src/md/os2/os2inrval.c b/pr/src/md/os2/os2inrval.c
index 469da314..d4c90073 100644
--- a/pr/src/md/os2/os2inrval.c
+++ b/pr/src/md/os2/os2inrval.c
@@ -39,57 +39,62 @@
#include "primpl.h"
-ULONG _os2_ticksPerSec = -1;
+static PRBool useHighResTimer = PR_FALSE;
+PRIntervalTime _os2_ticksPerSec = -1;
PRIntn _os2_bitShift = 0;
PRInt32 _os2_highMask = 0;
-
-
void
_PR_MD_INTERVAL_INIT()
{
- if (DosTmrQueryFreq(&_os2_ticksPerSec) == NO_ERROR)
- {
- while(_os2_ticksPerSec > PR_INTERVAL_MAX) {
- _os2_ticksPerSec >>= 1;
- _os2_bitShift++;
- _os2_highMask = (_os2_highMask << 1)+1;
- }
- }
- else
- _os2_ticksPerSec = -1;
+ ULONG timerFreq = 0; /* OS/2 high-resolution timer frequency in Hz */
+ APIRET rc = DosTmrQueryFreq(&timerFreq);
+ if (NO_ERROR == rc) {
+ useHighResTimer = PR_TRUE;
+ PR_ASSERT(timerFreq != 0);
+ while (timerFreq > PR_INTERVAL_MAX) {
+ timerFreq >>= 1;
+ _os2_bitShift++;
+ _os2_highMask = (_os2_highMask << 1)+1;
+ }
- PR_ASSERT(_os2_ticksPerSec > PR_INTERVAL_MIN && _os2_ticksPerSec < PR_INTERVAL_MAX);
+ _os2_ticksPerSec = timerFreq;
+ PR_ASSERT(_os2_ticksPerSec > PR_INTERVAL_MIN);
+ }
}
PRIntervalTime
_PR_MD_GET_INTERVAL()
{
- QWORD count;
-
- /* Sadly; nspr requires the interval to range from 1000 ticks per second
- * to only 100000 ticks per second; Counter is too high
- * resolution...
- */
- if (DosTmrQueryTime(&count) == NO_ERROR) {
- PRInt32 top = count.ulHi & _os2_highMask;
+ if (useHighResTimer) {
+ QWORD timestamp;
+ PRInt32 top;
+ APIRET rc = DosTmrQueryTime(&timestamp);
+ if (NO_ERROR != rc) {
+ return -1;
+ }
+ /* Sadly, nspr requires the interval to range from 1000 ticks per
+ * second to only 100000 ticks per second. DosTmrQueryTime is too
+ * high resolution...
+ */
+ top = timestamp.ulHi & _os2_highMask;
top = top << (32 - _os2_bitShift);
- count.ulLo = count.ulLo >> _os2_bitShift;
- count.ulHi = count.ulLo + top;
- return (PRUint32)count.ulLo;
- }
- else{
- ULONG msCount = PR_FAILURE;
- DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &msCount, sizeof(msCount));
- return msCount;
+ timestamp.ulLo = timestamp.ulLo >> _os2_bitShift;
+ timestamp.ulLo = timestamp.ulLo + top;
+ return (PRUint32)timestamp.ulLo;
+ } else {
+ ULONG msCount = -1;
+ DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &msCount, sizeof(msCount));
+ return msCount;
}
}
PRIntervalTime
_PR_MD_INTERVAL_PER_SEC()
{
- if(_os2_ticksPerSec != -1)
- return _os2_ticksPerSec;
- else
- return 1000;
+ if (useHighResTimer) {
+ return _os2_ticksPerSec;
+ } else {
+ return 1000;
+ }
}
diff --git a/pr/src/md/os2/os2io.c b/pr/src/md/os2/os2io.c
index 12e2f2a2..4897a31c 100644
--- a/pr/src/md/os2/os2io.c
+++ b/pr/src/md/os2/os2io.c
@@ -61,6 +61,7 @@
#include "primpl.h"
#include "prio.h"
#include <ctype.h>
+#include <string.h>
#ifdef XP_OS2_VACPP
#include <direct.h>
#else
@@ -147,8 +148,6 @@ _PR_MD_OPEN(const char *name, PRIntn osflags, int mode)
APIRET rc = 0;
PRUword actionTaken;
- ULONG CurMaxFH = 0;
- LONG ReqCount = 1;
ULONG fattr;
if (osflags & PR_SYNC) access |= OPEN_FLAGS_WRITE_THROUGH;
@@ -253,6 +252,12 @@ _PR_MD_WRITE(PRFileDesc *fd, const void *buf, PRInt32 len)
return -1;
}
+ if (len != bytes) {
+ rv = ERROR_DISK_FULL;
+ _PR_MD_MAP_WRITE_ERROR(rv);
+ return -1;
+ }
+
return bytes;
} /* --- end _PR_MD_WRITE() --- */
@@ -761,25 +766,43 @@ _PR_MD_UNLOCKFILE(PRInt32 f)
PRStatus
_PR_MD_SET_FD_INHERITABLE(PRFileDesc *fd, PRBool inheritable)
{
- int rv = 0;
+ APIRET rc = 0;
ULONG flags;
+ switch (fd->methods->file_type)
+ {
+ case PR_DESC_PIPE:
+ case PR_DESC_FILE:
+ rc = DosQueryFHState((HFILE)fd->secret->md.osfd, &flags);
+ if (rc != NO_ERROR) {
+ PR_SetError(PR_UNKNOWN_ERROR, _MD_ERRNO());
+ return PR_FAILURE;
+ }
- rv = DosQueryFHState((HFILE)fd->secret->md.osfd, &flags);
- if (rv != 0) {
- PR_SetError(PR_UNKNOWN_ERROR, _MD_ERRNO());
- return PR_FAILURE;
- }
+ if (inheritable)
+ flags &= ~OPEN_FLAGS_NOINHERIT;
+ else
+ flags |= OPEN_FLAGS_NOINHERIT;
+
+ /* Mask off flags DosSetFHState don't want. */
+ flags &= (OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT);
+ rc = DosSetFHState((HFILE)fd->secret->md.osfd, flags);
+ if (rc != NO_ERROR) {
+ PR_SetError(PR_UNKNOWN_ERROR, _MD_ERRNO());
+ return PR_FAILURE;
+ }
+ break;
- if (inheritable)
- flags &= ~OPEN_FLAGS_NOINHERIT;
- else
- flags |= OPEN_FLAGS_NOINHERIT;
+ case PR_DESC_LAYERED:
+ /* what to do here? */
+ PR_SetError(PR_UNKNOWN_ERROR, 87 /*ERROR_INVALID_PARAMETER*/);
+ return PR_FAILURE;
- rv = DosSetFHState((HFILE)fd->secret->md.osfd, flags);
- if (rv != 0) {
- PR_SetError(PR_UNKNOWN_ERROR, _MD_ERRNO());
- return PR_FAILURE;
+ case PR_DESC_SOCKET_TCP:
+ case PR_DESC_SOCKET_UDP:
+ /* These are global on OS/2. */
+ break;
}
+
return PR_SUCCESS;
}
diff --git a/pr/src/md/os2/os2misc.c b/pr/src/md/os2/os2misc.c
index be597fcd..4e9bb932 100644
--- a/pr/src/md/os2/os2misc.c
+++ b/pr/src/md/os2/os2misc.c
@@ -118,107 +118,28 @@ PR_Now(void)
static int assembleCmdLine(char *const *argv, char **cmdLine)
{
char *const *arg;
- char *p, *q;
int cmdLineSize;
- int numBackslashes;
- int i;
- int argNeedQuotes;
/*
* Find out how large the command line buffer should be.
*/
- cmdLineSize = 0;
- for (arg = argv; *arg; arg++) {
- /*
- * \ and " need to be escaped by a \. In the worst case,
- * every character is a \ or ", so the string of length
- * may double. If we quote an argument, that needs two ".
- * Finally, we need a space between arguments, a null between
- * the EXE name and the arguments, and 2 nulls at the end
- * of command line.
- */
- cmdLineSize += 2 * strlen(*arg) /* \ and " need to be escaped */
- + 2 /* we quote every argument */
- + 4; /* space in between, or final nulls */
- }
- p = *cmdLine = PR_MALLOC(cmdLineSize);
- if (p == NULL) {
+ cmdLineSize = 1; /* final null */
+ for (arg = argv+1; *arg; arg++) {
+ cmdLineSize += strlen(*arg) + 1; /* space in between */
+ }
+ *cmdLine = PR_MALLOC(cmdLineSize);
+ if (*cmdLine == NULL) {
return -1;
}
- for (arg = argv; *arg; arg++) {
- /* Add a space to separates the arguments */
- if (arg > argv + 1) {
- *p++ = ' ';
- }
- q = *arg;
- numBackslashes = 0;
- argNeedQuotes = 0;
-
- /* If the argument contains white space, it needs to be quoted. */
- if (strpbrk(*arg, " \f\n\r\t\v")) {
- argNeedQuotes = 1;
- }
+ (*cmdLine)[0] = '\0';
- if (argNeedQuotes) {
- *p++ = '"';
+ for (arg = argv+1; *arg; arg++) {
+ if (arg > argv +1) {
+ strcat(*cmdLine, " ");
}
- while (*q) {
- if (*q == '\\') {
- numBackslashes++;
- q++;
- } else if (*q == '"') {
- if (numBackslashes) {
- /*
- * Double the backslashes since they are followed
- * by a quote
- */
- for (i = 0; i < 2 * numBackslashes; i++) {
- *p++ = '\\';
- }
- numBackslashes = 0;
- }
- /* To escape the quote */
- *p++ = '\\';
- *p++ = *q++;
- } else {
- if (numBackslashes) {
- /*
- * Backslashes are not followed by a quote, so
- * don't need to double the backslashes.
- */
- for (i = 0; i < numBackslashes; i++) {
- *p++ = '\\';
- }
- numBackslashes = 0;
- }
- *p++ = *q++;
- }
- }
-
- /* Now we are at the end of this argument */
- if (numBackslashes) {
- /*
- * Double the backslashes if we have a quote string
- * delimiter at the end.
- */
- if (argNeedQuotes) {
- numBackslashes *= 2;
- }
- for (i = 0; i < numBackslashes; i++) {
- *p++ = '\\';
- }
- }
- if (argNeedQuotes) {
- *p++ = '"';
- }
- if(arg == argv)
- *p++ = '\0';
+ strcat(*cmdLine, *arg);
}
-
- /* Add 2 nulls at the end */
- *p++ = '\0';
- *p = '\0';
return 0;
}
@@ -308,138 +229,160 @@ static int compare(const void *arg1, const void *arg2)
{
return stricmp(* (char**)arg1, * (char**)arg2);
}
+
PRProcess * _PR_CreateOS2Process(
const char *path,
char *const *argv,
char *const *envp,
const PRProcessAttr *attr)
{
- char szFailed[CCHMAXPATH];
- RESULTCODES procInfo;
- APIRET retVal;
- char *cmdLine = NULL;
- char *envBlock = NULL;
- char **newEnvp;
- PRProcess *proc = NULL;
- HFILE hStdIn = 0,
- hStdOut = 0,
- hStdErr = 0;
- HFILE hStdInSave = -1,
- hStdOutSave = -1,
- hStdErrSave = -1;
-
- proc = PR_NEW(PRProcess);
- if (!proc) {
- PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
- goto errorExit;
- }
-
- if (assembleCmdLine(argv, &cmdLine) == -1) {
- PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
- goto errorExit;
- }
-
- if (envp == NULL) {
- newEnvp = NULL;
- } else {
- int i;
- int numEnv = 0;
- while (envp[numEnv]) {
- numEnv++;
- }
- newEnvp = (char **) PR_MALLOC((numEnv+1) * sizeof(char *));
- for (i = 0; i <= numEnv; i++) {
- newEnvp[i] = envp[i];
- }
- qsort((void *) newEnvp, (size_t) numEnv, sizeof(char *), compare);
- }
- if (assembleEnvBlock(newEnvp, &envBlock) == -1) {
- PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
- goto errorExit;
- }
-
- if (attr) {
- /* On OS/2, there is really no way to pass file handles for stdin, stdout,
- * and stderr to a new process. Instead, we can make it a child process
- * and make the given file handles a copy of our stdin, stdout, and stderr.
- * The child process then inherits ours, and we set ours back. Twisted
- * and gross I know. If you know a better way, please use it.
- */
- if (attr->stdinFd) {
- hStdIn = 0;
- DosDupHandle(hStdIn, &hStdInSave);
- DosDupHandle((HFILE) attr->stdinFd->secret->md.osfd, &hStdIn);
- }
- if (attr->stdoutFd) {
- hStdOut = 1;
- DosDupHandle(hStdOut, &hStdOutSave);
- DosDupHandle((HFILE) attr->stdoutFd->secret->md.osfd, &hStdOut);
+ PRProcess *proc = NULL;
+ char *cmdLine = NULL;
+ char **newEnvp = NULL;
+ char *envBlock = NULL;
+
+ STARTDATA startData = {0};
+ APIRET rc;
+ ULONG ulAppType = 0;
+ PID pid = 0;
+ char *pEnvWPS = NULL;
+ char *pszComSpec;
+ char pszEXEName[CCHMAXPATH] = "";
+ char pszFormatString[CCHMAXPATH];
+ char pszObjectBuffer[CCHMAXPATH];
+ char *pszFormatResult = NULL;
+
+ proc = PR_NEW(PRProcess);
+ if (!proc) {
+ PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
+ goto errorExit;
+ }
+
+ if (assembleCmdLine(argv, &cmdLine) == -1) {
+ PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
+ goto errorExit;
+ }
+
+ if (envp == NULL) {
+ newEnvp = NULL;
+ } else {
+ int i;
+ int numEnv = 0;
+ while (envp[numEnv]) {
+ numEnv++;
}
- if (attr->stderrFd) {
- hStdErr = 2;
- DosDupHandle(hStdErr, &hStdErrSave);
- DosDupHandle((HFILE) attr->stderrFd->secret->md.osfd, &hStdErr);
+ newEnvp = (char **) PR_MALLOC((numEnv+1) * sizeof(char *));
+ for (i = 0; i <= numEnv; i++) {
+ newEnvp[i] = envp[i];
}
- }
-
- retVal = DosExecPgm(szFailed,
- CCHMAXPATH,
- EXEC_ASYNCRESULT,
- cmdLine,
- envBlock,
- &procInfo,
- argv[0]);
-
- /* Restore our old values. Hope this works */
- if(hStdInSave != -1){
- DosDupHandle(hStdInSave, &hStdIn);
- DosClose(hStdInSave);
- }
- if(hStdOutSave != -1){
- DosDupHandle(hStdOutSave, &hStdOut);
- DosClose(hStdOutSave);
- }
- if(hStdErrSave != -1){
- DosDupHandle(hStdErrSave, &hStdErr);
- DosClose(hStdErrSave);
- }
+ qsort((void *) newEnvp, (size_t) numEnv, sizeof(char *), compare);
+ }
+ if (assembleEnvBlock(newEnvp, &envBlock) == -1) {
+ PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
+ goto errorExit;
+ }
+
+ if (attr) {
+ PR_ASSERT(!"Not implemented");
+ }
- if (retVal != NO_ERROR) {
- /* XXX what error code? */
- PR_SetError(PR_UNKNOWN_ERROR, retVal);
+ rc = DosQueryAppType(path, &ulAppType);
+ if (rc != NO_ERROR) {
+ char *pszDot = strrchr(path, '.');
+ if (pszDot) {
+ /* If it is a CMD file, launch the users command processor */
+ if (!stricmp(pszDot, ".cmd")) {
+ rc = DosScanEnv("COMSPEC", &pszComSpec);
+ if (!rc) {
+ strcpy(pszFormatString, "/C %s %s");
+ strcpy(pszEXEName, pszComSpec);
+ ulAppType = FAPPTYP_WINDOWCOMPAT;
+ }
+ }
+ }
+ }
+ if (ulAppType == 0) {
+ PR_SetError(PR_UNKNOWN_ERROR, 0);
goto errorExit;
- }
-
- proc->md.pid = procInfo.codeTerminate;
+ }
+
+ if ((ulAppType & FAPPTYP_WINDOWAPI) == FAPPTYP_WINDOWAPI) {
+ startData.SessionType = SSF_TYPE_PM;
+ }
+ else if (ulAppType & FAPPTYP_WINDOWCOMPAT) {
+ startData.SessionType = SSF_TYPE_WINDOWABLEVIO;
+ }
+ else {
+ startData.SessionType = SSF_TYPE_DEFAULT;
+ }
+
+ if (ulAppType & (FAPPTYP_WINDOWSPROT31 | FAPPTYP_WINDOWSPROT | FAPPTYP_WINDOWSREAL))
+ {
+ strcpy(pszEXEName, "WINOS2.COM");
+ startData.SessionType = PROG_31_STDSEAMLESSVDM;
+ strcpy(pszFormatString, "/3 %s %s");
+ }
+
+ startData.InheritOpt = SSF_INHERTOPT_SHELL;
+
+ if (pszEXEName[0]) {
+ pszFormatResult = PR_MALLOC(strlen(pszFormatString)+strlen(path)+strlen(cmdLine));
+ sprintf(pszFormatResult, pszFormatString, path, cmdLine);
+ startData.PgmInputs = pszFormatResult;
+ } else {
+ strcpy(pszEXEName, path);
+ startData.PgmInputs = cmdLine;
+ }
+ startData.PgmName = pszEXEName;
+
+ startData.Length = sizeof(startData);
+ startData.Related = SSF_RELATED_INDEPENDENT;
+ startData.ObjectBuffer = pszObjectBuffer;
+ startData.ObjectBuffLen = CCHMAXPATH;
+ startData.Environment = envBlock;
+
+ rc = DosStartSession(&startData, &ulAppType, &pid);
+
+ if ((rc != NO_ERROR) && (rc != ERROR_SMG_START_IN_BACKGROUND)) {
+ PR_SetError(PR_UNKNOWN_ERROR, 0);
+ }
+
+ proc->md.pid = pid;
- PR_DELETE(cmdLine);
- if (envBlock) {
- PR_DELETE(envBlock);
- }
- return proc;
+ if (pszFormatResult) {
+ PR_DELETE(pszFormatResult);
+ }
+
+ PR_DELETE(cmdLine);
+ if (newEnvp) {
+ PR_DELETE(newEnvp);
+ }
+ if (envBlock) {
+ PR_DELETE(envBlock);
+ }
+ return proc;
errorExit:
- if (cmdLine) {
- PR_DELETE(cmdLine);
- }
- if (envBlock) {
- PR_DELETE(envBlock);
- }
- if (proc) {
- PR_DELETE(proc);
- }
- return NULL;
-
-} /* _PR_CreateWindowsProcess */
+ if (cmdLine) {
+ PR_DELETE(cmdLine);
+ }
+ if (newEnvp) {
+ PR_DELETE(newEnvp);
+ }
+ if (envBlock) {
+ PR_DELETE(envBlock);
+ }
+ if (proc) {
+ PR_DELETE(proc);
+ }
+ return NULL;
+} /* _PR_CreateOS2Process */
PRStatus _PR_DetachOS2Process(PRProcess *process)
{
- /* This is basically what they did on Windows (CloseHandle)
- * but I don't think it will do much on OS/2. A process is
- * either created as a child or not. You can't 'detach' it
- * later on.
+ /* On OS/2, a process is either created as a child or not.
+ * You can't 'detach' it later on.
*/
- DosClose(process->md.pid);
PR_DELETE(process);
return PR_SUCCESS;
}
@@ -506,14 +449,12 @@ _PR_MD_WAKEUP_CPUS( void )
PRStatus _MD_CreateFileMap(PRFileMap *fmap, PRInt64 size)
{
- PR_ASSERT(!"Not implemented");
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return PR_FAILURE;
}
PRInt32 _MD_GetMemMapAlignment(void)
{
- PR_ASSERT(!"Not implemented");
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return -1;
}
@@ -523,21 +464,18 @@ void * _MD_MemMap(
PROffset64 offset,
PRUint32 len)
{
- PR_ASSERT(!"Not implemented");
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return NULL;
}
PRStatus _MD_MemUnmap(void *addr, PRUint32 len)
{
- PR_ASSERT(!"Not implemented");
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return PR_FAILURE;
}
PRStatus _MD_CloseFileMap(PRFileMap *fmap)
{
- PR_ASSERT(!"Not implemented");
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return PR_FAILURE;
}
@@ -577,43 +515,3 @@ unsigned long _System _DLL_InitTerm( unsigned long mod_handle, unsigned long fla
return rc;
}
-#ifndef XP_OS2_VACPP
-
-PRInt32 _PR_MD_ATOMIC_SET(PRInt32 *intp, PRInt32 val)
-{
- PRInt32 result;
- asm volatile ("lock ; xchg %0, %1"
- : "=r"(result), "=m"(intp)
- : "0"(val), "m"(intp));
- return result;
-}
-
-PRInt32 _PR_MD_ATOMIC_ADD(PRInt32 *intp, PRInt32 val)
-{
- PRInt32 result;
- asm volatile ("lock ; xadd %0, %1"
- : "=r"(result), "=m"(intp)
- : "0"(val), "m"(intp));
- return result + val;
-}
-
-PRInt32 _PR_MD_ATOMIC_INCREMENT(PRInt32 *val)
-{
- PRInt32 result;
- asm volatile ("lock ; xadd %0, %1"
- : "=r"(result), "=m"(*val)
- : "0"(1), "m"(*val));
- return result + 1;
-}
-
-PRInt32 _PR_MD_ATOMIC_DECREMENT(PRInt32 *val)
-{
- PRInt32 result;
- asm volatile ("lock ; xadd %0, %1"
- : "=r"(result), "=m"(*val)
- : "0"(1), "m"(*val));
- return result - 1;
-}
-
-#endif
-
diff --git a/pr/src/md/os2/os2poll.c b/pr/src/md/os2/os2poll.c
index 12f199f6..6accbfc8 100644
--- a/pr/src/md/os2/os2poll.c
+++ b/pr/src/md/os2/os2poll.c
@@ -37,54 +37,64 @@
*/
#ifdef XP_OS2_EMX
- #include <sys/time.h> /* For timeval. */
+ #include <sys/time.h> /* For timeval. */
#endif
#include "primpl.h"
-PRInt32 _PR_MD_PR_POLL(
- PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
+#ifndef BSD_SELECT
+/* Utility functions called when using OS/2 select */
+
+PRBool IsSocketSet( PRInt32 osfd, int* socks, int start, int count )
+{
+ int i;
+ PRBool isSet = PR_FALSE;
+
+ for( i = start; i < start+count; i++ )
+ {
+ if( socks[i] == osfd )
+ isSet = PR_TRUE;
+ }
+
+ return isSet;
+}
+#endif
+
+PRInt32 _PR_MD_PR_POLL(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
{
- PRInt32 osfd;
- int maxfd = -1;
+#ifdef BSD_SELECT
fd_set rd, wt, ex;
+#else
+ int rd, wt, ex;
+ int* socks;
+ unsigned long msecs;
+ int i, j;
+#endif
PRFileDesc *bottom;
PRPollDesc *pd, *epd;
- PRInt32 ready, err;
- PRThread *me = _PR_MD_CURRENT_THREAD();
- struct timeval tv, *tvp = NULL;
+ PRInt32 maxfd = -1, ready, err;
+ PRIntervalTime remaining, elapsed, start;
- /*
- * For restarting _MD_SELECT() if it is interrupted by a signal.
- * We use these variables to figure out how much time has elapsed
- * and how much of the timeout still remains.
- */
- PRIntervalTime start, elapsed, remaining;
+#ifdef BSD_SELECT
+ struct timeval tv, *tvp = NULL;
- if (_PR_PENDING_INTERRUPT(me))
+ FD_ZERO(&rd);
+ FD_ZERO(&wt);
+ FD_ZERO(&ex);
+#else
+ rd = 0;
+ wt = 0;
+ ex = 0;
+ socks = (int) PR_MALLOC( npds * 3 * sizeof(int) );
+
+ if (!socks)
{
- me->flags &= ~_PR_INTERRUPT;
- PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
+ PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
return -1;
}
+#endif
- /*
- ** Is it an empty set? If so, just sleep for the timeout and return
- */
- if (0 == npds)
- {
- PR_Sleep(timeout);
- return 0;
- }
-
- remaining = timeout;
- start = PR_IntervalNow();
-
- FD_ZERO(&rd);
- FD_ZERO(&wt);
- FD_ZERO(&ex);
-
- ready = 0;
+ ready = 0;
for (pd = pds, epd = pd + npds; pd < epd; pd++)
{
PRInt16 in_flags_read = 0, in_flags_write = 0;
@@ -95,19 +105,17 @@ PRInt32 _PR_MD_PR_POLL(
if (pd->in_flags & PR_POLL_READ)
{
in_flags_read = (pd->fd->methods->poll)(
- pd->fd, (PRInt16)(pd->in_flags & ~PR_POLL_WRITE),
- &out_flags_read);
+ pd->fd, pd->in_flags & ~PR_POLL_WRITE, &out_flags_read);
}
if (pd->in_flags & PR_POLL_WRITE)
{
in_flags_write = (pd->fd->methods->poll)(
- pd->fd, (PRInt16)(pd->in_flags & ~PR_POLL_READ),
- &out_flags_write);
+ pd->fd, pd->in_flags & ~PR_POLL_READ, &out_flags_write);
}
- if ((0 != (in_flags_read & out_flags_read))
- || (0 != (in_flags_write & out_flags_write)))
+ if ((0 != (in_flags_read & out_flags_read)) ||
+ (0 != (in_flags_write & out_flags_write)))
{
- /* this one's ready right now (buffered input) */
+ /* this one's ready right now */
if (0 == ready)
{
/*
@@ -128,37 +136,67 @@ PRInt32 _PR_MD_PR_POLL(
else
{
pd->out_flags = 0; /* pre-condition */
+
/* make sure this is an NSPR supported stack */
bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
PR_ASSERT(NULL != bottom); /* what to do about that? */
- if ((NULL != bottom)
- && (_PR_FILEDESC_OPEN == bottom->secret->state))
+ if ((NULL != bottom) &&
+ (_PR_FILEDESC_OPEN == bottom->secret->state))
{
if (0 == ready)
{
- osfd = bottom->secret->md.osfd;
- if (osfd > maxfd) maxfd = osfd;
+ PRInt32 osfd = bottom->secret->md.osfd;
+ if (osfd > maxfd)
+ maxfd = osfd;
if (in_flags_read & PR_POLL_READ)
{
pd->out_flags |= _PR_POLL_READ_SYS_READ;
+#ifdef BSD_SELECT
FD_SET(osfd, &rd);
+#else
+ socks[rd] = osfd;
+ rd++;
+#endif
}
if (in_flags_read & PR_POLL_WRITE)
{
pd->out_flags |= _PR_POLL_READ_SYS_WRITE;
+#ifdef BSD_SELECT
FD_SET(osfd, &wt);
+#else
+ socks[npds+wt] = osfd;
+ wt++;
+#endif
}
if (in_flags_write & PR_POLL_READ)
{
pd->out_flags |= _PR_POLL_WRITE_SYS_READ;
+#ifdef BSD_SELECT
FD_SET(osfd, &rd);
+#else
+ socks[rd] = osfd;
+ rd++;
+#endif
}
if (in_flags_write & PR_POLL_WRITE)
{
pd->out_flags |= _PR_POLL_WRITE_SYS_WRITE;
+#ifdef BSD_SELECT
FD_SET(osfd, &wt);
+#else
+ socks[npds+wt] = osfd;
+ wt++;
+#endif
+ }
+ if (pd->in_flags & PR_POLL_EXCEPT)
+ {
+#ifdef BSD_SELECT
+ FD_SET(osfd, &ex);
+#else
+ socks[npds*2+ex] = osfd;
+ ex++;
+#endif
}
- if (pd->in_flags & PR_POLL_EXCEPT) FD_SET(osfd, &ex);
}
}
else
@@ -176,36 +214,78 @@ PRInt32 _PR_MD_PR_POLL(
}
}
}
+ else
+ {
+ pd->out_flags = 0;
+ }
}
- if (0 != ready) return ready; /* no need to block */
+ if (0 != ready)
+ {
+#ifndef BSD_SELECT
+ PR_Free(socks);
+#endif
+ return ready; /* no need to block */
+ }
+
+ remaining = timeout;
+ start = PR_IntervalNow();
retry:
+#ifdef BSD_SELECT
if (timeout != PR_INTERVAL_NO_TIMEOUT)
{
PRInt32 ticksPerSecond = PR_TicksPerSecond();
tv.tv_sec = remaining / ticksPerSecond;
- tv.tv_usec = remaining - (ticksPerSecond * tv.tv_sec);
- tv.tv_usec = (PR_USEC_PER_SEC * tv.tv_usec) / ticksPerSecond;
+ tv.tv_usec = PR_IntervalToMicroseconds( remaining % ticksPerSecond );
tvp = &tv;
}
- ready = _MD_SELECT(maxfd + 1, &rd, &wt, &ex, tvp);
+ ready = bsdselect(maxfd + 1, &rd, &wt, &ex, tvp);
+#else
+ switch (timeout)
+ {
+ case PR_INTERVAL_NO_WAIT:
+ msecs = 0;
+ break;
+ case PR_INTERVAL_NO_TIMEOUT:
+ msecs = -1;
+ break;
+ default:
+ msecs = PR_IntervalToMilliseconds(remaining);
+ }
+
+ /* compact array */
+ for( i = rd, j = npds; j < npds+wt; i++,j++ )
+ socks[i] = socks[j];
+ for( i = rd+wt, j = npds*2; j < npds*2+ex; i++,j++ )
+ socks[i] = socks[j];
+
+ ready = os2_select(socks, rd, wt, ex, msecs);
+#endif
+
if (ready == -1 && errno == EINTR)
{
- if (timeout == PR_INTERVAL_NO_TIMEOUT) goto retry;
- else
+ if (timeout == PR_INTERVAL_NO_TIMEOUT)
+ goto retry;
+ else
{
- elapsed = (PRIntervalTime) (PR_IntervalNow() - start);
- if (elapsed > timeout) ready = 0; /* timed out */
- else
+ elapsed = (PRIntervalTime) (PR_IntervalNow() - start);
+ if (elapsed > timeout)
+ ready = 0; /* timed out */
+ else
{
- remaining = timeout - elapsed;
- goto retry;
+ remaining = timeout - elapsed;
+ goto retry;
}
- }
+ }
}
+ /*
+ ** Now to unravel the select sets back into the client's poll
+ ** descriptor list. Is this possibly an area for pissing away
+ ** a few cycles or what?
+ */
if (ready > 0)
{
ready = 0;
@@ -214,26 +294,44 @@ retry:
PRInt16 out_flags = 0;
if ((NULL != pd->fd) && (0 != pd->in_flags))
{
+ PRInt32 osfd;
bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
PR_ASSERT(NULL != bottom);
osfd = bottom->secret->md.osfd;
+#ifdef BSD_SELECT
if (FD_ISSET(osfd, &rd))
+#else
+ if( IsSocketSet(osfd, socks, 0, rd) )
+#endif
{
if (pd->out_flags & _PR_POLL_READ_SYS_READ)
out_flags |= PR_POLL_READ;
if (pd->out_flags & _PR_POLL_WRITE_SYS_READ)
out_flags |= PR_POLL_WRITE;
- }
+ }
+
+#ifdef BSD_SELECT
if (FD_ISSET(osfd, &wt))
+#else
+ if( IsSocketSet(osfd, socks, rd, wt) )
+#endif
{
if (pd->out_flags & _PR_POLL_READ_SYS_WRITE)
out_flags |= PR_POLL_READ;
if (pd->out_flags & _PR_POLL_WRITE_SYS_WRITE)
out_flags |= PR_POLL_WRITE;
+ }
+
+#ifdef BSD_SELECT
+ if (FD_ISSET(osfd, &ex))
+#else
+ if( IsSocketSet(osfd, socks, rd+wt, ex) )
+#endif
+ {
+ out_flags |= PR_POLL_EXCEPT;
}
- if (FD_ISSET(osfd, &ex)) out_flags |= PR_POLL_EXCEPT;
}
pd->out_flags = out_flags;
if (out_flags) ready++;
@@ -242,185 +340,40 @@ retry:
}
else if (ready < 0)
{
- err = _MD_ERRNO();
- if (err == EBADF)
+ err = _MD_ERRNO();
+ if (err == EBADF)
{
- /* Find the bad fds */
- ready = 0;
- for (pd = pds, epd = pd + npds; pd < epd; pd++)
+ /* Find the bad fds */
+ int optval;
+ int optlen = sizeof(optval);
+ ready = 0;
+ for (pd = pds, epd = pd + npds; pd < epd; pd++)
{
- int optval;
- int optlen = sizeof(optval);
- pd->out_flags = 0;
- if ((NULL == pd->fd) || (pd->in_flags == 0)) continue;
- bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
- if (getsockopt(bottom->secret->md.osfd, SOL_SOCKET,
- SO_TYPE, (char *) &optval, &optlen) == -1)
+ pd->out_flags = 0;
+ if ((NULL != pd->fd) && (0 != pd->in_flags))
{
- PR_ASSERT(_MD_ERRNO() == ENOTSOCK);
- if (_MD_ERRNO() == ENOTSOCK)
+ bottom = PR_GetIdentitiesLayer(pd->fd, PR_NSPR_IO_LAYER);
+ if (getsockopt(bottom->secret->md.osfd, SOL_SOCKET,
+ SO_TYPE, (char *) &optval, &optlen) == -1)
{
- pd->out_flags = PR_POLL_NVAL;
- ready++;
+ PR_ASSERT(sock_errno() == ENOTSOCK);
+ if (sock_errno() == ENOTSOCK)
+ {
+ pd->out_flags = PR_POLL_NVAL;
+ ready++;
+ }
}
}
- }
- PR_ASSERT(ready > 0);
- }
- else
- {
- PR_ASSERT(err != EINTR); /* should have been handled above */
- _PR_MD_MAP_SELECT_ERROR(err);
+ }
+ PR_ASSERT(ready > 0);
}
- }
- return ready;
- }
-
-#ifdef XP_OS2_EMX
-HMTX thread_select_mutex = 0; /* because EMX's select is not thread safe - duh! */
-
-typedef struct _thread_select_st {
- int nfds;
- int isrdfds;
- struct _fd_set *readfds;
- int iswrfds;
- struct _fd_set *writefds;
- int isexfds;
- struct _fd_set *exceptfds;
- int istimeout;
- struct timeval timeout;
- volatile HEV event;
- int result;
- int select_errno;
- volatile int done;
-} *pthread_select_t;
-
-void _thread_select(void * arg)
-{
- pthread_select_t self = arg;
- int result, chkstdin;
- struct _fd_set readfds;
- struct _fd_set writefds;
- struct _fd_set exceptfds;
- HEV event = self->event;
-
- chkstdin = (self->isrdfds && FD_ISSET(0,self->readfds))?1:0;
-
- do {
- struct timeval timeout = {0L,0L};
-
-
- if (self->isrdfds) readfds = *self->readfds;
- if (self->iswrfds) writefds = *self->writefds;
- if (self->isexfds) exceptfds = *self->exceptfds;
-
- if (chkstdin) FD_CLR(0,&readfds);
-
- if (!thread_select_mutex)
- DosCreateMutexSem(NULL,&thread_select_mutex,0,1);
- else
- DosRequestMutexSem(thread_select_mutex,SEM_INDEFINITE_WAIT);
- result = select(
- self->nfds,
- self->isrdfds?&readfds:NULL,
- self->iswrfds?&writefds:NULL,
- self->isexfds?&exceptfds:NULL,
- &timeout);
- DosReleaseMutexSem(thread_select_mutex);
-
- if (chkstdin) {
- int charcount = 0, res;
- res = ioctl(0,FIONREAD,&charcount);
- if (res==0 && charcount>0) FD_SET(0,&readfds);
- }
-
- if (result>0) {
- self->done++;
- if (self->isrdfds) *self->readfds = readfds;
- if (self->iswrfds) *self->writefds = writefds;
- if (self->isexfds) *self->exceptfds = exceptfds;
- } else
- if (result) self->done++;
- else DosSleep(1);
-
- } while (self->event!=0 && self->done==0);
-
- if (self->event) {
- self->select_errno = (result < 0)?errno:0;
- self->result = result;
- self->done = 3;
- DosPostEventSem(event);
- } else {
- self->done = 3;
- free(self);
- }
-
-}
-
-PRInt32
-_MD_SELECT(int nfds, fd_set *readfds, fd_set *writefds,
- fd_set *exceptfds, struct timeval *timeout)
-{
- pthread_select_t sel;
- HEV ev = 0;
- HTIMER timer = 0;
- int result = 0;
- APIRET rc;
- unsigned long msecs = SEM_INDEFINITE_WAIT;
-
- if (timeout) {
- if (timeout->tv_sec != 0 || timeout->tv_usec != 0)
- msecs = (timeout->tv_sec * 1000L) + (timeout->tv_usec / 1000L);
- else
- msecs = SEM_IMMEDIATE_RETURN;
- };
-
- if (!(sel = (pthread_select_t) malloc(sizeof(struct _thread_select_st)))) {
- result = -1;
- errno = ENOMEM;
- } else {
- sel->nfds = nfds;
- sel->isrdfds = readfds?1:0;
- if (sel->isrdfds) sel->readfds = readfds;
- sel->iswrfds = writefds?1:0;
- if (sel->iswrfds) sel->writefds = writefds;
- sel->isexfds = exceptfds?1:0;
- if (sel->isexfds) sel->exceptfds = exceptfds;
- sel->istimeout = timeout?1:0;
- if (sel->istimeout) sel->timeout = *timeout;
-
- rc = DosCreateEventSem(NULL,&ev,0,FALSE);
-
- sel->event = ev;
- if (msecs == SEM_IMMEDIATE_RETURN)
- sel->done = 1;
- else
- sel->done = 0;
-
- if (_beginthread(_thread_select,NULL,65536,(void *)sel) == -1) {
- result = -1; sel->event = 0;
- DosCloseEventSem(ev);
- } else {
- rc = DosWaitEventSem(ev,msecs);
- if ((!sel->done) && (msecs != SEM_IMMEDIATE_RETURN)) { /* Interrupted by other thread or timeout */
- sel->event = 0;
- result = 0;
- errno = ETIMEDOUT;
-
- } else {
- while (sel->done && sel->done != 3) {
- DosSleep(1);
- }
- sel->event = 0;
- result = sel->result;
- if (sel->select_errno) errno = sel->select_errno;
- free(sel);
- }
- rc = DosCloseEventSem(ev);
- }
- }
+ else
+ _PR_MD_MAP_SELECT_ERROR(err);
+ }
- return (result);
+#ifndef BSD_SELECT
+ PR_Free(socks);
+#endif
+ return ready;
}
-#endif
diff --git a/pr/src/md/os2/os2sock.c b/pr/src/md/os2/os2sock.c
index 529e0e5a..8830a675 100644
--- a/pr/src/md/os2/os2sock.c
+++ b/pr/src/md/os2/os2sock.c
@@ -46,9 +46,13 @@
#include "primpl.h"
#ifdef XP_OS2_EMX
- #include <sys/time.h> /* For timeval. */
+ #include <sys/time.h> /* For timeval. */
#endif
+#define _PR_INTERRUPT_CHECK_INTERVAL_SECS 5
+#define READ_FD 1
+#define WRITE_FD 2
+
void
_PR_MD_INIT_IO()
{
@@ -59,34 +63,19 @@ _PR_MD_INIT_IO()
PRInt32
-_PR_MD_SOCKET(int af, int type, int flags)
+_PR_MD_SOCKET(int domain, int type, int flags)
{
- int sock;
- PRUint32 one = 1;
- PRInt32 rv;
- PRInt32 err;
-
- sock = socket(af, type, flags);
+ PRInt32 osfd, err;
- if (sock == -1 )
- {
- int rv = sock_errno();
- soclose(sock);
- _PR_MD_MAP_SOCKET_ERROR(rv);
- return (PRInt32) -1;
- }
+ osfd = socket(domain, type, flags);
- /*
- ** Make the socket Non-Blocking
- */
- rv = ioctl( sock, FIONBIO, (char *) &one, sizeof(one));
- if ( rv != 0 )
+ if (osfd == -1)
{
err = sock_errno();
- return -1;
+ _PR_MD_MAP_SOCKET_ERROR(err);
}
- return (PRInt32)sock;
+ return(osfd);
}
/*
@@ -96,12 +85,13 @@ _PR_MD_SOCKET(int af, int type, int flags)
PRInt32
_MD_CloseSocket(PRInt32 osfd)
{
- PRInt32 rv = -1;
-
- rv = soclose((int) osfd );
- if (rv < 0)
- _PR_MD_MAP_SOCKET_ERROR(sock_errno());
+ PRInt32 rv, err;
+ rv = soclose(osfd);
+ if (rv == -1) {
+ err = sock_errno();
+ _PR_MD_MAP_CLOSE_ERROR(err);
+ }
return rv;
}
@@ -110,263 +100,283 @@ _MD_SocketAvailable(PRFileDesc *fd)
{
PRInt32 result;
- if (ioctl(fd->secret->md.osfd, FIONREAD, (char *) &result, sizeof(result)) < 0) {
+ if (so_ioctl(fd->secret->md.osfd, FIONREAD, (char *) &result, sizeof(result)) < 0) {
PR_SetError(PR_BAD_DESCRIPTOR_ERROR, sock_errno());
return -1;
}
return result;
}
-PRInt32
-_MD_Accept(PRFileDesc *fd, PRNetAddr *raddr, PRUint32 *rlen,
- PRIntervalTime timeout )
+static PRInt32
+socket_io_wait( PRInt32 osfd, PRInt32 fd_type, PRIntervalTime timeout )
{
- PRInt32 osfd = fd->secret->md.osfd;
- PRInt32 rv, err;
+ PRInt32 rv = -1;
+ PRThread *me = _PR_MD_CURRENT_THREAD();
+ PRIntervalTime epoch, now, elapsed, remaining;
+ PRBool wait_for_remaining;
+ PRInt32 syserror;
#ifdef BSD_SELECT
- fd_set rd;
- struct timeval tv, *tvp;
-
- FD_ZERO(&rd);
- FD_SET(osfd, &rd);
+ struct timeval tv;
+ fd_set rd_wr;
#else
int socks[1];
- socks[0] = osfd;
+ long lTimeout;
#endif
- if (timeout == PR_INTERVAL_NO_TIMEOUT)
- {
- while ((rv = accept(osfd, (struct sockaddr *) raddr, (int *) rlen)) == -1)
- {
- if (((err = sock_errno()) == EWOULDBLOCK)
- && (!fd->secret->nonblocking))
- {
+
+ switch (timeout) {
+ case PR_INTERVAL_NO_WAIT:
+ PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+ break;
+ case PR_INTERVAL_NO_TIMEOUT:
+ /*
+ * This is a special case of the 'default' case below.
+ * Please see the comments there.
+ */
#ifdef BSD_SELECT
- if ((rv = _MD_SELECT(osfd + 1, &rd, NULL, NULL,NULL)) == -1) {
+ tv.tv_sec = _PR_INTERRUPT_CHECK_INTERVAL_SECS;
+ tv.tv_usec = 0;
+ FD_ZERO(&rd_wr);
+ do {
+ FD_SET(osfd, &rd_wr);
+ if (fd_type == READ_FD)
+ rv = bsdselect(osfd + 1, &rd_wr, NULL, NULL, &tv);
+ else
+ rv = bsdselect(osfd + 1, NULL, &rd_wr, NULL, &tv);
#else
- if ((rv = _MD_SELECT(socks, 1, 0, 0, -1)) == -1) {
-#endif
- _PR_MD_MAP_SELECT_ERROR(sock_errno());
+ lTimeout = _PR_INTERRUPT_CHECK_INTERVAL_SECS * 1000;
+ do {
+ socks[0] = osfd;
+ if (fd_type == READ_FD)
+ rv = os2_select(socks, 1, 0, 0, lTimeout);
+ else
+ rv = os2_select(socks, 0, 1, 0, lTimeout);
+#endif
+ if (rv == -1 && (syserror = sock_errno()) != EINTR) {
+ _PR_MD_MAP_SELECT_ERROR(syserror);
break;
- }
- }
- else {
- _PR_MD_MAP_ACCEPT_ERROR(err);
- break;
- }
- }
- return(rv);
- }
- else if (timeout == PR_INTERVAL_NO_WAIT)
- {
- if ((rv = accept(osfd, (struct sockaddr *) raddr, (int *) rlen)) == -1)
- {
- if (((err = sock_errno()) == EWOULDBLOCK)
- && (!fd->secret->nonblocking))
- {
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
- }
- else
- {
- _PR_MD_MAP_ACCEPT_ERROR(err);
- }
- }
- return(rv);
- }
- else
- {
-retry:
- if ((rv = accept(osfd, (struct sockaddr *) raddr, (int *) rlen)) == -1)
- {
- if (((err = sock_errno()) == EWOULDBLOCK)
- && (!fd->secret->nonblocking))
- {
+ }
+ if (_PR_PENDING_INTERRUPT(me)) {
+ me->flags &= ~_PR_INTERRUPT;
+ PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
+ rv = -1;
+ break;
+ }
+ } while (rv == 0 || (rv == -1 && syserror == EINTR));
+ break;
+ default:
+ now = epoch = PR_IntervalNow();
+ remaining = timeout;
+#ifdef BSD_SELECT
+ FD_ZERO(&rd_wr);
+#endif
+ do {
+ /*
+ * We block in select for at most
+ * _PR_INTERRUPT_CHECK_INTERVAL_SECS seconds,
+ * so that there is an upper limit on the delay
+ * before the interrupt bit is checked.
+ */
#ifdef BSD_SELECT
- tv.tv_sec = PR_IntervalToSeconds(timeout);
- tv.tv_usec = PR_IntervalToMicroseconds(timeout - PR_SecondsToInterval(tv.tv_sec));
- tvp = &tv;
- rv = _MD_SELECT(osfd + 1, &rd, NULL, NULL, tvp);
+ wait_for_remaining = PR_TRUE;
+ tv.tv_sec = PR_IntervalToSeconds(remaining);
+ if (tv.tv_sec > _PR_INTERRUPT_CHECK_INTERVAL_SECS) {
+ wait_for_remaining = PR_FALSE;
+ tv.tv_sec = _PR_INTERRUPT_CHECK_INTERVAL_SECS;
+ tv.tv_usec = 0;
+ } else {
+ tv.tv_usec = PR_IntervalToMicroseconds(
+ remaining -
+ PR_SecondsToInterval(tv.tv_sec));
+ }
+ FD_SET(osfd, &rd_wr);
+ if (fd_type == READ_FD)
+ rv = bsdselect(osfd + 1, &rd_wr, NULL, NULL, &tv);
+ else
+ rv = bsdselect(osfd + 1, NULL, &rd_wr, NULL, &tv);
#else
- long lTimeout = PR_IntervalToMilliseconds(timeout);
- rv = _MD_SELECT(socks, 1, 0, 0, lTimeout);
+ wait_for_remaining = PR_TRUE;
+ lTimeout = PR_IntervalToMilliseconds(remaining);
+ if (lTimeout > _PR_INTERRUPT_CHECK_INTERVAL_SECS * 1000) {
+ wait_for_remaining = PR_FALSE;
+ lTimeout = _PR_INTERRUPT_CHECK_INTERVAL_SECS * 1000;
+ }
+ socks[0] = osfd;
+ if (fd_type == READ_FD)
+ rv = os2_select(socks, 1, 0, 0, lTimeout);
+ else
+ rv = os2_select(socks, 0, 1, 0, lTimeout);
#endif
- if (rv > 0) {
- goto retry;
- }
- else if (rv == 0)
- {
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+ /*
+ * we don't consider EINTR a real error
+ */
+ if (rv == -1 && (syserror = sock_errno()) != EINTR) {
+ _PR_MD_MAP_SELECT_ERROR(syserror);
+ break;
+ }
+ if (_PR_PENDING_INTERRUPT(me)) {
+ me->flags &= ~_PR_INTERRUPT;
+ PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
rv = -1;
- } else {
- _PR_MD_MAP_SELECT_ERROR(sock_errno());
+ break;
}
- } else {
- _PR_MD_MAP_ACCEPT_ERROR(err);
- }
+ /*
+ * We loop again if select timed out or got interrupted
+ * by a signal, and the timeout deadline has not passed yet.
+ */
+ if (rv == 0 || (rv == -1 && syserror == EINTR)) {
+ /*
+ * If select timed out, we know how much time
+ * we spent in blocking, so we can avoid a
+ * PR_IntervalNow() call.
+ */
+ if (rv == 0) {
+ if (wait_for_remaining) {
+ now += remaining;
+ } else {
+#ifdef BSD_SELECT
+ now += PR_SecondsToInterval(tv.tv_sec)
+ + PR_MicrosecondsToInterval(tv.tv_usec);
+#else
+ now += PR_MillisecondsToInterval(lTimeout);
+#endif
+ }
+ } else {
+ now = PR_IntervalNow();
+ }
+ elapsed = (PRIntervalTime) (now - epoch);
+ if (elapsed >= timeout) {
+ PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
+ rv = -1;
+ break;
+ } else {
+ remaining = timeout - elapsed;
+ }
+ }
+ } while (rv == 0 || (rv == -1 && syserror == EINTR));
+ break;
}
- }
return(rv);
-} /* end _MD_Accept() */
+}
+PRInt32
+_MD_Accept(PRFileDesc *fd, PRNetAddr *addr,
+ PRUint32 *addrlen, PRIntervalTime timeout)
+{
+ PRInt32 osfd = fd->secret->md.osfd;
+ PRInt32 rv, err;
+ PRThread *me = _PR_MD_CURRENT_THREAD();
+ while ((rv = accept(osfd, (struct sockaddr*) addr, (int*)addrlen)) == -1)
+ {
+ err = sock_errno();
+ if ((err == EWOULDBLOCK) || (err == ECONNABORTED))
+ {
+ if (fd->secret->nonblocking) {
+ break;
+ }
+ if ((rv = socket_io_wait(osfd, READ_FD, timeout)) < 0)
+ goto done;
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))){
+ continue;
+ } else {
+ break;
+ }
+ }
+ if (rv < 0) {
+ _PR_MD_MAP_ACCEPT_ERROR(err);
+ }
+done:
+ return(rv);
+}
PRInt32
_PR_MD_CONNECT(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen,
PRIntervalTime timeout)
{
+ PRInt32 rv, err;
+ PRThread *me = _PR_MD_CURRENT_THREAD();
PRInt32 osfd = fd->secret->md.osfd;
- PRInt32 rv;
- int err, len;
-#ifdef BSD_SELECT
-#ifdef XP_OS2//_VACPP
- fd_set wd;
-#else
- fd_set wd, ex;
-#endif #vacpp
- struct timeval tv, *tvp;
-#else
- int socks[1];
- long lTimeout = -1;
-#endif
- if ((rv = connect(osfd, (struct sockaddr *) addr, addrlen)) == -1)
+ /*
+ * We initiate the connection setup by making a nonblocking connect()
+ * call. If the connect() call fails, there are two cases we handle
+ * specially:
+ * 1. The connect() call was interrupted by a signal. In this case
+ * we simply retry connect().
+ * 2. The NSPR socket is nonblocking and connect() fails with
+ * EINPROGRESS. We first wait until the socket becomes writable.
+ * Then we try to find out whether the connection setup succeeded
+ * or failed.
+ */
+
+retry:
+ if ((rv = connect(osfd, (struct sockaddr *)addr, addrlen)) == -1)
{
err = sock_errno();
- if ((!fd->secret->nonblocking) && ((err == EINPROGRESS) || (err == EWOULDBLOCK)))
- {
-#ifdef BSD_SELECT
- if (timeout == PR_INTERVAL_NO_TIMEOUT)
- tvp = NULL;
- else
- {
- tv.tv_sec = PR_IntervalToSeconds(timeout);
- tv.tv_usec = PR_IntervalToMicroseconds(timeout - PR_SecondsToInterval(tv.tv_sec));
- tvp = &tv;
+
+ if (err == EINTR) {
+ if (_PR_PENDING_INTERRUPT(me)) {
+ me->flags &= ~_PR_INTERRUPT;
+ PR_SetError( PR_PENDING_INTERRUPT_ERROR, 0);
+ return -1;
}
+ goto retry;
+ }
- FD_ZERO(&wd);
- FD_SET(osfd, &wd);
-#ifdef XP_OS2//_VACPP
- rv = _MD_SELECT(osfd + 1, NULL, &wd, NULL, tvp);
-#else
- FD_ZERO(&ex);
- FD_SET(osfd, &ex);
- rv = _MD_SELECT(osfd + 1, NULL, &wd, &ex, tvp);
-#endif #vacpp
-#else #!bsd_select
- if (timeout == PR_INTERVAL_NO_TIMEOUT)
- lTimeout = -1;
- else
- {
- lTimeout = PR_IntervalToMilliseconds(timeout);
+ if (!fd->secret->nonblocking && (err == EINPROGRESS))
+ {
+ /*
+ * socket_io_wait() may return -1 or 1.
+ */
+
+ rv = socket_io_wait(osfd, WRITE_FD, timeout);
+ if (rv == -1) {
+ return -1;
}
-
- socks[0] = osfd;
-#ifdef XP_OS2//_VACPP
- rv = _MD_SELECT(socks, 0, 1, 0, lTimeout);
-#else
- rv = _MD_SELECT(socks, 0, 1, 1, lTimeout);
-#endif #vacpp
-#endif
- if (rv > 0)
- {
-#ifdef BSD_SELECT
-#ifdef XP_OS2//_VACPP
- if (FD_ISSET(osfd, &wd))
- {
- //DosSleep(0);
- len = sizeof(err);
- if (getsockopt(osfd, SOL_SOCKET, SO_ERROR,
- (char *) &err, &len) < 0)
- {
- _PR_MD_MAP_GETSOCKOPT_ERROR(sock_errno());
- return -1;
- }
- if (err != 0)
- {
- _PR_MD_MAP_CONNECT_ERROR(err);
- return -1;
- }
- else
- return 0; /* it's connected */
- }
- else
- return -1;
-#else
- if (FD_ISSET(osfd, &ex))
- {
- DosSleep(0);
- len = sizeof(err);
- if (getsockopt(osfd, SOL_SOCKET, SO_ERROR,
- (char *) &err, &len) < 0)
- {
- _PR_MD_MAP_GETSOCKOPT_ERROR(sock_errno());
- return -1;
- }
- if (err != 0)
- _PR_MD_MAP_CONNECT_ERROR(err);
- else
- PR_SetError(PR_UNKNOWN_ERROR, 0);
- return -1;
- }
- if (FD_ISSET(osfd, &wd))
- {
- /* it's connected */
- return 0;
- }
-#endif #vacpp
-#else #!bsd_select
- if (socks[0] == osfd)
- {
- len = sizeof(err);
- if (getsockopt(osfd, SOL_SOCKET, SO_ERROR,
- (char *) &err, &len) < 0)
- {
- _PR_MD_MAP_GETSOCKOPT_ERROR(sock_errno());
- return -1;
- }
-
- if (err != 0)
- {
- _PR_MD_MAP_CONNECT_ERROR(err);
- return -1;
- }
- else
- return 0; /* it's connected */
- }
- else
- return -1;
-#endif
+ PR_ASSERT(rv == 1);
+ if (_PR_PENDING_INTERRUPT(me)) {
+ me->flags &= ~_PR_INTERRUPT;
+ PR_SetError( PR_PENDING_INTERRUPT_ERROR, 0);
+ return -1;
}
- else if (rv == 0)
- {
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
- return(-1);
- } else if (rv < 0)
- {
- _PR_MD_MAP_SELECT_ERROR(sock_errno());
- return(-1);
+ err = _MD_os2_get_nonblocking_connect_error(osfd);
+ if (err != 0) {
+ _PR_MD_MAP_CONNECT_ERROR(err);
+ return -1;
}
- }
+ return 0;
+ }
+
_PR_MD_MAP_CONNECT_ERROR(err);
}
- return rv;
-}
+ return rv;
+} /* _MD_connect */
PRInt32
_PR_MD_BIND(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen)
{
- PRInt32 rv;
-
- rv = bind(fd->secret->md.osfd, (struct sockaddr*) &(addr->inet), addrlen);
+ PRInt32 rv, err;
+ rv = bind(fd->secret->md.osfd, (struct sockaddr *) addr, (int )addrlen);
+ if (rv < 0) {
+ err = sock_errno();
+ _PR_MD_MAP_BIND_ERROR(err);
+ }
+ return(rv);
+}
- if (rv == -1) {
- _PR_MD_MAP_BIND_ERROR(sock_errno());
- return -1;
- }
- return 0;
+PRInt32
+_PR_MD_LISTEN(PRFileDesc *fd, PRIntn backlog)
+{
+ PRInt32 rv, err;
+ rv = listen(fd->secret->md.osfd, backlog);
+ if (rv < 0) {
+ err = sock_errno();
+ _PR_MD_MAP_DEFAULT_ERROR(err);
+ }
+ return(rv);
}
@@ -376,70 +386,27 @@ _PR_MD_RECV(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,
{
PRInt32 osfd = fd->secret->md.osfd;
PRInt32 rv, err;
-#ifdef BSD_SELECT
- struct timeval tv, *tvp;
- fd_set rd;
-#else
- int socks[1];
- long lTimeout = -1;
-#endif
- int osflags;
+ PRThread *me = _PR_MD_CURRENT_THREAD();
- if (0 == flags) {
- osflags = 0;
- } else {
- PR_ASSERT(PR_MSG_PEEK == flags);
- osflags = MSG_PEEK;
- }
- while ((rv = recv( osfd, buf, amount, osflags)) == -1)
+ while ((rv = recv(osfd,buf,amount,flags)) == -1)
{
- if (((err = sock_errno()) == EWOULDBLOCK)
- && (!fd->secret->nonblocking))
- {
-#ifdef BSD_SELECT
- FD_ZERO(&rd);
- FD_SET(osfd, &rd);
- if (timeout == PR_INTERVAL_NO_TIMEOUT)
- {
- tvp = NULL;
- }
- else
- {
- tv.tv_sec = PR_IntervalToSeconds(timeout);
- tv.tv_usec = PR_IntervalToMicroseconds(
- timeout - PR_SecondsToInterval(tv.tv_sec));
- tvp = &tv;
- }
- if ((rv = _MD_SELECT(osfd + 1, &rd, NULL, NULL, tvp)) == -1)
-#else
- socks[0] = osfd;
- if (timeout == PR_INTERVAL_NO_TIMEOUT)
- {
- lTimeout = -1;
- }
- else
- {
- lTimeout = PR_IntervalToMilliseconds(timeout);
- }
- if ((rv = _MD_SELECT(socks, 1, 0, 0, lTimeout)) == -1)
-#endif
- {
- _PR_MD_MAP_SELECT_ERROR(sock_errno());
- return -1;
- }
- else if (rv == 0)
- {
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
- rv = -1;
+ err = sock_errno();
+ if ((err == EWOULDBLOCK)) {
+ if (fd->secret->nonblocking) {
break;
}
- }
- else
- {
- _PR_MD_MAP_RECV_ERROR(err);
+ if ((rv = socket_io_wait(osfd, READ_FD, timeout)) < 0)
+ goto done;
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))){
+ continue;
+ } else {
break;
}
- } /* end while() */
+ }
+ if (rv < 0) {
+ _PR_MD_MAP_RECV_ERROR(err);
+ }
+done:
return(rv);
}
@@ -449,108 +416,42 @@ _PR_MD_SEND(PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,
{
PRInt32 osfd = fd->secret->md.osfd;
PRInt32 rv, err;
-#ifdef BSD_SELECT
- struct timeval tv, *tvp;
- fd_set wd;
-#else
- int socks[1];
- long lTimeout = -1;
-#endif
- PRInt32 bytesSent = 0;
+ PRThread *me = _PR_MD_CURRENT_THREAD();
- while(bytesSent < amount )
+ while ((rv = send(osfd,buf,amount,flags)) == -1)
{
- while ((rv = send( osfd, (char *) buf, amount, 0 )) == -1)
- {
- if (((err = sock_errno()) == EWOULDBLOCK)
- && (!fd->secret->nonblocking))
- {
-#ifdef BSD_SELECT
- if ( timeout == PR_INTERVAL_NO_TIMEOUT )
- {
- tvp = NULL;
- }
- else
- {
- tv.tv_sec = PR_IntervalToSeconds(timeout);
- tv.tv_usec = PR_IntervalToMicroseconds(
- timeout - PR_SecondsToInterval(tv.tv_sec));
- tvp = &tv;
- }
- FD_ZERO(&wd);
- FD_SET(osfd, &wd);
- if ((rv = _MD_SELECT( osfd + 1, NULL, &wd, NULL,tvp)) == -1) {
-#else
- if ( timeout == PR_INTERVAL_NO_TIMEOUT )
- {
- lTimeout = -1;
- }
- else
- {
- lTimeout = PR_IntervalToMilliseconds(timeout);
- }
- socks[0] = osfd;
- if ((rv = _MD_SELECT( socks, 0, 1, 0, lTimeout)) == -1) {
-#endif
- _PR_MD_MAP_SELECT_ERROR(sock_errno());
- break;
- }
- if (rv == 0)
- {
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
- return -1;
- }
- }
- else {
- _PR_MD_MAP_SEND_ERROR(err);
- return -1;
+ err = sock_errno();
+ if ((err == EWOULDBLOCK)) {
+ if (fd->secret->nonblocking) {
+ break;
}
- }
- bytesSent += rv;
- if (fd->secret->nonblocking)
- {
+ if ((rv = socket_io_wait(osfd, WRITE_FD, timeout)) < 0)
+ goto done;
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))){
+ continue;
+ } else {
break;
}
- if ((rv >= 0) && (bytesSent < amount ))
- {
-#ifdef BSD_SELECT
- if ( timeout == PR_INTERVAL_NO_TIMEOUT )
- {
- tvp = NULL;
- }
- else
- {
- tv.tv_sec = PR_IntervalToSeconds(timeout);
- tv.tv_usec = PR_IntervalToMicroseconds(
- timeout - PR_SecondsToInterval(tv.tv_sec));
- tvp = &tv;
- }
- FD_ZERO(&wd);
- FD_SET(osfd, &wd);
- if ((rv = _MD_SELECT(osfd + 1, NULL, &wd, NULL,tvp)) == -1) {
-#else
- if ( timeout == PR_INTERVAL_NO_TIMEOUT )
- {
- lTimeout = -1;
- }
- else
- {
- lTimeout = PR_IntervalToMilliseconds(timeout);
- }
- socks[0] = osfd;
- if ((rv = _MD_SELECT(socks, 0, 1, 0,lTimeout)) == -1) {
-#endif
- _PR_MD_MAP_SELECT_ERROR(sock_errno());
- break;
- }
- if (rv == 0)
- {
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
- return -1;
- }
+ }
+
+ /*
+ * optimization; if bytes sent is less than "amount" call
+ * select before returning. This is because it is likely that
+ * the next send() call will return EWOULDBLOCK.
+ */
+ if ((!fd->secret->nonblocking) && (rv > 0) && (rv < amount)
+ && (timeout != PR_INTERVAL_NO_WAIT))
+ {
+ if (socket_io_wait(osfd, WRITE_FD, timeout)< 0) {
+ rv = -1;
+ goto done;
}
}
- return bytesSent;
+ if (rv < 0) {
+ _PR_MD_MAP_SEND_ERROR(err);
+ }
+done:
+ return(rv);
}
PRInt32
@@ -559,109 +460,29 @@ _PR_MD_SENDTO(PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags,
{
PRInt32 osfd = fd->secret->md.osfd;
PRInt32 rv, err;
- PRInt32 bytesSent = 0;
-#ifdef BSD_SELECT
- struct timeval tv, *tvp;
- fd_set wd;
-#else
- int socks[1];
- long lTimeout = -1;
-#endif
-
- while(bytesSent < amount)
+ PRThread *me = _PR_MD_CURRENT_THREAD();
+ while ((rv = sendto(osfd, buf, amount, flags,
+ (struct sockaddr *) addr, addrlen)) == -1)
{
- while ((rv = sendto( osfd, (char *) buf, amount, 0, (struct sockaddr *) addr,
- addrlen)) == -1)
- {
- if (((err = sock_errno()) == EWOULDBLOCK)
- && (!fd->secret->nonblocking))
- {
-#ifdef BSD_SELECT
- if ( timeout == PR_INTERVAL_NO_TIMEOUT )
- {
- tvp = NULL;
- }
- else
- {
- tv.tv_sec = PR_IntervalToSeconds(timeout);
- tv.tv_usec = PR_IntervalToMicroseconds(
- timeout - PR_SecondsToInterval(tv.tv_sec));
- tvp = &tv;
- }
- FD_ZERO(&wd);
- FD_SET(osfd, &wd);
- if ((rv = _MD_SELECT(osfd + 1, NULL, &wd, NULL, tvp)) == -1) {
-#else
- if ( timeout == PR_INTERVAL_NO_TIMEOUT )
- {
- lTimeout = -1;
- }
- else
- {
- lTimeout = PR_IntervalToMilliseconds(timeout);
- }
- socks[0] = osfd;
- if ((rv = _MD_SELECT(socks, 0, 1, 0, lTimeout)) == -1) {
-#endif
- _PR_MD_MAP_SELECT_ERROR(sock_errno());
- break;
- }
- if (rv == 0)
- {
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
- return -1;
- }
- }
- else {
- _PR_MD_MAP_SENDTO_ERROR(err);
- return -1;
- }
- }
- bytesSent += rv;
- if (fd->secret->nonblocking)
- {
- break;
- }
- if ((rv >= 0) && (bytesSent < amount ))
+ err = sock_errno();
+ if ((err == EWOULDBLOCK))
{
-#ifdef BSD_SELECT
- if ( timeout == PR_INTERVAL_NO_TIMEOUT )
- {
- tvp = NULL;
- }
- else
- {
- tv.tv_sec = PR_IntervalToSeconds(timeout);
- tv.tv_usec = PR_IntervalToMicroseconds(
- timeout - PR_SecondsToInterval(tv.tv_sec));
- tvp = &tv;
- }
- FD_ZERO(&wd);
- FD_SET(osfd, &wd);
- if ((rv = _MD_SELECT( osfd + 1, NULL, &wd, NULL, tvp)) == -1) {
-#else
- if ( timeout == PR_INTERVAL_NO_TIMEOUT )
- {
- lTimeout = -1;
- }
- else
- {
- lTimeout = PR_IntervalToMilliseconds(timeout);
- }
- socks[0] = osfd;
- if ((rv = _MD_SELECT( socks, 0, 1, 0, lTimeout)) == -1) {
-#endif
- _PR_MD_MAP_SELECT_ERROR(sock_errno());
+ if (fd->secret->nonblocking) {
break;
}
- if (rv == 0)
- {
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
- return -1;
- }
+ if ((rv = socket_io_wait(osfd, WRITE_FD, timeout)) < 0)
+ goto done;
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))){
+ continue;
+ } else {
+ break;
}
}
- return bytesSent;
+ if (rv < 0) {
+ _PR_MD_MAP_SENDTO_ERROR(err);
+ }
+done:
+ return(rv);
}
PRInt32
@@ -670,103 +491,86 @@ _PR_MD_RECVFROM(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,
{
PRInt32 osfd = fd->secret->md.osfd;
PRInt32 rv, err;
- PRUint32 addrlen_temp = *addrlen;
-#ifdef BSD_SELECT
- struct timeval tv, *tvp;
- fd_set rd;
-#else
- int socks[1];
- long lTimeout = -1;
-#endif
+ PRThread *me = _PR_MD_CURRENT_THREAD();
- while ((rv = recvfrom( osfd, (char *) buf, amount, 0, (struct sockaddr *) addr,
- (int *) addrlen)) == -1)
+ while( (*addrlen = PR_NETADDR_SIZE(addr)),
+ ((rv = recvfrom(osfd, buf, amount, flags,
+ (struct sockaddr *) addr, (int *)addrlen)) == -1))
{
- if (((err = sock_errno()) == EWOULDBLOCK)
- && (!fd->secret->nonblocking))
- {
-#ifdef BSD_SELECT
- if (timeout == PR_INTERVAL_NO_TIMEOUT)
- {
- tvp = NULL;
- }
- else
- {
- tv.tv_sec = PR_IntervalToSeconds(timeout);
- tv.tv_usec = PR_IntervalToMicroseconds(
- timeout - PR_SecondsToInterval(tv.tv_sec));
- tvp = &tv;
- }
- FD_ZERO(&rd);
- FD_SET(osfd, &rd);
- if ((rv = _MD_SELECT(osfd + 1, &rd, NULL, NULL, tvp)) == -1)
-#else
- if (timeout == PR_INTERVAL_NO_TIMEOUT)
- {
- lTimeout = -1;
- }
- else
- {
- lTimeout = PR_IntervalToMilliseconds(timeout);
- }
- socks[0] = osfd;
- if ((rv = _MD_SELECT(socks, 1, 0, 0, lTimeout)) == -1)
-#endif
- {
- _PR_MD_MAP_SELECT_ERROR(sock_errno());
- return -1;
- } else if (rv == 0)
- {
- PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
- rv = -1;
+ err = sock_errno();
+ if ((err == EWOULDBLOCK)) {
+ if (fd->secret->nonblocking) {
break;
}
-
- /* recvfrom blows this value away if it fails first time */
- *addrlen = addrlen_temp;
- }
- else
- {
- _PR_MD_MAP_RECVFROM_ERROR(err);
+ if ((rv = socket_io_wait(osfd, READ_FD, timeout)) < 0)
+ goto done;
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))){
+ continue;
+ } else {
break;
}
}
+ if (rv < 0) {
+ _PR_MD_MAP_RECVFROM_ERROR(err);
+ }
+done:
return(rv);
}
PRInt32
-_PR_MD_WRITEV(PRFileDesc *fd, const PRIOVec *iov, PRInt32 iov_size, PRIntervalTime timeout)
+_PR_MD_WRITEV(PRFileDesc *fd, const PRIOVec *iov, PRInt32 iov_size,
+ PRIntervalTime timeout)
{
- int index;
- int sent = 0;
- int rv;
+ PRInt32 rv, err;
+ PRThread *me = _PR_MD_CURRENT_THREAD();
+ PRInt32 index, amount = 0;
+ PRInt32 osfd = fd->secret->md.osfd;
- for (index=0; index < iov_size; index++)
- {
- rv = _PR_MD_SEND(fd, iov[index].iov_base, iov[index].iov_len, 0, timeout);
- if (rv > 0)
- sent += rv;
- if ( rv != iov[index].iov_len )
- {
- if (rv < 0)
- {
- if (fd->secret->nonblocking
- && (PR_GetError() == PR_WOULD_BLOCK_ERROR)
- && (sent > 0))
- {
- return sent;
- }
- else
- {
- return -1;
- }
+ /*
+ * Calculate the total number of bytes to be sent; needed for
+ * optimization later.
+ * We could avoid this if this number was passed in; but it is
+ * probably not a big deal because iov_size is usually small (less than
+ * 3)
+ */
+ if (!fd->secret->nonblocking) {
+ for (index=0; index<iov_size; index++) {
+ amount += iov[index].iov_len;
+ }
+ }
+
+ while ((rv = so_writev(osfd, (const struct iovec*)iov, iov_size)) == -1) {
+ err = sock_errno();
+ if ((err == EWOULDBLOCK)) {
+ if (fd->secret->nonblocking) {
+ break;
}
- /* Only a nonblocking socket can have partial sends */
- PR_ASSERT(fd->secret->nonblocking);
- return sent;
+ if ((rv = socket_io_wait(osfd, WRITE_FD, timeout))<0)
+ goto done;
+ } else if ((err == EINTR) && (!_PR_PENDING_INTERRUPT(me))){
+ continue;
+ } else {
+ break;
}
}
- return sent;
+
+ /*
+ * optimization; if bytes sent is less than "amount" call
+ * select before returning. This is because it is likely that
+ * the next writev() call will return EWOULDBLOCK.
+ */
+ if ((!fd->secret->nonblocking) && (rv > 0) && (rv < amount)
+ && (timeout != PR_INTERVAL_NO_WAIT)) {
+ if (socket_io_wait(osfd, WRITE_FD, timeout) < 0) {
+ rv = -1;
+ goto done;
+ }
+ }
+ if (rv < 0) {
+ _PR_MD_MAP_WRITEV_ERROR(err);
+ }
+done:
+ return(rv);
}
PRInt32
@@ -780,64 +584,92 @@ _PR_MD_SHUTDOWN(PRFileDesc *fd, PRIntn how)
return rv;
}
+PRInt32
+_PR_MD_SOCKETPAIR(int af, int type, int flags, PRInt32 *osfd)
+{
+ PRInt32 rv, err;
+
+ rv = socketpair(af, type, flags, osfd);
+ if (rv < 0) {
+ err = _MD_ERRNO();
+ _PR_MD_MAP_SOCKETPAIR_ERROR(err);
+ }
+ return rv;
+}
+
+
PRStatus
-_PR_MD_GETSOCKNAME(PRFileDesc *fd, PRNetAddr *addr, PRUint32 *len)
+_PR_MD_GETSOCKNAME(PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen)
{
- PRInt32 rv;
+ PRInt32 rv, err;
- rv = getsockname((int)fd->secret->md.osfd, (struct sockaddr *)addr, (int *) len);
- if (rv==0)
- return PR_SUCCESS;
- else {
- _PR_MD_MAP_GETSOCKNAME_ERROR(sock_errno());
- return PR_FAILURE;
+ rv = getsockname(fd->secret->md.osfd,
+ (struct sockaddr *) addr, (int *)addrlen);
+ if (rv < 0) {
+ err = sock_errno();
+ _PR_MD_MAP_GETSOCKNAME_ERROR(err);
}
+ return rv==0?PR_SUCCESS:PR_FAILURE;
}
PRStatus
-_PR_MD_GETPEERNAME(PRFileDesc *fd, PRNetAddr *addr, PRUint32 *len)
+_PR_MD_GETPEERNAME(PRFileDesc *fd, PRNetAddr *addr, PRUint32 *addrlen)
{
- PRInt32 rv;
+ PRInt32 rv, err;
- rv = getpeername((int)fd->secret->md.osfd, (struct sockaddr *)addr, (int *) len);
- if (rv==0)
- return PR_SUCCESS;
- else {
- _PR_MD_MAP_GETPEERNAME_ERROR(sock_errno());
- return PR_FAILURE;
+ rv = getpeername(fd->secret->md.osfd,
+ (struct sockaddr *) addr, (int *)addrlen);
+ if (rv < 0) {
+ err = sock_errno();
+ _PR_MD_MAP_GETPEERNAME_ERROR(err);
}
+ return rv==0?PR_SUCCESS:PR_FAILURE;
}
PRStatus
-_PR_MD_GETSOCKOPT(PRFileDesc *fd, PRInt32 level, PRInt32 optname, char* optval, PRInt32* optlen)
+_PR_MD_GETSOCKOPT(PRFileDesc *fd, PRInt32 level, PRInt32 optname,
+ char* optval, PRInt32* optlen)
{
- PRInt32 rv;
+ PRInt32 rv, err;
- rv = getsockopt((int)fd->secret->md.osfd, level, optname, optval, optlen);
- if (rv==0)
- return PR_SUCCESS;
- else {
- _PR_MD_MAP_GETSOCKOPT_ERROR(sock_errno());
- return PR_FAILURE;
+ rv = getsockopt(fd->secret->md.osfd, level, optname, optval, (int *)optlen);
+ if (rv < 0) {
+ err = sock_errno();
+ _PR_MD_MAP_GETSOCKOPT_ERROR(err);
}
+ return rv==0?PR_SUCCESS:PR_FAILURE;
}
PRStatus
-_PR_MD_SETSOCKOPT(PRFileDesc *fd, PRInt32 level, PRInt32 optname, const char* optval, PRInt32 optlen)
+_PR_MD_SETSOCKOPT(PRFileDesc *fd, PRInt32 level, PRInt32 optname,
+ const char* optval, PRInt32 optlen)
{
- PRInt32 rv;
+ PRInt32 rv, err;
- rv = setsockopt((int)fd->secret->md.osfd, level, optname, (char *) optval, optlen);
- if (rv==0)
- return PR_SUCCESS;
- else {
- _PR_MD_MAP_SETSOCKOPT_ERROR(sock_errno());
- return PR_FAILURE;
+ rv = setsockopt(fd->secret->md.osfd, level, optname, optval, optlen);
+ if (rv < 0) {
+ err = sock_errno();
+ _PR_MD_MAP_SETSOCKOPT_ERROR(err);
}
+ return rv==0?PR_SUCCESS:PR_FAILURE;
}
void
-_MD_MakeNonblock(PRFileDesc *f)
+_MD_MakeNonblock(PRFileDesc *fd)
{
- return; /* do nothing! */
+ PRInt32 osfd = fd->secret->md.osfd;
+ PRInt32 err;
+ PRUint32 one = 1;
+
+ if (osfd <= 2) {
+ /* Don't mess around with stdin, stdout or stderr */
+ return;
+ }
+
+ err = so_ioctl( osfd, FIONBIO, (char *) &one, sizeof(one));
+ if ( err != 0 )
+ {
+ err = sock_errno();
+ _PR_MD_MAP_SOCKET_ERROR(err);
+ }
}
diff --git a/pr/src/md/os2/os2thred.c b/pr/src/md/os2/os2thred.c
index d64ae0b9..45613573 100644
--- a/pr/src/md/os2/os2thred.c
+++ b/pr/src/md/os2/os2thred.c
@@ -43,6 +43,8 @@
#include <signal.h>
#endif
+#include <float.h>
+
/* --- globals ------------------------------------------------ */
_NSPR_TLS* pThreadLocalStorage = 0;
_PRInterruptTable _pr_interruptTable[] = { { 0 } };
@@ -88,19 +90,92 @@ _pr_SetThreadMDHandle(PRThread *thread)
thread->md.handle = ptib->tib_ptib2->tib2_ultid;
}
+/* On OS/2, some system function calls seem to change the FPU control word,
+ * such that we crash with a floating underflow exception. The FIX_FPU() call
+ * in jsnum.c does not always work, as sometimes FIX_FPU() is called BEFORE the
+ * OS/2 system call that horks the FPU control word. So, we set an exception
+ * handler that covers any floating point exceptions and resets the FPU CW to
+ * the required value.
+ */
+static ULONG
+_System OS2_FloatExcpHandler(PEXCEPTIONREPORTRECORD p1,
+ PEXCEPTIONREGISTRATIONRECORD p2,
+ PCONTEXTRECORD p3,
+ PVOID pv)
+{
+#ifdef DEBUG_pedemonte
+ printf("Entering exception handler; ExceptionNum = %x\n", p1->ExceptionNum);
+ switch(p1->ExceptionNum) {
+ case XCPT_FLOAT_DENORMAL_OPERAND:
+ printf("got XCPT_FLOAT_DENORMAL_OPERAND\n");
+ break;
+ case XCPT_FLOAT_DIVIDE_BY_ZERO:
+ printf("got XCPT_FLOAT_DIVIDE_BY_ZERO\n");
+ break;
+ case XCPT_FLOAT_INEXACT_RESULT:
+ printf("got XCPT_FLOAT_INEXACT_RESULT\n");
+ break;
+ case XCPT_FLOAT_INVALID_OPERATION:
+ printf("got XCPT_FLOAT_INVALID_OPERATION\n");
+ break;
+ case XCPT_FLOAT_OVERFLOW:
+ printf("got XCPT_FLOAT_OVERFLOW\n");
+ break;
+ case XCPT_FLOAT_STACK_CHECK:
+ printf("got XCPT_FLOAT_STACK_CHECK\n");
+ break;
+ case XCPT_FLOAT_UNDERFLOW:
+ printf("got XCPT_FLOAT_UNDERFLOW\n");
+ break;
+ }
+#endif
+
+ switch(p1->ExceptionNum) {
+ case XCPT_FLOAT_DENORMAL_OPERAND:
+ case XCPT_FLOAT_DIVIDE_BY_ZERO:
+ case XCPT_FLOAT_INEXACT_RESULT:
+ case XCPT_FLOAT_INVALID_OPERATION:
+ case XCPT_FLOAT_OVERFLOW:
+ case XCPT_FLOAT_STACK_CHECK:
+ case XCPT_FLOAT_UNDERFLOW:
+ {
+ unsigned cw = p3->ctx_env[0];
+ if ((cw & MCW_EM) != MCW_EM) {
+ /* Mask out all floating point exceptions */
+ p3->ctx_env[0] |= MCW_EM;
+ /* Following two lines set precision to 53 bit mantissa. See jsnum.c */
+ p3->ctx_env[0] &= ~MCW_PC;
+ p3->ctx_env[0] |= PC_53;
+ return XCPT_CONTINUE_EXECUTION;
+ }
+ }
+ }
+ return XCPT_CONTINUE_SEARCH;
+}
+
+PR_IMPLEMENT(void)
+PR_OS2_SetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* excpreg)
+{
+ /* setup the exception handler for the thread */
+ APIRET rv;
+ excpreg->ExceptionHandler = OS2_FloatExcpHandler;
+ excpreg->prev_structure = NULL;
+ rv = DosSetExceptionHandler(excpreg);
+ PR_ASSERT(rv == NO_ERROR);
+}
+
+PR_IMPLEMENT(void)
+PR_OS2_UnsetFloatExcpHandler(EXCEPTIONREGISTRATIONRECORD* excpreg)
+{
+ /* unset exception handler */
+ APIRET rv = DosUnsetExceptionHandler(excpreg);
+ PR_ASSERT(rv == NO_ERROR);
+}
PRStatus
_PR_MD_INIT_THREAD(PRThread *thread)
{
APIRET rv;
-#ifdef XP_OS2_EMX
- /* disable SIGPIPE */
- struct sigaction sa;
- sa.sa_handler = SIG_IGN;
- sa.sa_flags = 0;
- sigemptyset( &sa.sa_mask);
- sigaction( SIGPIPE, &sa, NULL);
-#endif
if (thread->flags & (_PR_PRIMORDIAL | _PR_ATTACHED)) {
_pr_SetThreadMDHandle(thread);
@@ -111,6 +186,29 @@ _PR_MD_INIT_THREAD(PRThread *thread)
return (rv == NO_ERROR) ? PR_SUCCESS : PR_FAILURE;
}
+typedef struct param_store
+{
+ void (*start)(void *);
+ PRThread* thread;
+} PARAMSTORE;
+
+/* This is a small intermediate function that sets/unsets the exception
+ handler before calling the initial thread function */
+static void
+ExcpStartFunc(void* arg)
+{
+ EXCEPTIONREGISTRATIONRECORD excpreg;
+ PARAMSTORE params, *pParams = arg;
+
+ PR_OS2_SetFloatExcpHandler(&excpreg);
+
+ params = *pParams;
+ PR_Free(pParams);
+ params.start(params.thread);
+
+ PR_OS2_UnsetFloatExcpHandler(&excpreg);
+}
+
PRStatus
_PR_MD_CREATE_THREAD(PRThread *thread,
void (*start)(void *),
@@ -119,15 +217,25 @@ _PR_MD_CREATE_THREAD(PRThread *thread,
PRThreadState state,
PRUint32 stackSize)
{
- thread->md.handle = thread->id = (TID) _beginthread(
- (void(* _Optlink)(void*))start,
- NULL,
- thread->stack->stackSize,
- thread);
+ PARAMSTORE* params = PR_Malloc(sizeof(PARAMSTORE));
+ params->start = start;
+ params->thread = thread;
+ thread->md.handle = thread->id = (TID) _beginthread(ExcpStartFunc,
+ NULL,
+ thread->stack->stackSize,
+ params);
if(thread->md.handle == -1) {
return PR_FAILURE;
}
- _PR_MD_SET_PRIORITY(&(thread->md), priority);
+
+ /*
+ * On OS/2, a thread is created with a thread priority of
+ * THREAD_PRIORITY_NORMAL
+ */
+
+ if (priority != PR_PRIORITY_NORMAL) {
+ _PR_MD_SET_PRIORITY(&(thread->md), priority);
+ }
return PR_SUCCESS;
}
@@ -270,3 +378,19 @@ _PR_MD_RESUME_THREAD(PRThread *thread)
}
}
+
+PRThread*
+_MD_CURRENT_THREAD(void)
+{
+ PRThread *thread;
+
+ thread = _MD_GET_ATTACHED_THREAD();
+
+ if (NULL == thread) {
+ thread = _PRI_AttachThread(PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL, 0);
+ }
+
+ PR_ASSERT(thread != NULL);
+ return thread;
+}
+
diff --git a/pr/src/md/os2/os2vaclegacy.s b/pr/src/md/os2/os2vaclegacy.s
new file mode 100644
index 00000000..106df3de
--- /dev/null
+++ b/pr/src/md/os2/os2vaclegacy.s
@@ -0,0 +1,74 @@
+/ -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+/
+/ The contents of this file are subject to the Mozilla Public
+/ License Version 1.1 (the "License"); you may not use this file
+/ except in compliance with the License. You may obtain a copy of
+/ the License at http://www.mozilla.org/MPL/
+/
+/ Software distributed under the License is distributed on an "AS
+/ IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+/ implied. See the License for the specific language governing
+/ rights and limitations under the License.
+/
+/ The Original Code is the Netscape Portable Runtime (NSPR).
+/
+/ The Initial Developer of the Original Code is InnoTek
+/ Systemberatung GmbH.
+/ Portions created by the Initial Developer are Copyright (C) 2003
+/ the Initial Developer. All Rights Reserved.
+/
+/ Contributor(s):
+/ InnoTek Systemberatung GmbH / Knut St. Osmundsen
+/
+/ Alternatively, the contents of this file may be used under the
+/ terms of the GNU General Public License Version 2 or later (the
+/ "GPL"), in which case the provisions of the GPL are applicable
+/ instead of those above. If you wish to allow use of your
+/ version of this file only under the terms of the GPL and not to
+/ allow others to use your version of this file under the MPL,
+/ indicate your decision by deleting the provisions above and
+/ replace them with the notice and other provisions required by
+/ the GPL. If you do not delete the provisions above, a recipient
+/ may use your version of this file under either the MPL or the
+/ GPL.
+/
+
+ .text
+ .align 4
+ .globl PR_NewMonitor
+PR_NewMonitor:
+ jmp _PR_NewMonitor
+
+ .align 4
+ .globl PR_EnterMonitor
+PR_EnterMonitor:
+ mov %eax, 4(%esp)
+ jmp _PR_EnterMonitor
+
+ .align 4
+ .globl PR_ExitMonitor
+PR_ExitMonitor:
+ mov %eax, 4(%esp)
+ jmp _PR_ExitMonitor
+
+
+
+ .align 4
+ .globl PR_AttachThread
+PR_AttachThread:
+ mov %eax, 4(%esp)
+ mov %edx, 8(%esp)
+ mov %ecx, 12(%esp)
+ jmp _PR_AttachThread
+
+ .align 4
+ .globl PR_DetachThread
+PR_DetachThread:
+ jmp _PR_DetachThread
+
+ .align 4
+ .globl PR_GetCurrentThread
+PR_GetCurrentThread:
+ jmp _PR_GetCurrentThread
+
+
diff --git a/pr/src/md/os2/os2vacpp.asm b/pr/src/md/os2/os2vacpp.asm
index 2581323b..6b49e407 100644
--- a/pr/src/md/os2/os2vacpp.asm
+++ b/pr/src/md/os2/os2vacpp.asm
@@ -30,59 +30,230 @@ COMMENT | -*- Mode: asm; tab-width: 8; c-basic-offset: 4 -*-
GPL.
Windows uses inline assembly for their atomic functions, so we have
- created an assembly file for VACPP on OS/2
+ created an assembly file for VACPP on OS/2.
+
+ This assembly file also contains an implementation of RAM semaphores.
+
+ Notes:
+ The ulTIDPID element of the RAMSEM structure is overloaded in the 386
+ implementation to hold the TID:PID in the lower 31 bits and the lock
+ bit in the high bit
|
+ page ,132
.486P
- .MODEL FLAT, OPTLINK
- .STACK
+ ASSUME CS:FLAT, DS:FLAT, SS:FLAT, ES:FLAT, FS:FLAT
+
+ EXTRN Dos32PostEventSem:PROC
+ EXTRN Dos32WaitEventSem:PROC
+ EXTRN Dos32ResetEventSem:PROC
+
+ramsem STRUC
+ ramsem_ulTIDPID DD ?
+ ramsem_hevSem DD ?
+ ramsem_cLocks DD ?
+ ramsem_cWaiting DW ?
+ ramsem_cPosts DW ?
+ramsem ENDS
+
+ERROR_SEM_TIMEOUT equ 121
+ERROR_NOT_OWNER equ 288
+SEM_RELEASE_UNOWNED equ 1
+SEM_RELEASE_ALL equ 2
+TS_LOCKBIT equ 31
+
+
+DATA SEGMENT DWORD USE32 PUBLIC 'DATA'
+
+ EXTRN plisCurrent:DWORD
+
+DATA ENDS
+
+CODE32 SEGMENT USE32 PUBLIC 'CODE'
+
+ PUBLIC SemRequest486
+ PUBLIC SemReleasex86
+
+ PUBLIC _PR_MD_ATOMIC_SET
+ PUBLIC _PR_MD_ATOMIC_ADD
+ PUBLIC _PR_MD_ATOMIC_INCREMENT
+ PUBLIC _PR_MD_ATOMIC_DECREMENT
+
+;;;---------------------------------------------------------------------------
+;;; APIRET _Optlink SemRequest(PRAMSEM pramsem, ULONG ulTimeout);
+;;;
+;;; Registers:
+;;; EAX - packed TID:PID word
+;;; ECX - address of RAMSEM structure
+;;; EDX - length of timeout in milli-seconds
+;;;---------------------------------------------------------------------------
+
+ ALIGN 10H
+SemRequest486 PROC
+ push ebx ; Save ebx (volatile)
+ mov ecx, eax ; PRAMSEM must be in ecx,
+ ; not eax, for cmpxchg
+
+ mov ebx, dword ptr [plisCurrent]
+ mov eax, dword ptr [ebx+4] ; Place thread id in high
+ ; word, process id in low
+ mov ax, word ptr [ebx] ; word
+ mov ebx,eax
+
+req486_test:
+ xor eax,eax
+ cmp (ramsem PTR [ecx]).ramsem_ulTIDPID, ebx ; If we own the sem, just
+ jz short req486_inc_exit ; increment the use count
+
+ lock inc (ramsem PTR [ecx]).ramsem_cWaiting ; inc waiting flag
+
+; lock ; Uncomment for SMP
+ DB 0F0h
+; cmpxchg (ramsem PTR [ecx]).ramsem_ulTIDPID, ebx
+; (byte 3 is the offset of ulProcessThread into the RAMSEM structure)
+ DB 00Fh
+ DB 0B1h
+ DB 019h
+ jnz short req486_sleep
+
+req486_inc_exit:
+ lock inc (ramsem PTR [ecx]).ramsem_cLocks
+
+req486_exit:
+ pop ebx ; Restore ebx
+ ret
+
+req486_sleep:
+ push ecx ; Save ecx (volatile)
+ push edx ; Save edx (volatile)
+ push edx ; timeout
+ push (ramsem PTR [ecx]).ramsem_hevSem
+ call Dos32WaitEventSem
+ add esp, 8
+ pop edx ; restore edx
+ pop ecx ; restore ecx
+ or eax, eax
+ jne req486_exit ; Exit, if error
- .CODE
+ push ecx ; Save ecx (volatile)
+ push edx ; Save edx (volatile)
+ sub esp, 4 ; Use stack space for
+ push esp ; dummy pulPostCt
+ push (ramsem PTR [ecx]).ramsem_hevSem
+ call Dos32ResetEventSem
+ add esp, 12
+ pop edx ; restore edx
+ pop ecx ; restore ecx
+ jmp req486_test ; Retry the semaphore
+
+SemRequest486 ENDP
+
+;;;---------------------------------------------------------------------
+;;; APIRET _Optlink SemReleasex86(PRAMSEM pramsem, ULONG flFlags);
+;;;
+;;; Registers:
+;;; EAX - address of RAMSEM structure
+;;; ECX - temporary variable
+;;; EDX - flags
+;;;---------------------------------------------------------------------
+
+ ALIGN 10H
+SemReleasex86 PROC
+ test edx, SEM_RELEASE_UNOWNED ; If set, don't bother
+ jnz short rel_ownerok ; getting/checking PID/TID
+
+ push ebx ; Save ebx (volatile)
+ mov ebx, dword ptr [plisCurrent]
+ mov ecx, dword ptr [ebx+4] ; Place thread id in high
+ ; word, process id in low
+ mov cx, word ptr [ebx] ; word
+ pop ebx ; Restore ebx
+
+ sub ecx, (ramsem PTR [eax]).ramsem_ulTIDPID ; This thread the owner?
+ shl ecx,1 ; Don't compare top bit
+ jnz short rel_notowner
+
+rel_ownerok:
+ test edx, SEM_RELEASE_ALL
+ jnz short rel_clear
+
+ lock dec (ramsem PTR [eax]).ramsem_cLocks
+ jnz short rel_exit
+
+rel_disown:
+ mov (ramsem PTR [eax]).ramsem_ulTIDPID, 0
+
+ lock inc (ramsem PTR [eax]).ramsem_cPosts
+ mov cx, (ramsem PTR [eax]).ramsem_cWaiting
+ cmp (ramsem PTR [eax]).ramsem_cPosts, cx
+ jne short rel_post
+
+rel_exit:
+ xor eax, eax
+ ret
+
+rel_clear:
+ lock mov (ramsem PTR [eax]).ramsem_cLocks,0
+ jmp rel_disown
+
+rel_notowner:
+ mov eax, ERROR_NOT_OWNER
+ ret
+
+rel_post:
+ mov (ramsem PTR [eax]).ramsem_cPosts, cx
+ push (ramsem PTR [eax]).ramsem_hevSem
+ call Dos32PostEventSem
+ add esp,4
+ xor eax,eax
+ ret
+SemReleasex86 ENDP
;;;---------------------------------------------------------------------
;;; PRInt32 _Optlink _PR_MD_ATOMIC_SET(PRInt32* val, PRInt32 newval)
;;;---------------------------------------------------------------------
-_PR_MD_ATOMIC_SET PROC OPTLINK EXPORT
- lock xchg dword ptr [eax],edx
+ ALIGN 10H
+_PR_MD_ATOMIC_SET proc
+ lock xchg dword ptr [eax],edx
mov eax, edx;
-
ret
_PR_MD_ATOMIC_SET endp
;;;---------------------------------------------------------------------
;;; PRInt32 _Optlink _PR_MD_ATOMIC_ADD(PRInt32* ptr, PRInt32 val)
;;;---------------------------------------------------------------------
-_PR_MD_ATOMIC_ADD PROC OPTLINK EXPORT
+ ALIGN 10H
+_PR_MD_ATOMIC_ADD proc
mov ecx, edx
lock xadd dword ptr [eax], edx
mov eax, edx
add eax, ecx
-
ret
_PR_MD_ATOMIC_ADD endp
;;;---------------------------------------------------------------------
;;; PRInt32 _Optlink _PR_MD_ATOMIC_INCREMENT(PRInt32* val)
;;;---------------------------------------------------------------------
-_PR_MD_ATOMIC_INCREMENT PROC OPTLINK EXPORT
+ ALIGN 10H
+_PR_MD_ATOMIC_INCREMENT proc
mov edx, 1
lock xadd dword ptr [eax], edx
mov eax, edx
inc eax
-
ret
_PR_MD_ATOMIC_INCREMENT endp
;;;---------------------------------------------------------------------
;;; PRInt32 _Optlink _PR_MD_ATOMIC_DECREMENT(PRInt32* val)
;;;---------------------------------------------------------------------
-_PR_MD_ATOMIC_DECREMENT PROC OPTLINK EXPORT
+ ALIGN 10H
+_PR_MD_ATOMIC_DECREMENT proc
mov edx, 0ffffffffh
lock xadd dword ptr [eax], edx
mov eax, edx
dec eax
-
ret
_PR_MD_ATOMIC_DECREMENT endp
- END
+CODE32 ENDS
+END
diff --git a/pr/src/md/unix/Makefile.in b/pr/src/md/unix/Makefile.in
index aa78f21b..8c5dfe80 100644
--- a/pr/src/md/unix/Makefile.in
+++ b/pr/src/md/unix/Makefile.in
@@ -76,6 +76,8 @@ ifeq ($(OS_ARCH),SunOS)
ULTRASPARC_ASOBJS = $(addprefix $(OBJDIR)/,$(ULTRASPARC_ASFILES:.s=.$(OBJ_SUFFIX)))
TARGETS += $(ULTRASPARC_ASOBJS) $(SHARED_LIBRARY)
RELEASE_LIBS = $(SHARED_LIBRARY)
+ RELEASE_LIBS_DEST = $(RELEASE_LIB_DIR)/cpu/sparcv8plus
+ lib_subdir = cpu/sparcv8plus
endif
endif
endif
@@ -98,8 +100,24 @@ $(ULTRASPARC_ASOBJS): $(ULTRASPARC_ASFILES)
/usr/ccs/bin/as -o $@ -K PIC -P -D_ASM -D__STDC__=0 -xarch=v9 $<
else
$(SHARED_LIBRARY): $(ULTRASPARC_ASOBJS)
- $(LD) -G -z text -o $@ $(ULTRASPARC_ASOBJS)
- $(INSTALL) -m 444 $@ $(dist_libdir)
+ $(LD) -G -z text -z endfiltee -o $@ $(ULTRASPARC_ASOBJS)
+ $(INSTALL) -m 444 $@ $(dist_libdir)/cpu/sparcv8plus
+ $(INSTALL) -m 444 $@ $(dist_bindir)/cpu/sparcv8plus
+#
+# The -f $(ORIGIN)/... linker flag uses the real file, after symbolic links
+# are resolved, as the origin. If NSDISTMODE is not "copy", libnspr4.so
+# will be installed as a symbolic link in $(dist_libdir), pointing to the
+# real libnspr4.so file in pr/src. Therefore we need to install an
+# additional copy of libnspr_flt4.so in pr/src/cpu/sparcv8plus.
+#
+ifneq ($(NSDISTMODE),copy)
+ $(INSTALL) -m 444 $@ ../../cpu/sparcv8plus
+endif
+
+ifneq ($(NSDISTMODE),copy)
+clobber realclean clobber_all distclean::
+ rm -rf ../../cpu
+endif
$(ULTRASPARC_ASOBJS): $(ULTRASPARC_ASFILES)
/usr/ccs/bin/as -o $@ -K PIC -P -D_ASM -D__STDC__=0 -xarch=v8plus $<
diff --git a/pr/src/md/unix/darwin.c b/pr/src/md/unix/darwin.c
index 43988b8d..82208c61 100644
--- a/pr/src/md/unix/darwin.c
+++ b/pr/src/md/unix/darwin.c
@@ -82,11 +82,11 @@ _MD_WAKEUP_WAITER(PRThread *thread)
return PR_SUCCESS;
}
-/* These functions should not be called for Rhapsody */
+/* These functions should not be called for Darwin */
void
_MD_YIELD(void)
{
- PR_NOT_REACHED("_MD_YIELD should not be called for Rhapsody.");
+ PR_NOT_REACHED("_MD_YIELD should not be called for Darwin.");
}
PRStatus
@@ -98,37 +98,10 @@ _MD_CREATE_THREAD(
PRThreadState state,
PRUint32 stackSize)
{
- PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for Rhapsody.");
+ PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for Darwin.");
return PR_FAILURE;
}
#endif /* ! _PR_PTHREADS */
-#if defined(_PR_PTHREADS)
-
-/*
-** Stubs for unimplemented functions
-*/
-
-int pthread_condattr_init(pthread_condattr_t *attr)
-{
- return 0;
-}
-
-int pthread_kill(pthread_t thread, int sig)
-{
- return ENOSYS;
-}
-
-typedef struct siginfo_t siginfo_t;
-
-int sigtimedwait(const sigset_t *set, siginfo_t *info,
- const struct timespec *timeout)
-{
- errno = ENOSYS;
- return -1;
-}
-
-#endif /* _PR_PTHREADS */
-
/* darwin.c */
diff --git a/pr/src/md/unix/irix.c b/pr/src/md/unix/irix.c
index 7ba079f8..f3d52f01 100644
--- a/pr/src/md/unix/irix.c
+++ b/pr/src/md/unix/irix.c
@@ -1451,7 +1451,7 @@ void _MD_EarlyInit(void)
_MD_IrixIntervalInit();
} /* _MD_EarlyInit */
-void _MD_IrixInit()
+void _MD_IrixInit(void)
{
#if !defined(_PR_PTHREADS)
struct sigaction sigact;
@@ -1567,7 +1567,7 @@ static PRUint32 pr_ticks_per_second = 0;
extern PRIntervalTime _PR_UNIX_GetInterval(void);
extern PRIntervalTime _PR_UNIX_TicksPerSecond(void);
-static void _MD_IrixIntervalInit()
+static void _MD_IrixIntervalInit(void)
{
/*
* As much as I would like, the service available through this
@@ -1634,12 +1634,12 @@ static void _MD_IrixIntervalInit()
}
} /* _MD_IrixIntervalInit */
-PRIntervalTime _MD_IrixIntervalPerSec()
+PRIntervalTime _MD_IrixIntervalPerSec(void)
{
return pr_ticks_per_second;
}
-PRIntervalTime _MD_IrixGetInterval()
+PRIntervalTime _MD_IrixGetInterval(void)
{
if (mmem_fd != -1)
{
diff --git a/pr/src/md/unix/openvms.c b/pr/src/md/unix/openvms.c
index 72c20432..73d52314 100644
--- a/pr/src/md/unix/openvms.c
+++ b/pr/src/md/unix/openvms.c
@@ -204,4 +204,80 @@ int thread_resume(PRThread *thr_id) {
return 0;
}
+/*
+** Stubs for nspr_symvec.opt
+**
+** PR_ResumeSet, PR_ResumeTest, and PR_SuspendAllSuspended
+** (defined in ptthread.c) used to be exported by mistake
+** (because they look like public functions). They have been
+** converted into static functions.
+**
+** There is an existing third-party binary that uses NSPR: the
+** Java plugin for Mozilla. Because it is part of the Java
+** SDK, we have no control over its releases. So we need these
+** stub functions to occupy the slots that used to be occupied
+** by PR_ResumeSet, PR_ResumeTest, and PR_SuspendAllSuspended
+** in the symbol vector so that LIBNSPR4 is backward compatible.
+**
+** The Java plugin was also using PR_CreateThread which we didn't
+** realise and hadn't "nailed down". So we now need to nail it down
+** to its Mozilla 1.1 position and have to insert 51 additional stubs
+** in order to achive this (stubs 4-54).
+**
+** Over time some of these stubs will get reused by new symbols.
+** - Stub54 is replaced by LL_MaxUint
+*/
+void PR_VMS_Stub1(void) { }
+void PR_VMS_Stub2(void) { }
+void PR_VMS_Stub3(void) { }
+void PR_VMS_Stub4(void) { }
+void PR_VMS_Stub5(void) { }
+void PR_VMS_Stub6(void) { }
+void PR_VMS_Stub7(void) { }
+void PR_VMS_Stub8(void) { }
+void PR_VMS_Stub9(void) { }
+void PR_VMS_Stub10(void) { }
+void PR_VMS_Stub11(void) { }
+void PR_VMS_Stub12(void) { }
+void PR_VMS_Stub13(void) { }
+void PR_VMS_Stub14(void) { }
+void PR_VMS_Stub15(void) { }
+void PR_VMS_Stub16(void) { }
+void PR_VMS_Stub17(void) { }
+void PR_VMS_Stub18(void) { }
+void PR_VMS_Stub19(void) { }
+void PR_VMS_Stub20(void) { }
+void PR_VMS_Stub21(void) { }
+void PR_VMS_Stub22(void) { }
+void PR_VMS_Stub23(void) { }
+void PR_VMS_Stub24(void) { }
+void PR_VMS_Stub25(void) { }
+void PR_VMS_Stub26(void) { }
+void PR_VMS_Stub27(void) { }
+void PR_VMS_Stub28(void) { }
+void PR_VMS_Stub29(void) { }
+void PR_VMS_Stub30(void) { }
+void PR_VMS_Stub31(void) { }
+void PR_VMS_Stub32(void) { }
+void PR_VMS_Stub33(void) { }
+void PR_VMS_Stub34(void) { }
+void PR_VMS_Stub35(void) { }
+void PR_VMS_Stub36(void) { }
+void PR_VMS_Stub37(void) { }
+void PR_VMS_Stub38(void) { }
+void PR_VMS_Stub39(void) { }
+void PR_VMS_Stub40(void) { }
+void PR_VMS_Stub41(void) { }
+void PR_VMS_Stub42(void) { }
+void PR_VMS_Stub43(void) { }
+void PR_VMS_Stub44(void) { }
+void PR_VMS_Stub45(void) { }
+void PR_VMS_Stub46(void) { }
+void PR_VMS_Stub47(void) { }
+void PR_VMS_Stub48(void) { }
+void PR_VMS_Stub49(void) { }
+void PR_VMS_Stub50(void) { }
+void PR_VMS_Stub51(void) { }
+void PR_VMS_Stub52(void) { }
+void PR_VMS_Stub53(void) { }
diff --git a/pr/src/md/unix/os_Darwin_ppc.s b/pr/src/md/unix/os_Darwin_ppc.s
new file mode 100644
index 00000000..f38ac644
--- /dev/null
+++ b/pr/src/md/unix/os_Darwin_ppc.s
@@ -0,0 +1,92 @@
+# -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+#
+# The contents of this file are subject to the Mozilla Public
+# License Version 1.1 (the "License"); you may not use this file
+# except in compliance with the License. You may obtain a copy of
+# the License at http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS
+# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+# implied. See the License for the specific language governing
+# rights and limitations under the License.
+#
+# The Original Code is the Netscape Portable Runtime (NSPR).
+#
+# The Initial Developer of the Original Code is Netscape
+# Communications Corporation. Portions created by Netscape are
+# Copyright (C) 2003 Netscape Communications Corporation. All
+# Rights Reserved.
+#
+# Contributor(s):
+#
+# Alternatively, the contents of this file may be used under the
+# terms of the GNU General Public License Version 2 or later (the
+# "GPL"), in which case the provisions of the GPL are applicable
+# instead of those above. If you wish to allow use of your
+# version of this file only under the terms of the GPL and not to
+# allow others to use your version of this file under the MPL,
+# indicate your decision by deleting the provisions above and
+# replace them with the notice and other provisions required by
+# the GPL. If you do not delete the provisions above, a recipient
+# may use your version of this file under either the MPL or the
+# GPL.
+#
+
+#
+# Based on the programming examples in The PowerPC Architecture:
+# A Specification for A New Family of RISC Processors, 2nd Ed.,
+# Book I, Section E.1, "Synchronization," pp. 249-256, May 1994.
+#
+
+.text
+
+#
+# PRInt32 __PR_DarwinPPC_AtomicIncrement(PRInt32 *val);
+#
+ .align 2
+ .globl __PR_DarwinPPC_AtomicIncrement
+__PR_DarwinPPC_AtomicIncrement:
+ lwarx r4,0,r3
+ addi r0,r4,1
+ stwcx. r0,0,r3
+ bne- __PR_DarwinPPC_AtomicIncrement
+ mr r3,r0
+ blr
+
+#
+# PRInt32 __PR_DarwinPPC_AtomicDecrement(PRInt32 *val);
+#
+ .align 2
+ .globl __PR_DarwinPPC_AtomicDecrement
+__PR_DarwinPPC_AtomicDecrement:
+ lwarx r4,0,r3
+ addi r0,r4,-1
+ stwcx. r0,0,r3
+ bne- __PR_DarwinPPC_AtomicDecrement
+ mr r3,r0
+ blr
+
+#
+# PRInt32 __PR_DarwinPPC_AtomicSet(PRInt32 *val, PRInt32 newval);
+#
+ .align 2
+ .globl __PR_DarwinPPC_AtomicSet
+__PR_DarwinPPC_AtomicSet:
+ lwarx r5,0,r3
+ stwcx. r4,0,r3
+ bne- __PR_DarwinPPC_AtomicSet
+ mr r3,r5
+ blr
+
+#
+# PRInt32 __PR_DarwinPPC_AtomicAdd(PRInt32 *ptr, PRInt32 val);
+#
+ .align 2
+ .globl __PR_DarwinPPC_AtomicAdd
+__PR_DarwinPPC_AtomicAdd:
+ lwarx r5,0,r3
+ add r0,r4,r5
+ stwcx. r0,0,r3
+ bne- __PR_DarwinPPC_AtomicAdd
+ mr r3,r0
+ blr
diff --git a/pr/src/md/unix/os_Linux_x86.s b/pr/src/md/unix/os_Linux_x86.s
index 4eacdc40..d691badd 100644
--- a/pr/src/md/unix/os_Linux_x86.s
+++ b/pr/src/md/unix/os_Linux_x86.s
@@ -109,3 +109,6 @@ _PR_x86_AtomicAdd:
xaddl %eax, (%ecx)
addl %edx, %eax
ret
+
+/ Magic indicating no need for an executable stack
+.section .note.GNU-stack, "", @progbits ; .previous
diff --git a/pr/src/md/unix/os_Linux_x86_64.s b/pr/src/md/unix/os_Linux_x86_64.s
new file mode 100644
index 00000000..567ae12d
--- /dev/null
+++ b/pr/src/md/unix/os_Linux_x86_64.s
@@ -0,0 +1,95 @@
+/ -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+/
+/ The contents of this file are subject to the Mozilla Public
+/ License Version 1.1 (the "License"); you may not use this file
+/ except in compliance with the License. You may obtain a copy of
+/ the License at http://www.mozilla.org/MPL/
+/
+/ Software distributed under the License is distributed on an "AS
+/ IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+/ implied. See the License for the specific language governing
+/ rights and limitations under the License.
+/
+/ The Original Code is the Netscape Portable Runtime (NSPR).
+/
+/ The Initial Developer of the Original Code is Netscape
+/ Communications Corporation. Portions created by Netscape are
+/ Copyright (C) 2004 Netscape Communications Corporation. All
+/ Rights Reserved.
+/
+/ Contributor(s):
+/
+/ Alternatively, the contents of this file may be used under the
+/ terms of the GNU General Public License Version 2 or later (the
+/ "GPL"), in which case the provisions of the GPL are applicable
+/ instead of those above. If you wish to allow use of your
+/ version of this file only under the terms of the GPL and not to
+/ allow others to use your version of this file under the MPL,
+/ indicate your decision by deleting the provisions above and
+/ replace them with the notice and other provisions required by
+/ the GPL. If you do not delete the provisions above, a recipient
+/ may use your version of this file under either the MPL or the
+/ GPL.
+/
+
+/ PRInt32 _PR_x86_64_AtomicIncrement(PRInt32 *val)
+/
+/ Atomically increment the integer pointed to by 'val' and return
+/ the result of the increment.
+/
+ .text
+ .globl _PR_x86_64_AtomicIncrement
+ .align 4
+_PR_x86_64_AtomicIncrement:
+ movl $1, %eax
+ lock
+ xaddl %eax, (%rdi)
+ incl %eax
+ ret
+
+/ PRInt32 _PR_x86_64_AtomicDecrement(PRInt32 *val)
+/
+/ Atomically decrement the integer pointed to by 'val' and return
+/ the result of the decrement.
+/
+ .text
+ .globl _PR_x86_64_AtomicDecrement
+ .align 4
+_PR_x86_64_AtomicDecrement:
+ movl $-1, %eax
+ lock
+ xaddl %eax, (%rdi)
+ decl %eax
+ ret
+
+/ PRInt32 _PR_x86_64_AtomicSet(PRInt32 *val, PRInt32 newval)
+/
+/ Atomically set the integer pointed to by 'val' to the new
+/ value 'newval' and return the old value.
+/
+ .text
+ .globl _PR_x86_64_AtomicSet
+ .align 4
+_PR_x86_64_AtomicSet:
+ movl %esi, %eax
+ lock
+ xchgl %eax, (%rdi)
+ ret
+
+/ PRInt32 _PR_x86_64_AtomicAdd(PRInt32 *ptr, PRInt32 val)
+/
+/ Atomically add 'val' to the integer pointed to by 'ptr'
+/ and return the result of the addition.
+/
+ .text
+ .globl _PR_x86_64_AtomicAdd
+ .align 4
+_PR_x86_64_AtomicAdd:
+ movl %esi, %eax
+ lock
+ xaddl %eax, (%rdi)
+ addl %esi, %eax
+ ret
+
+/ Magic indicating no need for an executable stack
+.section .note.GNU-stack, "", @progbits ; .previous
diff --git a/pr/src/md/unix/solaris.c b/pr/src/md/unix/solaris.c
index 4dd1c06e..0437903a 100644
--- a/pr/src/md/unix/solaris.c
+++ b/pr/src/md/unix/solaris.c
@@ -183,9 +183,28 @@ THREAD_KEY_T cpuid_key;
THREAD_KEY_T last_thread_key;
static sigset_t set, oldset;
+static void
+threadid_key_destructor(void *value)
+{
+ PRThread *me = (PRThread *)value;
+ PR_ASSERT(me != NULL);
+ /* the thread could be PRIMORDIAL (thus not ATTACHED) */
+ if (me->flags & _PR_ATTACHED) {
+ /*
+ * The Solaris thread library sets the thread specific
+ * data (the current thread) to NULL before invoking
+ * the destructor. We need to restore it to prevent the
+ * _PR_MD_CURRENT_THREAD() call in _PRI_DetachThread()
+ * from attaching the thread again.
+ */
+ _PR_MD_SET_CURRENT_THREAD(me);
+ _PRI_DetachThread();
+ }
+}
+
void _MD_EarlyInit(void)
{
- THR_KEYCREATE(&threadid_key, NULL);
+ THR_KEYCREATE(&threadid_key, threadid_key_destructor);
THR_KEYCREATE(&cpuid_key, NULL);
THR_KEYCREATE(&last_thread_key, NULL);
sigemptyset(&set);
@@ -337,7 +356,7 @@ void _MD_lock(struct _MDLock *md_lock)
mutex_lock(&md_lock->lock);
}
-PRThread *_pr_current_thread_tls()
+PRThread *_pr_attached_thread_tls()
{
PRThread *ret;
@@ -345,6 +364,21 @@ PRThread *_pr_current_thread_tls()
return ret;
}
+PRThread *_pr_current_thread_tls()
+{
+ PRThread *thread;
+
+ thread = _MD_GET_ATTACHED_THREAD();
+
+ if (NULL == thread) {
+ thread = _PRI_AttachThread(
+ PR_USER_THREAD, PR_PRIORITY_NORMAL, NULL, 0);
+ }
+ PR_ASSERT(thread != NULL);
+
+ return thread;
+}
+
PRStatus
_MD_wait(PRThread *thread, PRIntervalTime ticks)
{
diff --git a/pr/src/md/unix/unix.c b/pr/src/md/unix/unix.c
index 69153f27..c69a233a 100644
--- a/pr/src/md/unix/unix.c
+++ b/pr/src/md/unix/unix.c
@@ -64,7 +64,8 @@
* Make sure _PRSockLen_t is 32-bit, because we will cast a PRUint32* or
* PRInt32* pointer to a _PRSockLen_t* pointer.
*/
-#if defined(HAVE_SOCKLEN_T)
+#if defined(HAVE_SOCKLEN_T) \
+ || (defined(LINUX) && defined(__GLIBC__) && __GLIBC__ >= 2)
#define _PRSockLen_t socklen_t
#elif defined(IRIX) || defined(HPUX) || defined(OSF1) || defined(SOLARIS) \
|| defined(AIX4_1) || defined(LINUX) || defined(SONY) \
@@ -514,6 +515,7 @@ static PRInt32 socket_io_wait(PRInt32 osfd, PRInt32 fd_type,
struct timeval tv;
PRThread *me = _PR_MD_CURRENT_THREAD();
PRIntervalTime epoch, now, elapsed, remaining;
+ PRBool wait_for_remaining;
PRInt32 syserror;
fd_set rd_wr;
@@ -558,8 +560,10 @@ static PRInt32 socket_io_wait(PRInt32 osfd, PRInt32 fd_type,
* so that there is an upper limit on the delay
* before the interrupt bit is checked.
*/
+ wait_for_remaining = PR_TRUE;
tv.tv_sec = PR_IntervalToSeconds(remaining);
if (tv.tv_sec > _PR_INTERRUPT_CHECK_INTERVAL_SECS) {
+ wait_for_remaining = PR_FALSE;
tv.tv_sec = _PR_INTERRUPT_CHECK_INTERVAL_SECS;
tv.tv_usec = 0;
} else {
@@ -596,8 +600,12 @@ static PRInt32 socket_io_wait(PRInt32 osfd, PRInt32 fd_type,
* PR_IntervalNow() call.
*/
if (rv == 0) {
- now += PR_SecondsToInterval(tv.tv_sec)
- + PR_MicrosecondsToInterval(tv.tv_usec);
+ if (wait_for_remaining) {
+ now += remaining;
+ } else {
+ now += PR_SecondsToInterval(tv.tv_sec)
+ + PR_MicrosecondsToInterval(tv.tv_usec);
+ }
} else {
now = PR_IntervalNow();
}
@@ -625,6 +633,7 @@ static PRInt32 socket_io_wait(PRInt32 osfd, PRInt32 fd_type,
int msecs;
PRThread *me = _PR_MD_CURRENT_THREAD();
PRIntervalTime epoch, now, elapsed, remaining;
+ PRBool wait_for_remaining;
PRInt32 syserror;
struct pollfd pfd;
@@ -682,8 +691,10 @@ static PRInt32 socket_io_wait(PRInt32 osfd, PRInt32 fd_type,
* so that there is an upper limit on the delay
* before the interrupt bit is checked.
*/
+ wait_for_remaining = PR_TRUE;
msecs = PR_IntervalToMilliseconds(remaining);
if (msecs > _PR_INTERRUPT_CHECK_INTERVAL_SECS * 1000) {
+ wait_for_remaining = PR_FALSE;
msecs = _PR_INTERRUPT_CHECK_INTERVAL_SECS * 1000;
}
rv = _MD_POLL(&pfd, 1, msecs);
@@ -719,7 +730,11 @@ static PRInt32 socket_io_wait(PRInt32 osfd, PRInt32 fd_type,
* PR_IntervalNow() call.
*/
if (rv == 0) {
- now += PR_MillisecondsToInterval(msecs);
+ if (wait_for_remaining) {
+ now += remaining;
+ } else {
+ now += PR_MillisecondsToInterval(msecs);
+ }
} else {
now = PR_IntervalNow();
}
@@ -2713,6 +2728,28 @@ static void* _MD_Unix_mmap64(
} /* _MD_Unix_mmap64 */
#endif /* defined(_PR_NO_LARGE_FILES) || defined(SOLARIS2_5) */
+#if defined(OSF1) && defined(__GNUC__)
+
+/*
+ * On OSF1 V5.0A, <sys/stat.h> defines stat and fstat as
+ * macros when compiled under gcc, so it is rather tricky to
+ * take the addresses of the real functions the macros expend
+ * to. A simple solution is to define forwarder functions
+ * and take the addresses of the forwarder functions instead.
+ */
+
+static int stat_forwarder(const char *path, struct stat *buffer)
+{
+ return stat(path, buffer);
+}
+
+static int fstat_forwarder(int filedes, struct stat *buffer)
+{
+ return fstat(filedes, buffer);
+}
+
+#endif
+
static void _PR_InitIOV(void)
{
#if defined(SOLARIS2_5)
@@ -2757,8 +2794,13 @@ static void _PR_InitIOV(void)
#elif defined(_PR_HAVE_LARGE_OFF_T)
_md_iovector._open64 = open;
_md_iovector._mmap64 = mmap;
+#if defined(OSF1) && defined(__GNUC__)
+ _md_iovector._fstat64 = fstat_forwarder;
+ _md_iovector._stat64 = stat_forwarder;
+#else
_md_iovector._fstat64 = fstat;
_md_iovector._stat64 = stat;
+#endif
_md_iovector._lseek64 = lseek;
#else
#error "I don't know yet"
@@ -3221,9 +3263,6 @@ int _MD_unix_get_nonblocking_connect_error(int osfd)
int err;
_PRSockLen_t optlen = sizeof(err);
- printf("_MD_unix_get_nonblocking_connect_error: "
- "Assuming Large TCP/IP Stack -REVISIT- Never Tested!\n");
-
if (getsockopt(osfd, SOL_SOCKET, SO_ERROR,
(char *) &err, &optlen) == -1) {
return errno;
diff --git a/pr/src/md/unix/unix_errors.c b/pr/src/md/unix/unix_errors.c
index 1ce52e68..055961ee 100644
--- a/pr/src/md/unix/unix_errors.c
+++ b/pr/src/md/unix/unix_errors.c
@@ -108,6 +108,9 @@ void _MD_unix_map_default_error(int err)
case EFBIG:
prError = PR_FILE_TOO_BIG_ERROR;
break;
+ case EHOSTUNREACH:
+ prError = PR_HOST_UNREACHABLE_ERROR;
+ break;
case EINPROGRESS:
prError = PR_IN_PROGRESS_ERROR;
break;
@@ -847,3 +850,10 @@ void _MD_solaris_map_sendfile_error(int err)
_MD_unix_map_default_error(err) ;
}
#endif /* SOLARIS */
+
+#ifdef LINUX
+void _MD_linux_map_sendfile_error(int err)
+{
+ _MD_unix_map_default_error(err) ;
+}
+#endif /* LINUX */
diff --git a/pr/src/md/unix/uxpoll.c b/pr/src/md/unix/uxpoll.c
index 56a0d3dc..c3275acf 100644
--- a/pr/src/md/unix/uxpoll.c
+++ b/pr/src/md/unix/uxpoll.c
@@ -173,6 +173,8 @@ static PRInt32 NativeThreadPoll(
{
/* make poll() ignore this entry */
syspoll[index].fd = -1;
+ syspoll[index].events = 0;
+ pds[index].out_flags = 0;
}
}
@@ -384,6 +386,10 @@ static PRInt32 NativeThreadSelect(
}
}
}
+ else
+ {
+ pd->out_flags = 0;
+ }
}
if (0 != ready) return ready; /* no need to block */
@@ -396,8 +402,7 @@ retry:
{
PRInt32 ticksPerSecond = PR_TicksPerSecond();
tv.tv_sec = remaining / ticksPerSecond;
- tv.tv_usec = remaining - (ticksPerSecond * tv.tv_sec);
- tv.tv_usec = (PR_USEC_PER_SEC * tv.tv_usec) / ticksPerSecond;
+ tv.tv_usec = PR_IntervalToMicroseconds( remaining % ticksPerSecond );
tvp = &tv;
}
diff --git a/pr/src/md/unix/uxproces.c b/pr/src/md/unix/uxproces.c
index 58b5ab8a..d469f1c4 100644
--- a/pr/src/md/unix/uxproces.c
+++ b/pr/src/md/unix/uxproces.c
@@ -802,7 +802,7 @@ static void pr_InstallSigchldHandler()
#endif /* !defined(_PR_NATIVE_THREADS) */
-static PRStatus _MD_InitProcesses()
+static PRStatus _MD_InitProcesses(void)
{
#if !defined(_PR_NATIVE_THREADS)
int rv;
diff --git a/pr/src/md/unix/uxrng.c b/pr/src/md/unix/uxrng.c
index 64358289..6add3bf1 100644
--- a/pr/src/md/unix/uxrng.c
+++ b/pr/src/md/unix/uxrng.c
@@ -64,6 +64,18 @@ GetHighResClock(void *buf, size_t maxbytes)
#elif defined(HPUX)
+#ifdef __ia64
+#include <ia64/sys/inline.h>
+
+static size_t
+GetHighResClock(void *buf, size_t maxbytes)
+{
+ PRUint64 t;
+
+ t = _Asm_mov_from_ar(_AREG44);
+ return _pr_CopyLowBits(buf, maxbytes, &t, sizeof(t));
+}
+#else
static size_t
GetHighResClock(void *buf, size_t maxbytes)
{
@@ -73,6 +85,7 @@ GetHighResClock(void *buf, size_t maxbytes)
cr16val = ret_cr16();
return(_pr_CopyLowBits(buf, maxbytes, &cr16val, sizeof(cr16val)));
}
+#endif
#elif defined(OSF1)
@@ -88,7 +101,11 @@ GetHighResClock(void *buf, size_t maxbytes)
{
unsigned long t;
+#ifdef __GNUC__
+ __asm__("rpcc %0" : "=r" (t));
+#else
t = asm("rpcc %v0");
+#endif
return _pr_CopyLowBits(buf, maxbytes, &t, sizeof(t));
}
@@ -210,7 +227,7 @@ static size_t GetHighResClock(void *buf, size_t maxbuf)
}
iotimer_addr = (unsigned *)
mmap(0, pgoffmask, PROT_READ, MAP_PRIVATE, mfd, (int)raddr);
- if (iotimer_addr == (void*)-1) {
+ if (iotimer_addr == (unsigned*)-1) {
close(mfd);
iotimer_addr = NULL;
return 0;
diff --git a/pr/src/md/unix/uxshm.c b/pr/src/md/unix/uxshm.c
index 888cbe04..1dc98386 100644
--- a/pr/src/md/unix/uxshm.c
+++ b/pr/src/md/unix/uxshm.c
@@ -94,7 +94,7 @@ extern PRSharedMemory * _MD_OpenSharedMemory(
return( NULL );
}
- shm->ipcname = PR_MALLOC( strlen( ipcname ) + 1 );
+ shm->ipcname = (char*)PR_MALLOC( strlen( ipcname ) + 1 );
if ( NULL == shm->ipcname )
{
PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0 );
@@ -118,7 +118,7 @@ extern PRSharedMemory * _MD_OpenSharedMemory(
PR_DELETE( shm );
return( NULL );
}
- if ( close(osfd == -1 )) {
+ if ( close(osfd) == -1 ) {
_PR_MD_MAP_CLOSE_ERROR( errno );
PR_FREEIF( shm->ipcname );
PR_DELETE( shm );
@@ -243,7 +243,7 @@ extern PRStatus _MD_DeleteSharedMemory( const char *name )
_PR_MD_MAP_OPEN_ERROR( errno );
return( PR_FAILURE );
}
- if ( close(osfd == -1 )) {
+ if ( close(osfd) == -1 ) {
_PR_MD_MAP_CLOSE_ERROR( errno );
return( PR_FAILURE );
}
@@ -311,7 +311,6 @@ extern PRSharedMemory * _MD_OpenSharedMemory(
)
{
PRStatus rc = PR_SUCCESS;
- PRIntn id;
PRInt32 end;
PRSharedMemory *shm;
char ipcname[PR_IPC_NAME_SIZE];
diff --git a/pr/src/md/windows/Makefile.in b/pr/src/md/windows/Makefile.in
index 283fa4a4..dd5a613b 100644
--- a/pr/src/md/windows/Makefile.in
+++ b/pr/src/md/windows/Makefile.in
@@ -105,3 +105,13 @@ include $(topsrcdir)/config/rules.mk
export:: $(TARGETS)
+# Bug 122433 workaround: disable global optimization (-Og-) on ntio.c.
+ifdef BUILD_OPT
+ifeq ($(OS_TARGET), WINNT)
+ifndef NS_USE_GCC
+$(OBJDIR)/ntio.$(OBJ_SUFFIX): ntio.c
+ @$(MAKE_OBJDIR)
+ $(CC) -Fo$@ -c $(CFLAGS) -Og- $<
+endif
+endif
+endif
diff --git a/pr/src/md/windows/ntinrval.c b/pr/src/md/windows/ntinrval.c
index d6a3fe6f..857ada6a 100644
--- a/pr/src/md/windows/ntinrval.c
+++ b/pr/src/md/windows/ntinrval.c
@@ -45,9 +45,8 @@
#define QueryPerformanceCounter(x) FALSE
#endif
-PRIntn _nt_bitShift = 0;
-PRInt32 _nt_highMask = 0;
-PRInt32 _nt_ticksPerSec = -1;
+static PRIntn _nt_bitShift = 0;
+static PRInt32 _nt_ticksPerSec = -1;
void
_PR_MD_INTERVAL_INIT()
@@ -55,16 +54,33 @@ _PR_MD_INTERVAL_INIT()
LARGE_INTEGER count;
if (QueryPerformanceFrequency(&count)) {
+ /*
+ * HighPart is signed (LONG). Assert that its sign bit is 0
+ * because we will be right shifting it. LowPart is unsigned
+ * (DWORD).
+ */
+ PR_ASSERT(count.HighPart >= 0);
+ while(count.HighPart) {
+ count.LowPart = (count.HighPart << 31) + (count.LowPart >> 1);
+ count.HighPart >>= 1;
+ _nt_bitShift++;
+ }
while(count.LowPart > PR_INTERVAL_MAX) {
count.LowPart >>= 1;
_nt_bitShift++;
- _nt_highMask = (_nt_highMask << 1)+1;
}
- _nt_ticksPerSec = count.LowPart;
- PR_ASSERT(_nt_ticksPerSec > PR_INTERVAL_MIN);
- } else
- _nt_ticksPerSec = -1;
+ /*
+ * We can't use the performance counter if after
+ * normalization we are left with fewer than 32 bits.
+ */
+ if (_nt_bitShift <= 32) {
+ _nt_ticksPerSec = count.LowPart;
+ PR_ASSERT(_nt_ticksPerSec > PR_INTERVAL_MIN);
+ return;
+ }
+ }
+ _nt_ticksPerSec = -1;
}
PRIntervalTime
@@ -76,12 +92,15 @@ _PR_MD_GET_INTERVAL()
* to only 100000 ticks per second; QueryPerformanceCounter is too high
* resolution...
*/
- if (QueryPerformanceCounter(&count)) {
- PRInt32 top = count.HighPart & _nt_highMask;
- top = top << (32 - _nt_bitShift);
- count.LowPart = count.LowPart >> _nt_bitShift;
- count.LowPart = count.LowPart + top;
- return (PRUint32)count.LowPart;
+ if (_nt_ticksPerSec != -1) {
+ (void)QueryPerformanceCounter(&count);
+ PR_ASSERT(_nt_bitShift <= 32);
+ if (_nt_bitShift == 32) {
+ return (PRUint32)count.HighPart;
+ } else {
+ return (PRUint32)((count.HighPart << (32 - _nt_bitShift))
+ + (count.LowPart >> _nt_bitShift));
+ }
} else
#if defined(__MINGW32__)
return time();
diff --git a/pr/src/md/windows/ntio.c b/pr/src/md/windows/ntio.c
index 299033cc..345e253a 100644
--- a/pr/src/md/windows/ntio.c
+++ b/pr/src/md/windows/ntio.c
@@ -99,7 +99,11 @@ static DWORD dirAccessTable[] = {
* We store the value in a PRTime variable for convenience.
* This constant is used by _PR_FileTimeToPRTime().
*/
+#ifdef __GNUC__
+static const PRTime _pr_filetime_offset = 116444736000000000LL;
+#else
static const PRTime _pr_filetime_offset = 116444736000000000i64;
+#endif
#define _NEED_351_FILE_LOCKING_HACK
#ifdef _NEED_351_FILE_LOCKING_HACK
@@ -1328,6 +1332,7 @@ _PR_MD_FAST_ACCEPT(PRFileDesc *fd, PRNetAddr *raddr, PRUint32 *rlen,
me->flags &= ~_PR_INTERRUPT;
PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
_PR_THREAD_UNLOCK(me);
+ closesocket(accept_sock);
return -1;
}
me->io_pending = PR_TRUE;
@@ -1346,6 +1351,7 @@ _PR_MD_FAST_ACCEPT(PRFileDesc *fd, PRNetAddr *raddr, PRUint32 *rlen,
if ( (rv == 0) && ((err = GetLastError()) != ERROR_IO_PENDING)) {
/* Argh! The IO failed */
+ closesocket(accept_sock);
_PR_THREAD_LOCK(me);
me->io_pending = PR_FALSE;
me->state = _PR_RUNNING;
@@ -1365,12 +1371,14 @@ _PR_MD_FAST_ACCEPT(PRFileDesc *fd, PRNetAddr *raddr, PRUint32 *rlen,
_native_thread_io_nowait(me, rv, bytes);
} else if (_NT_IO_WAIT(me, timeout) == PR_FAILURE) {
PR_ASSERT(0);
+ closesocket(accept_sock);
return -1;
}
PR_ASSERT(me->io_pending == PR_FALSE || me->io_suspended == PR_TRUE);
if (me->io_suspended) {
+ closesocket(accept_sock);
if (_PR_PENDING_INTERRUPT(me)) {
me->flags &= ~_PR_INTERRUPT;
PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
@@ -1381,6 +1389,7 @@ _PR_MD_FAST_ACCEPT(PRFileDesc *fd, PRNetAddr *raddr, PRUint32 *rlen,
}
if (me->md.blocked_io_status == 0) {
+ closesocket(accept_sock);
_PR_MD_MAP_ACCEPTEX_ERROR(me->md.blocked_io_error);
return -1;
}
@@ -1446,6 +1455,7 @@ _PR_MD_FAST_ACCEPT_READ(PRFileDesc *sd, PRInt32 *newSock, PRNetAddr **raddr,
me->flags &= ~_PR_INTERRUPT;
PR_SetError(PR_PENDING_INTERRUPT_ERROR, 0);
_PR_THREAD_UNLOCK(me);
+ closesocket(*newSock);
return -1;
}
me->io_pending = PR_TRUE;
@@ -1463,6 +1473,7 @@ _PR_MD_FAST_ACCEPT_READ(PRFileDesc *sd, PRInt32 *newSock, PRNetAddr **raddr,
&(me->md.overlapped.overlapped));
if ( (rv == 0) && ((err = GetLastError()) != ERROR_IO_PENDING)) {
+ closesocket(*newSock);
_PR_THREAD_LOCK(me);
me->io_pending = PR_FALSE;
me->state = _PR_RUNNING;
@@ -1482,6 +1493,7 @@ _PR_MD_FAST_ACCEPT_READ(PRFileDesc *sd, PRInt32 *newSock, PRNetAddr **raddr,
_native_thread_io_nowait(me, rv, bytes);
} else if (_NT_IO_WAIT(me, timeout) == PR_FAILURE) {
PR_ASSERT(0);
+ closesocket(*newSock);
return -1;
}
@@ -1511,8 +1523,10 @@ retry:
callback(callbackArg);
madeCallback = PR_TRUE;
me->state = _PR_IO_WAIT;
- if (_NT_ResumeIO(me, timeout) == PR_FAILURE)
+ if (_NT_ResumeIO(me, timeout) == PR_FAILURE) {
+ closesocket(*newSock);
return -1;
+ }
goto retry;
}
@@ -1520,8 +1534,10 @@ retry:
/* Socket is connected but time not elapsed, RESUME IO */
timeout -= elapsed;
me->state = _PR_IO_WAIT;
- if (_NT_ResumeIO(me, timeout) == PR_FAILURE)
+ if (_NT_ResumeIO(me, timeout) == PR_FAILURE) {
+ closesocket(*newSock);
return -1;
+ }
goto retry;
}
} else {
@@ -1548,6 +1564,7 @@ retry:
PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
}
me->state = _PR_RUNNING;
+ closesocket(*newSock);
return -1;
}
@@ -2322,7 +2339,7 @@ _PR_MD_READ(PRFileDesc *fd, void *buf, PRInt32 len)
}
PRInt32
-_PR_MD_WRITE(PRFileDesc *fd, void *buf, PRInt32 len)
+_PR_MD_WRITE(PRFileDesc *fd, const void *buf, PRInt32 len)
{
PRInt32 f = fd->secret->md.osfd;
PRInt32 bytes;
@@ -2488,7 +2505,7 @@ _PR_MD_PIPEAVAILABLE(PRFileDesc *fd)
}
PROffset32
-_PR_MD_LSEEK(PRFileDesc *fd, PROffset32 offset, int whence)
+_PR_MD_LSEEK(PRFileDesc *fd, PROffset32 offset, PRSeekWhence whence)
{
DWORD moveMethod;
PROffset32 rv;
@@ -2521,7 +2538,7 @@ _PR_MD_LSEEK(PRFileDesc *fd, PROffset32 offset, int whence)
}
PROffset64
-_PR_MD_LSEEK64(PRFileDesc *fd, PROffset64 offset, int whence)
+_PR_MD_LSEEK64(PRFileDesc *fd, PROffset64 offset, PRSeekWhence whence)
{
DWORD moveMethod;
LARGE_INTEGER li;
@@ -2815,12 +2832,16 @@ _PR_MD_DELETE(const char *name)
}
}
-static void
+void
_PR_FileTimeToPRTime(const FILETIME *filetime, PRTime *prtm)
{
PR_ASSERT(sizeof(FILETIME) == sizeof(PRTime));
CopyMemory(prtm, filetime, sizeof(PRTime));
+#ifdef __GNUC__
+ *prtm = (*prtm - _pr_filetime_offset) / 10LL;
+#else
*prtm = (*prtm - _pr_filetime_offset) / 10i64;
+#endif
#ifdef DEBUG
/* Doublecheck our calculation. */
@@ -3168,7 +3189,7 @@ _PR_MD_RENAME(const char *from, const char *to)
}
PRInt32
-_PR_MD_ACCESS(const char *name, PRIntn how)
+_PR_MD_ACCESS(const char *name, PRAccessHow how)
{
PRInt32 rv;
diff --git a/pr/src/md/windows/ntmisc.c b/pr/src/md/windows/ntmisc.c
index c4bb8511..32a199ce 100644
--- a/pr/src/md/windows/ntmisc.c
+++ b/pr/src/md/windows/ntmisc.c
@@ -84,18 +84,12 @@ PRIntn _PR_MD_PUT_ENV(const char *name)
PR_IMPLEMENT(PRTime)
PR_Now(void)
{
- PRInt64 s, ms, ms2us, s2us;
- struct timeb b;
-
- ftime(&b);
- LL_I2L(ms2us, PR_USEC_PER_MSEC);
- LL_I2L(s2us, PR_USEC_PER_SEC);
- LL_I2L(s, b.time);
- LL_I2L(ms, b.millitm);
- LL_MUL(ms, ms, ms2us);
- LL_MUL(s, s, s2us);
- LL_ADD(s, s, ms);
- return s;
+ PRTime prt;
+ FILETIME ft;
+
+ GetSystemTimeAsFileTime(&ft);
+ _PR_FileTimeToPRTime(&ft, &prt);
+ return prt;
}
/*
@@ -792,7 +786,7 @@ PRInt32 _PR_MD_ATOMIC_DECREMENT(PRInt32 *val)
PRInt32 result;
asm volatile ("lock ; xadd %0, %1"
: "=r"(result), "=m"(*val)
- : "0"(1), "m"(*val));
+ : "0"(-1), "m"(*val));
//asm volatile("lock ; xadd %0, %1" : "=m" (val), "=a" (result) : "-1" (1));
return result - 1;
#else
@@ -814,8 +808,8 @@ PRInt32 _PR_MD_ATOMIC_ADD(PRInt32 *intp, PRInt32 val)
PRInt32 result;
//asm volatile("lock ; xadd %1, %0" : "=m" (intp), "=a" (result) : "1" (val));
asm volatile ("lock ; xadd %0, %1"
- : "=r"(result), "=m"(intp)
- : "0"(val), "m"(intp));
+ : "=r"(result), "=m"(*intp)
+ : "0"(val), "m"(*intp));
return result + val;
#else
__asm
diff --git a/pr/src/md/windows/ntthread.c b/pr/src/md/windows/ntthread.c
index 9c3e2baf..0e7ef6e0 100644
--- a/pr/src/md/windows/ntthread.c
+++ b/pr/src/md/windows/ntthread.c
@@ -191,6 +191,14 @@ _PR_MD_INIT_THREAD(PRThread *thread)
return PR_SUCCESS;
}
+static unsigned __stdcall
+pr_root(void *arg)
+{
+ PRThread *thread = (PRThread *)arg;
+ thread->md.start(thread);
+ return 0;
+}
+
PRStatus
_PR_MD_CREATE_THREAD(PRThread *thread,
void (*start)(void *),
@@ -200,23 +208,14 @@ _PR_MD_CREATE_THREAD(PRThread *thread,
PRUint32 stackSize)
{
-#if 0
- thread->md.handle = CreateThread(
- NULL, /* security attrib */
- thread->stack->stackSize, /* stack size */
- (LPTHREAD_START_ROUTINE)start, /* startup routine */
- (void *)thread, /* thread param */
- CREATE_SUSPENDED, /* create flags */
- &(thread->id) ); /* thread id */
-#else
+ thread->md.start = start;
thread->md.handle = (HANDLE) _beginthreadex(
NULL,
thread->stack->stackSize,
- (unsigned (__stdcall *)(void *))start,
+ pr_root,
(void *)thread,
CREATE_SUSPENDED,
&(thread->id));
-#endif
if(!thread->md.handle) {
PRErrorCode prerror;
thread->md.fiber_last_error = GetLastError();
@@ -238,6 +237,13 @@ _PR_MD_CREATE_THREAD(PRThread *thread,
}
thread->md.id = thread->id;
+ /*
+ * On windows, a thread is created with a thread priority of
+ * THREAD_PRIORITY_NORMAL.
+ */
+ if (priority != PR_PRIORITY_NORMAL) {
+ _PR_MD_SET_PRIORITY(&(thread->md), priority);
+ }
/* Activate the thread */
if ( ResumeThread( thread->md.handle ) != -1)
@@ -247,6 +253,21 @@ _PR_MD_CREATE_THREAD(PRThread *thread,
return PR_FAILURE;
}
+void
+_PR_MD_JOIN_THREAD(_MDThread *md)
+{
+ DWORD rv;
+
+ rv = WaitForSingleObject(md->handle, INFINITE);
+ PR_ASSERT(WAIT_OBJECT_0 == rv);
+}
+
+void
+_PR_MD_END_THREAD(void)
+{
+ _endthreadex(0);
+}
+
void
_PR_MD_YIELD(void)
{
diff --git a/pr/src/md/windows/w32poll.c b/pr/src/md/windows/w32poll.c
index 2747a040..403aa004 100644
--- a/pr/src/md/windows/w32poll.c
+++ b/pr/src/md/windows/w32poll.c
@@ -36,6 +36,9 @@
* This file implements _PR_MD_PR_POLL for Win32.
*/
+/* The default value of FD_SETSIZE is 64. */
+#define FD_SETSIZE 1024
+
#include "primpl.h"
#if !defined(_PR_GLOBAL_THREADS_ONLY)
@@ -105,6 +108,8 @@ PRInt32 _PR_MD_PR_POLL(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
{
int ready, err;
fd_set rd, wt, ex;
+ fd_set *rdp, *wtp, *exp;
+ int nrd, nwt, nex;
PRFileDesc *bottom;
PRPollDesc *pd, *epd;
PRThread *me = _PR_MD_CURRENT_THREAD();
@@ -127,6 +132,7 @@ PRInt32 _PR_MD_PR_POLL(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
return 0;
}
+ nrd = nwt = nex = 0;
FD_ZERO(&rd);
FD_ZERO(&wt);
FD_ZERO(&ex);
@@ -189,23 +195,30 @@ PRInt32 _PR_MD_PR_POLL(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
{
pd->out_flags |= _PR_POLL_READ_SYS_READ;
FD_SET(osfd, &rd);
+ nrd++;
}
if (in_flags_read & PR_POLL_WRITE)
{
pd->out_flags |= _PR_POLL_READ_SYS_WRITE;
FD_SET(osfd, &wt);
+ nwt++;
}
if (in_flags_write & PR_POLL_READ)
{
pd->out_flags |= _PR_POLL_WRITE_SYS_READ;
FD_SET(osfd, &rd);
+ nrd++;
}
if (in_flags_write & PR_POLL_WRITE)
{
pd->out_flags |= _PR_POLL_WRITE_SYS_WRITE;
FD_SET(osfd, &wt);
+ nwt++;
+ }
+ if (pd->in_flags & PR_POLL_EXCEPT) {
+ FD_SET(osfd, &ex);
+ nex++;
}
- if (pd->in_flags & PR_POLL_EXCEPT) FD_SET(osfd, &ex);
}
}
else
@@ -223,23 +236,40 @@ PRInt32 _PR_MD_PR_POLL(PRPollDesc *pds, PRIntn npds, PRIntervalTime timeout)
}
}
}
+ else
+ {
+ pd->out_flags = 0;
+ }
}
if (0 != ready) return ready; /* no need to block */
+ if ((nrd > FD_SETSIZE) || (nwt > FD_SETSIZE) || (nex > FD_SETSIZE)) {
+ PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
+ return -1;
+ }
+
+ rdp = (0 == nrd) ? NULL : &rd;
+ wtp = (0 == nwt) ? NULL : &wt;
+ exp = (0 == nex) ? NULL : &ex;
+
+ if ((NULL == rdp) && (NULL == wtp) && (NULL == exp)) {
+ PR_Sleep(timeout);
+ return 0;
+ }
+
if (timeout != PR_INTERVAL_NO_TIMEOUT)
{
PRInt32 ticksPerSecond = PR_TicksPerSecond();
tv.tv_sec = timeout / ticksPerSecond;
- tv.tv_usec = timeout - (ticksPerSecond * tv.tv_sec);
- tv.tv_usec = (PR_USEC_PER_SEC * tv.tv_usec) / ticksPerSecond;
+ tv.tv_usec = PR_IntervalToMicroseconds( timeout % ticksPerSecond );
tvp = &tv;
}
#if defined(_PR_GLOBAL_THREADS_ONLY)
- ready = _MD_SELECT(0, &rd, &wt, &ex, tvp);
+ ready = _MD_SELECT(0, rdp, wtp, exp, tvp);
#else
- ready = _PR_NTFiberSafeSelect(0, &rd, &wt, &ex, tvp);
+ ready = _PR_NTFiberSafeSelect(0, rdp, wtp, exp, tvp);
#endif
/*
diff --git a/pr/src/md/windows/w95io.c b/pr/src/md/windows/w95io.c
index 89669e6e..6ccdf410 100644
--- a/pr/src/md/windows/w95io.c
+++ b/pr/src/md/windows/w95io.c
@@ -41,6 +41,9 @@
#include "primpl.h"
#include <direct.h>
#include <mbstring.h>
+#ifdef MOZ_UNICODE
+#include <wchar.h>
+#endif /* MOZ_UNICODE */
struct _MDLock _pr_ioq_lock;
@@ -74,6 +77,10 @@ static const PRTime _pr_filetime_offset = 116444736000000000LL;
static const PRTime _pr_filetime_offset = 116444736000000000i64;
#endif
+#ifdef MOZ_UNICODE
+static void InitUnicodeSupport(void);
+#endif
+
void
_PR_MD_INIT_IO()
{
@@ -110,6 +117,10 @@ _PR_MD_INIT_IO()
#endif /* DEBUG */
_PR_NT_InitSids();
+
+#ifdef MOZ_UNICODE
+ InitUnicodeSupport();
+#endif
}
PRStatus
@@ -568,7 +579,7 @@ _PR_MD_DELETE(const char *name)
}
}
-static void
+void
_PR_FileTimeToPRTime(const FILETIME *filetime, PRTime *prtm)
{
PR_ASSERT(sizeof(FILETIME) == sizeof(PRTime));
@@ -1088,3 +1099,410 @@ _PR_MD_PIPEAVAILABLE(PRFileDesc *fd)
PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
return -1;
}
+
+#ifdef MOZ_UNICODE
+
+typedef HANDLE (WINAPI *CreateFileWFn) (LPCWSTR, DWORD, DWORD, LPSECURITY_ATTRIBUTES, DWORD, DWORD, HANDLE);
+static CreateFileWFn createFileW = NULL;
+typedef HANDLE (WINAPI *FindFirstFileWFn) (LPCWSTR, LPWIN32_FIND_DATAW);
+static FindFirstFileWFn findFirstFileW = NULL;
+typedef BOOL (WINAPI *FindNextFileWFn) (HANDLE, LPWIN32_FIND_DATAW);
+static FindNextFileWFn findNextFileW = NULL;
+typedef DWORD (WINAPI *GetFullPathNameWFn) (LPCWSTR, DWORD, LPWSTR, LPWSTR *);
+static GetFullPathNameWFn getFullPathNameW = NULL;
+typedef UINT (WINAPI *GetDriveTypeWFn) (LPCWSTR);
+static GetDriveTypeWFn getDriveTypeW = NULL;
+
+static void InitUnicodeSupport(void)
+{
+ HMODULE module;
+
+ /*
+ * The W functions do not exist on Win9x. NSPR won't run on Win9x
+ * if we call the W functions directly. Use GetProcAddress() to
+ * look up their addresses at run time.
+ */
+
+ module = GetModuleHandle("Kernel32.dll");
+ if (!module) {
+ return;
+ }
+
+ createFileW = (CreateFileWFn)GetProcAddress(module, "CreateFileW");
+ findFirstFileW = (FindFirstFileWFn)GetProcAddress(module, "FindFirstFileW");
+ findNextFileW = (FindNextFileWFn)GetProcAddress(module, "FindNextFileW");
+ getDriveTypeW = (GetDriveTypeWFn)GetProcAddress(module, "GetDriveTypeW");
+ getFullPathNameW = (GetFullPathNameWFn)GetProcAddress(module, "GetFullPathNameW");
+}
+
+/* ================ UTF16 Interfaces ================================ */
+void FlipSlashesW(PRUnichar *cp, int len)
+{
+ while (--len >= 0) {
+ if (cp[0] == L'/') {
+ cp[0] = L'\\';
+ }
+ cp++;
+ }
+} /* end FlipSlashesW() */
+
+PRInt32
+_PR_MD_OPEN_FILE_UTF16(const PRUnichar *name, PRIntn osflags, int mode)
+{
+ HANDLE file;
+ PRInt32 access = 0;
+ PRInt32 flags = 0;
+ PRInt32 flag6 = 0;
+ SECURITY_ATTRIBUTES sa;
+ LPSECURITY_ATTRIBUTES lpSA = NULL;
+ PSECURITY_DESCRIPTOR pSD = NULL;
+ PACL pACL = NULL;
+
+ if (!createFileW) {
+ PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
+ return -1;
+ }
+
+ if (osflags & PR_CREATE_FILE) {
+ if (_PR_NT_MakeSecurityDescriptorACL(mode, fileAccessTable,
+ &pSD, &pACL) == PR_SUCCESS) {
+ sa.nLength = sizeof(sa);
+ sa.lpSecurityDescriptor = pSD;
+ sa.bInheritHandle = FALSE;
+ lpSA = &sa;
+ }
+ }
+
+ if (osflags & PR_SYNC) flag6 = FILE_FLAG_WRITE_THROUGH;
+
+ if (osflags & PR_RDONLY || osflags & PR_RDWR)
+ access |= GENERIC_READ;
+ if (osflags & PR_WRONLY || osflags & PR_RDWR)
+ access |= GENERIC_WRITE;
+
+ if ( osflags & PR_CREATE_FILE && osflags & PR_EXCL )
+ flags = CREATE_NEW;
+ else if (osflags & PR_CREATE_FILE) {
+ if (osflags & PR_TRUNCATE)
+ flags = CREATE_ALWAYS;
+ else
+ flags = OPEN_ALWAYS;
+ } else {
+ if (osflags & PR_TRUNCATE)
+ flags = TRUNCATE_EXISTING;
+ else
+ flags = OPEN_EXISTING;
+ }
+
+ file = createFileW(name,
+ access,
+ FILE_SHARE_READ|FILE_SHARE_WRITE,
+ lpSA,
+ flags,
+ flag6,
+ NULL);
+ if (lpSA != NULL) {
+ _PR_NT_FreeSecurityDescriptorACL(pSD, pACL);
+ }
+ if (file == INVALID_HANDLE_VALUE) {
+ _PR_MD_MAP_OPEN_ERROR(GetLastError());
+ return -1;
+ }
+
+ return (PRInt32)file;
+}
+
+PRStatus
+_PR_MD_OPEN_DIR_UTF16(_MDDirUTF16 *d, const PRUnichar *name)
+{
+ PRUnichar filename[ MAX_PATH ];
+ int len;
+
+ if (!findFirstFileW) {
+ PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
+ return PR_FAILURE;
+ }
+
+ len = wcslen(name);
+ /* Need 5 bytes for \*.* and the trailing null byte. */
+ if (len + 5 > MAX_PATH) {
+ PR_SetError(PR_NAME_TOO_LONG_ERROR, 0);
+ return PR_FAILURE;
+ }
+ wcscpy(filename, name);
+
+ /*
+ * If 'name' ends in a slash or backslash, do not append
+ * another backslash.
+ */
+ if (filename[len - 1] == L'/' || filename[len - 1] == L'\\') {
+ len--;
+ }
+ wcscpy(&filename[len], L"\\*.*");
+ FlipSlashesW( filename, wcslen(filename) );
+
+ d->d_hdl = findFirstFileW( filename, &(d->d_entry) );
+ if ( d->d_hdl == INVALID_HANDLE_VALUE ) {
+ _PR_MD_MAP_OPENDIR_ERROR(GetLastError());
+ return PR_FAILURE;
+ }
+ d->firstEntry = PR_TRUE;
+ d->magic = _MD_MAGIC_DIR;
+ return PR_SUCCESS;
+}
+
+PRUnichar *
+_PR_MD_READ_DIR_UTF16(_MDDirUTF16 *d, PRIntn flags)
+{
+ PRInt32 err;
+ BOOL rv;
+ PRUnichar *fileName;
+
+ if (!findNextFileW) {
+ PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
+ return NULL;
+ }
+
+ if ( d ) {
+ while (1) {
+ if (d->firstEntry) {
+ d->firstEntry = PR_FALSE;
+ rv = 1;
+ } else {
+ rv = findNextFileW(d->d_hdl, &(d->d_entry));
+ }
+ if (rv == 0) {
+ break;
+ }
+ fileName = GetFileFromDIR(d);
+ if ( (flags & PR_SKIP_DOT) &&
+ (fileName[0] == L'.') && (fileName[1] == L'\0'))
+ continue;
+ if ( (flags & PR_SKIP_DOT_DOT) &&
+ (fileName[0] == L'.') && (fileName[1] == L'.') &&
+ (fileName[2] == L'\0'))
+ continue;
+ if ( (flags & PR_SKIP_HIDDEN) && FileIsHidden(d))
+ continue;
+ return fileName;
+ }
+ err = GetLastError();
+ PR_ASSERT(NO_ERROR != err);
+ _PR_MD_MAP_READDIR_ERROR(err);
+ return NULL;
+ }
+ PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
+ return NULL;
+}
+
+PRStatus
+_PR_MD_CLOSE_DIR_UTF16(_MDDirUTF16 *d)
+{
+ if ( d ) {
+ if (FindClose(d->d_hdl)) {
+ d->magic = (PRUint32)-1;
+ return PR_SUCCESS;
+ } else {
+ _PR_MD_MAP_CLOSEDIR_ERROR(GetLastError());
+ return PR_FAILURE;
+ }
+ }
+ PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
+ return PR_FAILURE;
+}
+
+#define _PR_IS_W_SLASH(ch) ((ch) == L'/' || (ch) == L'\\')
+
+/*
+ * IsRootDirectoryW --
+ *
+ * Return PR_TRUE if the pathname 'fn' is a valid root directory,
+ * else return PR_FALSE. The PRUnichar buffer pointed to by 'fn' must
+ * be writable. During the execution of this function, the contents
+ * of the buffer pointed to by 'fn' may be modified, but on return
+ * the original contents will be restored. 'buflen' is the size of
+ * the buffer pointed to by 'fn', in PRUnichars.
+ *
+ * Root directories come in three formats:
+ * 1. / or \, meaning the root directory of the current drive.
+ * 2. C:/ or C:\, where C is a drive letter.
+ * 3. \\<server name>\<share point name>\ or
+ * \\<server name>\<share point name>, meaning the root directory
+ * of a UNC (Universal Naming Convention) name.
+ */
+
+static PRBool
+IsRootDirectoryW(PRUnichar *fn, size_t buflen)
+{
+ PRUnichar *p;
+ PRBool slashAdded = PR_FALSE;
+ PRBool rv = PR_FALSE;
+
+ if (_PR_IS_W_SLASH(fn[0]) && fn[1] == L'\0') {
+ return PR_TRUE;
+ }
+
+ if (iswalpha(fn[0]) && fn[1] == L':' && _PR_IS_W_SLASH(fn[2])
+ && fn[3] == L'\0') {
+ rv = getDriveTypeW(fn) > 1 ? PR_TRUE : PR_FALSE;
+ return rv;
+ }
+
+ /* The UNC root directory */
+
+ if (_PR_IS_W_SLASH(fn[0]) && _PR_IS_W_SLASH(fn[1])) {
+ /* The 'server' part should have at least one character. */
+ p = &fn[2];
+ if (*p == L'\0' || _PR_IS_W_SLASH(*p)) {
+ return PR_FALSE;
+ }
+
+ /* look for the next slash */
+ do {
+ p++;
+ } while (*p != L'\0' && !_PR_IS_W_SLASH(*p));
+ if (*p == L'\0') {
+ return PR_FALSE;
+ }
+
+ /* The 'share' part should have at least one character. */
+ p++;
+ if (*p == L'\0' || _PR_IS_W_SLASH(*p)) {
+ return PR_FALSE;
+ }
+
+ /* look for the final slash */
+ do {
+ p++;
+ } while (*p != L'\0' && !_PR_IS_W_SLASH(*p));
+ if (_PR_IS_W_SLASH(*p) && p[1] != L'\0') {
+ return PR_FALSE;
+ }
+ if (*p == L'\0') {
+ /*
+ * GetDriveType() doesn't work correctly if the
+ * path is of the form \\server\share, so we add
+ * a final slash temporarily.
+ */
+ if ((p + 1) < (fn + buflen)) {
+ *p++ = L'\\';
+ *p = L'\0';
+ slashAdded = PR_TRUE;
+ } else {
+ return PR_FALSE; /* name too long */
+ }
+ }
+ rv = getDriveTypeW(fn) > 1 ? PR_TRUE : PR_FALSE;
+ /* restore the 'fn' buffer */
+ if (slashAdded) {
+ *--p = L'\0';
+ }
+ }
+ return rv;
+}
+
+PRInt32
+_PR_MD_GETFILEINFO64_UTF16(const PRUnichar *fn, PRFileInfo64 *info)
+{
+ HANDLE hFindFile;
+ WIN32_FIND_DATAW findFileData;
+ PRUnichar pathbuf[MAX_PATH + 1];
+
+ if (!findFirstFileW || !getFullPathNameW || !getDriveTypeW) {
+ PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
+ return -1;
+ }
+
+ if (NULL == fn || L'\0' == *fn) {
+ PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
+ return -1;
+ }
+
+ /*
+ * FindFirstFile() expands wildcard characters. So
+ * we make sure the pathname contains no wildcard.
+ */
+ if (NULL != wcspbrk(fn, L"?*")) {
+ PR_SetError(PR_FILE_NOT_FOUND_ERROR, 0);
+ return -1;
+ }
+
+ hFindFile = findFirstFileW(fn, &findFileData);
+ if (INVALID_HANDLE_VALUE == hFindFile) {
+ DWORD len;
+ PRUnichar *filePart;
+
+ /*
+ * FindFirstFile() does not work correctly on root directories.
+ * It also doesn't work correctly on a pathname that ends in a
+ * slash. So we first check to see if the pathname specifies a
+ * root directory. If not, and if the pathname ends in a slash,
+ * we remove the final slash and try again.
+ */
+
+ /*
+ * If the pathname does not contain ., \, and /, it cannot be
+ * a root directory or a pathname that ends in a slash.
+ */
+ if (NULL == wcspbrk(fn, L".\\/")) {
+ _PR_MD_MAP_OPENDIR_ERROR(GetLastError());
+ return -1;
+ }
+ len = getFullPathNameW(fn, sizeof(pathbuf)/sizeof(pathbuf[0]), pathbuf,
+ &filePart);
+ if (0 == len) {
+ _PR_MD_MAP_OPENDIR_ERROR(GetLastError());
+ return -1;
+ }
+ if (len > sizeof(pathbuf)/sizeof(pathbuf[0])) {
+ PR_SetError(PR_NAME_TOO_LONG_ERROR, 0);
+ return -1;
+ }
+ if (IsRootDirectoryW(pathbuf, sizeof(pathbuf)/sizeof(pathbuf[0]))) {
+ info->type = PR_FILE_DIRECTORY;
+ info->size = 0;
+ /*
+ * These timestamps don't make sense for root directories.
+ */
+ info->modifyTime = 0;
+ info->creationTime = 0;
+ return 0;
+ }
+ if (!_PR_IS_W_SLASH(pathbuf[len - 1])) {
+ _PR_MD_MAP_OPENDIR_ERROR(GetLastError());
+ return -1;
+ } else {
+ pathbuf[len - 1] = L'\0';
+ hFindFile = findFirstFileW(pathbuf, &findFileData);
+ if (INVALID_HANDLE_VALUE == hFindFile) {
+ _PR_MD_MAP_OPENDIR_ERROR(GetLastError());
+ return -1;
+ }
+ }
+ }
+
+ FindClose(hFindFile);
+
+ if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
+ info->type = PR_FILE_DIRECTORY;
+ } else {
+ info->type = PR_FILE_FILE;
+ }
+
+ info->size = findFileData.nFileSizeHigh;
+ info->size = (info->size << 32) + findFileData.nFileSizeLow;
+
+ _PR_FileTimeToPRTime(&findFileData.ftLastWriteTime, &info->modifyTime);
+
+ if (0 == findFileData.ftCreationTime.dwLowDateTime &&
+ 0 == findFileData.ftCreationTime.dwHighDateTime) {
+ info->creationTime = info->modifyTime;
+ } else {
+ _PR_FileTimeToPRTime(&findFileData.ftCreationTime,
+ &info->creationTime);
+ }
+
+ return 0;
+}
+/* ================ end of UTF16 Interfaces ================================ */
+#endif /* MOZ_UNICODE */
diff --git a/pr/src/md/windows/w95sock.c b/pr/src/md/windows/w95sock.c
index d36ffdcd..58944703 100644
--- a/pr/src/md/windows/w95sock.c
+++ b/pr/src/md/windows/w95sock.c
@@ -168,7 +168,6 @@ PRInt32
_PR_MD_BIND(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen)
{
PRInt32 rv;
- int one = 1;
rv = bind(fd->secret->md.osfd, (const struct sockaddr *)&(addr->inet), addrlen);
@@ -180,6 +179,20 @@ _PR_MD_BIND(PRFileDesc *fd, const PRNetAddr *addr, PRUint32 addrlen)
return 0;
}
+PRInt32
+_PR_MD_LISTEN(PRFileDesc *fd, PRIntn backlog)
+{
+ PRInt32 rv;
+
+ rv = listen(fd->secret->md.osfd, backlog);
+
+ if (rv == SOCKET_ERROR) {
+ _PR_MD_MAP_DEFAULT_ERROR(WSAGetLastError());
+ return -1;
+ }
+
+ return 0;
+}
PRInt32
_PR_MD_RECV(PRFileDesc *fd, void *buf, PRInt32 amount, PRIntn flags,
@@ -461,6 +474,7 @@ static PRInt32 socket_io_wait(
struct timeval tv;
PRThread *me = _PR_MD_CURRENT_THREAD();
PRIntervalTime elapsed, remaining;
+ PRBool wait_for_remaining;
fd_set rd_wr, ex;
int err, len;
@@ -547,8 +561,10 @@ static PRInt32 socket_io_wait(
* so that there is an upper limit on the delay
* before the interrupt bit is checked.
*/
+ wait_for_remaining = PR_TRUE;
tv.tv_sec = PR_IntervalToSeconds(remaining);
if (tv.tv_sec > _PR_INTERRUPT_CHECK_INTERVAL_SECS) {
+ wait_for_remaining = PR_FALSE;
tv.tv_sec = _PR_INTERRUPT_CHECK_INTERVAL_SECS;
tv.tv_usec = 0;
} else {
@@ -618,8 +634,12 @@ static PRInt32 socket_io_wait(
*/
if (rv == 0 )
{
- elapsed = PR_SecondsToInterval(tv.tv_sec)
- + PR_MicrosecondsToInterval(tv.tv_usec);
+ if (wait_for_remaining) {
+ elapsed = remaining;
+ } else {
+ elapsed = PR_SecondsToInterval(tv.tv_sec)
+ + PR_MicrosecondsToInterval(tv.tv_usec);
+ }
if (elapsed >= remaining) {
PR_SetError(PR_IO_TIMEOUT_ERROR, 0);
rv = -1;
diff --git a/pr/src/md/windows/w95thred.c b/pr/src/md/windows/w95thred.c
index a70f1620..f2519f42 100644
--- a/pr/src/md/windows/w95thred.c
+++ b/pr/src/md/windows/w95thred.c
@@ -106,6 +106,14 @@ _PR_MD_INIT_THREAD(PRThread *thread)
return PR_SUCCESS;
}
+static unsigned __stdcall
+pr_root(void *arg)
+{
+ PRThread *thread = (PRThread *)arg;
+ thread->md.start(thread);
+ return 0;
+}
+
PRStatus
_PR_MD_CREATE_THREAD(PRThread *thread,
void (*start)(void *),
@@ -115,14 +123,11 @@ _PR_MD_CREATE_THREAD(PRThread *thread,
PRUint32 stackSize)
{
+ thread->md.start = start;
thread->md.handle = (HANDLE) _beginthreadex(
NULL,
thread->stack->stackSize,
-#if defined(__MINGW32__)
- (void *)start,
-#else
- (unsigned (__stdcall *)(void *))start,
-#endif
+ pr_root,
(void *)thread,
CREATE_SUSPENDED,
&(thread->id));
@@ -131,7 +136,13 @@ _PR_MD_CREATE_THREAD(PRThread *thread,
}
thread->md.id = thread->id;
- _PR_MD_SET_PRIORITY(&(thread->md), priority);
+ /*
+ * On windows, a thread is created with a thread priority of
+ * THREAD_PRIORITY_NORMAL.
+ */
+ if (priority != PR_PRIORITY_NORMAL) {
+ _PR_MD_SET_PRIORITY(&(thread->md), priority);
+ }
/* Activate the thread */
if ( ResumeThread( thread->md.handle ) != -1)