From owner-svn-src-user@FreeBSD.ORG  Wed Jan  6 01:59:20 2010
Return-Path: <owner-svn-src-user@FreeBSD.ORG>
Delivered-To: svn-src-user@freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id CEC42106566B;
	Wed,  6 Jan 2010 01:59:20 +0000 (UTC)
	(envelope-from kmacy@FreeBSD.org)
Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c])
	by mx1.freebsd.org (Postfix) with ESMTP id B3EE48FC08;
	Wed,  6 Jan 2010 01:59:20 +0000 (UTC)
Received: from svn.freebsd.org (localhost [127.0.0.1])
	by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o061xKxr079657;
	Wed, 6 Jan 2010 01:59:20 GMT (envelope-from kmacy@svn.freebsd.org)
Received: (from kmacy@localhost)
	by svn.freebsd.org (8.14.3/8.14.3/Submit) id o061xKu7079656;
	Wed, 6 Jan 2010 01:59:20 GMT (envelope-from kmacy@svn.freebsd.org)
Message-Id: <201001060159.o061xKu7079656@svn.freebsd.org>
From: Kip Macy <kmacy@FreeBSD.org>
Date: Wed, 6 Jan 2010 01:59:20 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-user@freebsd.org
X-SVN-Group: user
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Cc: 
Subject: svn commit: r201628 - user/kmacy/releng_8_rump/lib/libunet
X-BeenThere: svn-src-user@freebsd.org
X-Mailman-Version: 2.1.5
Precedence: list
List-Id: "SVN commit messages for the experimental &quot; user&quot;
	src tree" <svn-src-user.freebsd.org>
List-Unsubscribe: <http://lists.freebsd.org/mailman/listinfo/svn-src-user>,
	<mailto:svn-src-user-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-user>
List-Post: <mailto:svn-src-user@freebsd.org>
List-Help: <mailto:svn-src-user-request@freebsd.org?subject=help>
List-Subscribe: <http://lists.freebsd.org/mailman/listinfo/svn-src-user>,
	<mailto:svn-src-user-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Wed, 06 Jan 2010 01:59:21 -0000

Author: kmacy
Date: Wed Jan  6 01:59:20 2010
New Revision: 201628
URL: http://svn.freebsd.org/changeset/base/201628

Log:
  implement most of sleep / wakeup

Modified:
  user/kmacy/releng_8_rump/lib/libunet/unet_kern_synch.c

Modified: user/kmacy/releng_8_rump/lib/libunet/unet_kern_synch.c
==============================================================================
--- user/kmacy/releng_8_rump/lib/libunet/unet_kern_synch.c	Wed Jan  6 00:20:37 2010	(r201627)
+++ user/kmacy/releng_8_rump/lib/libunet/unet_kern_synch.c	Wed Jan  6 01:59:20 2010	(r201628)
@@ -11,6 +11,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/kernel.h>
 #include <sys/ktr.h>
 #include <sys/lock.h>
+#include <sys/malloc.h>
 #include <sys/mutex.h>
 #include <sys/proc.h>
 #include <sys/resourcevar.h>
@@ -20,15 +21,76 @@ __FBSDID("$FreeBSD$");
 #include <sys/smp.h>
 #include <sys/sx.h>
 #include <sys/sysctl.h>
-#include <sys/sysproto.h>
 #include <sys/vmmeter.h>
 #ifdef KTRACE
 #include <sys/uio.h>
 #include <sys/ktrace.h>
 #endif
 
+#include <pthread.h>
+
 int	hogticks;
 
+typedef struct sleep_entry {
+	void 		*chan;
+	const char 	*wmesg;
+	pthread_cond_t	cond;
+	int		waiters;
+} *sleep_entry_t;
+
+static void synch_setup(void *dummy);
+SYSINIT(synch_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, synch_setup,
+    NULL);
+
+static struct se_head *se_active;
+static u_long se_hashmask;
+static pthread_mutex_t synch_lock;
+
+static void
+synch_setup(void *arg)
+{
+	pthread_mutexattr_t attr;
+
+	pthread_mutexattr_init(&attr);
+	pthread_mutex_init(&synch_lock, &attr);
+	se_active = hashinit(64, M_TEMP, &se_hashmask);
+}
+
+sleep_entry_t
+se_alloc(void *chan, const char *wmesg)
+{
+	sleep_entry_t se;
+	pthread_condattr_t attr;
+
+	se = malloc(sizeof(*se), M_DEVBUF, 0);
+	se->chan = chan;
+	se->wmesg = wmesg;
+	pthread_condattr_init(&attr);
+	pthread_cond_init(&se->cond, &attr);
+
+	/* insert in hash table */
+	return (se);
+}
+
+sleep_entry_t
+se_lookup(void *chan)
+{
+	/* lookup in hashtable */
+	return (NULL);
+}
+
+void
+se_free(sleep_entry_t se)
+{
+
+	if (--se->waiters == 0) {
+		/* unlink se */
+		pthread_cond_destroy(&se->cond);
+		free(se, M_DEVBUF);
+	}
+}
+		
+
 /*
  * General sleep call.  Suspends the current thread until a wakeup is
  * performed on the specified identifier.  The thread will then be made
@@ -48,22 +110,49 @@ int
 _sleep(void *ident, struct lock_object *lock, int priority,
     const char *wmesg, int timo)
 {
+	sleep_entry_t se;
+	int rv;
+	struct timespec ts;
+
+	pthread_mutex_lock(&synch_lock);
+	if ((se = se_lookup(ident)) != NULL)
+		se->waiters++;
+	else
+		se = se_alloc(ident, wmesg);
+	pthread_mutex_unlock(&synch_lock);
+	
+	if (timo)
+		rv = pthread_cond_timedwait(&se->cond, &lock->lo_mutex, &ts);
+	else
+		rv = pthread_cond_wait(&se->cond, &lock->lo_mutex);
+
+	pthread_mutex_lock(&synch_lock);
+	se_free(se);
+	pthread_mutex_unlock(&synch_lock);	
 
-	panic("");
+	return (rv);
 }
 
 void
 wakeup(void *chan)
 {
-	panic("");
-	
+	sleep_entry_t se;
+
+	pthread_mutex_lock(&synch_lock);
+	if ((se = se_lookup(chan)) != NULL)
+		pthread_cond_broadcast(&se->cond);
+	pthread_mutex_unlock(&synch_lock);
 }
 
 
 void
 wakeup_one(void *chan)
 {
+	sleep_entry_t se;
 
-	panic("");
+	pthread_mutex_lock(&synch_lock);
+	if ((se = se_lookup(chan)) != NULL)
+		pthread_cond_signal(&se->cond);
+	pthread_mutex_unlock(&synch_lock);
 }