summaryrefslogtreecommitdiffstats
path: root/lib/libkse/test/mutex_d.c
blob: 45d28a5f8ef32373ef80426d6492c55a36ce6190 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
/*
 * Copyright (c) 1998 Daniel M. Eischen <eischen@vigrid.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Daniel M. Eischen.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY DANIEL M. EISCHEN AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */
#include <stdlib.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <assert.h>
#include <errno.h>
#include "pthread.h"
#include <sched.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>

#if defined(_LIBC_R_)
#include <pthread_np.h>
#endif

#ifndef NELEMENTS
#define NELEMENTS(arr)	(sizeof (arr) / sizeof (arr[0]))
#endif

#ifndef NUM_THREADS
#define NUM_THREADS	10
#endif

#define MAX_THREAD_CMDS	10

static void log_error(const char *, ...) __printflike(1, 2);
static void log_trace (const char *, ...) __printflike(1, 2);
static void log (const char *, ...) __printflike(1, 2);

/*------------------------------------------------------------
 * Types
 *----------------------------------------------------------*/

typedef enum {
	STAT_INITIAL,		/* initial state */
	STAT_WAITCONDVAR,	/* waiting for condition variable signal */
	STAT_WAITMUTEX		/* waiting for mutex lock */
} thread_status_t;

typedef enum {
	FLAGS_REPORT_WAITCONDMUTEX	= 0x01,
	FLAGS_REPORT_WAITCONDVAR	= 0x02,
	FLAGS_REPORT_WAITMUTEX		= 0x04,
	FLAGS_REPORT_BUSY_LOOP		= 0x08,
	FLAGS_IS_BUSY			= 0x10,
	FLAGS_WAS_BUSY			= 0x20
} thread_flags_t;

typedef enum {
	CMD_NONE,
	CMD_TAKE_MUTEX,
	CMD_RELEASE_MUTEX,
	CMD_WAIT_FOR_SIGNAL,
	CMD_BUSY_LOOP,
	CMD_PROTECTED_OP,
	CMD_RELEASE_ALL
} thread_cmd_id_t;

typedef struct {
	thread_cmd_id_t	cmd_id;
	pthread_mutex_t	*mutex;
	pthread_cond_t	*cond;
} thread_cmd_t;

typedef struct {
	pthread_cond_t	cond_var;
	thread_status_t	status;
	thread_cmd_t	cmd;
	int		flags;
	int		priority;
	int		ret;
	pthread_t	tid;
	u_int8_t	id;
} thread_state_t;

typedef enum {
	M_POSIX,
	M_SS2_DEFAULT,
	M_SS2_ERRORCHECK,
	M_SS2_NORMAL,
	M_SS2_RECURSIVE
} mutex_kind_t;


/*------------------------------------------------------------
 * Constants
 *----------------------------------------------------------*/

const char *protocol_strs[] = {
	"PTHREAD_PRIO_NONE",
	"PTHREAD_PRIO_INHERIT",
	"PTHREAD_PRIO_PROTECT"
};

const int protocols[] = {
	PTHREAD_PRIO_NONE,
	PTHREAD_PRIO_INHERIT,
	PTHREAD_PRIO_PROTECT
};

const char *mutextype_strs[] = {
	"POSIX (type not specified)",
	"SS2 PTHREAD_MUTEX_DEFAULT",
	"SS2 PTHREAD_MUTEX_ERRORCHECK",
	"SS2 PTHREAD_MUTEX_NORMAL",
	"SS2 PTHREAD_MUTEX_RECURSIVE"
};

const int mutex_types[] = {
	0,				/* M_POSIX		*/
	PTHREAD_MUTEX_DEFAULT,		/* M_SS2_DEFAULT	*/
	PTHREAD_MUTEX_ERRORCHECK,	/* M_SS2_ERRORCHECK	*/
	PTHREAD_MUTEX_NORMAL,		/* M_SS2_NORMAL		*/
	PTHREAD_MUTEX_RECURSIVE		/* M_SS2_RECURSIVE	*/
};


/*------------------------------------------------------------
 * Objects
 *----------------------------------------------------------*/

static int		done = 0;
static int		trace_enabled = 0;
static int		use_global_condvar = 0;
static thread_state_t	states[NUM_THREADS];
static int		pipefd[2];

static pthread_mutex_t	waiter_mutex;
static pthread_mutex_t	cond_mutex;
static pthread_cond_t	cond_var;

static FILE *logfile;
static int error_count = 0, pass_count = 0, total = 0;


/*------------------------------------------------------------
 * Prototypes
 *----------------------------------------------------------*/
extern char *strtok_r(char *str, const char *sep, char **last);


/*------------------------------------------------------------
 * Functions
 *----------------------------------------------------------*/

#ifdef DEBUG
static void
kern_switch (pthread_t pthread_out, pthread_t pthread_in)
{
	if (pthread_out != NULL)
		printf ("Swapping out thread 0x%x, ", (int) pthread_out);
	else
		printf ("Swapping out kernel thread, ");

	if (pthread_in != NULL)
		printf ("swapping in thread 0x%x\n", (int) pthread_in);
	else
		printf ("swapping in kernel thread.\n");
}
#endif


static void
log_error (const char *fmt, ...)
{
	va_list ap;

	va_start (ap, fmt);
	fprintf (logfile, "FAIL: ");
	vfprintf (logfile, fmt, ap);
	error_count = error_count + 1;
	total = total + 1;
}


static void
log_pass (void)
{
	fprintf (logfile, "PASS\n");
	pass_count = pass_count + 1;
	total = total + 1;
}


static void
log_trace (const char *fmt, ...)
{
	va_list ap;

	if (trace_enabled) {
		va_start (ap, fmt);
		vfprintf (logfile, fmt, ap);
	}
}


static void
log (const char *fmt, ...)
{
	va_list ap;

	va_start (ap, fmt);
	vfprintf (logfile, fmt, ap);
}


static void
check_result (int expected, int actual)
{
	if (expected != actual)
		log_error ("expected %d, returned %d\n", expected, actual);
	else
		log_pass ();
}


/*
 * Check to see that the threads ran in the specified order.
 */
