summaryrefslogtreecommitdiff
path: root/src/pthreads.c
blob: add9ce8122ca3e72c97b4f6934b5893c6c10d9e8 (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
/*
 * Copyright (C) 1997-2013, Michael Jennings <mej@eterm.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <libast_internal.h>
#include <pthread.h>

/* *INDENT-OFF* */
static SPIF_CONST_TYPE(threadclass) pt_class = {
    {
        SPIF_DECL_CLASSNAME(pthreads),
        (spif_func_t) spif_pthreads_new,
        (spif_func_t) spif_pthreads_init,
        (spif_func_t) spif_pthreads_done,
        (spif_func_t) spif_pthreads_del,
        (spif_func_t) spif_pthreads_show,
        (spif_func_t) spif_pthreads_comp,
        (spif_func_t) spif_pthreads_dup,
        (spif_func_t) spif_pthreads_type
    },
    (spif_func_t) spif_pthreads_new_with_func,
    (spif_func_t) spif_pthreads_init_with_func,
    (spif_func_t) spif_pthreads_detach,
    (spif_func_t) spif_pthreads_get_condition,
    (spif_func_t) spif_pthreads_get_mutex,
    (spif_func_t) spif_pthreads_kill,
    (spif_func_t) spif_pthreads_run,
    (spif_func_t) spif_pthreads_tls_calloc,
    (spif_func_t) spif_pthreads_tls_free,
    (spif_func_t) spif_pthreads_tls_get,
    (spif_func_t) spif_pthreads_tls_malloc,
    (spif_func_t) spif_pthreads_tls_realloc,
    (spif_func_t) spif_pthreads_wait,
    (spif_func_t) spif_pthreads_wait_for
};
SPIF_TYPE(class) SPIF_CLASS_VAR(pthreads) = (spif_class_t) &pt_class;
SPIF_TYPE(threadclass) SPIF_THREADCLASS_VAR(pthreads) = &pt_class;

static SPIF_CONST_TYPE(mutexclass) ptm_class = {
    {
        SPIF_DECL_CLASSNAME(pthreads_mutex),
        (spif_func_t) spif_pthreads_mutex_new,
        (spif_func_t) spif_pthreads_mutex_init,
        (spif_func_t) spif_pthreads_mutex_done,
        (spif_func_t) spif_pthreads_mutex_del,
        (spif_func_t) spif_pthreads_mutex_show,
        (spif_func_t) spif_pthreads_mutex_comp,
        (spif_func_t) spif_pthreads_mutex_dup,
        (spif_func_t) spif_pthreads_mutex_type
    },
    (spif_func_t) spif_pthreads_mutex_lock,
    (spif_func_t) spif_pthreads_mutex_lock_nowait,
    (spif_func_t) spif_pthreads_mutex_unlock
};
SPIF_TYPE(class) SPIF_CLASS_VAR(pthreads_mutex) = (spif_class_t) &ptm_class;
SPIF_TYPE(mutexclass) SPIF_MUTEXCLASS_VAR(pthreads_mutex) = &ptm_class;

static SPIF_CONST_TYPE(conditionclass) ptc_class = {
    {
        SPIF_DECL_CLASSNAME(pthreads_condition),
        (spif_func_t) spif_pthreads_condition_new,
        (spif_func_t) spif_pthreads_condition_init,
        (spif_func_t) spif_pthreads_condition_done,
        (spif_func_t) spif_pthreads_condition_del,
        (spif_func_t) spif_pthreads_condition_show,
        (spif_func_t) spif_pthreads_condition_comp,
        (spif_func_t) spif_pthreads_condition_dup,
        (spif_func_t) spif_pthreads_condition_type
    },
    (spif_func_t) spif_pthreads_condition_broadcast,
    (spif_func_t) spif_pthreads_condition_signal,
    (spif_func_t) spif_pthreads_condition_wait,
    (spif_func_t) spif_pthreads_condition_wait_timed
};
SPIF_TYPE(class) SPIF_CLASS_VAR(pthreads_condition) = (spif_class_t) &ptc_class;
SPIF_TYPE(conditionclass) SPIF_CONDITIONCLASS_VAR(pthreads_condition) = &ptc_class;
/* *INDENT-ON* */

static void spif_pthreads_tls_destructor(void *ptr);

spif_pthreads_t
spif_pthreads_new(void)
{
    spif_pthreads_t self;

    self = SPIF_ALLOC(pthreads);
    if (!spif_pthreads_init(self)) {
        SPIF_DEALLOC(self);
        self = (spif_pthreads_t) NULL;
    }
    return self;
}

spif_pthreads_t
spif_pthreads_new_with_func(spif_thread_func_t func, spif_thread_data_t data)
{
    spif_pthreads_t self;

    self = SPIF_ALLOC(pthreads);
    if (!spif_pthreads_init_with_func(self, func, data)) {
        SPIF_DEALLOC(self);
        self = (spif_pthreads_t) NULL;
    }
    return self;
}

spif_bool_t
spif_pthreads_init(spif_pthreads_t self)
{
    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_THREADCLASS_VAR(pthreads)));
    self->handle = (pthread_t) 0;
    self->creator = (pthread_t) 0;
    pthread_attr_init(&self->attr);
    self->main_func = (spif_thread_func_t) NULL;
    self->data = (spif_thread_data_t) NULL;
    self->tls_keys = (spif_list_t) NULL;
    return TRUE;
}

