Bug 888581 - Allow integrating NSPR/NSS sockets into non-NSPR event loops, Add PR_SocketPollingHandle(), r=rrelyea
authorMiloslav Trmač <mitr@redhat.com>
Tue, 22 Oct 2013 23:06:22 +0200
changeset 4482 1bd2f00745d3331562fcc89eddc69d445eb92c89
parent 4481 63dba74ed1d7e20ee843063bac14b273d8aeb13d
child 4483 77c4ee30c1feead289574946e678883836bb3a43
push id33
push userkaie@kuix.de
push dateTue, 22 Oct 2013 21:07:05 +0000
reviewersrrelyea
bugs888581
Bug 888581 - Allow integrating NSPR/NSS sockets into non-NSPR event loops, Add PR_SocketPollingHandle(), r=rrelyea
pr/include/prio.h
pr/include/private/pprio.h
pr/src/io/prsocket.c
pr/src/pthreads/ptio.c
--- a/pr/include/prio.h
+++ b/pr/include/prio.h
@@ -1995,11 +1995,38 @@ NSPR_API(PRInt32) PR_Poll(
 NSPR_API(PRFileDesc *) PR_NewPollableEvent(void);
 
 NSPR_API(PRStatus) PR_DestroyPollableEvent(PRFileDesc *event);
 
 NSPR_API(PRStatus) PR_SetPollableEvent(PRFileDesc *event);
 
 NSPR_API(PRStatus) PR_WaitForPollableEvent(PRFileDesc *event);
 
+
+#ifdef _WIN64
+typedef __int64 PROsfd;
+#else
+typedef PRInt32 PROsfd;
+#endif
+
+/*
+ ************************************************************************
+ * FUNCTION:    PR_SocketPollingHandle
+ * DESCRIPTION:
+ * Return an OS native file handle of a socket, suitable for poll() or select().
+ * The caller must not use the file handle for anything else.
+ *
+ * INPUTS:
+ *     PRFileDesc *fd
+ *       Points to a PRFileDesc object representing a socket.
+ * OUTPUTS:    None
+ * RETURN:
+ *     PROsfd
+ *        An OS native file handle suitable for poll() or select() (if provided
+ *        by the OS), or -1 on failure.  The handle must not be closed by the
+ *        caller, it will be automatically invalidated by PR_Close(fd).
+ ************************************************************************
+ */
+NSPR_API(PROsfd) PR_SocketPollingHandle(PRFileDesc *fd);
+
 PR_END_EXTERN_C
 
 #endif /* prio_h___ */
--- a/pr/include/private/pprio.h
+++ b/pr/include/private/pprio.h
@@ -15,22 +15,16 @@
 #include "prtypes.h"
 #include "prio.h"
 
 PR_BEGIN_EXTERN_C
 
 /************************************************************************/
 /************************************************************************/
 
-#ifdef _WIN64
-typedef __int64 PROsfd;
-#else
-typedef PRInt32 PROsfd;
-#endif
-
 /* Return the method tables for files, tcp sockets and udp sockets */
 NSPR_API(const PRIOMethods*)    PR_GetFileMethods(void);
 NSPR_API(const PRIOMethods*)    PR_GetTCPMethods(void);
 NSPR_API(const PRIOMethods*)    PR_GetUDPMethods(void);
 NSPR_API(const PRIOMethods*)    PR_GetPipeMethods(void);
 
 /*
 ** Convert a NSPR socket handle to a native socket handle.
--- a/pr/src/io/prsocket.c
+++ b/pr/src/io/prsocket.c
@@ -1543,16 +1543,24 @@ PR_FileDesc2NativeHandle(PRFileDesc *fd)
     }
     if (!fd) {
         PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
         return -1;
     }
     return fd->secret->md.osfd;
 }
 
+PR_IMPLEMENT(PROsfd)
+PR_SocketPollingHandle(PRFileDesc *fd)
+{
+    if (!_pr_initialized) _PR_ImplicitInitialization();
+    /* Should this refuse a non-socket fd? */
+    return PR_FileDesc2NativeHandle(fd);
+}
+
 PR_IMPLEMENT(void)
 PR_ChangeFileDescNativeHandle(PRFileDesc *fd, PROsfd handle)
 {
 	if (fd)
 		fd->secret->md.osfd = handle;
 }
 
 /*
--- a/pr/src/pthreads/ptio.c
+++ b/pr/src/pthreads/ptio.c
@@ -4627,16 +4627,23 @@ PR_IMPLEMENT(PRInt32) PR_FileDesc2Native
     PRInt32 osfd = -1;
     bottom = (NULL == bottom) ?
         NULL : PR_GetIdentitiesLayer(bottom, PR_NSPR_IO_LAYER);
     if (NULL == bottom) PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
     else osfd = bottom->secret->md.osfd;
     return osfd;
 }  /* PR_FileDesc2NativeHandle */
 
+PR_IMPLEMENT(PROsfd) PR_SocketPollingHandle(PRFileDesc *fd)
+{
+    if (!_pr_initialized) _PR_ImplicitInitialization();
+    /* Should this refuse a non-socket fd? */
+    return PR_FileDesc2NativeHandle(fd);
+}  /* PR_SocketPollingHandle */
+
 PR_IMPLEMENT(void) PR_ChangeFileDescNativeHandle(PRFileDesc *fd,
     PRInt32 handle)
 {
     if (fd) fd->secret->md.osfd = handle;
 }  /*  PR_ChangeFileDescNativeHandle*/
 
 PR_IMPLEMENT(PRStatus) PR_LockFile(PRFileDesc *fd)
 {