From 53db4c7b29e5a82e3c8786b08437f6f9b686d2e1 Mon Sep 17 00:00:00 2001
From: Brian Behlendorf <behlendorf1@llnl.gov>
Date: Thu, 19 Mar 2009 19:43:09 -0700
Subject: [PATCH] Revert fix-pthreads moving to feature-pthreads branch

---
 cmd/zdb/zdb.c                          |   1 +
 cmd/zinject/translate.c                |   2 +
 cmd/ztest/ztest.c                      | 115 +++++++++--------
 lib/libuutil/uu_misc.c                 |   7 +-
 lib/libzpool/include/sys/zfs_context.h |  72 ++++++-----
 lib/libzpool/kernel.c                  | 168 ++++++++-----------------
 lib/libzpool/taskq.c                   |  14 +--
 7 files changed, 155 insertions(+), 224 deletions(-)

diff --git a/cmd/zdb/zdb.c b/cmd/zdb/zdb.c
index 09f23c1b26..0ced258656 100644
--- a/cmd/zdb/zdb.c
+++ b/cmd/zdb/zdb.c
@@ -52,6 +52,7 @@
 #include <sys/zfs_fuid.h>
 #include <sys/arc.h>
 #undef ZFS_MAXNAMELEN
+#undef verify
 #include <libzfs.h>
 
 const char cmdname[] = "zdb";
diff --git a/cmd/zinject/translate.c b/cmd/zinject/translate.c
index 0264aca6fd..c85e024b62 100644
--- a/cmd/zinject/translate.c
+++ b/cmd/zinject/translate.c
@@ -25,6 +25,8 @@
 
 #include <libzfs.h>
 
+#undef verify	/* both libzfs.h and zfs_context.h want to define this */
+
 #include <sys/zfs_context.h>
 
 #include <errno.h>
