hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
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
/*
 * Functional testing of the condvar implementation for Cobalt.
 *
 * Copyright (C) Gilles Chanteperdrix  <gilles.chanteperdrix@xenomai.org>
 *
 * Released under the terms of GPLv2.
 */
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <stdarg.h>
#include <pthread.h>
#include <semaphore.h>
#include <smokey/smokey.h>
 
smokey_test_plugin(posix_cond,
          SMOKEY_NOARGS,
          "Check POSIX condition variable services"
);
 
#define NS_PER_MS (1000000)
#define NS_PER_S (1000000000)
 
static unsigned long long timer_read(void)
{
   struct timespec ts;
 
   clock_gettime(CLOCK_REALTIME, &ts);
   return (unsigned long long)ts.tv_sec * NS_PER_S + ts.tv_nsec;
}
 
static inline unsigned long long timer_get_tsc(void)
{
   return clockobj_get_tsc();
}
 
static inline unsigned long long timer_tsc2ns(unsigned long long tsc)
{
   return clockobj_tsc_to_ns(tsc);
}
 
static void check_inner(const char *file, int line, const char *fn, const char *msg, int status, int expected)
{
   if (status == expected)
       return;
 
   fprintf(stderr, "FAILED %s %s: returned %d instead of %d - %s\n",
       fn, msg, status, expected, strerror(-status));
   exit(EXIT_FAILURE);
}
#define check(msg, status, expected) \
   check_inner(__FILE__, __LINE__, __func__, msg, status, expected)
 
#define check_unix(msg, status, expected)                \
   ({                                \
       int s = (status);                    \
       check_inner(__FILE__, __LINE__, __func__, msg, s < 0 ? -errno : s, expected); \
   })
 
static void check_sleep_inner(const char *fn,
              const char *prefix, unsigned long long start)
{
   unsigned long long diff = timer_tsc2ns(timer_get_tsc() - start);
 
   if (diff < 10 * NS_PER_MS) {
       fprintf(stderr, "%s waited %Ld.%03u us\n",
           prefix, diff / 1000, (unsigned)(diff % 1000));
       exit(EXIT_FAILURE);
   }
}
#define check_sleep(prefix, start) \
   check_sleep_inner(__func__, prefix, start)
 
static int mutex_init(pthread_mutex_t *mutex, int type, int proto)
{
   pthread_mutexattr_t mattr;
   int err;
 
   pthread_mutexattr_init(&mattr);
   pthread_mutexattr_settype(&mattr, type);
   err = pthread_mutexattr_setprotocol(&mattr, proto);
   if (err)
       goto out;
   if (proto == PTHREAD_PRIO_PROTECT)
       pthread_mutexattr_setprioceiling(&mattr, 3);
 
   err = pthread_mutex_init(mutex, &mattr);
  out:
   pthread_mutexattr_destroy(&mattr);
 
   return -err;
}
#define mutex_lock(mutex) (-pthread_mutex_lock(mutex))
#define mutex_unlock(mutex) (-pthread_mutex_unlock(mutex))
#define mutex_destroy(mutex) (-pthread_mutex_destroy(mutex))
 
static int cond_init(pthread_cond_t *cond, int absolute)
{
   pthread_condattr_t cattr;
   int ret;
 
   pthread_condattr_init(&cattr);
   ret = pthread_condattr_setclock(&cattr,
                   absolute ? CLOCK_REALTIME : CLOCK_MONOTONIC);
   if (ret) {
       pthread_condattr_destroy(&cattr);
       return ENOSYS;
   }
   ret = pthread_cond_init(cond, &cattr);
   pthread_condattr_destroy(&cattr);
 
   return -ret;
}
#define cond_signal(cond) (-pthread_cond_signal(cond))
 
static int cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex, unsigned long long ns)
{
   struct timespec ts;
 
   if (ns == 0)
       return -pthread_cond_wait(cond, mutex);
 
   clock_gettime(CLOCK_MONOTONIC, &ts);
   ns += ts.tv_nsec;
   ts.tv_sec += ns / NS_PER_S;
   ts.tv_nsec = ns % NS_PER_S;
 
   return -pthread_cond_timedwait(cond, mutex, &ts);
}
 
static int cond_wait_until(pthread_cond_t *cond, pthread_mutex_t *mutex, unsigned long long date)
{
   struct timespec ts = {
       .tv_sec = date / NS_PER_S,
       .tv_nsec = date % NS_PER_S,
   };
 
   return -pthread_cond_timedwait(cond, mutex, &ts);
}
#define cond_destroy(cond) (-pthread_cond_destroy(cond))
 
