[prev in list] [next in list] [prev in thread] [next in thread] 

List:       hurd-bug
Subject:    Cthread2Pthread
From:       Vicente Hernando Ara <zenton () es ! gnu ! org>
Date:       2002-12-25 7:17:31
[Download RAW message or body]

Hi all!

Here is attached a diff of libpipe directory, with code changed to
pthreads.
I have substituted condition_implies function with a pointer trick.

Waiting for some feedback. ;)

There are also some code for checking in
http://es.gnu.org/~zenton/Pthread

Thanks,
Vicente.

PD: Merry Christmas to all. ;)

["libpipe.diff" (libpipe.diff)]

diff -ur hurd-20021118_without-pthread/libpipe/Makefile libpipe/Makefile
--- hurd-20021118_without-pthread/libpipe/Makefile	Tue Aug 20 03:44:25 2002
+++ libpipe/Makefile	Wed Dec 25 09:15:40 2002
@@ -26,7 +26,7 @@
 LCLHDRS = pipe.h pq.h
 
 OBJS = $(SRCS:.c=.o)
-HURDLIBS=threads ports
+HURDLIBS=pthread ports
 
 include ../Makeconf
 
diff -ur hurd-20021118_without-pthread/libpipe/pipe.c libpipe/pipe.c
--- hurd-20021118_without-pthread/libpipe/pipe.c	Tue Aug 20 03:44:25 2002
+++ libpipe/pipe.c	Wed Dec 25 09:21:27 2002
@@ -35,7 +35,17 @@
 }
 
 /* Hold this lock before attempting to lock multiple pipes. */
-struct mutex pipe_multiple_lock = MUTEX_INITIALIZER;
+pthread_mutex_t pipe_multiple_lock; // = PTHREAD_MUTEX_INITIALIZER;
+
+/* Added to initialize the mutex. */
+void pipe_multiple_lock_init (void) __attribute__ ((constructor));
+          
+void
+pipe_multiple_lock_init (void)
+{
+  pthread_mutex_init (&pipe_multiple_lock, NULL);
+}
+
 
 /* ---------------------------------------------------------------- */
 
@@ -60,11 +70,12 @@
   bzero (&new->read_time, sizeof (new->read_time));
   bzero (&new->write_time, sizeof (new->write_time));
 
-  condition_init (&new->pending_reads);
-  condition_init (&new->pending_read_selects);
-  condition_init (&new->pending_writes);
-  condition_init (&new->pending_write_selects);
-  mutex_init (&new->lock);
+  pthread_cond_init (&new->pending_reads, NULL);
+  pthread_cond_init (&new->pending_read_selects, NULL);
+  pthread_cond_init (&new->pending_writes, NULL);
+  pthread_cond_init (&new->pending_write_selects, NULL);
+  new->pending_selects = NULL;  /* We have no implicated conditions yet. */
+  pthread_mutex_init (&new->lock, NULL);
 
   pq_create (&new->queue);
 
@@ -111,11 +122,13 @@
 	  if (pipe->writers)
 	    /* Wake up writers for the bad news... */
 	    {
-	      condition_broadcast (&pipe->pending_writes);
-	      condition_broadcast (&pipe->pending_write_selects);
+	      pthread_cond_broadcast (&pipe->pending_writes);
+	      pthread_cond_broadcast (&pipe->pending_write_selects);
+	      if (pipe->pending_selects)
+		pthread_cond_broadcast (pipe->pending_selects);
 	    }
 	}
-      mutex_unlock (&pipe->lock);
+      pthread_mutex_unlock (&pipe->lock);
     }
 }
 
@@ -133,11 +146,13 @@
 	  if (pipe->readers)
 	    /* Wake up readers for the bad news... */
 	    {
-	      condition_broadcast (&pipe->pending_reads);
-	      condition_broadcast (&pipe->pending_read_selects);
+	      pthread_cond_broadcast (&pipe->pending_reads);
+	      pthread_cond_broadcast (&pipe->pending_read_selects);
+	      if (pipe->pending_selects)
+		pthread_cond_broadcast (pipe->pending_selects);
 	    }
 	}
-      mutex_unlock (&pipe->lock);
+      pthread_mutex_unlock (&pipe->lock);
     }
 }
 
