Branch
Hash :
7b089321
Author :
Date :
2025-01-01T09:24:36
maint: run 'make update-copyright'
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
/* ISO C 11 condition variables for multithreading.
Copyright (C) 2008-2025 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Yoann Vandoorselaere <yoann@prelude-ids.org>, 2008,
and Bruno Haible <bruno@clisp.org>, 2008, 2019. */
#include <config.h>
#include <threads.h>
#include <errno.h>
#include <stdlib.h>
#if defined _WIN32 && ! defined __CYGWIN__
/* Use Windows threads. */
# define WIN32_LEAN_AND_MEAN /* avoid including junk */
# include <windows.h>
# include "windows-mutex.h"
# include "windows-recmutex.h"
# include "windows-timedmutex.h"
# include "windows-timedrecmutex.h"
#else
/* Use POSIX threads. */
# include <pthread.h>
#endif
#if defined _WIN32 && ! defined __CYGWIN__
/* Use Windows threads. */
int
cnd_init (cnd_t *cond)
{
int err = glwthread_cond_init (cond);
return (err == 0 ? thrd_success :
err == ENOMEM ? thrd_nomem :
thrd_error);
}
static int
mutex_lock (mtx_t *mutex)
{
switch (mutex->type)
{
case mtx_plain:
return glwthread_mutex_lock (&mutex->u.u_mutex);
case mtx_plain | mtx_recursive:
return glwthread_recmutex_lock (&mutex->u.u_recmutex);
case mtx_timed:
return glwthread_timedmutex_lock (&mutex->u.u_timedmutex);
case mtx_timed | mtx_recursive:
return glwthread_timedrecmutex_lock (&mutex->u.u_timedrecmutex);
default:
abort ();
}
}
static int
mutex_unlock (mtx_t *mutex)
{
switch (mutex->type)
{
case mtx_plain:
return glwthread_mutex_unlock (&mutex->u.u_mutex);
case mtx_plain | mtx_recursive:
return glwthread_recmutex_unlock (&mutex->u.u_recmutex);
case mtx_timed:
return glwthread_timedmutex_unlock (&mutex->u.u_timedmutex);
case mtx_timed | mtx_recursive:
return glwthread_timedrecmutex_unlock (&mutex->u.u_timedrecmutex);
default:
abort ();
}
}
int
cnd_wait (cnd_t *cond, mtx_t *mutex)
{
int err = glwthread_cond_wait (cond, mutex,
(int (*) (void *)) mutex_lock,
(int (*) (void *)) mutex_unlock);
return (err == 0 ? thrd_success : thrd_error);
}
int
cnd_timedwait (cnd_t *cond, mtx_t *mutex, const struct timespec *abstime)
{
int err = glwthread_cond_timedwait (cond, mutex,
(int (*) (void *)) mutex_lock,
(int (*) (void *)) mutex_unlock,
abstime);
return (err == 0 ? thrd_success :
err == ETIMEDOUT ? thrd_timedout :
thrd_error);
}
int
cnd_signal (cnd_t *cond)
{
int err = glwthread_cond_signal (cond);
return (err == 0 ? thrd_success :
err == ENOMEM ? thrd_nomem :
thrd_error);
}
int
cnd_broadcast (cnd_t *cond)
{
int err = glwthread_cond_broadcast (cond);
return (err == 0 ? thrd_success : thrd_error);
}
void
cnd_destroy (cnd_t *cond)
{
glwthread_cond_destroy (cond);
}
#else
/* Use POSIX threads. */
int
cnd_init (cnd_t *cond)
{
int err = pthread_cond_init (cond, NULL);
return (err == 0 ? thrd_success :
err == ENOMEM ? thrd_nomem :
thrd_error);
}
int
cnd_wait (cnd_t *cond, mtx_t *mutex)
{
int err = pthread_cond_wait (cond, mutex);
return (err == 0 ? thrd_success : thrd_error);
}
int
cnd_timedwait (cnd_t *cond, mtx_t *mutex, const struct timespec *abstime)
{
int err = pthread_cond_timedwait (cond, mutex, abstime);
return (err == 0 ? thrd_success :
err == ETIMEDOUT ? thrd_timedout :
thrd_error);
}
int
cnd_signal (cnd_t *cond)
{
int err = pthread_cond_signal (cond);
return (err == 0 ? thrd_success :
err == ENOMEM ? thrd_nomem :
thrd_error);
}
int
cnd_broadcast (cnd_t *cond)
{
int err = pthread_cond_broadcast (cond);
return (err == 0 ? thrd_success : thrd_error);
}
void
cnd_destroy (cnd_t *cond)
{
pthread_cond_destroy (cond);
}
#endif