static void
check_run_order (char *order)
{
	const char *sep = ":,";
	char *tok, *last, *idstr, *endptr;
	int expected_id, bytes, count = 0, errors = 0;
	u_int8_t id;

	assert ((tok = (char *) malloc (strlen(order) + 1)) != NULL);
	strcpy (tok, order);	/* tok has to be larger than order */
	assert (ioctl (pipefd[0], FIONREAD, &bytes) == 0);
	log_trace ("%d bytes read from FIFO.\n", bytes);

	for (idstr = strtok_r (tok, sep, &last);
	     (idstr != NULL) && (count < bytes);
	     idstr = strtok_r (NULL, sep, &last)) {

		/* Get the expected id: */
		expected_id = (int) strtol (idstr, &endptr, 10);
		assert ((endptr != NULL) && (*endptr == '\0'));

		/* Read the actual id from the pipe: */
		assert (read (pipefd[0], &id, sizeof (id)) == sizeof (id));
		count = count + sizeof (id);

		if (id != expected_id) {
			log_trace ("Thread %d ran out of order.\n", id);
			errors = errors + 1;
		}
		else {
			log_trace ("Thread %d at priority %d reporting.\n",
			    (int) id, states[id].priority);
		}
	}

	if (count < bytes) {
		/* Clear the pipe: */
		while (count < bytes) {
			read (pipefd[0], &id, sizeof (id));
			count = count + 1;
			errors = errors + 1;
		}
	}
	else if (bytes < count)
		errors = errors + count - bytes;

	if (errors == 0)
		log_pass ();
	else
		log_error ("%d threads ran out of order", errors);
}


static void *
waiter (void *arg)
{
	thread_state_t	*statep = (thread_state_t *) arg;
	pthread_mutex_t	*held_mutex[MAX_THREAD_CMDS];
	int 		held_mutex_owned[MAX_THREAD_CMDS];
	sigset_t	mask;
	struct timeval	tv1, tv2;
	thread_cmd_t	cmd;
	int 		i, mutex_count = 0;

	statep->status = STAT_INITIAL;

	/* Block all signals except for interrupt.*/
	sigfillset (&mask);
	sigdelset (&mask, SIGINT);
	sigprocmask (SIG_BLOCK, &mask, NULL);

	while (done == 0) {
		/* Wait for signal from the main thread to continue. */
		statep->status = STAT_WAITMUTEX;
		log_trace ("Thread %d: locking cond_mutex.\n",
		    (int) statep->id);
		pthread_mutex_lock (&cond_mutex);

		/* Do we report our status. */
		if (statep->flags & FLAGS_REPORT_WAITCONDMUTEX)
			write (pipefd[1], &statep->id, sizeof (statep->id));
		log_trace ("Thread %d: waiting for cond_var.\n",
		    (int) statep->id);

		/* Wait for a command. */
		statep->status = STAT_WAITCONDVAR;

		/*
		 * The threads are allowed commanded to wait either on
		 * their own unique condition variable (so they may be
		 * separately signaled) or on one global condition variable
		 * (so they may be signaled together).
		 */
		if (use_global_condvar != 0)
			pthread_cond_wait (&cond_var, &cond_mutex);
		else
			pthread_cond_wait (&statep->cond_var, &cond_mutex);

		/* Do we report our status? */
		if (statep->flags & FLAGS_REPORT_WAITCONDVAR) {
			write (pipefd[1], &statep->id, sizeof (statep->id));
			log_trace ("Thread %d: wrote to pipe.\n",
			    (int) statep->id);
		}
		log_trace ("Thread %d: received cond_var signal.\n",
		    (int) statep->id);

		/* Get a copy of the command before releasing the mutex. */
		cmd = statep->cmd;

		/* Clear the command after copying it. */
		statep->cmd.cmd_id = CMD_NONE;

		/* Unlock the condition variable mutex. */
		assert (pthread_mutex_unlock (&cond_mutex) == 0);

		/* Peform the command.*/
		switch (cmd.cmd_id) {
		case CMD_TAKE_MUTEX:
			statep->ret = pthread_mutex_lock (cmd.mutex);
			if (statep->ret == 0) {
				assert (mutex_count < sizeof (held_mutex));
				held_mutex[mutex_count] = cmd.mutex;
				held_mutex_owned[mutex_count] = 1;
				mutex_count++;
			}
			else {
				held_mutex_owned[mutex_count] = 0;
				log_trace ("Thread id %d unable to lock mutex, "
				    "error = %d\n", (int) statep->id,
				    statep->ret);
			}
			break;

		case CMD_RELEASE_MUTEX:
			assert ((mutex_count <= sizeof (held_mutex)) &&
			    (mutex_count > 0));
			mutex_count--;
			if (held_mutex_owned[mutex_count] != 0)
				assert (pthread_mutex_unlock
				    (held_mutex[mutex_count]) == 0);
			break;

		case CMD_WAIT_FOR_SIGNAL:
			assert (pthread_mutex_lock (cmd.mutex) == 0);
			assert (pthread_cond_wait (cmd.cond, cmd.mutex) == 0);
			assert (pthread_mutex_unlock (cmd.mutex) == 0);
			break;

		case CMD_BUSY_LOOP:
			log_trace ("Thread %d: Entering busy loop.\n",
			    (int) statep->id);
			/* Spin for 15 seconds. */
			assert (gettimeofday (&tv2, NULL) == 0);
			tv1.tv_sec = tv2.tv_sec + 5;
			tv1.tv_usec = tv2.tv_usec;
			statep->flags |= FLAGS_IS_BUSY;
			while (timercmp (&tv2, &tv1,<)) {
				assert (gettimeofday (&tv2, NULL) == 0);
			}
			statep->flags &= ~FLAGS_IS_BUSY;
			statep->flags |= FLAGS_WAS_BUSY;

			/* Do we report our status? */
			if (statep->flags & FLAGS_REPORT_BUSY_LOOP)
				write (pipefd[1], &statep->id,
				    sizeof (statep->id));

			log_trace ("Thread %d: Leaving busy loop.\n",
			    (int) statep->id);
			break;

		case CMD_PROTECTED_OP:
			assert (pthread_mutex_lock (cmd.mutex) == 0);
			statep->flags |= FLAGS_WAS_BUSY;
			/* Do we report our status? */
			if (statep->flags & FLAGS_REPORT_BUSY_LOOP)
				write (pipefd[1], &statep->id,
				    sizeof (statep->id));

			assert (pthread_mutex_unlock (cmd.mutex) == 0);
			break;

		case CMD_RELEASE_ALL:
			assert ((mutex_count <= sizeof (held_mutex)) &&
			    (mutex_count > 0));
			for (i = mutex_count - 1; i >= 0; i--) {
				if (held_mutex_owned[i] != 0)
					assert (pthread_mutex_unlock
					    (held_mutex[i]) == 0);
			}
			mutex_count = 0;
			break;

		case CMD_NONE:
		default:
			break;
		}

		/* Wait for the big giant waiter lock. */
		statep->status = STAT_WAITMUTEX;
		log_trace ("Thread %d: waiting for big giant lock.\n",
		    (int) statep->id);
		pthread_mutex_lock (&waiter_mutex);
		if (statep->flags & FLAGS_REPORT_WAITMUTEX)
			write (pipefd[1], &statep->id, sizeof (statep->id));
		log_trace ("Thread %d: got big giant lock.\n",
		    (int) statep->id);
		statep->status = STAT_INITIAL;
		pthread_mutex_unlock (&waiter_mutex);
	}

	log_trace ("Thread %d: Exiting thread 0x%x\n", (int) statep->id,
	    (int) pthread_self());
	pthread_exit (arg);
	return (NULL);
}


static void *
lock_twice (void *arg)
{
	thread_state_t	*statep = (thread_state_t *) arg;
	sigset_t	mask;

	statep->status = STAT_INITIAL;

	/* Block all signals except for interrupt.*/
	sigfillset (&mask);
	sigdelset (&mask, SIGINT);
	sigprocmask (SIG_BLOCK, &mask, NULL);

	/* Wait for a signal to continue. */
	log_trace ("Thread %d: locking cond_mutex.\n", (int) statep->id);
	pthread_mutex_lock (&cond_mutex);

	log_trace ("Thread %d: waiting for cond_var.\n", (int) statep->id);
	statep->status = STAT_WAITCONDVAR;
	pthread_cond_wait (&cond_var, &cond_mutex);

	log_trace ("Thread %d: received cond_var signal.\n", (int) statep->id);

	/* Unlock the condition variable mutex. */
	assert (pthread_mutex_unlock (&cond_mutex) == 0);

	statep->status = STAT_WAITMUTEX;
	/* Lock the mutex once. */
	assert (pthread_mutex_lock (statep->cmd.mutex) == 0);

	/* Lock it again and capture the error. */
	statep->ret = pthread_mutex_lock (statep->cmd.mutex);
	statep->status = 0;

	assert (pthread_mutex_unlock (statep->cmd.mutex) == 0);

	/* Unlock it again if it is locked recursively. */
	if (statep->ret == 0)
		pthread_mutex_unlock (statep->cmd.mutex);

	log_trace ("Thread %d: Exiting thread 0x%x\n", (int) statep->id,
	    (int) pthread_self());
	pthread_exit (arg);
	return (NULL);
}


static void
sighandler (int signo)
{
	log ("Signal handler caught signal %d, thread id 0x%x\n",
	    signo, (int) pthread_self());

	if (signo == SIGINT)
		done = 1;
}


static void
send_cmd (int id, thread_cmd_id_t cmd)
{
	assert (pthread_mutex_lock (&cond_mutex) == 0);
	assert (states[id].status == STAT_WAITCONDVAR);
	states[id].cmd.cmd_id = cmd;
	states[id].cmd.mutex = NULL;
	states[id].cmd.cond = NULL;
	/* Clear the busy flags. */
	states[id].flags &= ~(FLAGS_WAS_BUSY | FLAGS_IS_BUSY);
	assert (pthread_cond_signal (&states[id].cond_var) == 0);
	assert (pthread_mutex_unlock (&cond_mutex) == 0);
}


static void
send_mutex_cmd (int id, thread_cmd_id_t cmd, pthread_mutex_t *m)
{
	assert (pthread_mutex_lock (&cond_mutex) == 0);
	assert (states[id].status == STAT_WAITCONDVAR);
	states[id].cmd.cmd_id = cmd;
	states[id].cmd.mutex = m;
	states[id].cmd.cond = NULL;
	/* Clear the busy flags. */
	states[id].flags &= ~(FLAGS_WAS_BUSY | FLAGS_IS_BUSY);
	assert (pthread_cond_signal (&states[id].cond_var) == 0);
	assert (pthread_mutex_unlock (&cond_mutex) == 0);
}


static void
send_mutex_cv_cmd (int id, thread_cmd_id_t cmd, pthread_mutex_t *m,
    pthread_cond_t *cv)
{
	assert (pthread_mutex_lock (&cond_mutex) == 0);
	assert (states[id].status == STAT_WAITCONDVAR);
	states[id].cmd.cmd_id = cmd;
	states[id].cmd.mutex = m;
	states[id].cmd.cond = cv;
	/* Clear the busy flags. */
	states[id].flags &= ~(FLAGS_WAS_BUSY | FLAGS_IS_BUSY);
	assert (pthread_cond_signal (&states[id].cond_var) == 0);
	assert (pthread_mutex_unlock (&cond_mutex) == 0);
}


static void
mutex_init_test (void)
{
	pthread_mutexattr_t mattr;
	pthread_mutex_t	mutex;
	mutex_kind_t mkind;
	int mproto, ret;

	/*
	 * Initialize a mutex attribute.
	 *
	 * pthread_mutexattr_init not tested for: ENOMEM
	 */
	assert (pthread_mutexattr_init (&mattr) == 0);

	/*
	 * Initialize a mutex.
	 *
	 * pthread_mutex_init not tested for: EAGAIN ENOMEM EPERM EBUSY
	 */
	log ("Testing pthread_mutex_init\n");
	log ("--------------------------\n");

	for (mproto = 0; mproto < NELEMENTS(protocols); mproto++) {
		for (mkind = M_POSIX; mkind <= M_SS2_RECURSIVE; mkind++) {
			/* Initialize the mutex attribute. */
			assert (pthread_mutexattr_init (&mattr) == 0);
			assert (pthread_mutexattr_setprotocol (&mattr,
			    protocols[mproto]) == 0);

			/*
			 * Ensure that the first mutex type is a POSIX
			 * compliant mutex.
			 */
			if (mkind != M_POSIX) {
				assert (pthread_mutexattr_settype (&mattr,
				    mutex_types[mkind]) == 0);
			}

			log ("  Protocol %s, Type %s - ",
			    protocol_strs[mproto], mutextype_strs[mkind]);
			ret = pthread_mutex_init (&mutex, &mattr);
			check_result (/* expected */ 0, ret);
			assert (pthread_mutex_destroy (&mutex) == 0);

			/*
			 * Destroy a mutex attribute.
			 *
			 * XXX - There should probably be a magic number
			 *       associated with a mutex attribute so that
			 *       destroy can be reasonably sure the attribute
			 *       is valid.
			 *
			 * pthread_mutexattr_destroy not tested for: EINVAL
			 */
			assert (pthread_mutexattr_destroy (&mattr) == 0);
		}
	}
}


static void
mutex_destroy_test (void)
{
	pthread_mutexattr_t mattr;
	pthread_mutex_t	mutex;
	pthread_condattr_t cattr;
	pthread_cond_t	cv;
	pthread_attr_t pattr;
	int mproto, ret;
	mutex_kind_t mkind;
	thread_state_t state;

	/*
	 * Destroy a mutex.
	 *
	 * XXX - There should probably be a magic number associated
	 *       with a mutex so that destroy can be reasonably sure
	 *       the mutex is valid.
	 *
	 * pthread_mutex_destroy not tested for: 
	 */
	log ("Testing pthread_mutex_destroy\n");
	log ("-----------------------------\n");

	assert (pthread_attr_init (&pattr) == 0);
	assert (pthread_attr_setdetachstate (&pattr,
	    PTHREAD_CREATE_DETACHED) == 0);
	state.flags = 0;	/* No flags yet. */

	for (mproto = 0; mproto < NELEMENTS(protocols); mproto++) {
		for (mkind = M_POSIX; mkind <= M_SS2_RECURSIVE; mkind++) {
			/* Initialize the mutex attribute. */
			assert (pthread_mutexattr_init (&mattr) == 0);
			assert (pthread_mutexattr_setprotocol (&mattr,
			    protocols[mproto]) == 0);

			/*
			 * Ensure that the first mutex type is a POSIX
			 * compliant mutex.
			 */
			if (mkind != M_POSIX) {
				assert (pthread_mutexattr_settype (&mattr,
				    mutex_types[mkind]) == 0);
			}

			/* Create the mutex. */
			assert (pthread_mutex_init (&mutex, &mattr) == 0);

			log ("  Protocol %s, Type %s\n",
			    protocol_strs[mproto], mutextype_strs[mkind]);

			log ("    Destruction of unused mutex - ");
			assert (pthread_mutex_init (&mutex, &mattr) == 0);
			ret = pthread_mutex_destroy (&mutex);
			check_result (/* expected */ 0, ret);

			log ("    Destruction of mutex locked by self - ");
			assert (pthread_mutex_init (&mutex, &mattr) == 0);
			assert (pthread_mutex_lock (&mutex) == 0);
			ret = pthread_mutex_destroy (&mutex);
			check_result (/* expected */ EBUSY, ret);
			assert (pthread_mutex_unlock (&mutex) == 0);
			assert (pthread_mutex_destroy (&mutex) == 0);

			log ("    Destruction of mutex locked by another "
			    "thread - ");
			assert (pthread_mutex_init (&mutex, &mattr) == 0);
			send_mutex_cmd (0, CMD_TAKE_MUTEX, &mutex);
			sleep (1);
			ret = pthread_mutex_destroy (&mutex);
			check_result (/* expected */ EBUSY, ret);
			send_cmd (0, CMD_RELEASE_ALL);
			sleep (1);
			assert (pthread_mutex_destroy (&mutex) == 0);

			log ("    Destruction of mutex while being used in "
			    "cond_wait - ");
			assert (pthread_mutex_init (&mutex, &mattr) == 0);
			assert (pthread_condattr_init (&cattr) == 0);
			assert (pthread_cond_init (&cv, &cattr) == 0);
			send_mutex_cv_cmd (0, CMD_WAIT_FOR_SIGNAL, &mutex, &cv);
			sleep (1);
			ret = pthread_mutex_destroy (&mutex);
			check_result (/* expected */ EBUSY, ret);
			pthread_cond_signal (&cv);
			sleep (1);
			assert (pthread_mutex_destroy (&mutex) == 0);
		}
	}
}


static void
mutex_lock_test (void)
{
	pthread_mutexattr_t mattr;
	pthread_mutex_t	mutex;
	pthread_attr_t pattr;
	int mproto, ret;
	mutex_kind_t mkind;
	thread_state_t state;

	/*
	 * Lock a mutex.
	 *
	 * pthread_lock not tested for: 
	 */
	log ("Testing pthread_mutex_lock\n");
	log ("--------------------------\n");

	assert (pthread_attr_init (&pattr) == 0);
	assert (pthread_attr_setdetachstate (&pattr,
	    PTHREAD_CREATE_DETACHED) == 0);
	state.flags = 0;	/* No flags yet. */

	for (mproto = 0; mproto < NELEMENTS(protocols); mproto++) {
		for (mkind = M_POSIX; mkind <= M_SS2_RECURSIVE; mkind++) {
			/* Initialize the mutex attribute. */
			assert (pthread_mutexattr_init (&mattr) == 0);
			assert (pthread_mutexattr_setprotocol (&mattr,
			    protocols[mproto]) == 0);

			/*
			 * Ensure that the first mutex type is a POSIX
			 * compliant mutex.
			 */
			if (mkind != M_POSIX) {
				assert (pthread_mutexattr_settype (&mattr,
				    mutex_types[mkind]) == 0);
			}

			/* Create the mutex. */
			assert (pthread_mutex_init (&mutex, &mattr) == 0);

			log ("  Protocol %s, Type %s\n",
			    protocol_strs[mproto], mutextype_strs[mkind]);

			log ("    Lock on unlocked mutex - ");
			ret = pthread_mutex_lock (&mutex);
			check_result (/* expected */ 0, ret);
			pthread_mutex_unlock (&mutex);

			log ("    Lock on invalid mutex - ");
			ret = pthread_mutex_lock (NULL);
			check_result (/* expected */ EINVAL, ret);

			log ("    Lock on mutex held by self - ");
			assert (pthread_create (&state.tid, &pattr, lock_twice,
			    (void *) &state) == 0);
			/* Let the thread start. */
			sleep (1);
			state.cmd.mutex = &mutex;
			state.ret = 0xdeadbeef;
			assert (pthread_mutex_lock (&cond_mutex) == 0);
			assert (pthread_cond_signal (&cond_var) == 0);
			assert (pthread_mutex_unlock (&cond_mutex) == 0);
			/* Let the thread receive and process the command. */
			sleep (1);

			switch (mkind) {
			case M_POSIX:
				check_result (/* expected */ EDEADLK,
				    state.ret);
				break;
			case M_SS2_DEFAULT:
				check_result (/* expected */ EDEADLK,
				    state.ret);
				break;
			case M_SS2_ERRORCHECK:
				check_result (/* expected */ EDEADLK,
				    state.ret);
				break;
			case M_SS2_NORMAL:
				check_result (/* expected */ 0xdeadbeef,
				    state.ret);
				break;
			case M_SS2_RECURSIVE:
				check_result (/* expected */ 0, state.ret);
				break;
			}
			pthread_mutex_destroy (&mutex);
			pthread_mutexattr_destroy (&mattr);
		}
	}
}