@@ -157,36 +172,34 @@
 
   if (*select_type == SELECT_READ)
     {
-      mutex_lock (&rpipe->lock);
+      pthread_mutex_lock (&rpipe->lock);
       err = pipe_select_readable (rpipe, data_only);
-      mutex_unlock (&rpipe->lock);
+      pthread_mutex_unlock (&rpipe->lock);
     }
   else if (*select_type == SELECT_WRITE)
     {
-      mutex_lock (&wpipe->lock);
+      pthread_mutex_lock (&wpipe->lock);
       err = pipe_select_writable (wpipe);
-      mutex_unlock (&wpipe->lock);
+      pthread_mutex_unlock (&wpipe->lock);
     }
   else
     /* ugh */
     {
       int rpipe_blocked, wpipe_blocked;
-      struct condition pending_read_write_select;
+      pthread_cond_t pending_read_write_select;
       size_t wlimit = wpipe->write_limit;
-      struct mutex *lock =
+      pthread_mutex_t *lock =
 	(wpipe == rpipe ? &rpipe->lock : &pipe_multiple_lock);
 
-      condition_init (&pending_read_write_select);
-      condition_implies (&rpipe->pending_read_selects,
-			 &pending_read_write_select);
-      condition_implies (&wpipe->pending_write_selects,
-			 &pending_read_write_select);
+      pthread_cond_init (&pending_read_write_select, NULL);
 
-      mutex_lock (lock);
+      pthread_mutex_lock (lock);
+      rpipe->pending_selects = &pending_read_write_select;
+      wpipe->pending_selects = &pending_read_write_select;
       if (rpipe != wpipe)
 	{
-	  mutex_lock (&rpipe->lock);
-	  mutex_lock (&wpipe->lock);
+	  pthread_mutex_lock (&rpipe->lock);
+	  pthread_mutex_lock (&wpipe->lock);
 	}
 
       rpipe_blocked =
@@ -197,15 +210,15 @@
 	{
 	  if (rpipe != wpipe)
 	    {
-	      mutex_unlock (&rpipe->lock);
-	      mutex_unlock (&wpipe->lock);
+	      pthread_mutex_unlock (&rpipe->lock);
+	      pthread_mutex_unlock (&wpipe->lock);
 	    }
 	  if (hurd_condition_wait (&pending_read_write_select, lock))
 	    err = EINTR;
 	  if (rpipe != wpipe)
 	    {
-	      mutex_lock (&rpipe->lock);
-	      mutex_lock (&wpipe->lock);
+	      pthread_mutex_lock (&rpipe->lock);
+	      pthread_mutex_lock (&wpipe->lock);
 	    }
 	  rpipe_blocked =
 	    ! ((rpipe->flags & PIPE_BROKEN)
@@ -225,15 +238,12 @@
 
       if (rpipe != wpipe)
 	{
-	  mutex_unlock (&rpipe->lock);
-	  mutex_unlock (&wpipe->lock);
+	  pthread_mutex_unlock (&rpipe->lock);
+	  pthread_mutex_unlock (&wpipe->lock);
 	}
-      mutex_unlock (lock);
-
-      condition_unimplies (&rpipe->pending_read_selects,
-			   &pending_read_write_select);
-      condition_unimplies (&wpipe->pending_write_selects,
-			   &pending_read_write_select);
+      rpipe->pending_selects = NULL;
+      wpipe->pending_selects = NULL;
+      pthread_mutex_unlock (lock);
     }
 
   return err;
@@ -298,14 +308,16 @@
       timestamp (&pipe->write_time);
 
       /* And wakeup anyone that might be interested in it.  */
-      condition_broadcast (&pipe->pending_reads);
-      mutex_unlock (&pipe->lock);
+      pthread_cond_broadcast (&pipe->pending_reads);
+      pthread_mutex_unlock (&pipe->lock);
 
-      mutex_lock (&pipe->lock);	/* Get back the lock on PIPE.  */
+      pthread_mutex_lock (&pipe->lock);
       /* Only wakeup selects if there's still data available.  */
       if (pipe_is_readable (pipe, 0))
 	{
-	  condition_broadcast (&pipe->pending_read_selects);
+	  pthread_cond_broadcast (&pipe->pending_read_selects);
+	  if (pipe->pending_selects)
+	    pthread_cond_broadcast (pipe->pending_selects);
 	  /* We leave PIPE locked here, assuming the caller will soon unlock
 	     it and allow others access.  */
 	}
@@ -399,14 +411,16 @@
       timestamp (&pipe->read_time);
 
       /* And wakeup anyone that might be interested in it.  */
-      condition_broadcast (&pipe->pending_writes);
-      mutex_unlock (&pipe->lock);
+      pthread_cond_broadcast (&pipe->pending_writes);
+      pthread_mutex_unlock (&pipe->lock);
 
-      mutex_lock (&pipe->lock);	/* Get back the lock on PIPE.  */
+      pthread_mutex_lock (&pipe->lock);
       /* Only wakeup selects if there's still writing space available.  */
       if (pipe_readable (pipe, 1) < pipe->write_limit)
 	{
-	  condition_broadcast (&pipe->pending_write_selects);
+	  pthread_cond_broadcast (&pipe->pending_write_selects);
+	  if (pipe->pending_selects)
+	    pthread_cond_broadcast (pipe->pending_selects);
 	  /* We leave PIPE locked here, assuming the caller will soon unlock
 	     it and allow others access.  */
 	}
diff -ur hurd-20021118_without-pthread/libpipe/pipe.h libpipe/pipe.h
--- hurd-20021118_without-pthread/libpipe/pipe.h	Tue Aug 20 03:44:25 2002
+++ libpipe/pipe.h	Wed Dec 25 09:18:56 2002
@@ -23,7 +23,8 @@
 
 #define EWOULDBLOCK EAGAIN /* XXX */
 
-#include <cthreads.h>		/* For conditions & mutexes */
+#include <pthread.h>          /* For conditions & mutexes */
+#include <stdlib.h>           /* free function declaration. */
 
 #include "pq.h"
 
@@ -79,11 +80,13 @@
   time_value_t read_time;
   time_value_t write_time;
 
-  struct condition pending_reads;
-  struct condition pending_read_selects;
+  pthread_cond_t pending_reads;
+  pthread_cond_t pending_read_selects;
 
-  struct condition pending_writes;
-  struct condition pending_write_selects;
+  pthread_cond_t pending_writes;
+  pthread_cond_t pending_write_selects;
+
+  pthread_cond_t *pending_selects;
 
   /* The maximum number of characters that this pipe will hold without
      further writes blocking.  */
@@ -92,7 +95,7 @@
   /* Write requests of less than this much are always done atomically.  */
   size_t write_atomic;
 
-  struct mutex lock;
+  pthread_mutex_t lock;
 
   /* A queue of incoming packets, of type either PACKET_TYPE_DATA or
      PACKET_TYPE_CONTROL.  Each data packet represents one datagram for
@@ -227,7 +230,7 @@
 PIPE_EI void
 pipe_acquire_reader (struct pipe *pipe)
 {
-  mutex_lock (&pipe->lock);
+  pthread_mutex_lock (&pipe->lock);
   if (pipe->readers++ == 0)
     _pipe_first_reader (pipe);
 }
@@ -236,7 +239,7 @@
 PIPE_EI void
 pipe_acquire_writer (struct pipe *pipe)
 {
-  mutex_lock (&pipe->lock);
+  pthread_mutex_lock (&pipe->lock);
   if (pipe->writers++ == 0)
     _pipe_first_writer (pipe);
 }
@@ -249,7 +252,7 @@
   if (--pipe->readers == 0)
     _pipe_no_readers (pipe);
   else
-    mutex_unlock (&pipe->lock);
+    pthread_mutex_unlock (&pipe->lock);
 }
 
 /* Decrement PIPE's (which should be locked) writer count and unlock it.  If
@@ -260,7 +263,7 @@
   if (--pipe->writers == 0)
     _pipe_no_writers (pipe);
   else
-    mutex_unlock (&pipe->lock);
+    pthread_mutex_unlock (&pipe->lock);
 }
 
 /* Increment PIPE's reader count.  PIPE should be unlocked.  */
@@ -268,7 +271,7 @@
 pipe_add_reader (struct pipe *pipe)
 {
   pipe_acquire_reader (pipe);
-  mutex_unlock (&pipe->lock);
+  pthread_mutex_unlock (&pipe->lock);
 }
 
 /* Increment PIPE's writer count.  PIPE should be unlocked.  */
@@ -276,7 +279,7 @@
 pipe_add_writer (struct pipe *pipe)
 {
   pipe_acquire_writer (pipe);
-  mutex_unlock (&pipe->lock);
+  pthread_mutex_unlock (&pipe->lock);
 }
 
 /* Decrement PIPE's (which should be unlocked) reader count and unlock it.  If
@@ -284,7 +287,7 @@
 PIPE_EI void
 pipe_remove_reader (struct pipe *pipe)
 {
-  mutex_lock (&pipe->lock);
+  pthread_mutex_lock (&pipe->lock);
   pipe_release_reader (pipe);
 }
 
@@ -293,7 +296,7 @@
 PIPE_EI void
 pipe_remove_writer (struct pipe *pipe)
 {
-  mutex_lock (&pipe->lock);
+  pthread_mutex_lock (&pipe->lock);
   pipe_release_writer (pipe);
 }
 
@@ -359,7 +362,7 @@
   pipe_recv (pipe, noblock, 0, source, data, data_len, amount, 0,0,0,0)
 
 /* Hold this lock before attempting to lock multiple pipes. */
-extern struct mutex pipe_multiple_lock;
+extern pthread_mutex_t pipe_multiple_lock;
 
 /* Return when either RPIPE is available for reading (if SELECT_READ is set
    in *SELECT_TYPE), or WPIPE is available for writing (if select_write is


_______________________________________________
Bug-hurd mailing list
Bug-hurd@gnu.org
http://mail.gnu.org/mailman/listinfo/bug-hurd


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic