1 /* ISO C 11 condition variables for multithreading.
2 Copyright (C) 2008-2020 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Yoann Vandoorselaere <yoann@prelude-ids.org>, 2008,
18 and Bruno Haible <bruno@clisp.org>, 2008, 2019. */
27 #if defined _WIN32 && ! defined __CYGWIN__
28 /* Use Windows threads. */
30 # define WIN32_LEAN_AND_MEAN /* avoid including junk */
33 # include "windows-mutex.h"
34 # include "windows-recmutex.h"
35 # include "windows-timedmutex.h"
36 # include "windows-timedrecmutex.h"
39 /* Use POSIX threads. */
45 #if defined _WIN32 && ! defined __CYGWIN__
46 /* Use Windows threads. */
49 cnd_init (cnd_t
*cond
)
51 int err
= glwthread_cond_init (cond
);
52 return (err
== 0 ? thrd_success
:
53 err
== ENOMEM
? thrd_nomem
:
58 mutex_lock (mtx_t
*mutex
)
63 return glwthread_mutex_lock (&mutex
->u
.u_mutex
);
64 case mtx_plain
| mtx_recursive
:
65 return glwthread_recmutex_lock (&mutex
->u
.u_recmutex
);
67 return glwthread_timedmutex_lock (&mutex
->u
.u_timedmutex
);
68 case mtx_timed
| mtx_recursive
:
69 return glwthread_timedrecmutex_lock (&mutex
->u
.u_timedrecmutex
);
76 mutex_unlock (mtx_t
*mutex
)
81 return glwthread_mutex_unlock (&mutex
->u
.u_mutex
);
82 case mtx_plain
| mtx_recursive
:
83 return glwthread_recmutex_unlock (&mutex
->u
.u_recmutex
);
85 return glwthread_timedmutex_unlock (&mutex
->u
.u_timedmutex
);
86 case mtx_timed
| mtx_recursive
:
87 return glwthread_timedrecmutex_unlock (&mutex
->u
.u_timedrecmutex
);
94 cnd_wait (cnd_t
*cond
, mtx_t
*mutex
)
96 int err
= glwthread_cond_wait (cond
, mutex
,
97 (int (*) (void *)) mutex_lock
,
98 (int (*) (void *)) mutex_unlock
);
99 return (err
== 0 ? thrd_success
: thrd_error
);
103 cnd_timedwait (cnd_t
*cond
, mtx_t
*mutex
, const struct timespec
*abstime
)
105 int err
= glwthread_cond_timedwait (cond
, mutex
,
106 (int (*) (void *)) mutex_lock
,
107 (int (*) (void *)) mutex_unlock
,
109 return (err
== 0 ? thrd_success
:
110 err
== ETIMEDOUT
? thrd_timedout
:
115 cnd_signal (cnd_t
*cond
)
117 int err
= glwthread_cond_signal (cond
);
118 return (err
== 0 ? thrd_success
:
119 err
== ENOMEM
? thrd_nomem
:
124 cnd_broadcast (cnd_t
*cond
)
126 int err
= glwthread_cond_broadcast (cond
);
127 return (err
== 0 ? thrd_success
: thrd_error
);
131 cnd_destroy (cnd_t
*cond
)
133 glwthread_cond_destroy (cond
);
137 /* Use POSIX threads. */
140 cnd_init (cnd_t
*cond
)
142 int err
= pthread_cond_init (cond
, NULL
);
143 return (err
== 0 ? thrd_success
:
144 err
== ENOMEM
? thrd_nomem
:
149 cnd_wait (cnd_t
*cond
, mtx_t
*mutex
)
151 int err
= pthread_cond_wait (cond
, mutex
);
152 return (err
== 0 ? thrd_success
: thrd_error
);
156 cnd_timedwait (cnd_t
*cond
, mtx_t
*mutex
, const struct timespec
*abstime
)
158 int err
= pthread_cond_timedwait (cond
, mutex
, abstime
);
159 return (err
== 0 ? thrd_success
:
160 err
== ETIMEDOUT
? thrd_timedout
:
165 cnd_signal (cnd_t
*cond
)
167 int err
= pthread_cond_signal (cond
);
168 return (err
== 0 ? thrd_success
:
169 err
== ENOMEM
? thrd_nomem
:
174 cnd_broadcast (cnd_t
*cond
)
176 int err
= pthread_cond_broadcast (cond
);
177 return (err
== 0 ? thrd_success
: thrd_error
);
181 cnd_destroy (cnd_t
*cond
)
183 pthread_cond_destroy (cond
);