rx: Build libtool library
[openafs.git] / src / rx / rx_pthread.c
index eb4f0e0..a1af2b4 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
+#include <afs/opr.h>
 
-#include <sys/types.h>
-#include <errno.h>
-#include <signal.h>
-#include <string.h>
-#ifndef AFS_NT40_ENV
-# include <sys/socket.h>
-# include <sys/file.h>
-# include <netdb.h>
-# include <netinet/in.h>
-# include <net/if.h>
-# include <sys/ioctl.h>
-# include <sys/time.h>
-# include <unistd.h>
-#endif
-#include <sys/stat.h>
-#include <rx/rx.h>
-#include <rx/rx_globals.h>
 #include <assert.h>
-#include <rx/rx_pthread.h>
-#include <rx/rx_clock.h>
 
-/*
- * Number of times the event handling thread was signalled because a new
- * event was scheduled earlier than the lastest event.
- *
- * Protected by event_handler_mutex
- */
-static long rx_pthread_n_event_wakeups;
+#ifdef AFS_PTHREAD_ENV
+
+#include "rx.h"
+#include "rx_globals.h"
+#include "rx_pthread.h"
+#include "rx_clock.h"
+#include "rx_atomic.h"
+#include "rx_internal.h"
+#include "rx_pthread.h"
+#ifdef AFS_NT40_ENV
+#include "rx_xmit_nt.h"
+#endif
+
+static void rxi_SetThreadNum(int threadID);
 
 /* Set rx_pthread_event_rescheduled if event_handler should just try
  * again instead of sleeping.
@@ -72,6 +62,13 @@ static int listeners_started = 0;
 afs_kmutex_t rx_clock_mutex;
 struct clock rxi_clockNow;
 
+static rx_atomic_t threadHiNum;
+
+int
+rx_NewThreadId(void) {
+    return rx_atomic_inc_and_read(&threadHiNum);
+}
+
 /*
  * Delay the current thread the specified number of seconds.
  */
@@ -99,8 +96,7 @@ server_entry(void *argp)
     void (*server_proc) (void *) = (void (*)(void *))argp;
     server_proc(NULL);
     dpf(("rx_pthread.c: server_entry: Server proc returned unexpectedly\n"));
-    exit(1);
-    return NULL;
+    return (void *) -1; /* reused as return value, see pthread(3) */
 }
 
 /*
@@ -114,14 +110,11 @@ rxi_StartServerProc(void *(*proc) (void *), int stacksize)
     AFS_SIGSET_DECL;
 
     if (pthread_attr_init(&tattr) != 0) {
-       dpf(("Unable to Create Rx server thread (pthread_attr_init)\n"));
-       exit(1);
+       osi_Panic("Unable to Create Rx server thread (pthread_attr_init)\n");
     }
 
     if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) != 0) {
-       dpf
-           (("Unable to Create Rx server thread (pthread_attr_setdetachstate)\n"));
-       exit(1);
+       osi_Panic("Unable to Create Rx server thread (pthread_attr_setdetachstate)\n");
     }
 
     /*
@@ -129,8 +122,7 @@ rxi_StartServerProc(void *(*proc) (void *), int stacksize)
      */
     AFS_SIGSET_CLEAR();
     if (pthread_create(&thread, &tattr, server_entry, (void *)proc) != 0) {
-       dpf(("Unable to Create Rx server thread\n"));
-       exit(1);
+       osi_Panic("Unable to Create Rx server thread\n");
     }
     AFS_SIGSET_RESTORE();
 }
