xref: /freebsd/lib/libstdthreads/cnd.c (revision fc6f0665bae406f564e6eb6e1deea849d368a708)
1*fc6f0665SEd Schouten /*-
2*fc6f0665SEd Schouten  * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
3*fc6f0665SEd Schouten  * All rights reserved.
4*fc6f0665SEd Schouten  *
5*fc6f0665SEd Schouten  * Redistribution and use in source and binary forms, with or without
6*fc6f0665SEd Schouten  * modification, are permitted provided that the following conditions
7*fc6f0665SEd Schouten  * are met:
8*fc6f0665SEd Schouten  * 1. Redistributions of source code must retain the above copyright
9*fc6f0665SEd Schouten  *    notice, this list of conditions and the following disclaimer.
10*fc6f0665SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
11*fc6f0665SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
12*fc6f0665SEd Schouten  *    documentation and/or other materials provided with the distribution.
13*fc6f0665SEd Schouten  *
14*fc6f0665SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*fc6f0665SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*fc6f0665SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*fc6f0665SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*fc6f0665SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*fc6f0665SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*fc6f0665SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*fc6f0665SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*fc6f0665SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*fc6f0665SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*fc6f0665SEd Schouten  * SUCH DAMAGE.
25*fc6f0665SEd Schouten  *
26*fc6f0665SEd Schouten  * $FreeBSD$
27*fc6f0665SEd Schouten  */
28*fc6f0665SEd Schouten 
29*fc6f0665SEd Schouten #include <sys/cdefs.h>
30*fc6f0665SEd Schouten __FBSDID("$FreeBSD$");
31*fc6f0665SEd Schouten 
32*fc6f0665SEd Schouten #include <errno.h>
33*fc6f0665SEd Schouten #include <pthread.h>
34*fc6f0665SEd Schouten 
35*fc6f0665SEd Schouten #include "threads.h"
36*fc6f0665SEd Schouten 
37*fc6f0665SEd Schouten int
38*fc6f0665SEd Schouten cnd_broadcast(cnd_t *cond)
39*fc6f0665SEd Schouten {
40*fc6f0665SEd Schouten 
41*fc6f0665SEd Schouten 	if (pthread_cond_broadcast(cond) != 0)
42*fc6f0665SEd Schouten 		return (thrd_error);
43*fc6f0665SEd Schouten 	return (thrd_success);
44*fc6f0665SEd Schouten }
45*fc6f0665SEd Schouten 
46*fc6f0665SEd Schouten void
47*fc6f0665SEd Schouten cnd_destroy(cnd_t *cond)
48*fc6f0665SEd Schouten {
49*fc6f0665SEd Schouten 
50*fc6f0665SEd Schouten 	(void)pthread_cond_destroy(cond);
51*fc6f0665SEd Schouten }
52*fc6f0665SEd Schouten 
53*fc6f0665SEd Schouten int
54*fc6f0665SEd Schouten cnd_init(cnd_t *cond)
55*fc6f0665SEd Schouten {
56*fc6f0665SEd Schouten 
57*fc6f0665SEd Schouten 	switch (pthread_cond_init(cond, NULL)) {
58*fc6f0665SEd Schouten 	case 0:
59*fc6f0665SEd Schouten 		return (thrd_success);
60*fc6f0665SEd Schouten 	case ENOMEM:
61*fc6f0665SEd Schouten 		return (thrd_nomem);
62*fc6f0665SEd Schouten 	default:
63*fc6f0665SEd Schouten 		return (thrd_error);
64*fc6f0665SEd Schouten 	}
65*fc6f0665SEd Schouten }
66*fc6f0665SEd Schouten 
67*fc6f0665SEd Schouten int
68*fc6f0665SEd Schouten cnd_signal(cnd_t *cond)
69*fc6f0665SEd Schouten {
70*fc6f0665SEd Schouten 
71*fc6f0665SEd Schouten 	if (pthread_cond_signal(cond) != 0)
72*fc6f0665SEd Schouten 		return (thrd_error);
73*fc6f0665SEd Schouten 	return (thrd_success);
74*fc6f0665SEd Schouten }
75*fc6f0665SEd Schouten 
76*fc6f0665SEd Schouten int
77*fc6f0665SEd Schouten cnd_timedwait(cnd_t *restrict cond, mtx_t *restrict mtx,
78*fc6f0665SEd Schouten     const struct timespec *restrict ts)
79*fc6f0665SEd Schouten {
80*fc6f0665SEd Schouten 
81*fc6f0665SEd Schouten 	switch (pthread_cond_timedwait(cond, mtx, ts)) {
82*fc6f0665SEd Schouten 	case 0:
83*fc6f0665SEd Schouten 		return (thrd_success);
84*fc6f0665SEd Schouten 	case ETIMEDOUT:
85*fc6f0665SEd Schouten 		return (thrd_timedout);
86*fc6f0665SEd Schouten 	default:
87*fc6f0665SEd Schouten 		return (thrd_error);
88*fc6f0665SEd Schouten 	}
89*fc6f0665SEd Schouten }
90*fc6f0665SEd Schouten 
91*fc6f0665SEd Schouten int
92*fc6f0665SEd Schouten cnd_wait(cnd_t *cond, mtx_t *mtx)
93*fc6f0665SEd Schouten {
94*fc6f0665SEd Schouten 
95*fc6f0665SEd Schouten 	if (pthread_cond_wait(cond, mtx) != 0)
96*fc6f0665SEd Schouten 		return (thrd_error);
97*fc6f0665SEd Schouten 	return (thrd_success);
98*fc6f0665SEd Schouten }
99