Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[fs/lustre-release.git] / lnet / ulnds / procapi.c
index 2a3fbd8..00a7ae4 100644 (file)
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#ifndef __CYGWIN__
+#include <syscall.h>
+#endif
+#include <sys/socket.h>
 #include <procbridge.h>
 #include <pqtimer.h>
 #include <dispatch.h>
 #include <errno.h>
 
 
+/* XXX CFS workaround, to give a chance to let nal thread wake up
+ * from waiting in select
+ */
+static int procbridge_notifier_handler(void *arg)
+{
+    static char buf[8];
+    procbridge p = (procbridge) arg;
+
+    syscall(SYS_read, p->notifier[1], buf, sizeof(buf));
+    return 1;
+}
+
+void procbridge_wakeup_nal(procbridge p)
+{
+    static char buf[8];
+    syscall(SYS_write, p->notifier[0], buf, sizeof(buf));
+}
+
 /* Function: forward
  * Arguments: nal_t *nal: pointer to my top-side nal structure
  *            id: the command to pass to the lower layer
@@ -49,7 +71,7 @@
  *   side, and collects the result
  */
 static int procbridge_forward(nal_t *n, int id, void *args, size_t args_len,
-                             void *ret, ptl_size_t ret_len)
+                             void *ret, size_t ret_len)
 {
     bridge b = (bridge) n->nal_data;
 
@@ -79,6 +101,7 @@ static int procbridge_shutdown(nal_t *n, int ni)
     procbridge p=(procbridge)b->local;
 
     p->nal_flags |= NAL_FLAG_STOPPING;
+    procbridge_wakeup_nal(p);
 
     do {
         pthread_mutex_lock(&p->mutex);
@@ -104,6 +127,22 @@ static int procbridge_validate(nal_t *nal, void *base, size_t extent)
 }
 
 
+static void procbridge_lock(nal_t * n, unsigned long *flags)
+{
+    bridge b=(bridge)n->nal_data;
+    procbridge p=(procbridge)b->local;
+
+    pthread_mutex_lock(&p->mutex);
+}
+
+static void procbridge_unlock(nal_t * n, unsigned long *flags)
+{
+    bridge b=(bridge)n->nal_data;
+    procbridge p=(procbridge)b->local;
+
+    pthread_mutex_unlock(&p->mutex);
+}
+
 /* Function: yield
  * Arguments:  pid:
  *
@@ -112,19 +151,43 @@ static int procbridge_validate(nal_t *nal, void *base, size_t extent)
  *   overload it to explicitly block until signalled by the
  *   lower half.
  */
-static void procbridge_yield(nal_t *n)
+static int procbridge_yield(nal_t *n, unsigned long *flags, int milliseconds)
 {
     bridge b=(bridge)n->nal_data;
     procbridge p=(procbridge)b->local;
 
-    pthread_mutex_lock(&p->mutex);
-    pthread_cond_wait(&p->cond,&p->mutex);
-    pthread_mutex_unlock(&p->mutex);
+    if (milliseconds == 0)
+            return 0;
+            
+    if (milliseconds < 0) {
+        pthread_cond_wait(&p->cond,&p->mutex);
+    } else {
+        struct timeval then;
+        struct timeval now;
+        struct timespec timeout;
+
+        gettimeofday(&then, NULL);
+        timeout.tv_sec = then.tv_sec + milliseconds/1000;
+        timeout.tv_nsec = then.tv_usec * 1000 + milliseconds % 1000 * 1000000;
+        if (timeout.tv_nsec >= 1000000000) {
+                timeout.tv_sec++;
+                timeout.tv_nsec -= 1000000000;
+        }
+
+        pthread_cond_timedwait(&p->cond, &p->mutex, &timeout);
+
+        gettimeofday(&now, NULL);
+        milliseconds -= (now.tv_sec - then.tv_sec) * 1000 + 
+                        (now.tv_usec - then.tv_usec) / 1000;
+        
+        if (milliseconds < 0)
+                milliseconds = 0;
+    }
+
+    return (milliseconds);
 }
 
 
-static void procbridge_lock(nal_t * nal, unsigned long *flags){}
-static void procbridge_unlock(nal_t * nal, unsigned long *flags){}
 /* api_nal
  *  the interface vector to allow the generic code to access
  *  this nal. this is seperate from the library side nal_cb.
@@ -192,8 +255,20 @@ nal_t *procbridge_interface(int num_interface,
     pthread_mutex_init(&p->mutex,0);
     pthread_cond_init(&p->cond, 0);
     p->nal_flags = 0;
-    pthread_mutex_init(&p->nal_cb_lock, 0);
 
+    /* initialize notifier */
+    if (socketpair(AF_UNIX, SOCK_STREAM, 0, p->notifier)) {
+        perror("socketpair failed");
+        return NULL;
+    }
+
+    if (!register_io_handler(p->notifier[1], READ_HANDLER,
+                procbridge_notifier_handler, p)) {
+        perror("fail to register notifier handler");
+        return NULL;
+    }
+
+    /* create nal thread */
     if (pthread_create(&p->t, NULL, nal_thread, &args)) {
         perror("nal_init: pthread_create");
         return(NULL);