spif_bool_t
spif_pthreads_init_with_func(spif_pthreads_t self, spif_thread_func_t func, spif_thread_data_t data)
{
    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_THREADCLASS_VAR(pthreads)));
    self->handle = (pthread_t) 0;
    self->creator = (pthread_t) 0;
    pthread_attr_init(&self->attr);
    self->main_func = func;
    self->data = data;
    self->tls_keys = (spif_list_t) NULL;
    return TRUE;
}

spif_bool_t
spif_pthreads_done(spif_pthreads_t self)
{
    spif_bool_t ret = TRUE;

    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    if (self->handle) {
        spif_pthreads_kill(self, SIGTERM);
        self->handle = (pthread_t) 0;
    }
    self->creator = pthread_self();
    pthread_attr_destroy(&self->attr);
    pthread_attr_init(&self->attr);
    self->main_func = (spif_thread_func_t) NULL;
    if (self->tls_keys) {
        SPIF_LIST_DEL(self->tls_keys);
        self->tls_keys = (spif_list_t) NULL;
    }
    return ret;
}

spif_bool_t
spif_pthreads_del(spif_pthreads_t self)
{
    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    spif_pthreads_done(self);
    SPIF_DEALLOC(self);
    return TRUE;
}

spif_str_t
spif_pthreads_show(spif_pthreads_t self, spif_charptr_t name, spif_str_t buff, size_t indent)
{
    spif_char_t tmp[4096];

    if (SPIF_PTHREADS_ISNULL(self)) {
        SPIF_OBJ_SHOW_NULL(pthreads, name, buff, indent, tmp);
        return buff;
    }

    memset(tmp, ' ', indent);
    snprintf((char *) tmp + indent, sizeof(tmp) - indent,
             "(spif_pthreads_t) %s:  %10p { \"",
             name, (spif_ptr_t) self);
    if (SPIF_STR_ISNULL(buff)) {
        buff = spif_str_new_from_ptr(tmp);
    } else {
        spif_str_append_from_ptr(buff, tmp);
    }

    indent += 2;
    if (indent < sizeof(tmp)) {
        memset(tmp, ' ', indent);
    }
    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(pthread_t) handle:  %ld\n", (long) self->handle);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(pthread_t) creator:  %ld\n", (long) self->creator);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(pthread_attr_t) attr:  %10p {...}\n", &self->attr);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_thread_func_t) main_func:  %10p\n", self->main_func);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(spif_thread_data_t) data:  %10p\n", self->data);
    spif_str_append_from_ptr(buff, tmp);

    if (SPIF_LIST_ISNULL(self->tls_keys)) {
        SPIF_OBJ_SHOW_NULL(list, "tls_keys", buff, indent, tmp);
    } else {
        buff = SPIF_LIST_SHOW(self->tls_keys, buff, indent);
    }

    snprintf((char *) tmp, sizeof(tmp), "}\n");
    spif_str_append_from_ptr(buff, tmp);
    return buff;
}

spif_cmp_t
spif_pthreads_comp(spif_pthreads_t self, spif_pthreads_t other)
{
    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    if (pthread_equal(self->handle, other->handle)) {
        return SPIF_CMP_EQUAL;
    }
    return SPIF_OBJ_COMP(self, other);
}