static int thread_msleep(unsigned ms)
{
   struct timespec ts = {
       .tv_sec = (ms * NS_PER_MS) / NS_PER_S,
       .tv_nsec = (ms * NS_PER_MS) % NS_PER_S,
   };
 
   return -nanosleep(&ts, NULL);
}
 
struct thread_startup {
   sem_t ready;
   void *(*handler)(void *);
   void *cookie;
};
 
static void *thread_trampoline(void *arg)
{
   struct thread_startup *startup = arg;
 
   sem_post(&startup->ready);
 
   return startup->handler(startup->cookie);
}
 
static int thread_spawn(pthread_t *thread, int prio,
           void *(*handler)(void *cookie), void *cookie)
{
   struct thread_startup startup;
   struct sched_param param;
   pthread_attr_t tattr;
   int err;
 
   sem_init(&startup.ready, 0, 0);
   startup.handler = handler;
   startup.cookie = cookie;
 
   pthread_attr_init(&tattr);
   pthread_attr_setinheritsched(&tattr, PTHREAD_EXPLICIT_SCHED);
   pthread_attr_setschedpolicy(&tattr, SCHED_FIFO);
   param.sched_priority = prio;
   pthread_attr_setschedparam(&tattr, &param);
   pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
 
   err = pthread_create(thread, &tattr, thread_trampoline, &startup);
 
   check("wait startup", sem_wait(&startup.ready), 0);
 
   pthread_attr_destroy(&tattr);
   sem_destroy(&startup.ready);
 
   return -err;
}
#define thread_yield() sched_yield()
#define thread_kill(thread, sig) (-__STD(pthread_kill(thread, sig)))
#define thread_self() pthread_self()
#define thread_join(thread) (-pthread_join(thread, NULL))
 
struct cond_mutex {
   pthread_mutex_t *mutex;
   pthread_cond_t *cond;
   pthread_t tid;
};
 
static void *cond_signaler(void *cookie)
{
   unsigned long long start;
   struct cond_mutex *cm = cookie;
 
   start = timer_get_tsc();
   check("mutex_lock", mutex_lock(cm->mutex), 0);
   check_sleep("mutex_lock", start);
   thread_msleep(10);
   check("cond_signal", cond_signal(cm->cond), 0);
   check("mutex_unlock", mutex_unlock(cm->mutex), 0);
 
   return NULL;
}
 
static void autoinit_simple_conddestroy(void)
{
   pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
   pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
   unsigned int invalmagic = ~0x86860505; // ~COBALT_COND_MAGIC
 
   memcpy((char *)&cond2 + sizeof(cond2) - sizeof(invalmagic),
       &invalmagic, sizeof(invalmagic));
 
   smokey_trace("%s", __func__);
   check("cond_destroy", cond_destroy(&cond), 0);
   check("cond_destroy invalid", cond_destroy(&cond2), -EINVAL);
}
 