static void
mutex_unlock_test (void)
{
	const int test_thread_id = 0;	/* ID of test thread */
	pthread_mutexattr_t mattr;
	pthread_mutex_t	mutex;
	int mproto, ret;
	mutex_kind_t mkind;

	/*
	 * Unlock a mutex.
	 *
	 * pthread_unlock not tested for: 
	 */
	log ("Testing pthread_mutex_unlock\n");
	log ("----------------------------\n");

	for (mproto = 0; mproto < NELEMENTS(protocols); mproto++) {
		for (mkind = M_POSIX; mkind <= M_SS2_RECURSIVE; mkind++) {
			/* Initialize the mutex attribute. */
			assert (pthread_mutexattr_init (&mattr) == 0);
			assert (pthread_mutexattr_setprotocol (&mattr,
			    protocols[mproto]) == 0);

			/*
			 * Ensure that the first mutex type is a POSIX
			 * compliant mutex.
			 */
			if (mkind != M_POSIX) {
				assert (pthread_mutexattr_settype (&mattr,
				    mutex_types[mkind]) == 0);
			}

			/* Create the mutex. */
			assert (pthread_mutex_init (&mutex, &mattr) == 0);

			log ("  Protocol %s, Type %s\n",
			    protocol_strs[mproto], mutextype_strs[mkind]);

			log ("    Unlock on mutex held by self - ");
			assert (pthread_mutex_lock (&mutex) == 0);
			ret = pthread_mutex_unlock (&mutex);
			check_result (/* expected */ 0, ret);

			log ("    Unlock on invalid mutex - ");
			ret = pthread_mutex_unlock (NULL);
			check_result (/* expected */ EINVAL, ret);

			log ("    Unlock on mutex locked by another thread - ");
			send_mutex_cmd (test_thread_id, CMD_TAKE_MUTEX, &mutex);
			sleep (1);
			ret = pthread_mutex_unlock (&mutex);
			switch (mkind) {
			case M_POSIX:
				check_result (/* expected */ EPERM, ret);
				break;
			case M_SS2_DEFAULT:
				check_result (/* expected */ EPERM, ret);
				break;
			case M_SS2_ERRORCHECK:
				check_result (/* expected */ EPERM, ret);
				break;
			case M_SS2_NORMAL:
				check_result (/* expected */ EPERM, ret);
				break;
			case M_SS2_RECURSIVE:
				check_result (/* expected */ EPERM, ret);
				break;
			}
			if (ret == 0) {
				/*
				 * If for some reason we were able to unlock
				 * the mutex, relock it so that the test
				 * thread has no problems releasing the mutex.
				 */
				pthread_mutex_lock (&mutex);
			}
			send_cmd (test_thread_id, CMD_RELEASE_ALL);
			sleep (1);

			pthread_mutex_destroy (&mutex);
			pthread_mutexattr_destroy (&mattr);
		}
	}
}


static void
queueing_order_test (void)
{
	int i;

	log ("Testing queueing order\n");
	log ("----------------------\n");
	assert (pthread_mutex_lock (&waiter_mutex) == 0);
	/*
	 * Tell the threads to report when they take the waiters mutex.
	 */
	assert (pthread_mutex_lock (&cond_mutex) == 0);
	for (i = 0; i < NUM_THREADS; i++) {
		states[i].flags = FLAGS_REPORT_WAITMUTEX;
		assert (pthread_cond_signal (&states[i].cond_var) == 0);
	}
	assert (pthread_mutex_unlock (&cond_mutex) == 0);

	/* Signal the threads to continue. */
	sleep (1);

	/* Use the global condition variable next time. */
	use_global_condvar = 1;

	/* Release the waiting threads and allow them to run again. */
	assert (pthread_mutex_unlock (&waiter_mutex) == 0);
	sleep (1);

	log ("  Queueing order on a mutex - ");
	check_run_order ("9,8,7,6,5,4,3,2,1,0");
	for (i = 0; i < NUM_THREADS; i = i + 1) {
		/* Tell the threads to report when they've been signaled. */
		states[i].flags = FLAGS_REPORT_WAITCONDVAR;
	}

	/*
	 * Prevent the threads from continuing their loop after we
	 * signal them.
	 */
	assert (pthread_mutex_lock (&waiter_mutex) == 0);


	log ("  Queueing order on a condition variable - ");
	/*
	 * Signal one thread to run and see that the highest priority
	 * thread executes.
	 */
	assert (pthread_mutex_lock (&cond_mutex) == 0);
	assert (pthread_cond_signal (&cond_var) == 0);
	assert (pthread_mutex_unlock (&cond_mutex) == 0);
	sleep (1);
	if (states[NUM_THREADS - 1].status != STAT_WAITMUTEX)
		log_error ("highest priority thread does not run.\n");

	/* Signal the remaining threads. */
	assert (pthread_mutex_lock (&cond_mutex) == 0);
	assert (pthread_cond_broadcast (&cond_var) == 0);
	assert (pthread_mutex_unlock (&cond_mutex) == 0);
	sleep (1);

	check_run_order ("9,8,7,6,5,4,3,2,1,0");
	for (i = 0; i < NUM_THREADS; i = i + 1) {
		/* Tell the threads not to report anything. */
		states[i].flags = 0;
	}

	/* Use the thread unique condition variable next time. */
	use_global_condvar = 0;

	/* Allow the threads to continue their loop. */
	assert (pthread_mutex_unlock (&waiter_mutex) == 0);
	sleep (1);
}