spif_pthreads_t
spif_pthreads_dup(spif_pthreads_t self)
{
    spif_pthreads_t tmp;

    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), (spif_pthreads_t) NULL);
    tmp = SPIF_ALLOC(pthreads);
    memcpy(tmp, self, SPIF_SIZEOF_TYPE(pthreads));
    tmp->tls_keys = SPIF_LIST_DUP(self->tls_keys);
    return tmp;
}

spif_classname_t
spif_pthreads_type(spif_pthreads_t self)
{
    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), (spif_classname_t) SPIF_NULLSTR_TYPE(classname));
    return SPIF_OBJ_CLASSNAME(self);
}

spif_bool_t
spif_pthreads_detach(spif_pthreads_t self)
{
    int ret;

    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    REQUIRE_RVAL(self->handle, FALSE);

    ret = pthread_detach(self->handle);
    if (!ret) {
        return TRUE;
    } else if ((errno == EINVAL) || (ret == EINVAL)) {
        return TRUE;
    } else {
        return FALSE;
    }
}

spif_condition_t
spif_pthreads_get_condition(spif_pthreads_t self)
{
    spif_pthreads_condition_t cond;

    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), (spif_condition_t) NULL);
    cond = spif_pthreads_condition_new();
    spif_pthreads_mutex_set_creator(SPIF_PTHREADS_MUTEX(cond), SPIF_THREAD(self));
    return (spif_condition_t) cond;
}

spif_mutex_t
spif_pthreads_get_mutex(spif_pthreads_t self)
{
    spif_pthreads_mutex_t mutex;

    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), (spif_mutex_t) NULL);
    mutex = spif_pthreads_mutex_new();
    spif_pthreads_mutex_set_creator(mutex, SPIF_THREAD(self));
    return (spif_mutex_t) mutex;
}

spif_bool_t
spif_pthreads_kill(spif_pthreads_t self, int sig)
{
    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    REQUIRE_RVAL(self->handle, FALSE);

    return ((pthread_kill(self->handle, sig)) ? (FALSE) : (TRUE));
}

spif_bool_t
spif_pthreads_run(spif_pthreads_t self)
{
    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    REQUIRE_RVAL(self->main_func != (spif_thread_func_t) NULL, FALSE);
    REQUIRE_RVAL(self->handle == (pthread_t) 0, FALSE);

    if (!pthread_create(&self->handle, &self->attr, self->main_func, self)) {
        return TRUE;
    } else {
        return FALSE;
    }
}

spif_tls_handle_t
spif_pthreads_tls_calloc(spif_pthreads_t self, size_t count, size_t size)
{
    spif_tls_handle_t handle;
    spif_ptr_t data;

    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), (spif_tls_handle_t) -1);
    REQUIRE_RVAL(count, (spif_tls_handle_t) -1);
    REQUIRE_RVAL(size, (spif_tls_handle_t) -1);

    handle = spif_pthreads_tls_malloc(self, count * size);
    data = spif_pthreads_tls_get(self, handle);
    memset(data, 0, count * size);
    return handle;
}

spif_bool_t
spif_pthreads_tls_free(spif_pthreads_t self, spif_tls_handle_t handle)
{
    spif_mbuff_t key;

    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_LIST_ISNULL(self->tls_keys), FALSE);

    key = (spif_mbuff_t) SPIF_LIST_REMOVE_AT(self->tls_keys, handle);
    if (handle != SPIF_LIST_COUNT(self->tls_keys)) {
        /* If this isn't the last item in our list, insert an empty placeholder
           so that numeric indexes which have already been given out will remain valid. */
        SPIF_LIST_INSERT_AT(self->tls_keys, (spif_obj_t) NULL, handle);
    }
    if (!SPIF_MBUFF_ISNULL(key)) {
        spif_ptr_t ptr;

        ptr = (spif_ptr_t) pthread_getspecific(*((pthread_key_t *) SPIF_MBUFF_BUFF(key)));
        if (ptr && !pthread_key_delete(*((pthread_key_t *) SPIF_MBUFF_BUFF(key)))) {
            FREE(ptr);
            return TRUE;
        }
    }
    return FALSE;
}

spif_ptr_t
spif_pthreads_tls_get(spif_pthreads_t self, spif_tls_handle_t handle)
{
    spif_mbuff_t key;

    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), (spif_ptr_t) NULL);
    REQUIRE_RVAL(!SPIF_LIST_ISNULL(self->tls_keys), FALSE);

    key = (spif_mbuff_t) SPIF_LIST_GET(self->tls_keys, handle);
    if (!SPIF_MBUFF_ISNULL(key)) {
        return ((spif_ptr_t) pthread_getspecific(*((pthread_key_t *) SPIF_MBUFF_BUFF(key))));
    }
    return (spif_ptr_t) NULL;
}

