[newlib-cygwin] Cygwin: Add C11 threads API

Corinna Vinschen corinna@sourceware.org
Sun Aug 30 12:30:10 GMT 2020


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=75a669790e6eb0f07f4d33cd71c7e9b565a84ee8

commit 75a669790e6eb0f07f4d33cd71c7e9b565a84ee8
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Sat Aug 29 21:29:24 2020 +0200

    Cygwin: Add C11 threads API
    
    Code taken from FreeBSD, which implements C11 threads as
    wrapper around pthreads.  Fix up machine/_threads.h which
    is called from newlib's machine-independent threads.h to
    match Cygwin's pthreads types.
    
    Add the FreeBSD source files to libc subdir and take
    opportunity to define LIBC_OFILES var in Makefile.
    
    Add new symbols to common.din and sort symbols.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/Makefile.in                |  46 ++++++----
 winsup/cygwin/common.din                 |  61 +++++++++----
 winsup/cygwin/include/machine/_threads.h |  63 +++++++++++++
 winsup/cygwin/libc/call_once.c           |  46 ++++++++++
 winsup/cygwin/libc/cnd.c                 | 100 +++++++++++++++++++++
 winsup/cygwin/libc/mtx.c                 | 125 ++++++++++++++++++++++++++
 winsup/cygwin/libc/thrd.c                | 150 +++++++++++++++++++++++++++++++
 winsup/cygwin/libc/tss.c                 |  73 +++++++++++++++
 8 files changed, 627 insertions(+), 37 deletions(-)

