1 Use POSIX threads in userspace.
3 Index: zfs+chaos4/cmd/lztest/ztest.c
4 ===================================================================
5 --- zfs+chaos4.orig/cmd/lztest/ztest.c
6 +++ zfs+chaos4/cmd/lztest/ztest.c
7 @@ -141,7 +141,7 @@ typedef struct ztest_args {
12 + pthread_t za_thread;
16 @@ -224,17 +224,17 @@ ztest_info_t ztest_info[] = {
17 * Stuff we need to share writably between parent and child.
19 typedef struct ztest_shared {
20 - mutex_t zs_vdev_lock;
21 - rwlock_t zs_name_lock;
22 - uint64_t zs_vdev_primaries;
23 - uint64_t zs_enospc_count;
24 - hrtime_t zs_start_time;
25 - hrtime_t zs_stop_time;
28 - ztest_info_t zs_info[ZTEST_FUNCS];
29 - mutex_t zs_sync_lock[ZTEST_SYNC_LOCKS];
30 - uint64_t zs_seq[ZTEST_SYNC_LOCKS];
31 + pthread_mutex_t zs_vdev_lock;
32 + pthread_rwlock_t zs_name_lock;
33 + uint64_t zs_vdev_primaries;
34 + uint64_t zs_enospc_count;
35 + hrtime_t zs_start_time;
36 + hrtime_t zs_stop_time;
39 + ztest_info_t zs_info[ZTEST_FUNCS];
40 + pthread_mutex_t zs_sync_lock[ZTEST_SYNC_LOCKS];
41 + uint64_t zs_seq[ZTEST_SYNC_LOCKS];
44 static char ztest_dev_template[] = "%s/%s.%llua";
45 @@ -818,7 +818,7 @@ ztest_spa_create_destroy(ztest_args_t *z
46 * Attempt to create an existing pool. It shouldn't matter
47 * what's in the nvroot; we should fail with EEXIST.
49 - (void) rw_rdlock(&ztest_shared->zs_name_lock);
50 + (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
51 nvroot = make_vdev_root(0, 0, 0, 0, 1);
52 error = spa_create(za->za_pool, nvroot, NULL, NULL);
54 @@ -834,7 +834,7 @@ ztest_spa_create_destroy(ztest_args_t *z
55 fatal(0, "spa_destroy() = %d", error);
58 - (void) rw_unlock(&ztest_shared->zs_name_lock);
59 + (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
63 @@ -851,7 +851,7 @@ ztest_vdev_add_remove(ztest_args_t *za)
64 if (zopt_verbose >= 6)
65 (void) printf("adding vdev\n");
67 - (void) mutex_lock(&ztest_shared->zs_vdev_lock);
68 + (void) pthread_mutex_lock(&ztest_shared->zs_vdev_lock);
70 spa_config_enter(spa, RW_READER, FTAG);
72 @@ -869,7 +869,7 @@ ztest_vdev_add_remove(ztest_args_t *za)
73 error = spa_vdev_add(spa, nvroot);
76 - (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
77 + (void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
80 ztest_record_enospc("spa_vdev_add");
81 @@ -927,7 +927,7 @@ ztest_vdev_attach_detach(ztest_args_t *z
82 int error, expected_error;
85 - (void) mutex_lock(&ztest_shared->zs_vdev_lock);
86 + (void) pthread_mutex_lock(&ztest_shared->zs_vdev_lock);
88 spa_config_enter(spa, RW_READER, FTAG);
90 @@ -1054,7 +1054,7 @@ ztest_vdev_attach_detach(ztest_args_t *z
91 oldpath, newpath, replacing, error, expected_error);
94 - (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
95 + (void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
99 @@ -1071,7 +1071,7 @@ ztest_vdev_LUN_growth(ztest_args_t *za)
103 - (void) mutex_lock(&ztest_shared->zs_vdev_lock);
104 + (void) pthread_mutex_lock(&ztest_shared->zs_vdev_lock);
107 * Pick a random leaf vdev.
108 @@ -1102,7 +1102,7 @@ ztest_vdev_LUN_growth(ztest_args_t *za)
112 - (void) mutex_unlock(&ztest_shared->zs_vdev_lock);
113 + (void) pthread_mutex_unlock(&ztest_shared->zs_vdev_lock);
117 @@ -1198,7 +1198,7 @@ ztest_dmu_objset_create_destroy(ztest_ar
121 - (void) rw_rdlock(&ztest_shared->zs_name_lock);
122 + (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
123 (void) snprintf(name, 100, "%s/%s_temp_%llu", za->za_pool, za->za_pool,
124 (u_longlong_t)za->za_instance);
126 @@ -1242,7 +1242,7 @@ ztest_dmu_objset_create_destroy(ztest_ar
128 if (error == ENOSPC) {
129 ztest_record_enospc("dmu_objset_create");
130 - (void) rw_unlock(&ztest_shared->zs_name_lock);
131 + (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
134 fatal(0, "dmu_objset_create(%s) = %d", name, error);
135 @@ -1321,7 +1321,7 @@ ztest_dmu_objset_create_destroy(ztest_ar
137 fatal(0, "dmu_objset_destroy(%s) = %d", name, error);
139 - (void) rw_unlock(&ztest_shared->zs_name_lock);
140 + (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
144 @@ -1335,7 +1335,7 @@ ztest_dmu_snapshot_create_destroy(ztest_
146 char osname[MAXNAMELEN];
148 - (void) rw_rdlock(&ztest_shared->zs_name_lock);
149 + (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
150 dmu_objset_name(os, osname);
151 (void) snprintf(snapname, 100, "%s@%llu", osname,
152 (u_longlong_t)za->za_instance);
153 @@ -1348,7 +1348,7 @@ ztest_dmu_snapshot_create_destroy(ztest_
154 ztest_record_enospc("dmu_take_snapshot");
155 else if (error != 0 && error != EEXIST)
156 fatal(0, "dmu_take_snapshot() = %d", error);
157 - (void) rw_unlock(&ztest_shared->zs_name_lock);
158 + (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
161 #define ZTEST_TRAVERSE_BLOCKS 1000
162 @@ -1992,7 +1992,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
163 int bs = ZTEST_DIROBJ_BLOCKSIZE;
165 uint64_t off, txg_how;
167 + pthread_mutex_t *lp;
168 char osname[MAXNAMELEN];
169 char iobuf[SPA_MAXBLOCKSIZE];
170 blkptr_t blk = { 0 };
171 @@ -2041,7 +2041,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
174 lp = &ztest_shared->zs_sync_lock[b];
175 - (void) mutex_lock(lp);
176 + (void) pthread_mutex_lock(lp);
178 wbt->bt_objset = dmu_objset_id(os);
179 wbt->bt_object = ZTEST_DIROBJ;
180 @@ -2087,7 +2087,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
181 dmu_write(os, ZTEST_DIROBJ, off, btsize, wbt, tx);
184 - (void) mutex_unlock(lp);
185 + (void) pthread_mutex_unlock(lp);
187 if (ztest_random(1000) == 0)
188 (void) poll(NULL, 0, 1); /* open dn_notxholds window */
189 @@ -2106,7 +2106,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
191 * dmu_sync() the block we just wrote.
193 - (void) mutex_lock(lp);
194 + (void) pthread_mutex_lock(lp);
196 blkoff = P2ALIGN_TYPED(off, bs, uint64_t);
197 error = dmu_buf_hold(os, ZTEST_DIROBJ, blkoff, FTAG, &db);
198 @@ -2114,7 +2114,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
200 dprintf("dmu_buf_hold(%s, %d, %llx) = %d\n",
201 osname, ZTEST_DIROBJ, blkoff, error);
202 - (void) mutex_unlock(lp);
203 + (void) pthread_mutex_unlock(lp);
206 blkoff = off - blkoff;
207 @@ -2122,7 +2122,7 @@ ztest_dmu_write_parallel(ztest_args_t *z
208 dmu_buf_rele(db, FTAG);
211 - (void) mutex_unlock(lp);
212 + (void) pthread_mutex_unlock(lp);
215 dprintf("dmu_sync(%s, %d, %llx) = %d\n",
216 @@ -2502,7 +2502,7 @@ ztest_dsl_prop_get_set(ztest_args_t *za)
217 char osname[MAXNAMELEN];
220 - (void) rw_rdlock(&ztest_shared->zs_name_lock);
221 + (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
223 dmu_objset_name(os, osname);
225 @@ -2541,7 +2541,7 @@ ztest_dsl_prop_get_set(ztest_args_t *za)
229 - (void) rw_unlock(&ztest_shared->zs_name_lock);
230 + (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
234 @@ -2693,7 +2693,7 @@ ztest_spa_rename(ztest_args_t *za)
238 - (void) rw_wrlock(&ztest_shared->zs_name_lock);
239 + (void) pthread_rwlock_wrlock(&ztest_shared->zs_name_lock);
241 oldname = za->za_pool;
242 newname = umem_alloc(strlen(oldname) + 5, UMEM_NOFAIL);
243 @@ -2745,7 +2745,7 @@ ztest_spa_rename(ztest_args_t *za)
245 umem_free(newname, strlen(newname) + 1);
247 - (void) rw_unlock(&ztest_shared->zs_name_lock);
248 + (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
252 @@ -3090,13 +3090,13 @@ ztest_run(char *pool)
259 - (void) _mutex_init(&zs->zs_vdev_lock, USYNC_THREAD, NULL);
260 - (void) rwlock_init(&zs->zs_name_lock, USYNC_THREAD, NULL);
261 + (void) pthread_mutex_init(&zs->zs_vdev_lock, NULL);
262 + (void) pthread_rwlock_init(&zs->zs_name_lock, NULL);
264 for (t = 0; t < ZTEST_SYNC_LOCKS; t++)
265 - (void) _mutex_init(&zs->zs_sync_lock[t], USYNC_THREAD, NULL);
266 + (void) pthread_mutex_init(&zs->zs_sync_lock[t], NULL);
269 * Destroy one disk before we even start.
270 @@ -3153,7 +3153,7 @@ ztest_run(char *pool)
271 * start the thread before setting the zio_io_fail_shift, which
272 * will indicate our failure rate.
274 - error = thr_create(0, 0, ztest_suspend_monitor, NULL, THR_BOUND, &tid);
275 + error = pthread_create(&tid, NULL, ztest_suspend_monitor, NULL);
277 fatal(0, "can't create suspend monitor thread: error %d",
279 @@ -3217,7 +3217,7 @@ ztest_run(char *pool)
280 if (t < zopt_datasets) {
282 int test_future = FALSE;
283 - (void) rw_rdlock(&ztest_shared->zs_name_lock);
284 + (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
285 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
286 error = dmu_objset_create(name, DMU_OST_OTHER, NULL, 0,
287 ztest_create_cb, NULL);
288 @@ -3225,7 +3225,7 @@ ztest_run(char *pool)
290 } else if (error == ENOSPC) {
291 zs->zs_enospc_count++;
292 - (void) rw_unlock(&ztest_shared->zs_name_lock);
293 + (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
295 } else if (error != 0) {
296 fatal(0, "dmu_objset_create(%s) = %d",
297 @@ -3236,7 +3236,7 @@ ztest_run(char *pool)
299 fatal(0, "dmu_objset_open('%s') = %d",
301 - (void) rw_unlock(&ztest_shared->zs_name_lock);
302 + (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
304 ztest_dmu_check_future_leak(&za[t]);
305 zr.zr_os = za[d].za_os;
306 @@ -3245,15 +3245,15 @@ ztest_run(char *pool)
307 za[d].za_zilog = zil_open(za[d].za_os, NULL);
310 - error = thr_create(0, 0, ztest_thread, &za[t], THR_BOUND,
312 + error = pthread_create(&za[t].za_thread, NULL, ztest_thread,
315 fatal(0, "can't create thread %d: error %d",
320 - error = thr_join(za[t].za_thread, NULL, NULL);
321 + error = pthread_join(za[t].za_thread, NULL);
323 fatal(0, "thr_join(%d) = %d", t, error);
325 @@ -3276,14 +3276,14 @@ ztest_run(char *pool)
326 * If we had out-of-space errors, destroy a random objset.
328 if (zs->zs_enospc_count != 0) {
329 - (void) rw_rdlock(&ztest_shared->zs_name_lock);
330 + (void) pthread_rwlock_rdlock(&ztest_shared->zs_name_lock);
331 d = (int)ztest_random(zopt_datasets);
332 (void) snprintf(name, 100, "%s/%s_%d", pool, pool, d);
333 if (zopt_verbose >= 3)
334 (void) printf("Destroying %s to free up space\n", name);
335 (void) dmu_objset_find(name, ztest_destroy_cb, &za[d],
336 DS_FIND_SNAPSHOTS | DS_FIND_CHILDREN);
337 - (void) rw_unlock(&ztest_shared->zs_name_lock);
338 + (void) pthread_rwlock_unlock(&ztest_shared->zs_name_lock);
341 txg_wait_synced(spa_get_dsl(spa), 0);
342 @@ -3301,7 +3301,7 @@ ztest_run(char *pool)
343 mutex_enter(&spa->spa_zio_lock);
344 cv_broadcast(&spa->spa_zio_cv);
345 mutex_exit(&spa->spa_zio_lock);
346 - error = thr_join(tid, NULL, NULL);
347 + error = pthread_join(tid, NULL);
349 fatal(0, "thr_join(%d) = %d", tid, error);
351 Index: zfs+chaos4/lib/libuutil/uu_misc.c
352 ===================================================================
353 --- zfs+chaos4.orig/lib/libuutil/uu_misc.c
354 +++ zfs+chaos4/lib/libuutil/uu_misc.c
358 #include <sys/debug.h>
362 #if !defined(TEXT_DOMAIN)
363 @@ -70,11 +69,12 @@ static va_list uu_panic_args;
364 static pthread_t uu_panic_thread;
366 static uint32_t _uu_main_error;
367 +static __thread int _uu_main_thread = 0;
370 uu_set_error(uint_t code)
372 - if (thr_main() != 0) {
373 + if (_uu_main_thread) {
374 _uu_main_error = code;
377 @@ -103,7 +103,7 @@ uu_set_error(uint_t code)
381 - if (thr_main() != 0)
382 + if (_uu_main_thread)
383 return (_uu_main_error);
385 if (uu_error_key_setup < 0) /* can't happen? */
386 @@ -255,5 +255,6 @@ uu_release_child(void)
390 + _uu_main_thread = 1;
391 (void) pthread_atfork(uu_lockup, uu_release, uu_release_child);
393 Index: zfs+chaos4/lib/libzfscommon/include/sys/zfs_context_user.h
394 ===================================================================
395 --- zfs+chaos4.orig/lib/libzfscommon/include/sys/zfs_context_user.h
396 +++ zfs+chaos4/lib/libzfscommon/include/sys/zfs_context_user.h
397 @@ -52,8 +52,7 @@ extern "C" {
403 +#include <pthread.h>
407 @@ -191,13 +190,15 @@ _NOTE(CONSTCOND) } while (0)
411 -#define curthread ((void *)(uintptr_t)thr_self())
413 +/* XXX: not portable */
414 +#define curthread ((void *)(uintptr_t)pthread_self())
416 typedef struct kthread kthread_t;
418 #define thread_create(stk, stksize, func, arg, len, pp, state, pri) \
419 zk_thread_create(func, arg)
420 -#define thread_exit() thr_exit(NULL)
421 +#define thread_exit() pthread_exit(NULL)
423 extern kthread_t *zk_thread_create(void (*func)(), void *arg);
425 @@ -207,28 +208,18 @@ extern kthread_t *zk_thread_create(void
429 +#define MTX_MAGIC 0x9522f51362a6e326ull
430 typedef struct kmutex {
432 - boolean_t initialized;
435 + pthread_mutex_t m_lock;
438 -#define MUTEX_DEFAULT USYNC_THREAD
440 -#define MUTEX_HELD(m) _mutex_held(&(m)->m_lock)
443 - * Argh -- we have to get cheesy here because the kernel and userland
444 - * have different signatures for the same routine.
446 -extern int _mutex_init(mutex_t *mp, int type, void *arg);
447 -extern int _mutex_destroy(mutex_t *mp);
449 -#define mutex_init(mp, b, c, d) zmutex_init((kmutex_t *)(mp))
450 -#define mutex_destroy(mp) zmutex_destroy((kmutex_t *)(mp))
451 +#define MUTEX_DEFAULT 0
452 +#define MUTEX_HELD(m) ((m)->m_owner == curthread)
454 -extern void zmutex_init(kmutex_t *mp);
455 -extern void zmutex_destroy(kmutex_t *mp);
456 +extern void mutex_init(kmutex_t *mp, char *name, int type, void *cookie);
457 +extern void mutex_destroy(kmutex_t *mp);
458 extern void mutex_enter(kmutex_t *mp);
459 extern void mutex_exit(kmutex_t *mp);
460 extern int mutex_tryenter(kmutex_t *mp);
461 @@ -237,23 +228,24 @@ extern void *mutex_owner(kmutex_t *mp);
465 +#define RW_MAGIC 0x4d31fb123648e78aull
466 typedef struct krwlock {
468 - boolean_t initialized;
473 + pthread_rwlock_t rw_lock;
481 -#define RW_DEFAULT USYNC_THREAD
484 -#define RW_READ_HELD(x) _rw_read_held(&(x)->rw_lock)
485 +#define RW_DEFAULT 0
487 -#undef RW_WRITE_HELD
488 -#define RW_WRITE_HELD(x) _rw_write_held(&(x)->rw_lock)
489 +#define RW_READ_HELD(x) ((x)->rw_readers > 0)
490 +#define RW_WRITE_HELD(x) ((x)->rw_wr_owner == curthread)
491 +#define RW_LOCK_HELD(x) (RW_READ_HELD(x) || RW_WRITE_HELD(x))
493 extern void rw_init(krwlock_t *rwlp, char *name, int type, void *arg);
494 extern void rw_destroy(krwlock_t *rwlp);
495 @@ -271,9 +263,13 @@ extern gid_t *crgetgroups(cred_t *cr);
497 * Condition variables
499 -typedef cond_t kcondvar_t;
500 +#define CV_MAGIC 0xd31ea9a83b1b30c4ull
501 +typedef struct kcondvar {
506 -#define CV_DEFAULT USYNC_THREAD
507 +#define CV_DEFAULT 0
509 extern void cv_init(kcondvar_t *cv, char *name, int type, void *arg);
510 extern void cv_destroy(kcondvar_t *cv);
511 @@ -444,7 +440,8 @@ extern void delay(clock_t ticks);
512 #define minclsyspri 60
513 #define maxclsyspri 99
515 -#define CPU_SEQID (thr_self() & (max_ncpus - 1))
516 +/* XXX: not portable */
517 +#define CPU_SEQID (pthread_self() & (max_ncpus - 1))
521 Index: zfs+chaos4/lib/libzpool/kernel.c
522 ===================================================================
523 --- zfs+chaos4.orig/lib/libzpool/kernel.c
524 +++ zfs+chaos4/lib/libzpool/kernel.c
526 #include <sys/zfs_context.h>
527 #include <sys/zmod.h>
528 #include <sys/utsname.h>
529 +#include <sys/time.h>
532 * Emulation of kernel services in userland.
533 @@ -60,11 +61,15 @@ struct utsname utsname = {
535 zk_thread_create(void (*func)(), void *arg)
540 - VERIFY(thr_create(0, 0, (void *(*)(void *))func, arg, THR_DETACHED,
542 + pthread_attr_t attr;
543 + VERIFY(pthread_attr_init(&attr) == 0);
544 + VERIFY(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0);
546 + VERIFY(pthread_create(&tid, &attr, (void *(*)(void *))func, arg) == 0);
548 + /* XXX: not portable */
549 return ((void *)(uintptr_t)tid);
552 @@ -97,30 +102,37 @@ kstat_delete(kstat_t *ksp)
553 * =========================================================================
556 -zmutex_init(kmutex_t *mp)
557 +mutex_init(kmutex_t *mp, char *name, int type, void *cookie)
559 + ASSERT(type == MUTEX_DEFAULT);
560 + ASSERT(cookie == NULL);
562 +#ifdef IM_FEELING_LUCKY
563 + ASSERT(mp->m_magic != MTX_MAGIC);
567 - mp->initialized = B_TRUE;
568 - (void) _mutex_init(&mp->m_lock, USYNC_THREAD, NULL);
569 + mp->m_magic = MTX_MAGIC;
570 + VERIFY3S(pthread_mutex_init(&mp->m_lock, NULL), ==, 0);
574 -zmutex_destroy(kmutex_t *mp)
575 +mutex_destroy(kmutex_t *mp)
577 - ASSERT(mp->initialized == B_TRUE);
578 + ASSERT(mp->m_magic == MTX_MAGIC);
579 ASSERT(mp->m_owner == NULL);
580 - (void) _mutex_destroy(&(mp)->m_lock);
581 + VERIFY3S(pthread_mutex_destroy(&(mp)->m_lock), ==, 0);
582 mp->m_owner = (void *)-1UL;
583 - mp->initialized = B_FALSE;
588 mutex_enter(kmutex_t *mp)
590 - ASSERT(mp->initialized == B_TRUE);
591 + ASSERT(mp->m_magic == MTX_MAGIC);
592 ASSERT(mp->m_owner != (void *)-1UL);
593 ASSERT(mp->m_owner != curthread);
594 - VERIFY(mutex_lock(&mp->m_lock) == 0);
595 + VERIFY3S(pthread_mutex_lock(&mp->m_lock), ==, 0);
596 ASSERT(mp->m_owner == NULL);
597 mp->m_owner = curthread;
599 @@ -128,9 +140,9 @@ mutex_enter(kmutex_t *mp)
601 mutex_tryenter(kmutex_t *mp)
603 - ASSERT(mp->initialized == B_TRUE);
604 + ASSERT(mp->m_magic == MTX_MAGIC);
605 ASSERT(mp->m_owner != (void *)-1UL);
606 - if (0 == mutex_trylock(&mp->m_lock)) {
607 + if (0 == pthread_mutex_trylock(&mp->m_lock)) {
608 ASSERT(mp->m_owner == NULL);
609 mp->m_owner = curthread;
611 @@ -142,16 +154,16 @@ mutex_tryenter(kmutex_t *mp)
613 mutex_exit(kmutex_t *mp)
615 - ASSERT(mp->initialized == B_TRUE);
616 + ASSERT(mp->m_magic == MTX_MAGIC);
617 ASSERT(mutex_owner(mp) == curthread);
619 - VERIFY(mutex_unlock(&mp->m_lock) == 0);
620 + VERIFY3S(pthread_mutex_unlock(&mp->m_lock), ==, 0);
624 mutex_owner(kmutex_t *mp)
626 - ASSERT(mp->initialized == B_TRUE);
627 + ASSERT(mp->m_magic == MTX_MAGIC);
628 return (mp->m_owner);
631 @@ -164,31 +176,48 @@ mutex_owner(kmutex_t *mp)
633 rw_init(krwlock_t *rwlp, char *name, int type, void *arg)
635 - rwlock_init(&rwlp->rw_lock, USYNC_THREAD, NULL);
636 + ASSERT(type == RW_DEFAULT);
637 + ASSERT(arg == NULL);
639 +#ifdef IM_FEELING_LUCKY
640 + ASSERT(rwlp->rw_magic != RW_MAGIC);
643 + VERIFY3S(pthread_rwlock_init(&rwlp->rw_lock, NULL), ==, 0);
644 rwlp->rw_owner = NULL;
645 - rwlp->initialized = B_TRUE;
646 + rwlp->rw_wr_owner = NULL;
647 + rwlp->rw_readers = 0;
648 + rwlp->rw_magic = RW_MAGIC;
652 rw_destroy(krwlock_t *rwlp)
654 - rwlock_destroy(&rwlp->rw_lock);
655 - rwlp->rw_owner = (void *)-1UL;
656 - rwlp->initialized = B_FALSE;
657 + ASSERT(rwlp->rw_magic == RW_MAGIC);
659 + VERIFY3S(pthread_rwlock_destroy(&rwlp->rw_lock), ==, 0);
660 + rwlp->rw_magic = 0;
664 rw_enter(krwlock_t *rwlp, krw_t rw)
666 - ASSERT(!RW_LOCK_HELD(rwlp));
667 - ASSERT(rwlp->initialized == B_TRUE);
668 - ASSERT(rwlp->rw_owner != (void *)-1UL);
669 + ASSERT(rwlp->rw_magic == RW_MAGIC);
670 ASSERT(rwlp->rw_owner != curthread);
671 + ASSERT(rwlp->rw_wr_owner != curthread);
673 - if (rw == RW_READER)
674 - (void) rw_rdlock(&rwlp->rw_lock);
676 - (void) rw_wrlock(&rwlp->rw_lock);
677 + if (rw == RW_READER) {
678 + VERIFY3S(pthread_rwlock_rdlock(&rwlp->rw_lock), ==, 0);
679 + ASSERT(rwlp->rw_wr_owner == NULL);
681 + atomic_inc_uint(&rwlp->rw_readers);
683 + VERIFY3S(pthread_rwlock_wrlock(&rwlp->rw_lock), ==, 0);
684 + ASSERT(rwlp->rw_wr_owner == NULL);
685 + ASSERT3U(rwlp->rw_readers, ==, 0);
687 + rwlp->rw_wr_owner = curthread;
690 rwlp->rw_owner = curthread;
692 @@ -196,11 +225,16 @@ rw_enter(krwlock_t *rwlp, krw_t rw)
694 rw_exit(krwlock_t *rwlp)
696 - ASSERT(rwlp->initialized == B_TRUE);
697 - ASSERT(rwlp->rw_owner != (void *)-1UL);
698 + ASSERT(rwlp->rw_magic == RW_MAGIC);
699 + ASSERT(RW_LOCK_HELD(rwlp));
701 + if (RW_READ_HELD(rwlp))
702 + atomic_dec_uint(&rwlp->rw_readers);
704 + rwlp->rw_wr_owner = NULL;
706 rwlp->rw_owner = NULL;
707 - (void) rw_unlock(&rwlp->rw_lock);
708 + VERIFY3S(pthread_rwlock_unlock(&rwlp->rw_lock), ==, 0);
712 @@ -208,19 +242,29 @@ rw_tryenter(krwlock_t *rwlp, krw_t rw)
716 - ASSERT(rwlp->initialized == B_TRUE);
717 - ASSERT(rwlp->rw_owner != (void *)-1UL);
718 + ASSERT(rwlp->rw_magic == RW_MAGIC);
721 - rv = rw_tryrdlock(&rwlp->rw_lock);
722 + rv = pthread_rwlock_tryrdlock(&rwlp->rw_lock);
724 - rv = rw_trywrlock(&rwlp->rw_lock);
725 + rv = pthread_rwlock_trywrlock(&rwlp->rw_lock);
728 + ASSERT(rwlp->rw_wr_owner == NULL);
730 + if (rw == RW_READER)
731 + atomic_inc_uint(&rwlp->rw_readers);
733 + ASSERT3U(rwlp->rw_readers, ==, 0);
734 + rwlp->rw_wr_owner = curthread;
737 rwlp->rw_owner = curthread;
741 + VERIFY3S(rv, ==, EBUSY);
746 @@ -228,8 +272,7 @@ rw_tryenter(krwlock_t *rwlp, krw_t rw)
748 rw_tryupgrade(krwlock_t *rwlp)
750 - ASSERT(rwlp->initialized == B_TRUE);
751 - ASSERT(rwlp->rw_owner != (void *)-1UL);
752 + ASSERT(rwlp->rw_magic == RW_MAGIC);
756 @@ -243,22 +286,34 @@ rw_tryupgrade(krwlock_t *rwlp)
758 cv_init(kcondvar_t *cv, char *name, int type, void *arg)
760 - VERIFY(cond_init(cv, type, NULL) == 0);
761 + ASSERT(type == CV_DEFAULT);
763 +#ifdef IM_FEELING_LUCKY
764 + ASSERT(cv->cv_magic != CV_MAGIC);
767 + cv->cv_magic = CV_MAGIC;
769 + VERIFY3S(pthread_cond_init(&cv->cv, NULL), ==, 0);
773 cv_destroy(kcondvar_t *cv)
775 - VERIFY(cond_destroy(cv) == 0);
776 + ASSERT(cv->cv_magic == CV_MAGIC);
777 + VERIFY3S(pthread_cond_destroy(&cv->cv), ==, 0);
782 cv_wait(kcondvar_t *cv, kmutex_t *mp)
784 + ASSERT(cv->cv_magic == CV_MAGIC);
785 ASSERT(mutex_owner(mp) == curthread);
787 - int ret = cond_wait(cv, &mp->m_lock);
788 - VERIFY(ret == 0 || ret == EINTR);
789 + int ret = pthread_cond_wait(&cv->cv, &mp->m_lock);
791 + VERIFY3S(ret, ==, EINTR);
792 mp->m_owner = curthread;
795 @@ -266,29 +321,38 @@ clock_t
796 cv_timedwait(kcondvar_t *cv, kmutex_t *mp, clock_t abstime)
803 + ASSERT(cv->cv_magic == CV_MAGIC);
806 delta = abstime - lbolt;
810 - ts.tv_sec = delta / hz;
811 - ts.tv_nsec = (delta % hz) * (NANOSEC / hz);
812 + VERIFY(gettimeofday(&tv, NULL) == 0);
814 + ts.tv_sec = tv.tv_sec + delta / hz;
815 + ts.tv_nsec = tv.tv_usec * 1000 + (delta % hz) * (NANOSEC / hz);
816 + if (ts.tv_nsec >= NANOSEC) {
818 + ts.tv_nsec -= NANOSEC;
821 ASSERT(mutex_owner(mp) == curthread);
823 - error = cond_reltimedwait(cv, &mp->m_lock, &ts);
824 + error = pthread_cond_timedwait(&cv->cv, &mp->m_lock, &ts);
825 mp->m_owner = curthread;
827 - if (error == ETIME)
828 + if (error == ETIMEDOUT)
834 - ASSERT(error == 0);
835 + VERIFY3S(error, ==, 0);
839 @@ -296,13 +360,15 @@ top:
841 cv_signal(kcondvar_t *cv)
843 - VERIFY(cond_signal(cv) == 0);
844 + ASSERT(cv->cv_magic == CV_MAGIC);
845 + VERIFY3S(pthread_cond_signal(&cv->cv), ==, 0);
849 cv_broadcast(kcondvar_t *cv)
851 - VERIFY(cond_broadcast(cv) == 0);
852 + ASSERT(cv->cv_magic == CV_MAGIC);
853 + VERIFY3S(pthread_cond_broadcast(&cv->cv), ==, 0);
857 @@ -549,11 +615,11 @@ __dprintf(const char *file, const char *
858 dprintf_find_string(func)) {
859 /* Print out just the function name if requested */
861 - /* XXX: the following printf may not be portable */
862 + /* XXX: the following 2 printfs may not be portable */
863 if (dprintf_find_string("pid"))
864 (void) printf("%llu ", (u_longlong_t) getpid());
865 if (dprintf_find_string("tid"))
866 - (void) printf("%u ", (uint_t) thr_self());
867 + (void) printf("%u ", (uint_t) pthread_self());
868 if (dprintf_find_string("cpu"))
869 (void) printf("%u ", getcpuid());
870 if (dprintf_find_string("time"))
871 Index: zfs+chaos4/lib/libzpool/taskq.c
872 ===================================================================
873 --- zfs+chaos4.orig/lib/libzpool/taskq.c
874 +++ zfs+chaos4/lib/libzpool/taskq.c
875 @@ -43,7 +43,7 @@ struct taskq {
876 krwlock_t tq_threadlock;
877 kcondvar_t tq_dispatch_cv;
878 kcondvar_t tq_wait_cv;
879 - thread_t *tq_threadlist;
880 + pthread_t *tq_threadlist;
884 @@ -186,7 +186,7 @@ taskq_create(const char *name, int nthre
885 tq->tq_maxalloc = maxalloc;
886 tq->tq_task.task_next = &tq->tq_task;
887 tq->tq_task.task_prev = &tq->tq_task;
888 - tq->tq_threadlist = kmem_alloc(nthreads * sizeof (thread_t), KM_SLEEP);
889 + tq->tq_threadlist = kmem_alloc(nthreads * sizeof (pthread_t), KM_SLEEP);
891 if (flags & TASKQ_PREPOPULATE) {
892 mutex_enter(&tq->tq_lock);
893 @@ -196,8 +196,8 @@ taskq_create(const char *name, int nthre
896 for (t = 0; t < nthreads; t++)
897 - VERIFY(thr_create(0, 0, taskq_thread,
898 - tq, THR_BOUND, &tq->tq_threadlist[t]) == 0);
899 + VERIFY(pthread_create(&tq->tq_threadlist[t],
900 + NULL, taskq_thread, tq) == 0);
904 @@ -227,9 +227,9 @@ taskq_destroy(taskq_t *tq)
905 mutex_exit(&tq->tq_lock);
907 for (t = 0; t < nthreads; t++)
908 - VERIFY(thr_join(tq->tq_threadlist[t], NULL, NULL) == 0);
909 + VERIFY(pthread_join(tq->tq_threadlist[t], NULL) == 0);
911 - kmem_free(tq->tq_threadlist, nthreads * sizeof (thread_t));
912 + kmem_free(tq->tq_threadlist, nthreads * sizeof (pthread_t));
914 rw_destroy(&tq->tq_threadlock);
915 mutex_destroy(&tq->tq_lock);
916 @@ -248,7 +248,7 @@ taskq_member(taskq_t *tq, void *t)
919 for (i = 0; i < tq->tq_nthreads; i++)
920 - if (tq->tq_threadlist[i] == (thread_t)(uintptr_t)t)
921 + if (tq->tq_threadlist[i] == (pthread_t)(uintptr_t)t)