Merge up from branch.
[shibboleth/cpp-sp.git] / oncrpc / svc.c
index 8b4a3fe..0077d6c 100644 (file)
@@ -60,6 +60,9 @@ static char sccsid[] = "@(#)svc.c 1.41 87/10/13 Copyr 1984 Sun Micro";
 #include <sys/errno.h>
 #include <rpc/rpc.h>
 #include <rpc/pmap_clnt.h>
+#include <pthread.h>
+
+pthread_mutex_t __thr_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 extern int errno;
 #endif
@@ -103,6 +106,11 @@ xprt_register(xprt)
        SVCXPRT *xprt;
 {
        register int sock = xprt->xp_sock;
+#ifdef WIN32
+    EnterCriticalSection(&__thr_mutex);
+#else
+    pthread_mutex_lock(&__thr_mutex);
+#endif
 
 #ifdef FD_SETSIZE
        if (xports == NULL) {
@@ -131,7 +139,7 @@ xprt_register(xprt)
                nt_rpc_report(str);
        }
 #else
-       if (sock < _rpc_dtablesize()) {
+       if (sock < FD_SETSIZE) {
                xports[sock] = xprt;
                FD_SET(sock, &svc_fdset);
        }
@@ -143,6 +151,11 @@ xprt_register(xprt)
        }
 #endif /* def FD_SETSIZE */
 
+#ifdef WIN32
+    LeaveCriticalSection(&__thr_mutex);
+#else
+    pthread_mutex_unlock(&__thr_mutex);
+#endif
 }
 
 /*
@@ -153,6 +166,11 @@ xprt_unregister(xprt)
        SVCXPRT *xprt;
 { 
        register int sock = xprt->xp_sock;
+#ifdef WIN32
+    EnterCriticalSection(&__thr_mutex);
+#else
+    pthread_mutex_lock(&__thr_mutex);
+#endif
 
 #ifdef FD_SETSIZE
 #ifdef WIN32
@@ -160,7 +178,7 @@ xprt_unregister(xprt)
                xports[sock] = (SVCXPRT *)0;
                FD_CLR((unsigned)sock, &svc_fdset);
 #else
-       if ((sock < _rpc_dtablesize()) && (xports[sock] == xprt)) {
+       if ((sock < FD_SETSIZE) && (xports[sock] == xprt)) {
                xports[sock] = (SVCXPRT *)0;
                FD_CLR(sock, &svc_fdset);
 #endif
@@ -171,6 +189,12 @@ xprt_unregister(xprt)
                svc_fds &= ~(1 << sock);
        }
 #endif /* def FD_SETSIZE */
+
+#ifdef WIN32
+    LeaveCriticalSection(&__thr_mutex);
+#else
+    pthread_mutex_unlock(&__thr_mutex);
+#endif
 }
 
 
@@ -479,20 +503,26 @@ svc_getreqset(readfds)
        for ( i=0; i<readfds->fd_count; i++ ) {
                sock = readfds->fd_array[i];
                /* sock has input waiting */
-               xprt = xports[sock];
+        EnterCriticalSection(&__thr_mutex);
+        xprt = xports[sock];
+        LeaveCriticalSection(&__thr_mutex);
 #else
-       setsize = _rpc_dtablesize();    
+       setsize = FD_SETSIZE;   
        maskp = (u_long *)readfds->fds_bits;
        for (sock = 0; sock < setsize; sock += NFDBITS) {
            for (mask = *maskp++; bit = ffs(mask); mask ^= (1 << (bit - 1))) {
                /* sock has input waiting */
-               xprt = xports[sock + bit - 1];
+        pthread_mutex_lock(&__thr_mutex);
+        xprt = xports[sock + bit - 1];
+        pthread_mutex_unlock(&__thr_mutex);
 #endif
 #else
        for (sock = 0; readfds_local != 0; sock++, readfds_local >>= 1) {
            if ((readfds_local & 1) != 0) {
                /* sock has input waiting */
-               xprt = xports[sock];
+        pthread_mutex_lock(&__thr_mutex);
+        xprt = xports[sock];
+        pthread_mutex_unlock(&__thr_mutex);
 #endif /* def FD_SETSIZE */
                /* now receive msgs from xprtprt (support batch calls) */
                do {
@@ -548,4 +578,6 @@ svc_getreqset(readfds)
                } while (stat == XPRT_MOREREQS);
            }
        }
+#if !defined(FD_SETSIZE) || !defined(WIN32)
 }
+#endif