diff --git a/winsup/cygwin/Makefile.in b/winsup/cygwin/Makefile.in
index fac81759e..03e3b0027 100644
--- a/winsup/cygwin/Makefile.in
+++ b/winsup/cygwin/Makefile.in
@@ -151,6 +151,32 @@ DLL_IMPORTS:=${shell $(CC) -print-file-name=w32api/libkernel32.a} ${shell $(CC)
 
 MT_SAFE_OBJECTS:=
 #
+LIBC_OFILES:= \
+	arc4random_stir.o \
+	base64.o \
+	bsdlib.o \
+	call_once.o \
+	cnd.o \
+	fnmatch.o \
+	fts.o \
+	ftw.o \
+	getentropy.o \
+	getopt.o \
+	inet_addr.o \
+	inet_network.o \
+	minires-os-if.o \
+	minires.o \
+	msgcat.o \
+	mtx.o \
+	nftw.o \
+	rcmd.o \
+	rexec.o \
+	strfmon.o \
+	strptime.o \
+	thrd.o \
+	tss.o \
+	xsique.o
+
 MATH_OFILES:= \
 	acoshl.o \
 	acosl.o \
@@ -251,11 +277,8 @@ TZCODE_OFILES:=localtime.o
 DLL_OFILES:= \
 	advapi32.o \
 	aio.o \
-	arc4random_stir.o \
 	assert.o \
 	autoload.o \
-	base64.o \
-	bsdlib.o \
 	clock.o \
 	ctype.o \
 	cxx.o \
@@ -313,21 +336,14 @@ DLL_OFILES:= \
 	fhandler_windows.o \
 	fhandler_zero.o \
 	flock.o \
-	fnmatch.o \
 	fork.o \
 	forkable.o \
-	fts.o \
-	ftw.o \
-	getentropy.o \
-	getopt.o \
 	glob.o \
 	glob_pattern_p.o \
 	globals.o \
 	grp.o \
 	heap.o \
 	hookapi.o \
-	inet_addr.o \
-	inet_network.o \
 	init.o \
 	ioctl.o \
 	ipc.o \
@@ -337,19 +353,15 @@ DLL_OFILES:= \
 	loadavg.o \
 	lsearch.o \
 	malloc_wrapper.o \
-	minires-os-if.o \
-	minires.o \
 	miscfuncs.o \
 	mktemp.o \
 	mmap.o \
 	mmap_alloc.o \
 	msg.o \
-	msgcat.o \
 	mount.o \
 	net.o \
 	netdb.o \
 	nfs.o \
-	nftw.o \
 	nlsfuncs.o \
 	ntea.o \
 	passwd.o \
@@ -368,8 +380,6 @@ DLL_OFILES:= \
 	regfree.o \
 	registry.o \
 	resource.o \
-	rexec.o \
-	rcmd.o \
 	scandir.o \
 	sched.o \
 	sec_acl.o \
@@ -388,9 +398,7 @@ DLL_OFILES:= \
 	smallprint.o \
 	spawn.o \
 	strace.o \
-	strfmon.o \
 	strfuncs.o \
-	strptime.o \
 	strsep.o \
 	strsig.o \
 	sync.o \
@@ -409,9 +417,9 @@ DLL_OFILES:= \
 	wincap.o \
 	window.o \
 	winf.o \
-	xsique.o \
 	$(EXTRA_OFILES) \
 	$(MALLOC_OFILES) \
+	$(LIBC_OFILES) \
 	$(MATH_OFILES) \
 	$(TZCODE_OFILES) \
 	$(MT_SAFE_OBJECTS)
diff --git a/winsup/cygwin/common.din b/winsup/cygwin/common.din
index d781922a3..6e5dde80d 100644
--- a/winsup/cygwin/common.din
+++ b/winsup/cygwin/common.din
@@ -38,7 +38,6 @@ sys_sigabbrev DATA
 sys_siglist DATA
 
 # Exported functions
-_Exit SIGFE
 __assert NOSIGFE
 __assert_func NOSIGFE
 __assertfail NOSIGFE
@@ -126,6 +125,7 @@ __xdrrec_getrec SIGFE
 __xdrrec_setnonblock SIGFE
 __xpg_sigpause SIGFE
 __xpg_strerror_r SIGFE
+_Exit SIGFE
 _exit SIGFE
 _feinitialise NOSIGFE
 _fscanf_r SIGFE
@@ -233,6 +233,7 @@ asinhl NOSIGFE
 asinl NOSIGFE
 asnprintf SIGFE
 asprintf SIGFE
+at_quick_exit SIGFE
 atan NOSIGFE
 atan2 NOSIGFE
 atan2f NOSIGFE
@@ -248,7 +249,6 @@ atoff SIGFE
 atoi NOSIGFE
 atol NOSIGFE
 atoll NOSIGFE
-at_quick_exit SIGFE
 basename NOSIGFE
 bcmp NOSIGFE
 bcopy NOSIGFE
@@ -267,6 +267,7 @@ cacosh NOSIGFE
 cacoshf NOSIGFE
 cacoshl NOSIGFE
 cacosl NOSIGFE
+call_once SIGFE
 calloc SIGFE
 canonicalize_file_name SIGFE
 carg NOSIGFE
@@ -335,6 +336,12 @@ clogl NOSIGFE
 close SIGFE
 closedir SIGFE
 closelog SIGFE
+cnd_broadcast SIGFE
+cnd_destroy SIGFE
+cnd_init SIGFE
+cnd_signal SIGFE
+cnd_timedwait SIGFE
+cnd_wait SIGFE
 confstr NOSIGFE
 conj NOSIGFE
 conjf NOSIGFE
@@ -635,8 +642,6 @@ gammaf NOSIGFE
 gammaf_r NOSIGFE
 gcvt SIGFE
 gcvtf SIGFE
-GetCommandLineA@0 = cygwin_GetCommandLineA@0 NOSIGFE
-GetCommandLineW@0 = cygwin_GetCommandLineW@0 NOSIGFE
 get_avphys_pages SIGFE
 get_current_dir_name SIGFE
 get_nprocs SIGFE
@@ -647,6 +652,8 @@ getc SIGFE
 getc_unlocked SIGFE
 getchar SIGFE
 getchar_unlocked SIGFE
+GetCommandLineA@0 = cygwin_GetCommandLineA@0 NOSIGFE
+GetCommandLineW@0 = cygwin_GetCommandLineW@0 NOSIGFE
 getcontext NOSIGFE
 getcwd SIGFE
 getdelim = __getdelim SIGFE
@@ -891,9 +898,9 @@ logb NOSIGFE
 logbf NOSIGFE
 logbl NOSIGFE
 logf NOSIGFE
-logl NOSIGFE
 login SIGFE
 login_tty SIGFE
+logl NOSIGFE
 logout SIGFE
 logwtmp SIGFE
 longjmp NOSIGFE
@@ -972,6 +979,12 @@ msgget SIGFE
 msgrcv SIGFE
 msgsnd SIGFE
 msync SIGFE
+mtx_destroy SIGFE
+mtx_init SIGFE
+mtx_lock SIGFE
+mtx_timedlock SIGFE
+mtx_trylock SIGFE
+mtx_unlock SIGFE
 munlock SIGFE
 munmap SIGFE
 nan NOSIGFE
@@ -1017,14 +1030,19 @@ posix_madvise SIGFE
 posix_memalign SIGFE
 posix_openpt SIGFE
 posix_spawn SIGFE
+posix_spawn_file_actions_addclose SIGFE
+posix_spawn_file_actions_adddup2 SIGFE
+posix_spawn_file_actions_addopen SIGFE
+posix_spawn_file_actions_destroy SIGFE
+posix_spawn_file_actions_init SIGFE
 posix_spawnattr_destroy SIGFE
-posix_spawnattr_init SIGFE
 posix_spawnattr_getflags NOSIGFE
 posix_spawnattr_getpgroup NOSIGFE
 posix_spawnattr_getschedparam NOSIGFE
 posix_spawnattr_getschedpolicy NOSIGFE
 posix_spawnattr_getsigdefault NOSIGFE
 posix_spawnattr_getsigmask NOSIGFE
+posix_spawnattr_init SIGFE
 posix_spawnattr_setflags NOSIGFE
 posix_spawnattr_setpgroup NOSIGFE
 posix_spawnattr_setschedparam NOSIGFE
@@ -1032,11 +1050,6 @@ posix_spawnattr_setschedpolicy NOSIGFE
 posix_spawnattr_setsigdefault NOSIGFE
 posix_spawnattr_setsigmask NOSIGFE
 posix_spawnp SIGFE
-posix_spawn_file_actions_destroy SIGFE
-posix_spawn_file_actions_init SIGFE
-posix_spawn_file_actions_addclose SIGFE
-posix_spawn_file_actions_adddup2 SIGFE
-posix_spawn_file_actions_addopen SIGFE
 pow NOSIGFE
 pow10 NOSIGFE
 pow10f NOSIGFE
@@ -1070,13 +1083,13 @@ pthread_attr_setscope SIGFE
 pthread_attr_setstack SIGFE
 pthread_attr_setstackaddr SIGFE
 pthread_attr_setstacksize SIGFE
-pthread_barrierattr_init SIGFE
-pthread_barrierattr_setpshared SIGFE
-pthread_barrierattr_getpshared SIGFE
-pthread_barrierattr_destroy SIGFE
-pthread_barrier_init SIGFE
 pthread_barrier_destroy SIGFE
+pthread_barrier_init SIGFE
 pthread_barrier_wait SIGFE
+pthread_barrierattr_destroy SIGFE
+pthread_barrierattr_getpshared SIGFE
+pthread_barrierattr_init SIGFE
+pthread_barrierattr_setpshared SIGFE
 pthread_cancel SIGFE
 pthread_cond_broadcast SIGFE
 pthread_cond_destroy SIGFE
@@ -1176,9 +1189,9 @@ putwc_unlocked SIGFE
 putwchar SIGFE
 putwchar_unlocked SIGFE
 pwrite SIGFE
-quick_exit SIGFE
 qsort NOSIGFE
 qsort_r NOSIGFE
+quick_exit SIGFE
 quotactl SIGFE
 raise SIGFE
 rand NOSIGFE
@@ -1293,7 +1306,6 @@ sendto = cygwin_sendto SIGFE
 setbuf SIGFE
 setbuffer SIGFE
 setcontext NOSIGFE
-sethostname SIGFE
 setdtablesize SIGFE
 setegid SIGFE
 setenv SIGFE
@@ -1302,6 +1314,7 @@ setgid SIGFE
 setgrent NOSIGFE
 setgroups SIGFE
 sethostent NOSIGFE
+sethostname SIGFE
 setitimer SIGFE
 setjmp NOSIGFE
 setlinebuf SIGFE
@@ -1495,6 +1508,14 @@ tfind NOSIGFE
 tgamma NOSIGFE
 tgammaf NOSIGFE
 tgammal NOSIGFE
+thrd_create SIGFE
+thrd_current SIGFE
+thrd_detach SIGFE
+thrd_equal SIGFE
+thrd_exit SIGFE
+thrd_join SIGFE
+thrd_sleep SIGFE
+thrd_yield SIGFE
 time SIGFE
 timegm NOSIGFE
 timelocal SIGFE
@@ -1529,6 +1550,10 @@ truncate SIGFE
 truncf NOSIGFE
 truncl NOSIGFE
 tsearch SIGFE
+tss_create SIGFE
+tss_delete SIGFE
+tss_get SIGFE
+tss_set SIGFE
 ttyname SIGFE
 ttyname_r SIGFE
 ttyslot NOSIGFE
diff --git a/winsup/cygwin/include/machine/_threads.h b/winsup/cygwin/include/machine/_threads.h
new file mode 100644
index 000000000..1e1250fa4
--- /dev/null
+++ b/winsup/cygwin/include/machine/_threads.h
@@ -0,0 +1,63 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef _MACHINE__THREADS_H_
+#define	_MACHINE__THREADS_H_
+
+#include <sys/types.h>
+#include <sys/lock.h>
+
+/*
+ * The C11 threads interface.
+ *
+ * This interface is implemented as a light-weight wrapper around
+ * <pthread.h>.  To prevent namespace pollution, the once_flag object,
+ * its corresponding ONCE_FLAG_INIT and TSS_DTOR_ITERATIONS have been
+ * copied from this header file.  They must be kept in sync.
+ */
+
+typedef pthread_cond_t	cnd_t;
+typedef pthread_mutex_t	mtx_t;
+typedef pthread_t	thrd_t;
+typedef pthread_key_t	tss_t;
+
+/* pthread_once_t */
+typedef struct {
+  mtx_t mutex;
+  int state;
+} once_flag;
+
+/* PTHREAD_ONCE_INIT */
+#define	ONCE_FLAG_INIT { ((pthread_mutex_t)19), 0 }
+
+/* PTHREAD_DESTRUCTOR_ITERATIONS */
+#define	TSS_DTOR_ITERATIONS 4
+
+#endif /* _MACHINE__THREADS_H_ */
diff --git a/winsup/cygwin/libc/call_once.c b/winsup/cygwin/libc/call_once.c
new file mode 100644
index 000000000..bb8618ce2
--- /dev/null
+++ b/winsup/cygwin/libc/call_once.c
@@ -0,0 +1,46 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <pthread.h>
+
+#include "threads.h"
+
+void
+call_once(once_flag *flag, void (*func)(void))
+{
+
+	(void)pthread_once((pthread_once_t *)flag, func);
+}
+
+_Static_assert(sizeof(once_flag) == sizeof(pthread_once_t),
+    "once_flag must be of the same size as pthread_once_t");
diff --git a/winsup/cygwin/libc/cnd.c b/winsup/cygwin/libc/cnd.c
new file mode 100644
index 000000000..a27d55d72
--- /dev/null
+++ b/winsup/cygwin/libc/cnd.c
@@ -0,0 +1,100 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <errno.h>
+#include <pthread.h>
+
+#include "threads.h"
+
+int
+cnd_broadcast(cnd_t *cond)
+{
+
+	if (pthread_cond_broadcast(cond) != 0)
+		return (thrd_error);
+	return (thrd_success);
+}
+
+void
+cnd_destroy(cnd_t *cond)
+{
+
+	(void)pthread_cond_destroy(cond);
+}
+
+int
+cnd_init(cnd_t *cond)
+{
+
+	switch (pthread_cond_init(cond, NULL)) {
+	case 0:
+		return (thrd_success);
+	case ENOMEM:
+		return (thrd_nomem);
+	default:
+		return (thrd_error);
+	}
+}
+
+int
+cnd_signal(cnd_t *cond)
+{
+
+	if (pthread_cond_signal(cond) != 0)
+		return (thrd_error);
+	return (thrd_success);
+}
+
+int
+cnd_timedwait(cnd_t *restrict cond, mtx_t *restrict mtx,
+    const struct timespec *restrict ts)
+{
+
+	switch (pthread_cond_timedwait(cond, mtx, ts)) {
+	case 0:
+		return (thrd_success);
+	case ETIMEDOUT:
+		return (thrd_timedout);
+	default:
+		return (thrd_error);
+	}
+}
+
+int
+cnd_wait(cnd_t *cond, mtx_t *mtx)
+{
+
+	if (pthread_cond_wait(cond, mtx) != 0)
+		return (thrd_error);
+	return (thrd_success);
+}
diff --git a/winsup/cygwin/libc/mtx.c b/winsup/cygwin/libc/mtx.c
new file mode 100644
index 000000000..dca0849cc
--- /dev/null
+++ b/winsup/cygwin/libc/mtx.c
@@ -0,0 +1,125 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <errno.h>
+#include <pthread.h>
+
+#include "threads.h"
+
+void
+mtx_destroy(mtx_t *mtx)
+{
+
+	(void)pthread_mutex_destroy(mtx);
+}
+
+int
+mtx_init(mtx_t *mtx, int type)
+{
+	pthread_mutexattr_t attr;
+	int mt;
+
+	switch (type) {
+	case mtx_plain:
+	case mtx_timed:
+		mt = PTHREAD_MUTEX_NORMAL;
+		break;
+#ifdef __CYGWIN__
+	/* This is a hack to support apps running under GLibc.  mtx_plain is
+	   defined as 0 in GLibc, so apps calling mtx_init with just the
+	   mtx_recursive flag don't fail, as they should.  Rather they run
+	   as if mtx_plain | mtx_recursive has been given as type. */
+	case mtx_recursive:
+#endif
+	case mtx_plain | mtx_recursive:
+	case mtx_timed | mtx_recursive:
+		mt = PTHREAD_MUTEX_RECURSIVE;
+		break;
+	default:
+		return (thrd_error);
+	}
+
+	if (pthread_mutexattr_init(&attr) != 0)
+		return (thrd_error);
+	if (pthread_mutexattr_settype(&attr, mt) != 0)
+		return (thrd_error);
+	if (pthread_mutex_init(mtx, &attr) != 0)
+		return (thrd_error);
+	return (thrd_success);
+}
+
+int
+mtx_lock(mtx_t *mtx)
+{
+
+	if (pthread_mutex_lock(mtx) != 0)
+		return (thrd_error);
+	return (thrd_success);
+}
+
+int
+mtx_timedlock(mtx_t *restrict mtx, const struct timespec *restrict ts)
+{
+
+	switch (pthread_mutex_timedlock(mtx, ts)) {
+	case 0:
+		return (thrd_success);
+	case ETIMEDOUT:
+		return (thrd_timedout);
+	default:
+		return (thrd_error);
+	}
+}
+
+int
+mtx_trylock(mtx_t *mtx)
+{
+
+	switch (pthread_mutex_trylock(mtx)) {
+	case 0:
+		return (thrd_success);
+	case EBUSY:
+		return (thrd_busy);
+	default:
+		return (thrd_error);
+	}
+}
+
+int
+mtx_unlock(mtx_t *mtx)
+{
+
+	if (pthread_mutex_unlock(mtx) != 0)
+		return (thrd_error);
+	return (thrd_success);
+}
diff --git a/winsup/cygwin/libc/thrd.c b/winsup/cygwin/libc/thrd.c
new file mode 100644
index 000000000..fc79f66ca
--- /dev/null
+++ b/winsup/cygwin/libc/thrd.c
@@ -0,0 +1,150 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifdef __CYGWIN__
+#include <errno.h>
+#endif
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <pthread.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+#include "threads.h"
+
+struct thrd_param {
+	thrd_start_t	 func;
+	void		*arg;
+};
+
+static void *
+thrd_entry(void *arg)
+{
+	struct thrd_param tp;
+
+	tp = *(struct thrd_param *)arg;
+	free(arg);
+	return ((void *)(intptr_t)tp.func(tp.arg));
+}
+
+int
+thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
+{
+	struct thrd_param *tp;
+
+	/*
+	 * Work around return type inconsistency.  Wrap execution using
+	 * a function conforming to pthread_create()'s start_routine.
+	 */
+	tp = malloc(sizeof(*tp));
+	if (tp == NULL)
+		return (thrd_nomem);
+	tp->func = func;
+	tp->arg = arg;
+	if (pthread_create(thr, NULL, thrd_entry, tp) != 0) {
+		free(tp);
+		return (thrd_error);
+	}
+	return (thrd_success);
+}
+
+thrd_t
+thrd_current(void)
+{
+
+	return (pthread_self());
+}
+
+int
+thrd_detach(thrd_t thr)
+{
+
+	if (pthread_detach(thr) != 0)
+		return (thrd_error);
+	return (thrd_success);
+}
+
+int
+thrd_equal(thrd_t thr0, thrd_t thr1)
+{
+
+	return (pthread_equal(thr0, thr1));
+}
+
+_Noreturn void
+thrd_exit(int res)
+{
+
+	pthread_exit((void *)(intptr_t)res);
+}
+
+int
+thrd_join(thrd_t thr, int *res)
+{
+	void *value_ptr;
+
+	if (pthread_join(thr, &value_ptr) != 0)
+		return (thrd_error);
+	if (res != NULL)
+		*res = (intptr_t)value_ptr;
+	return (thrd_success);
+}
+
+int
+thrd_sleep(const struct timespec *duration, struct timespec *remaining)
+{
+#ifdef __CYGWIN__
+	/* Return value per POSIX:
+	    0 on successful sleep,
+	   -1 if a signal occurred,
+	   other negative value if an error occurred. */
+	int ret = clock_nanosleep(CLOCK_REALTIME, 0, duration, remaining);
+	switch (ret) {
+	case 0:
+		return 0;
+	case EINTR:
+		return -1;
+	default:
+		return -ret;	/* EPERM isn't generated by clock_nanosleep
+				   so this is safe. */
+	}
+#else
+	int ret = nanosleep(duration, remaining);
+#endif
+}
+
+void
+thrd_yield(void)
+{
+
+	pthread_yield();
+}
diff --git a/winsup/cygwin/libc/tss.c b/winsup/cygwin/libc/tss.c
new file mode 100644
index 000000000..9f15b3365
--- /dev/null
+++ b/winsup/cygwin/libc/tss.c
@@ -0,0 +1,73 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#ifdef __CYGWIN__
+#include <limits.h>
+#endif
+#include <pthread.h>
+
+#include "threads.h"
+
+int
+tss_create(tss_t *key, tss_dtor_t dtor)
+{
+	if (pthread_key_create(key, dtor) != 0)
+		return (thrd_error);
+	return (thrd_success);
+}
+
+void
+tss_delete(tss_t key)
+{
+
+	(void)pthread_key_delete(key);
+}
+
+void *
+tss_get(tss_t key)
+{
+
+	return (pthread_getspecific(key));
+}
+
+int
+tss_set(tss_t key, void *val)
+{
+
+	if (pthread_setspecific(key, val) != 0)
+		return (thrd_error);
+	return (thrd_success);
+}
+
+_Static_assert(TSS_DTOR_ITERATIONS == PTHREAD_DESTRUCTOR_ITERATIONS,
+    "TSS_DTOR_ITERATIONS must be identical to PTHREAD_DESTRUCTOR_ITERATIONS");


More information about the Cygwin-cvs mailing list