spif_tls_handle_t
spif_pthreads_tls_malloc(spif_pthreads_t self, spif_memidx_t size)
{
    pthread_key_t key;
    spif_ptr_t ptr;

    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), (spif_tls_handle_t) -1);

    if (SPIF_LIST_ISNULL(self->tls_keys)) {
        self->tls_keys = SPIF_LIST_NEW(array);
    }
    ptr = MALLOC(size);

    if (!pthread_key_create(&key, spif_pthreads_tls_destructor)) {
        spif_mbuff_t tls_key;

        /* Store the pointer. */
        pthread_setspecific(key, ptr);

        /* The key is actually copied into the mbuff buffer. */
        tls_key = spif_mbuff_new_from_ptr((spif_byteptr_t) &key, sizeof(pthread_key_t));

        /* Since the pointer returned by MALLOC() is only stored locally in
           this function and as keyed in the TLS data store, it cannot be
           retrieved by any other thread.  Thus, thread-local storage.  Yay! */
        if (SPIF_LIST_APPEND(self->tls_keys, tls_key)) {
            return (SPIF_LIST_COUNT(self->tls_keys) - 1);
        }
    }
    return ((spif_tls_handle_t) -1);
}

static void
spif_pthreads_tls_destructor(void *ptr)
{
    FREE(ptr);
}

spif_bool_t
spif_pthreads_tls_realloc(spif_pthreads_t self, spif_tls_handle_t handle, spif_memidx_t size)
{
    spif_mbuff_t key;

    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    REQUIRE_RVAL(!SPIF_LIST_ISNULL(self->tls_keys), FALSE);

    key = (spif_mbuff_t) SPIF_LIST_GET(self->tls_keys, handle);
    if (!SPIF_MBUFF_ISNULL(key)) {
        spif_ptr_t ptr;

        ptr = (spif_ptr_t) pthread_getspecific(*((pthread_key_t *) SPIF_MBUFF_BUFF(key)));
        if (ptr) {
            ptr = REALLOC(ptr, size);
            if (ptr) {
                pthread_setspecific(*((pthread_key_t *) SPIF_MBUFF_BUFF(key)), ptr);
                return TRUE;
            }
        }
    }
    return FALSE;
}

spif_bool_t
spif_pthreads_wait(spif_pthreads_t self, spif_condition_t cond)
{
    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    ASSERT_RVAL(!SPIF_PTHREADS_CONDITION_ISNULL(SPIF_PTHREADS_CONDITION(cond)), FALSE);
}

spif_bool_t
spif_pthreads_wait_for(spif_pthreads_t self, spif_pthreads_t other)
{
    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(self), FALSE);
    ASSERT_RVAL(!SPIF_PTHREADS_ISNULL(other), FALSE);
}

SPIF_DEFINE_PROPERTY_FUNC_C(pthreads, pthread_t, handle);
SPIF_DEFINE_PROPERTY_FUNC_C(pthreads, pthread_t, creator);
SPIF_DEFINE_PROPERTY_FUNC_C(pthreads, pthread_attr_t, attr);
SPIF_DEFINE_PROPERTY_FUNC(pthreads, thread_func, main_func);
SPIF_DEFINE_PROPERTY_FUNC(pthreads, thread_data, data);
SPIF_DEFINE_PROPERTY_FUNC(pthreads, list, tls_keys);

spif_pthreads_mutex_t
spif_pthreads_mutex_new(void)
{
    spif_pthreads_mutex_t self;

    self = SPIF_ALLOC(pthreads_mutex);
    if (!spif_pthreads_mutex_init(self)) {
        SPIF_DEALLOC(self);
        self = (spif_pthreads_mutex_t) NULL;
    }
    return self;
}

spif_bool_t
spif_pthreads_mutex_init(spif_pthreads_mutex_t self)
{
    ASSERT_RVAL(!SPIF_PTHREADS_MUTEX_ISNULL(self), FALSE);
    /* ***NOT NEEDED*** spif_obj_init(SPIF_OBJ(self)); */
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_MUTEXCLASS_VAR(pthreads_mutex)));
    self->creator = (spif_thread_t) NULL;
    pthread_mutex_init(&self->mutex, NULL);
    return TRUE;
}

