1228904Sed/*-
2228904Sed * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
3228904Sed * All rights reserved.
4228904Sed *
5228904Sed * Redistribution and use in source and binary forms, with or without
6228904Sed * modification, are permitted provided that the following conditions
7228904Sed * are met:
8228904Sed * 1. Redistributions of source code must retain the above copyright
9228904Sed *    notice, this list of conditions and the following disclaimer.
10228904Sed * 2. Redistributions in binary form must reproduce the above copyright
11228904Sed *    notice, this list of conditions and the following disclaimer in the
12228904Sed *    documentation and/or other materials provided with the distribution.
13228904Sed *
14228904Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15228904Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16228904Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17228904Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18228904Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19228904Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20228904Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21228904Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22228904Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23228904Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24228904Sed * SUCH DAMAGE.
25228904Sed *
26228904Sed * $FreeBSD$
27228904Sed */
28228904Sed
29228904Sed#include <sys/cdefs.h>
30228904Sed__FBSDID("$FreeBSD$");
31228904Sed
32228904Sed#include <errno.h>
33228904Sed#include <pthread.h>
34228904Sed
35228904Sed#include "threads.h"
36228904Sed
37228904Sedint
38228904Sedcnd_broadcast(cnd_t *cond)
39228904Sed{
40228904Sed
41228904Sed	if (pthread_cond_broadcast(cond) != 0)
42228904Sed		return (thrd_error);
43228904Sed	return (thrd_success);
44228904Sed}
45228904Sed
46228904Sedvoid
47228904Sedcnd_destroy(cnd_t *cond)
48228904Sed{
49228904Sed
50228904Sed	(void)pthread_cond_destroy(cond);
51228904Sed}
52228904Sed
53228904Sedint
54228904Sedcnd_init(cnd_t *cond)
55228904Sed{
56228904Sed
57228904Sed	switch (pthread_cond_init(cond, NULL)) {
58228904Sed	case 0:
59228904Sed		return (thrd_success);
60228904Sed	case ENOMEM:
61228904Sed		return (thrd_nomem);
62228904Sed	default:
63228904Sed		return (thrd_error);
64228904Sed	}
65228904Sed}
66228904Sed
67228904Sedint
68228904Sedcnd_signal(cnd_t *cond)
69228904Sed{
70228904Sed
71228904Sed	if (pthread_cond_signal(cond) != 0)
72228904Sed		return (thrd_error);
73228904Sed	return (thrd_success);
74228904Sed}
75228904Sed
76228904Sedint
77228904Sedcnd_timedwait(cnd_t *restrict cond, mtx_t *restrict mtx,
78228904Sed    const struct timespec *restrict ts)
79228904Sed{
80228904Sed
81228904Sed	switch (pthread_cond_timedwait(cond, mtx, ts)) {
82228904Sed	case 0:
83228904Sed		return (thrd_success);
84228904Sed	case ETIMEDOUT:
85228904Sed		return (thrd_timedout);
86228904Sed	default:
87228904Sed		return (thrd_error);
88228904Sed	}
89228904Sed}
90228904Sed
91228904Sedint
92228904Sedcnd_wait(cnd_t *cond, mtx_t *mtx)
93228904Sed{
94228904Sed
95228904Sed	if (pthread_cond_wait(cond, mtx) != 0)
96228904Sed		return (thrd_error);
97228904Sed	return (thrd_success);
98228904Sed}
99