static void
mutex_prioceiling_test (void)
{
	const int test_thread_id = 0;	/* ID of test thread */
	pthread_mutexattr_t mattr;
	struct sched_param param;
	pthread_mutex_t	m[3];
	mutex_kind_t	mkind;
	int		i, ret, policy, my_prio, old_ceiling;

	log ("Testing priority ceilings\n");
	log ("-------------------------\n");
	for (mkind = M_POSIX; mkind <= M_SS2_RECURSIVE; mkind++) {

		log ("  Protype PTHREAD_PRIO_PROTECT, Type %s\n",
		    mutextype_strs[mkind]);

		/*
		 * Initialize and create a mutex.
		 */
		assert (pthread_mutexattr_init (&mattr) == 0);

		/* Get this threads current priority. */
		assert (pthread_getschedparam (pthread_self(), &policy,
		    &param) == 0);
		my_prio = param.sched_priority;	/* save for later use */
		log_trace ("Current scheduling policy %d, priority %d\n",
		    policy, my_prio);

		/*
		 * Initialize and create 3 priority protection mutexes with
		 * default (max priority) ceilings.
		 */
		assert (pthread_mutexattr_setprotocol(&mattr,
		    PTHREAD_PRIO_PROTECT) == 0);

		/*
		 * Ensure that the first mutex type is a POSIX
		 * compliant mutex.
		 */
		if (mkind != M_POSIX) {
			assert (pthread_mutexattr_settype (&mattr,
			    mutex_types[mkind]) == 0);
		}

		for (i = 0; i < 3; i++)
			assert (pthread_mutex_init (&m[i], &mattr) == 0);

		/*
		 * Set the ceiling priorities for the 3 priority protection
		 * mutexes to, 5 less than, equal to, and 5 greater than,
		 * this threads current priority.
		 */
		for (i = 0; i < 3; i++)
			assert (pthread_mutex_setprioceiling (&m[i],
			    my_prio - 5 + 5*i, &old_ceiling) == 0);

		/*
		 * Check that if we attempt to take a mutex whose priority
		 * ceiling is lower than our priority, we get an error.
		 */
		log ("    Lock with ceiling priority < thread priority - ");
		ret = pthread_mutex_lock (&m[0]);
		check_result (/* expected */ EINVAL, ret);
		if (ret == 0)
			pthread_mutex_unlock (&m[0]);

		/*
		 * Check that we can take a mutex whose priority ceiling
		 * is equal to our priority.
		 */
		log ("    Lock with ceiling priority = thread priority - ");
		ret = pthread_mutex_lock (&m[1]);
		check_result (/* expected */ 0, ret);
		if (ret == 0)
			pthread_mutex_unlock (&m[1]);

		/*
		 * Check that we can take a mutex whose priority ceiling
		 * is higher than our priority.
		 */
		log ("    Lock with ceiling priority > thread priority - ");
		ret = pthread_mutex_lock (&m[2]);
		check_result (/* expected */ 0, ret);
		if (ret == 0)
			pthread_mutex_unlock (&m[2]);

		/*
		 * Have the test thread go into a busy loop for 5 seconds
		 * and see that it doesn't block this thread (since the
		 * priority ceiling of mutex 0 and the priority of the test
		 * thread are both less than the priority of this thread).
		 */
		log ("    Preemption with ceiling priority < thread "
		    "priority - ");
		/* Have the test thread take mutex 0. */
		send_mutex_cmd (test_thread_id, CMD_TAKE_MUTEX, &m[0]);
		sleep (1);

		log_trace ("Sending busy command.\n");
		send_cmd (test_thread_id, CMD_BUSY_LOOP);
		log_trace ("Busy sent, yielding\n");
		pthread_yield ();
		log_trace ("Returned from yield.\n");
		if (states[test_thread_id].flags &
		    (FLAGS_IS_BUSY | FLAGS_WAS_BUSY))
			log_error ("test thread inproperly preempted us.\n");
		else {
			/* Let the thread finish its busy loop. */
			sleep (6);
			if ((states[test_thread_id].flags & FLAGS_WAS_BUSY) == 0)
				log_error ("test thread never finished.\n");
			else
				log_pass ();
		}
		states[test_thread_id].flags &= ~FLAGS_WAS_BUSY;

		/* Have the test thread release mutex 0. */
		send_cmd (test_thread_id, CMD_RELEASE_ALL);
		sleep (1);

		/*
		 * Have the test thread go into a busy loop for 5 seconds
		 * and see that it preempts this thread (since the priority
		 * ceiling of mutex 1 is the same as the priority of this
		 * thread).  The test thread should not run to completion
		 * as its time quantum should expire before the 5 seconds
		 * are up.
		 */
		log ("    Preemption with ceiling priority = thread "
		    "priority - ");

		/* Have the test thread take mutex 1. */
		send_mutex_cmd (test_thread_id, CMD_TAKE_MUTEX, &m[1]);
		sleep (1);

		log_trace ("Sending busy\n");
		send_cmd (test_thread_id, CMD_BUSY_LOOP);
		log_trace ("Busy sent, yielding\n");
		pthread_yield ();
		log_trace ("Returned from yield.\n");
		if ((states[test_thread_id].flags & FLAGS_IS_BUSY) == 0)
			log_error ("test thread did not switch in on yield.\n");
		else if (states[test_thread_id].flags & FLAGS_WAS_BUSY)
			log_error ("test thread ran to completion.\n");
		else {
			/* Let the thread finish its busy loop. */
			sleep (6);
			if ((states[test_thread_id].flags & FLAGS_WAS_BUSY) == 0)
				log_error ("test thread never finished.\n");
			else
				log_pass ();
		}
		states[test_thread_id].flags &= ~FLAGS_WAS_BUSY;

		/* Have the test thread release mutex 1. */
		send_cmd (test_thread_id, CMD_RELEASE_ALL);
		sleep (1);

		/*
		 * Set the scheduling policy of the test thread to SCHED_FIFO
		 * and have it go into a busy loop for 5 seconds.  This
		 * thread is SCHED_RR, and since the priority ceiling of
		 * mutex 1 is the same as the priority of this thread, the
		 * test thread should run to completion once it is switched
		 * in.
		 */
		log ("    SCHED_FIFO scheduling and ceiling priority = "
		    "thread priority - ");
		param.sched_priority = states[test_thread_id].priority;
		assert (pthread_setschedparam (states[test_thread_id].tid,
		    SCHED_FIFO, &param) == 0);

		/* Have the test thread take mutex 1. */
		send_mutex_cmd (test_thread_id, CMD_TAKE_MUTEX, &m[1]);
		sleep (1);

		log_trace ("Sending busy\n");
		send_cmd (test_thread_id, CMD_BUSY_LOOP);
		log_trace ("Busy sent, yielding\n");
		pthread_yield ();
		log_trace ("Returned from yield.\n");
		if ((states[test_thread_id].flags & FLAGS_WAS_BUSY) == 0) {
			log_error ("test thread did not run to completion.\n");
			/* Let the thread finish it's busy loop. */
			sleep (6);
		}
		else
			log_pass ();
		states[test_thread_id].flags &= ~FLAGS_WAS_BUSY;

		/* Restore the test thread scheduling parameters. */
		param.sched_priority = states[test_thread_id].priority;
		assert (pthread_setschedparam (states[test_thread_id].tid,
		    SCHED_RR, &param) == 0);

		/* Have the test thread release mutex 1. */
		send_cmd (test_thread_id, CMD_RELEASE_ALL);
		sleep (1);

		/*
		 * Have the test thread go into a busy loop for 5 seconds
		 * and see that it preempts this thread (since the priority
		 * ceiling of mutex 2 is the greater than the priority of
		 * this thread).  The test thread should run to completion
		 * and block this thread because its active priority is
		 * higher.
		 */
		log ("    SCHED_FIFO scheduling and ceiling priority > "
		    "thread priority - ");
		/* Have the test thread take mutex 2. */
		send_mutex_cmd (test_thread_id, CMD_TAKE_MUTEX, &m[2]);
		sleep (1);

		log_trace ("Sending busy\n");
		send_cmd (test_thread_id, CMD_BUSY_LOOP);
		log_trace ("Busy sent, yielding\n");
		pthread_yield ();
		log_trace ("Returned from yield.\n");
		if ((states[test_thread_id].flags & FLAGS_IS_BUSY) != 0) {
			log_error ("test thread did not run to completion.\n");
			/* Let the thread finish it's busy loop. */
			sleep (6);
		}
		else if ((states[test_thread_id].flags & FLAGS_WAS_BUSY) == 0)
			log_error ("test thread never finished.\n");
		else
			log_pass ();
		states[test_thread_id].flags &= ~FLAGS_WAS_BUSY;

		/* Have the test thread release mutex 2. */
		send_cmd (test_thread_id, CMD_RELEASE_ALL);
		sleep (1);

		/* Destroy the mutexes. */
		for (i = 0; i < 3; i++)
			assert (pthread_mutex_destroy (&m[i]) == 0);
	}
}


