rm_/lib/pthread-cond.c

200 lines
5.0 KiB
C
Raw Normal View History

2020-09-02 16:47:03 +08:00
/* POSIX condition variables.
2022-07-28 14:16:50 +08:00
Copyright (C) 2010-2022 Free Software Foundation, Inc.
2020-09-02 16:47:03 +08:00
2022-07-28 14:16:50 +08:00
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.
2020-09-02 16:47:03 +08:00
2022-07-28 14:16:50 +08:00
This file is distributed in the hope that it will be useful,
2020-09-02 16:47:03 +08:00
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2022-07-28 14:16:50 +08:00
GNU Lesser General Public License for more details.
2020-09-02 16:47:03 +08:00
2022-07-28 14:16:50 +08:00
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/>. */
2020-09-02 16:47:03 +08:00
/* Written by Paul Eggert, 2010, and Bruno Haible <bruno@clisp.org>, 2019. */
#include <config.h>
/* Specification. */
#include <pthread.h>
#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
# include "windows-thread.h"
#else
# include <errno.h>
# include <limits.h>
# include <sys/time.h>
# include <time.h>
#endif
#if ((defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS) || !HAVE_PTHREAD_H
int
pthread_condattr_init (pthread_condattr_t *attr)
{
*attr = 0;
return 0;
}
int
2022-07-28 14:16:50 +08:00
pthread_condattr_destroy (_GL_UNUSED pthread_condattr_t *attr)
2020-09-02 16:47:03 +08:00
{
return 0;
}
#endif
#if (defined _WIN32 && ! defined __CYGWIN__) && USE_WINDOWS_THREADS
/* Use Windows threads. */
int
pthread_cond_init (pthread_cond_t *cond,
2022-07-28 14:16:50 +08:00
_GL_UNUSED const pthread_condattr_t *attr)
2020-09-02 16:47:03 +08:00
{
return glwthread_cond_init (cond);
}
int
pthread_cond_wait (pthread_cond_t *cond, pthread_mutex_t *mutex)
{
return glwthread_cond_wait (cond, mutex,
(int (*) (void *)) pthread_mutex_lock,
(int (*) (void *)) pthread_mutex_unlock);
}
int
pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex,
const struct timespec *abstime)
{
return glwthread_cond_timedwait (cond, mutex,
(int (*) (void *)) pthread_mutex_lock,
(int (*) (void *)) pthread_mutex_unlock,
abstime);
}
int
pthread_cond_signal (pthread_cond_t *cond)
{
return glwthread_cond_signal (cond);
}
int
pthread_cond_broadcast (pthread_cond_t *cond)
{
return glwthread_cond_broadcast (cond);
}
int
pthread_cond_destroy (pthread_cond_t *cond)
{
return glwthread_cond_destroy (cond);
}
#elif HAVE_PTHREAD_H
/* Provide workarounds for POSIX threads. */
#else
/* Provide a dummy implementation for single-threaded applications. */
int
2022-07-28 14:16:50 +08:00
pthread_cond_init (_GL_UNUSED pthread_cond_t *cond,
_GL_UNUSED const pthread_condattr_t *attr)
2020-09-02 16:47:03 +08:00
{
/* COND is never seriously used. */
return 0;
}
int
2022-07-28 14:16:50 +08:00
pthread_cond_wait (_GL_UNUSED pthread_cond_t *cond,
_GL_UNUSED pthread_mutex_t *mutex)
2020-09-02 16:47:03 +08:00
{
/* No other thread can signal this condition variable.
Wait endlessly. */
for (;;)
{
struct timespec duration;
duration.tv_sec = 86400;
2022-07-28 14:16:50 +08:00
duration.tv_nsec = 0;
2020-09-02 16:47:03 +08:00
nanosleep (&duration, NULL);
}
}
int
2022-07-28 14:16:50 +08:00
pthread_cond_timedwait (_GL_UNUSED pthread_cond_t *cond,
_GL_UNUSED pthread_mutex_t *mutex,
2020-09-02 16:47:03 +08:00
const struct timespec *abstime)
{
/* No other thread can signal this condition variable.
Wait until ABSTIME is reached. */
for (;;)
{
struct timeval currtime;
unsigned long remaining;
struct timespec duration;
gettimeofday (&currtime, NULL);
if (currtime.tv_sec > abstime->tv_sec)
remaining = 0;
else
{
unsigned long seconds = abstime->tv_sec - currtime.tv_sec;
remaining = seconds * 1000000000;
if (remaining / 1000000000 != seconds) /* overflow? */
remaining = ULONG_MAX;
else
{
long nanoseconds =
abstime->tv_nsec - currtime.tv_usec * 1000;
if (nanoseconds >= 0)
{
remaining += nanoseconds;
if (remaining < nanoseconds) /* overflow? */
remaining = ULONG_MAX;
}
else
{
if (remaining >= - nanoseconds)
remaining -= (- nanoseconds);
else
remaining = 0;
}
}
}
if (remaining == 0)
return ETIMEDOUT;
/* Sleep up to REMAINING ns. */
duration.tv_sec = remaining / 1000000000;
duration.tv_nsec = remaining % 1000000000;
nanosleep (&duration, NULL);
}
}
int
2022-07-28 14:16:50 +08:00
pthread_cond_signal (_GL_UNUSED pthread_cond_t *cond)
2020-09-02 16:47:03 +08:00
{
/* No threads can currently be blocked on COND. */
return 0;
}
int
2022-07-28 14:16:50 +08:00
pthread_cond_broadcast (_GL_UNUSED pthread_cond_t *cond)
2020-09-02 16:47:03 +08:00
{
/* No threads can currently be blocked on COND. */
return 0;
}
int
2022-07-28 14:16:50 +08:00
pthread_cond_destroy (_GL_UNUSED pthread_cond_t *cond)
2020-09-02 16:47:03 +08:00
{
/* COND is never seriously used. */
return 0;
}
#endif