static void autoinit_simple_condwait(void)
{
   pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
   unsigned long long start;
   pthread_mutex_t mutex;
   struct cond_mutex cm = {
       .mutex = &mutex,
       .cond = &cond,
   };
   pthread_t cond_signaler_tid;
 
   smokey_trace("%s", __func__);
 
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
   check("thread_spawn",
         thread_spawn(&cond_signaler_tid, 2, cond_signaler, &cm), 0);
   thread_msleep(11);
 
   start = timer_get_tsc();
   check("cond_wait", cond_wait(&cond, &mutex, 0), 0);
   check_sleep("cond_wait", start);
   thread_msleep(10);
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("thread_join", thread_join(cond_signaler_tid), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void simple_condwait(void)
{
   unsigned long long start;
   pthread_mutex_t mutex;
   pthread_cond_t cond;
   struct cond_mutex cm = {
       .mutex = &mutex,
       .cond = &cond,
   };
   pthread_t cond_signaler_tid;
 
   smokey_trace("%s", __func__);
 
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("cond_init", cond_init(&cond, 0), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
   check("thread_spawn",
         thread_spawn(&cond_signaler_tid, 2, cond_signaler, &cm), 0);
   thread_msleep(11);
 
   start = timer_get_tsc();
   check("cond_wait", cond_wait(&cond, &mutex, 0), 0);
   check_sleep("cond_wait", start);
   thread_msleep(10);
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("thread_join", thread_join(cond_signaler_tid), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void relative_condwait(void)
{
   unsigned long long start;
   pthread_mutex_t mutex;
   pthread_cond_t cond;
 
   smokey_trace("%s", __func__);
 
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("cond_init", cond_init(&cond, 0), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
 
   start = timer_get_tsc();
   check("cond_wait",
         cond_wait(&cond, &mutex, 10 * NS_PER_MS), -ETIMEDOUT);
   check_sleep("cond_wait", start);
   thread_msleep(10);
 
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void autoinit_absolute_condwait(void)
{
   pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
   unsigned long long start;
   pthread_mutex_t mutex;
 
   smokey_trace("%s", __func__);
 
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
 
   start = timer_get_tsc();
   check("cond_wait",
         cond_wait_until(&cond, &mutex, timer_read() + 10 * NS_PER_MS),
         -ETIMEDOUT);
   check_sleep("cond_wait", start);
 
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void absolute_condwait(void)
{
   unsigned long long start;
   pthread_mutex_t mutex;
   pthread_cond_t cond;
 
   smokey_trace("%s", __func__);
 
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("cond_init", cond_init(&cond, 1), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
 
   start = timer_get_tsc();
   check("cond_wait",
         cond_wait_until(&cond, &mutex, timer_read() + 10 * NS_PER_MS),
         -ETIMEDOUT);
   check_sleep("cond_wait", start);
 
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void *cond_killer(void *cookie)
{
   unsigned long long start;
   struct cond_mutex *cm = cookie;
 
   start = timer_get_tsc();
   check("mutex_lock", mutex_lock(cm->mutex), 0);
   check_sleep("mutex_lock", start);
   thread_msleep(10);
   check("thread_kill", thread_kill(cm->tid, SIGRTMIN), 0);
   check("mutex_unlock", mutex_unlock(cm->mutex), 0);
 
   return NULL;
}
 
static volatile int sig_seen;
 
static void sighandler(int sig)
{
   ++sig_seen;
}
 
static void sig_norestart_condwait(void)
{
   unsigned long long start;
   pthread_mutex_t mutex;
   pthread_cond_t cond;
   struct cond_mutex cm = {
       .mutex = &mutex,
       .cond = &cond,
       .tid = thread_self(),
   };
   pthread_t cond_killer_tid;
   struct sigaction sa = {
       .sa_handler = sighandler,
       .sa_flags = 0,
   };
   sigemptyset(&sa.sa_mask);
 
   smokey_trace("%s", __func__);
 
   check_unix("sigaction", sigaction(SIGRTMIN, &sa, NULL), 0);
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("cond_init", cond_init(&cond, 0), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
   check("thread_spawn",
         thread_spawn(&cond_killer_tid, 2, cond_killer, &cm), 0);
   thread_msleep(11);
 
   start = timer_get_tsc();
   sig_seen = 0;
   check("cond_wait", cond_wait(&cond, &mutex, 0), 0);
   check_sleep("cond_wait", start);
   check("sig_seen", sig_seen, 1);
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("thread_join", thread_join(cond_killer_tid), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void sig_restart_condwait(void)
{
   unsigned long long start;
   pthread_mutex_t mutex;
   pthread_cond_t cond;
   struct cond_mutex cm = {
       .mutex = &mutex,
       .cond = &cond,
       .tid = thread_self(),
   };
   pthread_t cond_killer_tid;
   struct sigaction sa = {
       .sa_handler = sighandler,
       .sa_flags = 0,
   };
   sigemptyset(&sa.sa_mask);
 
   smokey_trace("%s", __func__);
 
   check_unix("sigaction", sigaction(SIGRTMIN, &sa, NULL), 0);
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("cond_init", cond_init(&cond, 0), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
   check("thread_spawn",
         thread_spawn(&cond_killer_tid, 2, cond_killer, &cm), 0);
   thread_msleep(11);
 
   start = timer_get_tsc();
   sig_seen = 0;
   check("cond_wait", cond_wait(&cond, &mutex, 0), 0);
   check_sleep("cond_wait", start);
   check("sig_seen", sig_seen, 1);
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("thread_join", thread_join(cond_killer_tid), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void *mutex_killer(void *cookie)
{
   unsigned long long start;
   struct cond_mutex *cm = cookie;
 
   start = timer_get_tsc();
   check("mutex_lock", mutex_lock(cm->mutex), 0);
   check_sleep("mutex_lock", start);
   check("cond_signal", cond_signal(cm->cond), 0);
   thread_msleep(10);
   check("thread_kill", thread_kill(cm->tid, SIGRTMIN), 0);
   check("mutex_unlock", mutex_unlock(cm->mutex), 0);
 
   return NULL;
}
 
static void sig_norestart_condwait_mutex(void)
{
   unsigned long long start;
   pthread_mutex_t mutex;
   pthread_cond_t cond;
   struct cond_mutex cm = {
       .mutex = &mutex,
       .cond = &cond,
       .tid = thread_self(),
   };
   pthread_t mutex_killer_tid;
   struct sigaction sa = {
       .sa_handler = sighandler,
       .sa_flags = 0,
   };
   sigemptyset(&sa.sa_mask);
 
   smokey_trace("%s", __func__);
 
   check_unix("sigaction", sigaction(SIGRTMIN, &sa, NULL), 0);
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("cond_init", cond_init(&cond, 0), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
   check("thread_spawn",
         thread_spawn(&mutex_killer_tid, 2, mutex_killer, &cm), 0);
   thread_msleep(11);
 
   sig_seen = 0;
   start = timer_get_tsc();
   check("cond_wait", cond_wait(&cond, &mutex, 0), 0);
   check_sleep("cond_wait", start);
   check("sig_seen", sig_seen, 1);
   thread_msleep(10);
 
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("thread_join", thread_join(mutex_killer_tid), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void sig_restart_condwait_mutex(void)
{
   unsigned long long start;
   pthread_mutex_t mutex;
   pthread_cond_t cond;
   struct cond_mutex cm = {
       .mutex = &mutex,
       .cond = &cond,
       .tid = thread_self(),
   };
   pthread_t mutex_killer_tid;
   struct sigaction sa = {
       .sa_handler = sighandler,
       .sa_flags = SA_RESTART,
   };
   sigemptyset(&sa.sa_mask);
 
   smokey_trace("%s", __func__);
 
   check_unix("sigaction", sigaction(SIGRTMIN, &sa, NULL), 0);
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("cond_init", cond_init(&cond, 0), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
   check("thread_spawn",
         thread_spawn(&mutex_killer_tid, 2, mutex_killer, &cm), 0);
   thread_msleep(11);
 
   sig_seen = 0;
   start = timer_get_tsc();
 
   check("cond_wait", cond_wait(&cond, &mutex, 0), 0);
   check_sleep("cond_wait", start);
   thread_msleep(10);
 
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("thread_join", thread_join(mutex_killer_tid), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void *double_killer(void *cookie)
{
   unsigned long long start;
   struct cond_mutex *cm = cookie;
 
   start = timer_get_tsc();
   check("mutex_lock", mutex_lock(cm->mutex), 0);
   check_sleep("mutex_lock", start);
   check("thread_kill 1", thread_kill(cm->tid, SIGRTMIN), 0);
   thread_msleep(10);
   check("thread_kill 2", thread_kill(cm->tid, SIGRTMIN), 0);
   check("mutex_unlock", mutex_unlock(cm->mutex), 0);
 
   return NULL;
}
 
static void sig_norestart_double(void)
{
   unsigned long long start;
   pthread_mutex_t mutex;
   pthread_cond_t cond;
   struct cond_mutex cm = {
       .mutex = &mutex,
       .cond = &cond,
       .tid = thread_self(),
   };
   pthread_t double_killer_tid;
   struct sigaction sa = {
       .sa_handler = sighandler,
       .sa_flags = 0,
   };
   sigemptyset(&sa.sa_mask);
 
   smokey_trace("%s", __func__);
 
   check_unix("sigaction", sigaction(SIGRTMIN, &sa, NULL), 0);
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("cond_init", cond_init(&cond, 0), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
   check("thread_spawn",
         thread_spawn(&double_killer_tid, 2, double_killer, &cm), 0);
   thread_msleep(11);
 
   sig_seen = 0;
   start = timer_get_tsc();
   check("cond_wait", cond_wait(&cond, &mutex, 0), 0);
   check_sleep("cond_wait", start);
   check("sig_seen", sig_seen, 2);
   thread_msleep(10);
 
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("thread_join", thread_join(double_killer_tid), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void sig_restart_double(void)
{
   unsigned long long start;
   pthread_mutex_t mutex;
   pthread_cond_t cond;
   struct cond_mutex cm = {
       .mutex = &mutex,
       .cond = &cond,
       .tid = thread_self(),
   };
   pthread_t double_killer_tid;
   struct sigaction sa = {
       .sa_handler = sighandler,
       .sa_flags = SA_RESTART,
   };
   sigemptyset(&sa.sa_mask);
 
   smokey_trace("%s", __func__);
 
   check_unix("sigaction", sigaction(SIGRTMIN, &sa, NULL), 0);
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("cond_init", cond_init(&cond, 0), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
   check("thread_spawn",
         thread_spawn(&double_killer_tid, 2, double_killer, &cm), 0);
   thread_msleep(11);
 
   sig_seen = 0;
   start = timer_get_tsc();
 
   check("cond_wait", cond_wait(&cond, &mutex, 0), 0);
   check_sleep("cond_wait", start);
   check("sig_seen", sig_seen, 2);
   thread_msleep(10);
 
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("thread_join", thread_join(double_killer_tid), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void *cond_destroyer(void *cookie)
{
   unsigned long long start;
   struct cond_mutex *cm = cookie;
 
   start = timer_get_tsc();
   check("mutex_lock", mutex_lock(cm->mutex), 0);
   check_sleep("mutex_lock", start);
   thread_msleep(10);
   check("cond_destroy", cond_destroy(cm->cond), -EBUSY);
   check("mutex_unlock", mutex_unlock(cm->mutex), 0);
 
   return NULL;
}
 
static void cond_destroy_whilewait(void)
{
   unsigned long long start;
   pthread_mutex_t mutex;
   pthread_cond_t cond;
   struct cond_mutex cm = {
       .mutex = &mutex,
       .cond = &cond,
       .tid = thread_self(),
   };
   pthread_t cond_destroyer_tid;
   struct sigaction sa = {
       .sa_handler = sighandler,
       .sa_flags = SA_RESTART,
   };
   sigemptyset(&sa.sa_mask);
 
   smokey_trace("%s", __func__);
 
   check_unix("sigaction", sigaction(SIGRTMIN, &sa, NULL), 0);
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_NONE), 0);
   check("cond_init", cond_init(&cond, 0), 0);
   check("mutex_lock", mutex_lock(&mutex), 0);
   check("thread_spawn",
         thread_spawn(&cond_destroyer_tid, 2, cond_destroyer, &cm), 0);
   thread_msleep(11);
 
   start = timer_get_tsc();
 
   check("cond_wait", cond_wait(&cond, &mutex, 10 * NS_PER_MS), -ETIMEDOUT);
   check_sleep("cond_wait", start);
   thread_msleep(10);
 
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("thread_join", thread_join(cond_destroyer_tid), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
static void *cond_delayed_signaler(void *cookie)
{
   struct cond_mutex *cm = cookie;
 
   thread_msleep(10);
 
   check("mutex_lock", mutex_lock(cm->mutex), 0);
   check("cond_signal", cond_signal(cm->cond), 0);
   check("mutex_unlock", mutex_unlock(cm->mutex), 0);
 
   return NULL;
}
 
static void cond_ppmutex(void)
{
   pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
   pthread_mutex_t mutex;
   struct cond_mutex cm = {
       .mutex = &mutex,
       .cond = &cond,
   };
   pthread_t cond_signaler_tid;
 
   smokey_trace("%s", __func__);
 
   check("mutex_init", mutex_init(&mutex, PTHREAD_MUTEX_DEFAULT,
                      PTHREAD_PRIO_PROTECT), 0);
   check("thread_spawn",
         thread_spawn(&cond_signaler_tid, 3, cond_delayed_signaler,
              &cm), 0);
 
   thread_msleep(1);
   check("mutex_lock", mutex_lock(&mutex), 0);
 
   check("cond_wait", cond_wait(&cond, &mutex, 0), 0);
   check("mutex_unlock", mutex_unlock(&mutex), 0);
   check("thread_join", thread_join(cond_signaler_tid), 0);
   check("mutex_destroy", mutex_destroy(&mutex), 0);
   check("cond_destroy", cond_destroy(&cond), 0);
}
 
int run_posix_cond(struct smokey_test *t, int argc, char *const argv[])
{
   struct sched_param sparam;
 
   /* Set scheduling parameters for the current process */
   sparam.sched_priority = 2;
   pthread_setschedparam(pthread_self(), SCHED_FIFO, &sparam);
 
   autoinit_simple_conddestroy();
   autoinit_simple_condwait();
   simple_condwait();
   relative_condwait();
   autoinit_absolute_condwait();
   absolute_condwait();
   sig_norestart_condwait();
   sig_restart_condwait();
   sig_norestart_condwait_mutex();
   sig_restart_condwait_mutex();
   sig_norestart_double();
   sig_restart_double();
   cond_destroy_whilewait();
   cond_ppmutex();
 
   return 0;
}