static void
mutex_prioinherit_test (void)
{
	pthread_mutexattr_t mattr;
	struct sched_param param;
	pthread_mutex_t	m[3];
	mutex_kind_t	mkind;
	int		i, policy, my_prio;

	/* Get this threads current priority. */
	assert (pthread_getschedparam (pthread_self(), &policy,
	    &param) == 0);
	my_prio = param.sched_priority;	/* save for later use */
	log_trace ("Current scheduling policy %d, priority %d\n",
	    policy, my_prio);

	log ("Testing priority inheritence\n");
	log ("----------------------------\n");
	for (mkind = M_POSIX; mkind <= M_SS2_RECURSIVE; mkind++) {

		log ("  Protype PTHREAD_PRIO_INHERIT, Type %s\n",
		    mutextype_strs[mkind]);

		/*
		 * Initialize and create a mutex.
		 */
		assert (pthread_mutexattr_init (&mattr) == 0);

		/*
		 * Initialize and create 3 priority inheritence mutexes with
		 * default (max priority) ceilings.
		 */
		assert (pthread_mutexattr_setprotocol(&mattr,
		    PTHREAD_PRIO_INHERIT) == 0);

		/*
		 * Ensure that the first mutex type is a POSIX
		 * compliant mutex.
		 */
		if (mkind != M_POSIX) {
			assert (pthread_mutexattr_settype (&mattr,
			    mutex_types[mkind]) == 0);
		}

		for (i = 0; i < 3; i++)
			assert (pthread_mutex_init (&m[i], &mattr) == 0);

		/*
		 * Test setup:
		 *   Thread 4 - take mutex 0, 1
		 *   Thread 2 - enter protected busy loop with mutex 0
		 *   Thread 3 - enter protected busy loop with mutex 1
		 *   Thread 4 - enter protected busy loop with mutex 2
		 *   Thread 5 - enter busy loop
		 *   Thread 6 - enter protected busy loop with mutex 0
		 *   Thread 4 - releases mutexes 1 and 0.
		 *
		 * Expected results:
		 *   Threads complete in order 4, 6, 5, 3, 2
		 */
		log ("    Simple inheritence test - ");

		/*
		 * Command thread 4 to take mutexes 0 and 1.
		 */
		send_mutex_cmd (4, CMD_TAKE_MUTEX, &m[0]);
		sleep (1);	/* Allow command to be received. */
		send_mutex_cmd (4, CMD_TAKE_MUTEX, &m[1]);
		sleep (1);

		/*
		 * Tell the threads to report themselves when they are
		 * at the bottom of their loop (waiting on wait_mutex).
		 */
		for (i = 0; i < NUM_THREADS; i++)
			states[i].flags |= FLAGS_REPORT_WAITMUTEX;

		/*
		 * Command thread 2 to take mutex 0 and thread 3 to take
		 * mutex 1, both via a protected operation command.  Since
		 * thread 4 owns mutexes 0 and 1, both threads 2 and 3
		 * will block until the mutexes are released by thread 4.
		 */
		log_trace ("Commanding protected operation to thread 2.\n");
		send_mutex_cmd (2, CMD_PROTECTED_OP, &m[0]);
		log_trace ("Commanding protected operation to thread 3.\n");
		send_mutex_cmd (3, CMD_PROTECTED_OP, &m[1]);
		sleep (1);

		/*
		 * Command thread 4 to take mutex 2 via a protected operation
		 * and thread 5 to enter a busy loop for 5 seconds.  Since
		 * thread 5 has higher priority than thread 4, thread 5 will
		 * enter the busy loop before thread 4 is activated.
		 */
		log_trace ("Commanding protected operation to thread 4.\n");
		send_mutex_cmd (4, CMD_PROTECTED_OP, &m[2]);
		log_trace ("Commanding busy loop to thread 5.\n");
		send_cmd (5, CMD_BUSY_LOOP);
		sleep (1);
		if ((states[5].flags & FLAGS_IS_BUSY) == 0)
			log_error ("thread 5 is not running.\n");
		log_trace ("Commanding protected operation thread 6.\n");
		send_mutex_cmd (6, CMD_PROTECTED_OP, &m[0]);
		sleep (1);
		if ((states[4].flags & FLAGS_WAS_BUSY) == 0)
			log_error ("thread 4 failed to inherit priority.\n");
		states[4].flags = 0;
		send_cmd (4, CMD_RELEASE_ALL);
		sleep (5);
		check_run_order ("4,6,5,3,2");

		/*
		 * Clear the flags.
		 */
		for (i = 0; i < NUM_THREADS; i++)
			states[i].flags = 0;

		/*
		 * Test setup:
		 *   Thread 2 - enter busy loop (SCHED_FIFO)
		 *   Thread 4 - take mutex 0
		 *   Thread 4 - priority change to same priority as thread 2
		 *   Thread 4 - release mutex 0
		 *
		 * Expected results:
		 *   Since thread 4 owns a priority mutex, it should be
		 *   placed at the front of the run queue (for its new
		 *   priority slot) when its priority is lowered to the
		 *   same priority as thread 2.  If thread 4 did not own
		 *   a priority mutex, then it would have been added to
		 *   the end of the run queue and thread 2 would have
		 *   executed until it blocked (because it's scheduling
		 *   policy is SCHED_FIFO).
		 *   
		 */
		log ("    Inheritence test with change of priority - ");

		/*
		 * Change threads 2 and 4 scheduling policies to be
		 * SCHED_FIFO.
		 */
		param.sched_priority = states[2].priority;
		assert (pthread_setschedparam (states[2].tid, SCHED_FIFO,
		    &param) == 0);
		param.sched_priority = states[4].priority;
		assert (pthread_setschedparam (states[4].tid, SCHED_FIFO,
		    &param) == 0);

		/*
		 * Command thread 4 to take mutex 0.
		 */
		send_mutex_cmd (4, CMD_TAKE_MUTEX, &m[0]);
		sleep (1);

		/*
		 * Command thread 2 to enter busy loop.
		 */
		send_cmd (2, CMD_BUSY_LOOP);
		sleep (1);	/* Allow command to be received. */

		/*
		 * Command thread 4 to enter busy loop.
		 */
		send_cmd (4, CMD_BUSY_LOOP);
		sleep (1);	/* Allow command to be received. */

		/* Have threads 2 and 4 report themselves. */
		states[2].flags = FLAGS_REPORT_WAITMUTEX;
		states[4].flags = FLAGS_REPORT_WAITMUTEX;

		/* Change the priority of thread 4. */
		param.sched_priority = states[2].priority;
		assert (pthread_setschedparam (states[4].tid, SCHED_FIFO,
		    &param) == 0);
		sleep (5);
		check_run_order ("4,2");

		/* Clear the flags */
		states[2].flags = 0;
		states[4].flags = 0;

		/* Reset the policies. */
		param.sched_priority = states[2].priority;
		assert (pthread_setschedparam (states[2].tid, SCHED_RR,
		    &param) == 0);
		param.sched_priority = states[4].priority;
		assert (pthread_setschedparam (states[4].tid, SCHED_RR,
		    &param) == 0);

		send_cmd (4, CMD_RELEASE_MUTEX);
		sleep (1);

		/* Destroy the mutexes. */
		for (i = 0; i < 3; i++)
			assert (pthread_mutex_destroy (&m[i]) == 0);
	}
}