diff --git a/cmd/ztest/ztest.c b/cmd/ztest/ztest.c
index ce494fa797..4503a3d028 100644
--- a/cmd/ztest/ztest.c
+++ b/cmd/ztest/ztest.c
@@ -138,7 +138,7 @@ typedef struct ztest_args {
 	spa_t		*za_spa;
 	objset_t	*za_os;
 	zilog_t		*za_zilog;
-	pthread_t	za_thread;
+	thread_t	za_thread;
 	uint64_t	za_instance;
 	uint64_t	za_random;
 	uint64_t	za_diroff;
@@ -221,18 +221,18 @@ ztest_info_t ztest_info[] = {
  * Stuff we need to share writably between parent and child.
  */
 typedef struct ztest_shared {
-	pthread_mutex_t		zs_vdev_lock;
-	pthread_rwlock_t	zs_name_lock;
-	uint64_t		zs_vdev_primaries;
-	uint64_t		zs_vdev_aux;
-	uint64_t		zs_enospc_count;
-	hrtime_t		zs_start_time;
-	hrtime_t		zs_stop_time;
-	uint64_t		zs_alloc;
-	uint64_t		zs_space;
-	ztest_info_t		zs_info[ZTEST_FUNCS];
-	pthread_mutex_t		zs_sync_lock[ZTEST_SYNC_LOCKS];
-	uint64_t		zs_seq[ZTEST_SYNC_LOCKS];
+	mutex_t		zs_vdev_lock;
+	rwlock_t	zs_name_lock;
+	uint64_t	zs_vdev_primaries;
+	uint64_t	zs_vdev_aux;
+	uint64_t	zs_enospc_count;
+	hrtime_t	zs_start_time;
+	hrtime_t	zs_stop_time;
+	uint64_t	zs_alloc;
+	uint64_t	zs_space;
+	ztest_info_t	zs_info[ZTEST_FUNCS];
+	mutex_t		zs_sync_lock[ZTEST_SYNC_LOCKS];
+	uint64_t	zs_seq[ZTEST_SYNC_LOCKS];
 } ztest_shared_t;
 
 static char ztest_dev_template[] = "%s/%s.%llua";
@@ -804,7 +804,7 @@ ztest_spa_create_destroy(ztest_args_t *za)
 	 * Attempt to create an existing pool.  It shouldn't matter
 	 * what's in the nvroot; we should fail with EEXIST.
 	 */
-	(void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
+	(void) rw_rdlock(&ztest_shared->zs_name_lock);
 	nvroot = make_vdev_root("/dev/bogus", NULL, 0, 0, 0, 0, 0, 1);
 	error = spa_create(za->za_pool, nvroot, NULL, NULL, NULL);
 	nvlist_free(nvroot);
@@ -820,7 +820,7 @@ ztest_spa_create_destroy(ztest_args_t *za)
 		fatal(0, "spa_destroy() = %d", error);
 
 	spa_close(spa, FTAG);
-	(void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
+	(void) rw_unlock(&ztest_shared->zs_name_lock);
 }
 
 static vdev_t *
@@ -850,7 +850,7 @@ ztest_vdev_add_remove(ztest_args_t *za)
 	nvlist_t *nvroot;
 	int error;
 
-	(void) pthread_mutex_lock(&ztest_shared->zs_vdev_lock);
+	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
 
 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
 
@@ -868,7 +868,7 @@ ztest_vdev_add_remove(ztest_args_t *za)
 	error = spa_vdev_add(spa, nvroot);
 	nvlist_free(nvroot);
 
-	(void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
+	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
 
 	if (error == ENOSPC)
 		ztest_record_enospc("spa_vdev_add");
@@ -897,7 +897,7 @@ ztest_vdev_aux_add_remove(ztest_args_t *za)
 		aux = ZPOOL_CONFIG_L2CACHE;
 	}
 
-	(void) pthread_mutex_lock(&ztest_shared->zs_vdev_lock);
+	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
 
 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
 
@@ -953,7 +953,7 @@ ztest_vdev_aux_add_remove(ztest_args_t *za)
 			fatal(0, "spa_vdev_remove(%llu) = %d", guid, error);
 	}
 
-	(void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
+	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
 }
 
 /*
@@ -979,7 +979,7 @@ ztest_vdev_attach_detach(ztest_args_t *za)
 	int oldvd_is_log;
 	int error, expected_error;
 
-	(void) pthread_mutex_lock(&ztest_shared->zs_vdev_lock);
+	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
 
 	spa_config_enter(spa, SCL_VDEV, FTAG, RW_READER);
 
@@ -1039,7 +1039,7 @@ ztest_vdev_attach_detach(ztest_args_t *za)
 		if (error != 0 && error != ENODEV && error != EBUSY &&
 		    error != ENOTSUP)
 			fatal(0, "detach (%s) returned %d", oldpath, error);
-		(void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
+		(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
 		return;
 	}
 
@@ -1132,7 +1132,7 @@ ztest_vdev_attach_detach(ztest_args_t *za)
 		    (longlong_t)newsize, replacing, error, expected_error);
 	}
 
-	(void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
+	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
 }
 
 /*
@@ -1148,7 +1148,7 @@ ztest_vdev_LUN_growth(ztest_args_t *za)
 	size_t fsize;
 	int fd;
 
-	(void) pthread_mutex_lock(&ztest_shared->zs_vdev_lock);
+	(void) mutex_lock(&ztest_shared->zs_vdev_lock);
 
 	/*
 	 * Pick a random leaf vdev.
@@ -1179,7 +1179,7 @@ ztest_vdev_LUN_growth(ztest_args_t *za)
 		(void) close(fd);
 	}
 
-	(void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
+	(void) mutex_unlock(&ztest_shared->zs_vdev_lock);
 }
 
 /* ARGSUSED */
@@ -1278,7 +1278,7 @@ ztest_dmu_objset_create_destroy(ztest_args_t *za)
 	uint64_t seq;
 	uint64_t objects;
 
-	(void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
+	(void) rw_rdlock(&ztest_shared->zs_name_lock);
 	(void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
 	    (u_longlong_t)za->za_instance);
 
@@ -1321,7 +1321,7 @@ ztest_dmu_objset_create_destroy(ztest_args_t *za)
 	if (error) {
 		if (error == ENOSPC) {
 			ztest_record_enospc("dmu_objset_create");
-			(void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
+			(void) rw_unlock(&ztest_shared->zs_name_lock);
 			return;
 		}
 		fatal(0, "dmu_objset_create(%s) = %d", name, error);
@@ -1403,7 +1403,7 @@ ztest_dmu_objset_create_destroy(ztest_args_t *za)
 	if (error)
 		fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
 
-	(void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
+	(void) rw_unlock(&ztest_shared->zs_name_lock);
 }
 
 /*
@@ -1417,7 +1417,7 @@ ztest_dmu_snapshot_create_destroy(ztest_args_t *za)
 	char snapname[100];
 	char osname[MAXNAMELEN];
 
-	(void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
+	(void) rw_rdlock(&ztest_shared->zs_name_lock);
 	dmu_objset_name(os, osname);
 	(void) snprintf(snapname, 100, "%s@%llu", osname,
 	    (u_longlong_t)za->za_instance);
@@ -1430,7 +1430,7 @@ ztest_dmu_snapshot_create_destroy(ztest_args_t *za)
 		ztest_record_enospc("dmu_take_snapshot");
 	else if (error != 0 && error != EEXIST)
 		fatal(0, "dmu_take_snapshot() = %d", error);
-	(void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
+	(void) rw_unlock(&ztest_shared->zs_name_lock);
 }
 
 /*
@@ -1928,7 +1928,7 @@ ztest_dmu_write_parallel(ztest_args_t *za)
 	int bs = ZTEST_DIROBJ_BLOCKSIZE;
 	int do_free = 0;
 	uint64_t off, txg, txg_how;
-	pthread_mutex_t *lp;
+	mutex_t *lp;
 	char osname[MAXNAMELEN];
 	char iobuf[SPA_MAXBLOCKSIZE];
 	blkptr_t blk = { 0 };
@@ -1978,7 +1978,7 @@ ztest_dmu_write_parallel(ztest_args_t *za)
 	txg = dmu_tx_get_txg(tx);
 
 	lp = &ztest_shared->zs_sync_lock[b];
-	(void) pthread_mutex_lock(lp);
+	(void) mutex_lock(lp);
 
 	wbt->bt_objset = dmu_objset_id(os);
 	wbt->bt_object = ZTEST_DIROBJ;
@@ -2031,7 +2031,7 @@ ztest_dmu_write_parallel(ztest_args_t *za)
 		dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
 	}
 
-	(void) pthread_mutex_unlock(lp);
+	(void) mutex_unlock(lp);
 
 	if (ztest_random(1000) == 0)
 		(void) poll(NULL, 0, 1); /* open dn_notxholds window */
@@ -2050,13 +2050,13 @@ ztest_dmu_write_parallel(ztest_args_t *za)
 	/*
 	 * dmu_sync() the block we just wrote.
 	 */
-	(void) pthread_mutex_lock(lp);
+	(void) mutex_lock(lp);
 
 	blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
 	error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
 	za->za_dbuf = db;
 	if (error) {
-		(void) pthread_mutex_unlock(lp);
+		(void) mutex_unlock(lp);
 		return;
 	}
 	blkoff = off - blkoff;
@@ -2064,7 +2064,7 @@ ztest_dmu_write_parallel(ztest_args_t *za)
 	dmu_buf_rele(db, FTAG);
 	za->za_dbuf = NULL;
 
-	(void) pthread_mutex_unlock(lp);
+	(void) mutex_unlock(lp);
 
 	if (error)
 		return;
@@ -2444,7 +2444,7 @@ ztest_dsl_prop_get_set(ztest_args_t *za)
 	char osname[MAXNAMELEN];
 	int error;
 
-	(void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
+	(void) rw_rdlock(&ztest_shared->zs_name_lock);
 
 	dmu_objset_name(os, osname);
 
@@ -2483,7 +2483,7 @@ ztest_dsl_prop_get_set(ztest_args_t *za)
 		}
 	}
 
-	(void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
+	(void) rw_unlock(&ztest_shared->zs_name_lock);
 }
 
 /*
@@ -2647,7 +2647,7 @@ ztest_spa_rename(ztest_args_t *za)
 	int error;
 	spa_t *spa;
 
-	(void) pthread_rwlock_wrlock(&ztest_shared->zs_name_lock);
+	(void) rw_wrlock(&ztest_shared->zs_name_lock);
 
 	oldname = za->za_pool;
 	newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
@@ -2699,7 +2699,7 @@ ztest_spa_rename(ztest_args_t *za)
 
 	umem_free(newname, strlen(newname) + 1);
 
-	(void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
+	(void) rw_unlock(&ztest_shared->zs_name_lock);
 }
 
 
@@ -2937,18 +2937,15 @@ ztest_spa_import_export(char *oldname, char *newname)
 	nvlist_free(config);
 }
 
-static void *
-ztest_resume(void *arg)
+static void
+ztest_resume(spa_t *spa)
 {
-	spa_t *spa = arg;
-
 	if (spa_suspended(spa)) {
 		spa_vdev_state_enter(spa);
 		vdev_clear(spa, NULL);
 		(void) spa_vdev_state_exit(spa, NULL, 0);
 		zio_resume(spa);
 	}
-	return (NULL);
 }
 
 static void *
@@ -3038,16 +3035,15 @@ ztest_run(char *pool)
 	ztest_args_t *za;
 	spa_t *spa;
 	char name[100];
-	pthread_t resume_tid;
+	thread_t resume_tid;
 
 	ztest_exiting = B_FALSE;
 
-	(void) pthread_mutex_init(&zs->zs_vdev_lock, NULL);
-	(void) pthread_rwlock_init(&zs->zs_name_lock, NULL);
+	(void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL);
+	(void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL);
 
 	for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
-		(void) pthread_mutex_init(&zs->zs_sync_lock[t], NULL);
-
+		(void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL);
 
 	/*
 	 * Destroy one disk before we even start.
@@ -3114,7 +3110,8 @@ ztest_run(char *pool)
 	/*
 	 * Create a thread to periodically resume suspended I/O.
 	 */
-	VERIFY(pthread_create(&resume_tid, NULL, ztest_resume_thread, spa)==0);
+	VERIFY(thr_create(0, 0, ztest_resume_thread, spa, THR_BOUND,
+	    &resume_tid) == 0);
 
 	/*
 	 * Verify that we can safely inquire about about any object,
@@ -3163,7 +3160,7 @@ ztest_run(char *pool)
 
 		if (t < zopt_datasets) {
 			int test_future = FALSE;
-			(void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
+			(void) rw_rdlock(&ztest_shared->zs_name_lock);
 			(void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
 			error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
 			    ztest_create_cb, NULL);
@@ -3171,7 +3168,7 @@ ztest_run(char *pool)
 				test_future = TRUE;
 			} else if (error == ENOSPC) {
 				zs->zs_enospc_count++;
-				(void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
+				(void) rw_unlock(&ztest_shared->zs_name_lock);
 				break;
 			} else if (error != 0) {
 				fatal(0, "dmu_objset_create(%s) = %d",
@@ -3182,7 +3179,7 @@ ztest_run(char *pool)
 			if (error)
 				fatal(0, "dmu_objset_open('%s') = %d",
 				    name, error);
-			(void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
+			(void) rw_unlock(&ztest_shared->zs_name_lock);
 			if (test_future)
 				ztest_dmu_check_future_leak(&za[t]);
 			zil_replay(za[d].za_os, za[d].za_os,
@@ -3190,12 +3187,12 @@ ztest_run(char *pool)
 			za[d].za_zilog = zil_open(za[d].za_os, NULL);
 		}
 
-		VERIFY(pthread_create(&za[t].za_thread, NULL, ztest_thread,
-		    &za[t]) == 0);
+		VERIFY(thr_create(0, 0, ztest_thread, &za[t], THR_BOUND,
+		    &za[t].za_thread) == 0);
 	}
 
 	while (--t >= 0) {
-		VERIFY(pthread_join(za[t].za_thread, NULL) == 0);
+		VERIFY(thr_join(za[t].za_thread, NULL, NULL) == 0);
 		if (t < zopt_datasets) {
 			zil_close(za[t].za_zilog);
 			dmu_objset_close(za[t].za_os);
@@ -3214,14 +3211,14 @@ ztest_run(char *pool)
 	 * If we had out-of-space errors, destroy a random objset.
 	 */
 	if (zs->zs_enospc_count != 0) {
-		(void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
+		(void) rw_rdlock(&ztest_shared->zs_name_lock);
 		d = (int)ztest_random(zopt_datasets);
 		(void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
 		if (zopt_verbose >= 3)
 			(void) printf("Destroying %s to free up space\n", name);
 		(void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
 		    DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
-		(void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
+		(void) rw_unlock(&ztest_shared->zs_name_lock);
 	}
 
 	txg_wait_synced(spa_get_dsl(spa), 0);
@@ -3230,7 +3227,7 @@ ztest_run(char *pool)
 
 	/* Kill the resume thread */
 	ztest_exiting = B_TRUE;
-	VERIFY(pthread_join(resume_tid, NULL) == 0);
+	VERIFY(thr_join(resume_tid, NULL, NULL) == 0);
 	ztest_resume(spa);
 
 	/*
diff --git a/lib/libuutil/uu_misc.c b/lib/libuutil/uu_misc.c
index fc57328c2b..74ec177c11 100644
--- a/lib/libuutil/uu_misc.c
+++ b/lib/libuutil/uu_misc.c
@@ -37,6 +37,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/debug.h>
+#include <thread.h>
 #include <unistd.h>
 
 #if !defined(TEXT_DOMAIN)
@@ -69,12 +70,11 @@ static va_list		uu_panic_args;
 static pthread_t	uu_panic_thread;
 
 static uint32_t		_uu_main_error;
-static __thread int	_uu_main_thread = 0;
 
 void
 uu_set_error(uint_t code)
 {
-	if (_uu_main_thread) {
+	if (thr_main() != 0) {
 		_uu_main_error = code;
 		return;
 	}
@@ -103,7 +103,7 @@ uu_set_error(uint_t code)
 uint32_t
 uu_error(void)
 {
-	if (_uu_main_thread)
+	if (thr_main() != 0)
 		return (_uu_main_error);
 
 	if (uu_error_key_setup < 0)	/* can't happen? */
@@ -251,6 +251,5 @@ uu_release_child(void)
 static void
 uu_init(void)
 {
-	_uu_main_thread = 1;
 	(void) pthread_atfork(uu_lockup, uu_release, uu_release_child);
 }
diff --git a/lib/libzpool/include/sys/zfs_context.h b/lib/libzpool/include/sys/zfs_context.h
index 657c642825..a50e4b0ac3 100644
--- a/lib/libzpool/include/sys/zfs_context.h
+++ b/lib/libzpool/include/sys/zfs_context.h
@@ -50,7 +50,8 @@ extern "C" {
 #include <errno.h>
 #include <string.h>
 #include <strings.h>
-#include <pthread.h>
+#include <synch.h>
+#include <thread.h>
 #include <assert.h>
 #include <alloca.h>
 #include <umem.h>
@@ -103,7 +104,6 @@ extern void vpanic(const char *, __va_list);
 #define	fm_panic	panic
 
 /* This definition is copied from assert.h. */
-#ifndef verify
 #if defined(__STDC__)
 #if __STDC_VERSION__ - 0 >= 199901L
 #define	verify(EX) (void)((EX) || \
@@ -114,10 +114,7 @@ extern void vpanic(const char *, __va_list);
 #else
 #define	verify(EX) (void)((EX) || (_assert("EX", __FILE__, __LINE__), 0))
 #endif	/* __STDC__ */
-#endif
 
-#undef VERIFY
-#undef ASSERT
 
 #define	VERIFY	verify
 #define	ASSERT	assert
@@ -190,18 +187,15 @@ _NOTE(CONSTCOND) } while (0)
 /*
  * Threads
  */
-#define	curthread		((void *)(uintptr_t)pthread_self())
-#define	tsd_get(key)		pthread_getspecific(key)
-#define	tsd_set(key, val)	pthread_setspecific(key, val)
+#define	curthread	((void *)(uintptr_t)thr_self())
 
 typedef struct kthread kthread_t;
-typedef void (*thread_func_t)(void *);
 
 #define	thread_create(stk, stksize, func, arg, len, pp, state, pri)	\
-	zk_thread_create((thread_func_t)func, arg)
-#define	thread_exit() pthread_exit(NULL)
+	zk_thread_create(func, arg)
+#define	thread_exit() thr_exit(NULL)
 
-extern kthread_t *zk_thread_create(thread_func_t func, void *arg);
+extern kthread_t *zk_thread_create(void (*func)(), void *arg);
 
 #define	issig(why)	(FALSE)
 #define	ISSIG(thr, why)	(FALSE)
@@ -209,18 +203,28 @@ extern kthread_t *zk_thread_create(thread_func_t func, void *arg);
 /*
  * Mutexes
  */
-#define MTX_MAGIC 0x9522f51362a6e326ull
 typedef struct kmutex {
 	void		*m_owner;
-	uint64_t	m_magic;
-	pthread_mutex_t	m_lock;
+	boolean_t	initialized;
+	mutex_t		m_lock;
 } kmutex_t;
 
-#define	MUTEX_DEFAULT	0
-#define	MUTEX_HELD(m)	((m)->m_owner == curthread)
+#define	MUTEX_DEFAULT	USYNC_THREAD
+#undef MUTEX_HELD
+#define	MUTEX_HELD(m) _mutex_held(&(m)->m_lock)
 
-extern void mutex_init(kmutex_t *mp, char *name, int type, void *cookie);
-extern void mutex_destroy(kmutex_t *mp);
+/*
+ * Argh -- we have to get cheesy here because the kernel and userland
+ * have different signatures for the same routine.
+ */
+extern int _mutex_init(mutex_t *mp, int type, void *arg);
+extern int _mutex_destroy(mutex_t *mp);
+
+#define	mutex_init(mp, b, c, d)		zmutex_init((kmutex_t *)(mp))
+#define	mutex_destroy(mp)		zmutex_destroy((kmutex_t *)(mp))
+
+extern void zmutex_init(kmutex_t *mp);
+extern void zmutex_destroy(kmutex_t *mp);
 extern void mutex_enter(kmutex_t *mp);
 extern void mutex_exit(kmutex_t *mp);
 extern int mutex_tryenter(kmutex_t *mp);
@@ -229,24 +233,23 @@ extern void *mutex_owner(kmutex_t *mp);
 /*
  * RW locks
  */
-#define RW_MAGIC 0x4d31fb123648e78aull
 typedef struct krwlock {
-	void			*rw_owner;
-	void			*rw_wr_owner;
-	uint64_t		rw_magic;
-	pthread_rwlock_t	rw_lock;
-	uint_t			rw_readers;
+	void		*rw_owner;
+	boolean_t	initialized;
+	rwlock_t	rw_lock;
 } krwlock_t;
 
 typedef int krw_t;
 
 #define	RW_READER	0
 #define	RW_WRITER	1
-#define	RW_DEFAULT	0
+#define	RW_DEFAULT	USYNC_THREAD
 
-#define	RW_READ_HELD(x)		((x)->rw_readers > 0)
-#define	RW_WRITE_HELD(x)	((x)->rw_wr_owner == curthread)
-#define	RW_LOCK_HELD(x)		(RW_READ_HELD(x) || RW_WRITE_HELD(x))
+#undef RW_READ_HELD
+#define	RW_READ_HELD(x)		_rw_read_held(&(x)->rw_lock)
+
+#undef RW_WRITE_HELD
+#define	RW_WRITE_HELD(x)	_rw_write_held(&(x)->rw_lock)
 
 extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
 extern void rw_destroy(krwlock_t *rwlp);
@@ -264,13 +267,9 @@ extern gid_t *crgetgroups(cred_t *cr);
 /*
  * Condition variables
  */
-#define CV_MAGIC 0xd31ea9a83b1b30c4ull
-typedef struct kcondvar {
-	uint64_t cv_magic;
-	pthread_cond_t cv;
-} kcondvar_t;
+typedef cond_t kcondvar_t;
 
-#define	CV_DEFAULT	0
+#define	CV_DEFAULT	USYNC_THREAD
 
 extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
 extern void cv_destroy(kcondvar_t *cv);
@@ -444,8 +443,7 @@ extern void delay(clock_t ticks);
 #define	minclsyspri	60
 #define	maxclsyspri	99
 
-/* XXX: not portable */
-#define	CPU_SEQID	(pthread_self() & (max_ncpus - 1))
+#define	CPU_SEQID	(thr_self() & (max_ncpus - 1))
 
 #define	kcred		NULL
 #define	CRED()		NULL
diff --git a/lib/libzpool/kernel.c b/lib/libzpool/kernel.c
index 4fe4eac54a..89108fe5b2 100644
--- a/lib/libzpool/kernel.c
+++ b/lib/libzpool/kernel.c
@@ -36,7 +36,6 @@
 #include <sys/zfs_context.h>
 #include <sys/zmod.h>
 #include <sys/utsname.h>
-#include <sys/time.h>
 #include <sys/systeminfo.h>
 
 /*
@@ -58,17 +57,13 @@ struct utsname utsname = {
  */
 /*ARGSUSED*/
 kthread_t *
-zk_thread_create(thread_func_t func, void *arg)
+zk_thread_create(void (*func)(), void *arg)
 {
-	pthread_t tid;
+	thread_t tid;
 
-	pthread_attr_t attr;
-	VERIFY(pthread_attr_init(&attr) == 0);
-	VERIFY(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0);
+	VERIFY(thr_create(0, 0, (void *(*)(void *))func, arg, THR_DETACHED,
+	    &tid) == 0);
 
-	VERIFY(pthread_create(&tid, &attr, (void *(*)(void *))func, arg) == 0);
-
-	/* XXX: not portable */
 	return ((void *)(uintptr_t)tid);
 }
 
@@ -101,37 +96,30 @@ kstat_delete(kstat_t *ksp)
  * =========================================================================
  */
 void
-mutex_init(kmutex_t *mp, char *name, int type, void *cookie)
+zmutex_init(kmutex_t *mp)
 {
-	ASSERT(type == MUTEX_DEFAULT);
-	ASSERT(cookie == NULL);
-
-#ifdef IM_FEELING_LUCKY
-	ASSERT(mp->m_magic != MTX_MAGIC);
-#endif
-
 	mp->m_owner = NULL;
-	mp->m_magic = MTX_MAGIC;
-	VERIFY3S(pthread_mutex_init(&mp->m_lock, NULL), ==, 0);
+	mp->initialized = B_TRUE;
+	(void) _mutex_init(&mp->m_lock, USYNC_THREAD, NULL);
 }
 
 void
-mutex_destroy(kmutex_t *mp)
+zmutex_destroy(kmutex_t *mp)
 {
-	ASSERT(mp->m_magic == MTX_MAGIC);
+	ASSERT(mp->initialized == B_TRUE);
 	ASSERT(mp->m_owner == NULL);
-	VERIFY3S(pthread_mutex_destroy(&(mp)->m_lock), ==, 0);
+	(void) _mutex_destroy(&(mp)->m_lock);
 	mp->m_owner = (void *)-1UL;
-	mp->m_magic = 0;
+	mp->initialized = B_FALSE;
 }
 
 void
 mutex_enter(kmutex_t *mp)
 {
-	ASSERT(mp->m_magic == MTX_MAGIC);
+	ASSERT(mp->initialized == B_TRUE);
 	ASSERT(mp->m_owner != (void *)-1UL);
 	ASSERT(mp->m_owner != curthread);
-	VERIFY3S(pthread_mutex_lock(&mp->m_lock), ==, 0);
+	VERIFY(mutex_lock(&mp->m_lock) == 0);
 	ASSERT(mp->m_owner == NULL);
 	mp->m_owner = curthread;
 }
@@ -139,9 +127,9 @@ mutex_enter(kmutex_t *mp)
 int
 mutex_tryenter(kmutex_t *mp)
 {
-	ASSERT(mp->m_magic == MTX_MAGIC);
+	ASSERT(mp->initialized == B_TRUE);
 	ASSERT(mp->m_owner != (void *)-1UL);
-	if (0 == pthread_mutex_trylock(&mp->m_lock)) {
+	if (0 == mutex_trylock(&mp->m_lock)) {
 		ASSERT(mp->m_owner == NULL);
 		mp->m_owner = curthread;
 		return (1);
@@ -153,16 +141,16 @@ mutex_tryenter(kmutex_t *mp)
 void
 mutex_exit(kmutex_t *mp)
 {
-	ASSERT(mp->m_magic == MTX_MAGIC);
+	ASSERT(mp->initialized == B_TRUE);
 	ASSERT(mutex_owner(mp) == curthread);
 	mp->m_owner = NULL;
-	VERIFY3S(pthread_mutex_unlock(&mp->m_lock), ==, 0);
+	VERIFY(mutex_unlock(&mp->m_lock) == 0);
 }
 
 void *
 mutex_owner(kmutex_t *mp)
 {
-	ASSERT(mp->m_magic == MTX_MAGIC);
+	ASSERT(mp->initialized == B_TRUE);
 	return (mp->m_owner);
 }
 
@@ -175,48 +163,31 @@ mutex_owner(kmutex_t *mp)
 void
 rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
 {
-	ASSERT(type == RW_DEFAULT);
-	ASSERT(arg == NULL);
-
-#ifdef IM_FEELING_LUCKY
-	ASSERT(rwlp->rw_magic != RW_MAGIC);
-#endif
-
-	VERIFY3S(pthread_rwlock_init(&rwlp->rw_lock, NULL), ==, 0);
+	rwlock_init(&rwlp->rw_lock, USYNC_THREAD, NULL);
 	rwlp->rw_owner = NULL;
-	rwlp->rw_wr_owner = NULL;
-	rwlp->rw_readers = 0;
-	rwlp->rw_magic = RW_MAGIC;
+	rwlp->initialized = B_TRUE;
 }
 
 void
 rw_destroy(krwlock_t *rwlp)
 {
-	ASSERT(rwlp->rw_magic == RW_MAGIC);
-
-	VERIFY3S(pthread_rwlock_destroy(&rwlp->rw_lock), ==, 0);
-	rwlp->rw_magic = 0;
+	rwlock_destroy(&rwlp->rw_lock);
+	rwlp->rw_owner = (void *)-1UL;
+	rwlp->initialized = B_FALSE;
 }
 
 void
 rw_enter(krwlock_t *rwlp, krw_t rw)
 {
-	ASSERT(rwlp->rw_magic == RW_MAGIC);
+	ASSERT(!RW_LOCK_HELD(rwlp));
+	ASSERT(rwlp->initialized == B_TRUE);
+	ASSERT(rwlp->rw_owner != (void *)-1UL);
 	ASSERT(rwlp->rw_owner != curthread);
-	ASSERT(rwlp->rw_wr_owner != curthread);
 
-	if (rw == RW_READER) {
-		VERIFY3S(pthread_rwlock_rdlock(&rwlp->rw_lock), ==, 0);
-		ASSERT(rwlp->rw_wr_owner == NULL);
-
-		atomic_inc_uint(&rwlp->rw_readers);
-	} else {
-		VERIFY3S(pthread_rwlock_wrlock(&rwlp->rw_lock), ==, 0);
-		ASSERT(rwlp->rw_wr_owner == NULL);
-		ASSERT3U(rwlp->rw_readers, ==, 0);
-
-		rwlp->rw_wr_owner = curthread;
-	}
+	if (rw == RW_READER)
+		VERIFY(rw_rdlock(&rwlp->rw_lock) == 0);
+	else
+		VERIFY(rw_wrlock(&rwlp->rw_lock) == 0);
 
 	rwlp->rw_owner = curthread;
 }
@@ -224,16 +195,11 @@ rw_enter(krwlock_t *rwlp, krw_t rw)
 void
 rw_exit(krwlock_t *rwlp)
 {
-	ASSERT(rwlp->rw_magic == RW_MAGIC);
-	ASSERT(RW_LOCK_HELD(rwlp));
-
-	if (RW_READ_HELD(rwlp))
-		atomic_dec_uint(&rwlp->rw_readers);
-	else
-		rwlp->rw_wr_owner = NULL;
+	ASSERT(rwlp->initialized == B_TRUE);
+	ASSERT(rwlp->rw_owner != (void *)-1UL);
 
 	rwlp->rw_owner = NULL;
-	VERIFY3S(pthread_rwlock_unlock(&rwlp->rw_lock), ==, 0);
+	VERIFY(rw_unlock(&rwlp->rw_lock) == 0);
 }
 
 int
@@ -241,29 +207,19 @@ rw_tryenter(krwlock_t *rwlp, krw_t rw)
 {
 	int rv;
 
-	ASSERT(rwlp->rw_magic == RW_MAGIC);
+	ASSERT(rwlp->initialized == B_TRUE);
+	ASSERT(rwlp->rw_owner != (void *)-1UL);
 
 	if (rw == RW_READER)
-		rv = pthread_rwlock_tryrdlock(&rwlp->rw_lock);
+		rv = rw_tryrdlock(&rwlp->rw_lock);
 	else
-		rv = pthread_rwlock_trywrlock(&rwlp->rw_lock);
+		rv = rw_trywrlock(&rwlp->rw_lock);
 
 	if (rv == 0) {
-		ASSERT(rwlp->rw_wr_owner == NULL);
-
-		if (rw == RW_READER)
-			atomic_inc_uint(&rwlp->rw_readers);
-		else {
-			ASSERT3U(rwlp->rw_readers, ==, 0);
-			rwlp->rw_wr_owner = curthread;
-		}
-
 		rwlp->rw_owner = curthread;
 		return (1);
 	}
 
-	VERIFY3S(rv, ==, EBUSY);
-
 	return (0);
 }
 
@@ -271,7 +227,8 @@ rw_tryenter(krwlock_t *rwlp, krw_t rw)
 int
 rw_tryupgrade(krwlock_t *rwlp)
 {
-	ASSERT(rwlp->rw_magic == RW_MAGIC);
+	ASSERT(rwlp->initialized == B_TRUE);
+	ASSERT(rwlp->rw_owner != (void *)-1UL);
 
 	return (0);
 }
@@ -285,34 +242,22 @@ rw_tryupgrade(krwlock_t *rwlp)
 void
 cv_init(kcondvar_t *cv, char *name, int type, void *arg)
 {
-	ASSERT(type == CV_DEFAULT);
-
-#ifdef IM_FEELING_LUCKY
-	ASSERT(cv->cv_magic != CV_MAGIC);
-#endif
-
-	cv->cv_magic = CV_MAGIC;
-
-	VERIFY3S(pthread_cond_init(&cv->cv, NULL), ==, 0);
+	VERIFY(cond_init(cv, type, NULL) == 0);
 }
 
 void
 cv_destroy(kcondvar_t *cv)
 {
-	ASSERT(cv->cv_magic == CV_MAGIC);
-	VERIFY3S(pthread_cond_destroy(&cv->cv), ==, 0);
-	cv->cv_magic = 0;
+	VERIFY(cond_destroy(cv) == 0);
 }
 
 void
 cv_wait(kcondvar_t *cv, kmutex_t *mp)
 {
-	ASSERT(cv->cv_magic == CV_MAGIC);
 	ASSERT(mutex_owner(mp) == curthread);
 	mp->m_owner = NULL;
-	int ret = pthread_cond_wait(&cv->cv, &mp->m_lock);
-	if (ret != 0)
-		VERIFY3S(ret, ==, EINTR);
+	int ret = cond_wait(cv, &mp->m_lock);
+	VERIFY(ret == 0 || ret == EINTR);
 	mp->m_owner = curthread;
 }
 
@@ -320,38 +265,29 @@ clock_t
 cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
 {
 	int error;
-	struct timeval tv;
 	timestruc_t ts;
 	clock_t delta;
 
-	ASSERT(cv->cv_magic == CV_MAGIC);
-
 top:
 	delta = abstime - lbolt;
 	if (delta <= 0)
 		return (-1);
 
-	VERIFY(gettimeofday(&tv, NULL) == 0);
-
-	ts.tv_sec = tv.tv_sec + delta / hz;
-	ts.tv_nsec = tv.tv_usec * 1000 + (delta % hz) * (NANOSEC / hz);
-	if (ts.tv_nsec >= NANOSEC) {
-		ts.tv_sec++;
-		ts.tv_nsec -= NANOSEC;
-	}
+	ts.tv_sec = delta / hz;
+	ts.tv_nsec = (delta % hz) * (NANOSEC / hz);
 
 	ASSERT(mutex_owner(mp) == curthread);
 	mp->m_owner = NULL;
-	error = pthread_cond_timedwait(&cv->cv, &mp->m_lock, &ts);
+	error = cond_reltimedwait(cv, &mp->m_lock, &ts);
 	mp->m_owner = curthread;
 
-	if (error == ETIMEDOUT)
+	if (error == ETIME)
 		return (-1);
 
 	if (error == EINTR)
 		goto top;
 
-	VERIFY3S(error, ==, 0);
+	ASSERT(error == 0);
 
 	return (1);
 }
@@ -359,15 +295,13 @@ top:
 void
 cv_signal(kcondvar_t *cv)
 {
-	ASSERT(cv->cv_magic == CV_MAGIC);
-	VERIFY3S(pthread_cond_signal(&cv->cv), ==, 0);
+	VERIFY(cond_signal(cv) == 0);
 }
 
 void
 cv_broadcast(kcondvar_t *cv)
 {
-	ASSERT(cv->cv_magic == CV_MAGIC);
-	VERIFY3S(pthread_cond_broadcast(&cv->cv), ==, 0);
+	VERIFY(cond_broadcast(cv) == 0);
 }
 
 /*
@@ -609,7 +543,7 @@ __dprintf(const char *file, const char *func, int line, const char *fmt, ...)
 		if (dprintf_find_string("pid"))
 			(void) printf("%d ", getpid());
 		if (dprintf_find_string("tid"))
-			(void) printf("%u ", (uint_t) pthread_self());
+			(void) printf("%u ", thr_self());
 		if (dprintf_find_string("cpu"))
 			(void) printf("%u ", getcpuid());
 		if (dprintf_find_string("time"))
diff --git a/lib/libzpool/taskq.c b/lib/libzpool/taskq.c
index d28f6024bb..93acdcf8e4 100644
--- a/lib/libzpool/taskq.c
+++ b/lib/libzpool/taskq.c
@@ -42,7 +42,7 @@ struct taskq {
 	krwlock_t	tq_threadlock;
 	kcondvar_t	tq_dispatch_cv;
 	kcondvar_t	tq_wait_cv;
-	pthread_t	*tq_threadlist;
+	thread_t	*tq_threadlist;
 	int		tq_flags;
 	int		tq_active;
 	int		tq_nthreads;
@@ -185,7 +185,7 @@ taskq_create(const char *name, int nthreads, pri_t pri,
 	tq->tq_maxalloc = maxalloc;
 	tq->tq_task.task_next = &tq->tq_task;
 	tq->tq_task.task_prev = &tq->tq_task;
-	tq->tq_threadlist = kmem_alloc(nthreads * sizeof (pthread_t), KM_SLEEP);
+	tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
 
 	if (flags & TASKQ_PREPOPULATE) {
 		mutex_enter(&tq->tq_lock);
@@ -195,8 +195,8 @@ taskq_create(const char *name, int nthreads, pri_t pri,
 	}
 
 	for (t = 0; t < nthreads; t++)
-		VERIFY(pthread_create(&tq->tq_threadlist[t],
-		    NULL, taskq_thread, tq) == 0);
+		(void) thr_create(0, 0, taskq_thread,
+		    tq, THR_BOUND, &tq->tq_threadlist[t]);
 
 	return (tq);
 }
@@ -226,9 +226,9 @@ taskq_destroy(taskq_t *tq)
 	mutex_exit(&tq->tq_lock);
 
 	for (t = 0; t < nthreads; t++)
-		VERIFY(pthread_join(tq->tq_threadlist[t], NULL) == 0);
+		(void) thr_join(tq->tq_threadlist[t], NULL, NULL);
 
-	kmem_free(tq->tq_threadlist, nthreads * sizeof (pthread_t));
+	kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
 
 	rw_destroy(&tq->tq_threadlock);
 	mutex_destroy(&tq->tq_lock);
@@ -247,7 +247,7 @@ taskq_member(taskq_t *tq, void *t)
 		return (1);
 
 	for (i = 0; i < tq->tq_nthreads; i++)
-		if (tq->tq_threadlist[i] == (pthread_t)(uintptr_t)t)
+		if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
 			return (1);
 
 	return (0);