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