int main (int argc, char *argv[])
{
	pthread_mutexattr_t mattr;
	pthread_condattr_t cattr;
	pthread_attr_t	pattr;
	int		i, policy, main_prio;
	void *		exit_status;
	sigset_t	mask;
	struct sigaction act;
	struct sched_param param;

	logfile = stdout;
 
	assert (pthread_getschedparam (pthread_self (), &policy, &param) == 0);
	main_prio = param.sched_priority;

	/* Setupt our signal mask. */
	sigfillset (&mask);
	sigdelset (&mask, SIGINT);
	sigprocmask (SIG_SETMASK, &mask, NULL);

	/* Install a signal handler for SIGINT */
	sigemptyset (&act.sa_mask);
	sigaddset (&act.sa_mask, SIGINT);
	act.sa_handler = sighandler;
	act.sa_flags = SA_RESTART;
	sigaction (SIGINT, &act, NULL);

	/*
	 * Initialize the thread attribute.
	 */
	assert (pthread_attr_init (&pattr) == 0);
	assert (pthread_attr_setdetachstate (&pattr,
	    PTHREAD_CREATE_JOINABLE) == 0);

	/*
	 * Initialize and create the waiter and condvar mutexes.
	 */
	assert (pthread_mutexattr_init (&mattr) == 0);
	assert (pthread_mutex_init (&waiter_mutex, &mattr) == 0);
	assert (pthread_mutex_init (&cond_mutex, &mattr) == 0);

	/*
	 * Initialize and create a condition variable.
	 */
	assert (pthread_condattr_init (&cattr) == 0);
	assert (pthread_cond_init (&cond_var, &cattr) == 0);

	/* Create a pipe to catch the results of thread wakeups. */
	assert (pipe (pipefd) == 0);

#ifdef DEBUG
	assert (pthread_switch_add_np (kern_switch) == 0);
#endif

	/*
	 * Create the waiting threads.
	 */
	for (i = 0; i < NUM_THREADS; i++) {
		assert (pthread_cond_init (&states[i].cond_var, &cattr) == 0);
		states[i].id = (u_int8_t) i;  /* NUM_THREADS must be <= 256 */
		states[i].status = 0;
		states[i].cmd.cmd_id = CMD_NONE;
		states[i].flags = 0;	/* No flags yet. */
		assert (pthread_create (&states[i].tid, &pattr, waiter,
		    (void *) &states[i]) == 0);
		param.sched_priority = main_prio - 10 + i;
		states[i].priority = param.sched_priority;
		assert (pthread_setschedparam (states[i].tid, SCHED_OTHER,
		    &param) == 0);
#if defined(_LIBC_R_)
		{
			char buf[30];

			snprintf (buf, sizeof(buf), "waiter_%d", i);
			pthread_set_name_np (states[i].tid, buf);
		}
#endif
	}

	/* Allow the threads to start. */
	sleep (1);
	log_trace ("Done creating threads.\n");

	log ("\n");
	mutex_init_test ();
	log ("\n");
	mutex_destroy_test ();
	log ("\n");
	mutex_lock_test ();
	log ("\n");
	mutex_unlock_test ();
	log ("\n");
	queueing_order_test ();
	log ("\n");
	mutex_prioinherit_test ();
	log ("\n");
	mutex_prioceiling_test ();
	log ("\n");

	log ("Total tests %d, passed %d, failed %d\n",
	    total, pass_count, error_count);

	/* Set the done flag and signal the threads to exit. */
	log_trace ("Setting done flag.\n");
	done = 1;

	/*
	 * Wait for the threads to finish.
	 */
	log_trace ("Trying to join threads.\n");
	for (i = 0; i < NUM_THREADS; i++) {
		send_cmd (i, CMD_NONE);
		assert (pthread_join (states[i].tid, &exit_status) == 0);
	}

	/* Clean up after ourselves. */
	close (pipefd[0]);
	close (pipefd[1]);

	if (error_count != 0)
		exit (EX_OSERR);	/* any better ideas??? */
	else
		exit (EX_OK);
}
OpenPOWER on IntegriCloud