spif_bool_t
spif_pthreads_mutex_done(spif_pthreads_mutex_t self)
{
    spif_bool_t ret = TRUE;

    ASSERT_RVAL(!SPIF_PTHREADS_MUTEX_ISNULL(self), FALSE);
    self->creator = (spif_thread_t) NULL;
    if (!pthread_mutex_unlock(&self->mutex)) {
        /* It was locked.  Destroy it. */
        pthread_mutex_destroy(&self->mutex);
    }
    pthread_mutex_init(&self->mutex, NULL);
    return ret;
}

spif_bool_t
spif_pthreads_mutex_del(spif_pthreads_mutex_t self)
{
    ASSERT_RVAL(!SPIF_PTHREADS_MUTEX_ISNULL(self), FALSE);
    spif_pthreads_mutex_done(self);
    SPIF_DEALLOC(self);
    return TRUE;
}

spif_str_t
spif_pthreads_mutex_show(spif_pthreads_mutex_t self, spif_charptr_t name, spif_str_t buff, size_t indent)
{
    spif_char_t tmp[4096];

    if (SPIF_PTHREADS_MUTEX_ISNULL(self)) {
        SPIF_OBJ_SHOW_NULL(pthreads_mutex, name, buff, indent, tmp);
        return buff;
    }

    memset(tmp, ' ', indent);
    snprintf((char *) tmp + indent, sizeof(tmp) - indent,
             "(spif_pthreads_mutex_t) %s:  %10p { \"",
             name, (spif_ptr_t) self);
    if (SPIF_STR_ISNULL(buff)) {
        buff = spif_str_new_from_ptr(tmp);
    } else {
        spif_str_append_from_ptr(buff, tmp);
    }

    indent += 2;
    if (indent < sizeof(tmp)) {
        memset(tmp, ' ', indent);
    }
    if (SPIF_THREAD_ISNULL(self->creator)) {
        SPIF_OBJ_SHOW_NULL(thread, "creator", buff, indent, tmp);
    } else {
        buff = SPIF_THREAD_SHOW(self->creator, buff, indent);
    }

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(pthread_mutex_t) mutex:  %10p {...}\n", &self->mutex);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp, sizeof(tmp), "}\n");
    spif_str_append_from_ptr(buff, tmp);
    return buff;
}

spif_cmp_t
spif_pthreads_mutex_comp(spif_pthreads_mutex_t self, spif_pthreads_mutex_t other)
{
    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    return SPIF_OBJ_COMP(self, other);
}

spif_pthreads_mutex_t
spif_pthreads_mutex_dup(spif_pthreads_mutex_t self)
{
    spif_pthreads_mutex_t tmp;

    ASSERT_RVAL(!SPIF_PTHREADS_MUTEX_ISNULL(self), (spif_pthreads_mutex_t) NULL);
    tmp = SPIF_ALLOC(pthreads_mutex);
    memcpy(tmp, self, SPIF_SIZEOF_TYPE(pthreads_mutex));
    return tmp;
}

spif_classname_t
spif_pthreads_mutex_type(spif_pthreads_mutex_t self)
{
    ASSERT_RVAL(!SPIF_PTHREADS_MUTEX_ISNULL(self), (spif_classname_t) SPIF_NULLSTR_TYPE(classname));
    return SPIF_OBJ_CLASSNAME(self);
}

spif_bool_t
spif_pthreads_mutex_lock(spif_pthreads_mutex_t self)
{

}

spif_bool_t
spif_pthreads_mutex_lock_nowait(spif_pthreads_mutex_t self)
{

}

spif_bool_t
spif_pthreads_mutex_unlock(spif_pthreads_mutex_t self)
{

}

SPIF_DEFINE_PROPERTY_FUNC(pthreads_mutex, thread, creator);
SPIF_DEFINE_PROPERTY_FUNC_C(pthreads_mutex, pthread_mutex_t, mutex);

spif_pthreads_condition_t
spif_pthreads_condition_new(void)
{
    spif_pthreads_condition_t self;

    self = SPIF_ALLOC(pthreads_condition);
    if (!spif_pthreads_condition_init(self)) {
        SPIF_DEALLOC(self);
        self = (spif_pthreads_condition_t) NULL;
    }
    return self;
}