@@ -141,7 +133,6 @@ rxi_StartServerProc(void *(*proc) (void *), int stacksize)
 static void *
 event_handler(void *argp)
 {
-    struct clock rx_pthread_last_event_wait_time = { 0, 0 };
     unsigned long rx_pthread_n_event_expired = 0;
     unsigned long rx_pthread_n_event_waits = 0;
     long rx_pthread_n_event_woken = 0;
@@ -175,8 +166,8 @@ event_handler(void *argp)
        error = CV_TIMEDWAIT(&rx_event_handler_cond, &event_handler_mutex, &rx_pthread_next_event_time);
         if (error == 0) {
            rx_pthread_n_event_woken++;
-        } 
-#ifdef AFS_NT40_ENV        
+        }
+#ifdef AFS_NT40_ENV
         else if (error == ETIMEDOUT) {
            rx_pthread_n_event_expired++;
        } else {
@@ -224,6 +215,9 @@ rxi_ListenerProc(osi_socket sock, int *tnop, struct rx_call **newcallp)
     MUTEX_EXIT(&listener_mutex);
 
     for (;;) {
+        /* See if a check for additional packets was issued */
+        rx_CheckPackets();
+
        /*
         * Grab a new packet only if necessary (otherwise re-use the old one)
         */
@@ -232,8 +226,7 @@ rxi_ListenerProc(osi_socket sock, int *tnop, struct rx_call **newcallp)
        } else {
            if (!(p = rxi_AllocPacket(RX_PACKET_CLASS_RECEIVE))) {
                /* Could this happen with multiple socket listeners? */
-               dpf(("rxi_Listener: no packets!"));     /* Shouldn't happen */
-               exit(1);
+               osi_Panic("rxi_Listener: no packets!"); /* Shouldn't happen */
            }
        }
 
@@ -257,19 +250,19 @@ static void *
 rx_ListenerProc(void *argp)
 {
     int threadID;
-    osi_socket sock = (osi_socket)argp;
+    osi_socket sock = (osi_socket)(intptr_t)argp;
     struct rx_call *newcall;
 
     while (1) {
        newcall = NULL;
        threadID = -1;
        rxi_ListenerProc(sock, &threadID, &newcall);
-       /* assert(threadID != -1); */
-       /* assert(newcall != NULL); */
+       /* osi_Assert(threadID != -1); */
+       /* osi_Assert(newcall != NULL); */
        sock = OSI_NULLSOCKET;
-       assert(pthread_setspecific(rx_thread_id_key, (void *)threadID) == 0);
+       rxi_SetThreadNum(threadID);
        rxi_ServerProc(threadID, newcall, &sock);
-       /* assert(sock != OSI_NULLSOCKET); */
+       /* osi_Assert(sock != OSI_NULLSOCKET); */
     }
     /* not reached */
     return NULL;
@@ -291,7 +284,7 @@ rx_ServerProc(void * dummy)
     /* threadID is used for making decisions in GetCall.  Get it by bumping
      * number of threads handling incoming calls */
     /* Unique thread ID: used for scheduling purposes *and* as index into
-     * the host hold table (fileserver). 
+     * the host hold table (fileserver).
      * The previously used rxi_availProcs is unsuitable as it
      * will already go up and down as packets arrive while the server
      * threads are still initialising! The recently introduced
@@ -302,7 +295,7 @@ rx_ServerProc(void * dummy)
      * thread... chose the latter.
      */
     MUTEX_ENTER(&rx_pthread_mutex);
-    threadID = ++rxi_pthread_hinum;
+    threadID = rx_NewThreadId();
     if (rxi_fcfs_thread_num == 0 && rxi_fcfs_thread_num != threadID)
        rxi_fcfs_thread_num = threadID;
     MUTEX_EXIT(&rx_pthread_mutex);
@@ -311,13 +304,13 @@ rx_ServerProc(void * dummy)
 
     while (1) {
        sock = OSI_NULLSOCKET;
-       assert(pthread_setspecific(rx_thread_id_key, (void *)threadID) == 0);
+       rxi_SetThreadNum(threadID);
        rxi_ServerProc(threadID, newcall, &sock);
-       /* assert(sock != OSI_NULLSOCKET); */
+       /* osi_Assert(sock != OSI_NULLSOCKET); */
        newcall = NULL;
        rxi_ListenerProc(sock, &threadID, &newcall);
-       /* assert(threadID != -1); */
-       /* assert(newcall != NULL); */
+       /* osi_Assert(threadID != -1); */
+       /* osi_Assert(newcall != NULL); */
     }
     /* not reached */
     return NULL;
@@ -328,7 +321,7 @@ rx_ServerProc(void * dummy)
  * listener processes (one for each socket); these are started by GetUdpSocket.
  *
  * The event handling process *is* started here (the old listener used
- * to also handle events). The listener threads can't actually start 
+ * to also handle events). The listener threads can't actually start
  * listening until rxi_StartListener is called because most of R may not
  * be initialized when rxi_Listen is called.
  */
@@ -342,26 +335,19 @@ rxi_StartListener(void)
                return;
 
     if (pthread_attr_init(&tattr) != 0) {
-       dpf
-           (("Unable to create Rx event handling thread (pthread_attr_init)\n"));
-       exit(1);
+       osi_Panic("Unable to create Rx event handling thread (pthread_attr_init)\n");
     }
 
     if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) != 0) {
-       dpf
-           (("Unable to create Rx event handling thread (pthread_attr_setdetachstate)\n"));
-       exit(1);
+       osi_Panic("Unable to create Rx event handling thread (pthread_attr_setdetachstate)\n");
     }
 
     AFS_SIGSET_CLEAR();
     if (pthread_create(&event_handler_thread, &tattr, event_handler, NULL) !=
        0) {
-       dpf(("Unable to create Rx event handling thread\n"));
-       exit(1);
+       osi_Panic("Unable to create Rx event handling thread\n");
     }