spif_bool_t
spif_pthreads_condition_init(spif_pthreads_condition_t self)
{
    ASSERT_RVAL(!SPIF_PTHREADS_CONDITION_ISNULL(self), FALSE);
    spif_pthreads_mutex_init(SPIF_PTHREADS_MUTEX(self));
    spif_obj_set_class(SPIF_OBJ(self), SPIF_CLASS(SPIF_CONDITIONCLASS_VAR(pthreads_condition)));
    pthread_cond_init(&self->cond, NULL);
    return TRUE;
}

spif_bool_t
spif_pthreads_condition_done(spif_pthreads_condition_t self)
{
    spif_bool_t ret = TRUE;

    ASSERT_RVAL(!SPIF_PTHREADS_CONDITION_ISNULL(self), FALSE);
    ret = spif_pthreads_mutex_done(SPIF_PTHREADS_MUTEX(self));
    pthread_cond_destroy(&self->cond);
    pthread_cond_init(&self->cond, NULL);
    return ret;
}

spif_bool_t
spif_pthreads_condition_del(spif_pthreads_condition_t self)
{
    ASSERT_RVAL(!SPIF_PTHREADS_CONDITION_ISNULL(self), FALSE);
    spif_pthreads_condition_done(self);
    SPIF_DEALLOC(self);
    return TRUE;
}

spif_str_t
spif_pthreads_condition_show(spif_pthreads_condition_t self, spif_charptr_t name, spif_str_t buff, size_t indent)
{
    spif_char_t tmp[4096];

    if (SPIF_PTHREADS_CONDITION_ISNULL(self)) {
        SPIF_OBJ_SHOW_NULL(pthreads_condition, name, buff, indent, tmp);
        return buff;
    }

    memset(tmp, ' ', indent);
    snprintf((char *) tmp + indent, sizeof(tmp) - indent,
             "(spif_pthreads_condition_t) %s:  %10p { \"",
             name, (spif_ptr_t) self);
    if (SPIF_STR_ISNULL(buff)) {
        buff = spif_str_new_from_ptr(tmp);
    } else {
        spif_str_append_from_ptr(buff, tmp);
    }

    indent += 2;
    if (indent < sizeof(tmp)) {
        memset(tmp, ' ', indent);
    }
    buff = spif_pthreads_mutex_show(SPIF_PTHREADS_MUTEX(self), "self", buff, indent);

    snprintf((char *) tmp + indent, sizeof(tmp) - indent, "(pthread_cond_t) cond:  %10p {...}\n", &self->cond);
    spif_str_append_from_ptr(buff, tmp);

    snprintf((char *) tmp, sizeof(tmp), "}\n");
    spif_str_append_from_ptr(buff, tmp);
    return buff;
}

spif_cmp_t
spif_pthreads_condition_comp(spif_pthreads_condition_t self, spif_pthreads_condition_t other)
{
    SPIF_OBJ_COMP_CHECK_NULL(self, other);
    return SPIF_OBJ_COMP(self, other);
}

spif_pthreads_condition_t
spif_pthreads_condition_dup(spif_pthreads_condition_t self)
{
    spif_pthreads_condition_t tmp;

    ASSERT_RVAL(!SPIF_PTHREADS_CONDITION_ISNULL(self), (spif_pthreads_condition_t) NULL);
    tmp = SPIF_ALLOC(pthreads_condition);
    memcpy(tmp, self, SPIF_SIZEOF_TYPE(pthreads_condition));
    return tmp;
}

spif_classname_t
spif_pthreads_condition_type(spif_pthreads_condition_t self)
{
    ASSERT_RVAL(!SPIF_PTHREADS_CONDITION_ISNULL(self), (spif_classname_t) SPIF_NULLSTR_TYPE(classname));
    return SPIF_OBJ_CLASSNAME(self);
}

spif_bool_t
spif_pthreads_condition_broadcast(spif_pthreads_condition_t self)
{

}

spif_bool_t
spif_pthreads_condition_signal(spif_pthreads_condition_t self)
{

}

spif_bool_t
spif_pthreads_condition_wait(spif_pthreads_condition_t self)
{

}

spif_bool_t
spif_pthreads_condition_wait_timed(spif_pthreads_condition_t self, spif_int32_t delay)
{

}

SPIF_DEFINE_PROPERTY_FUNC_C(pthreads_condition, pthread_cond_t, cond);