-    MUTEX_ENTER(&rx_pthread_mutex);
-    ++rxi_pthread_hinum;
-    MUTEX_EXIT(&rx_pthread_mutex);
+    rx_NewThreadId();
     AFS_SIGSET_RESTORE();
 
     MUTEX_ENTER(&listener_mutex);
@@ -382,25 +368,18 @@ rxi_Listen(osi_socket sock)
     AFS_SIGSET_DECL;
 
     if (pthread_attr_init(&tattr) != 0) {
-       dpf
-           (("Unable to create socket listener thread (pthread_attr_init)\n"));
-       exit(1);
+       osi_Panic("Unable to create socket listener thread (pthread_attr_init)\n");
     }
 
     if (pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED) != 0) {
-       dpf
-           (("Unable to create socket listener thread (pthread_attr_setdetachstate)\n"));
-       exit(1);
+       osi_Panic("Unable to create socket listener thread (pthread_attr_setdetachstate)\n");
     }
 
     AFS_SIGSET_CLEAR();
-    if (pthread_create(&thread, &tattr, rx_ListenerProc, (void *)sock) != 0) {
-       dpf(("Unable to create socket listener thread\n"));
-       exit(1);
+    if (pthread_create(&thread, &tattr, rx_ListenerProc, (void *)(intptr_t)sock) != 0) {
+       osi_Panic("Unable to create socket listener thread\n");
     }
-    MUTEX_ENTER(&rx_pthread_mutex);
-    ++rxi_pthread_hinum;
-    MUTEX_EXIT(&rx_pthread_mutex);
+    rx_NewThreadId();
     AFS_SIGSET_RESTORE();
     return 0;
 }
@@ -414,7 +393,7 @@ int
 rxi_Recvmsg(osi_socket socket, struct msghdr *msg_p, int flags)
 {
     int ret;
-#if defined(HAVE_LINUX_ERRQUEUE_H) && defined(ADAPT_PMTU)
+#ifdef AFS_RXERRQ_ENV
     while((rxi_HandleSocketError(socket)) > 0)
       ;
 #endif
@@ -443,7 +422,7 @@ rxi_Sendmsg(osi_socket socket, struct msghdr *msg_p, int flags)
 #ifndef AFS_NT40_ENV
         if (errno > 0)
           return -errno;
-#elif
+#else
             if (WSAGetLastError() > 0)
               return -WSAGetLastError();
 #endif
@@ -454,9 +433,8 @@ rxi_Sendmsg(osi_socket socket, struct msghdr *msg_p, int flags)
 
 struct rx_ts_info_t * rx_ts_info_init(void) {
     struct rx_ts_info_t * rx_ts_info;
-    rx_ts_info = (rx_ts_info_t *) malloc(sizeof(rx_ts_info_t));
-    assert(rx_ts_info != NULL && pthread_setspecific(rx_ts_info_key, rx_ts_info) == 0);
-    memset(rx_ts_info, 0, sizeof(rx_ts_info_t));
+    rx_ts_info = calloc(1, sizeof(rx_ts_info_t));
+    osi_Assert(rx_ts_info != NULL && pthread_setspecific(rx_ts_info_key, rx_ts_info) == 0);
 #ifdef RX_ENABLE_TSFPQ
     queue_Init(&rx_ts_info->_FPQ);
 
@@ -467,3 +445,25 @@ struct rx_ts_info_t * rx_ts_info_init(void) {
 #endif /* RX_ENABLE_TSFPQ */
     return rx_ts_info;
 }
+
+int
+rx_GetThreadNum(void) {
+    return (intptr_t)pthread_getspecific(rx_thread_id_key);
+}
+
+static void
+rxi_SetThreadNum(int threadID) {
+    osi_Assert(pthread_setspecific(rx_thread_id_key,
+                                  (void *)(intptr_t)threadID) == 0);
+}
+
+int
+rx_SetThreadNum(void) {
+    int threadId;
+
+    threadId = rx_NewThreadId();
+    rxi_SetThreadNum(threadId);
+    return threadId;
+}
+
+#endif