Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libpthread Avoid incompatible function pointer casts in ...
details: https://anonhg.NetBSD.org/src/rev/199a0f27361f
branches: trunk
changeset: 450919:199a0f27361f
user: kamil <kamil%NetBSD.org@localhost>
date: Mon Apr 29 20:11:43 2019 +0000
description:
Avoid incompatible function pointer casts in thrd_create(3)
Use an intermediate function trampoline to workaround different function
pointer prototypes.
While there, correct scenario returning thrd_nomem from thrd_create(3).
In practice ENOMEM is rarely returned from pthread(3).
Older code worked on tested ports, but was depending on unneeded UB.
diffstat:
lib/libpthread/thrd.c | 49 +++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 43 insertions(+), 6 deletions(-)
diffs (82 lines):
diff -r af117e13fe48 -r 199a0f27361f lib/libpthread/thrd.c
--- a/lib/libpthread/thrd.c Mon Apr 29 19:08:11 2019 +0000
+++ b/lib/libpthread/thrd.c Mon Apr 29 20:11:43 2019 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: thrd.c,v 1.2 2019/04/24 18:47:54 kamil Exp $ */
+/* $NetBSD: thrd.c,v 1.3 2019/04/29 20:11:43 kamil Exp $ */
/*-
* Copyright (c) 2016 The NetBSD Foundation, Inc.
@@ -30,30 +30,67 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: thrd.c,v 1.2 2019/04/24 18:47:54 kamil Exp $");
+__RCSID("$NetBSD: thrd.c,v 1.3 2019/04/29 20:11:43 kamil Exp $");
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <sched.h>
+#include <stdlib.h>
#include <time.h>
#include <threads.h>
+struct __thrd_tramp_data {
+ thrd_start_t func;
+ void *arg;
+};
+
+static void *
+__thrd_create_tramp(void *arg)
+{
+ struct __thrd_tramp_data *cookie;
+ int ret;
+
+ _DIAGASSERT(arg != NULL);
+
+ cookie = (struct __thrd_tramp_data *)arg;
+
+ ret = (cookie->func)(cookie->arg);
+
+ free(cookie);
+
+ return (void *)(intptr_t)ret;
+}
+
int
thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
{
+ struct __thrd_tramp_data *cookie;
+ int error;
_DIAGASSERT(thr != NULL);
_DIAGASSERT(func != NULL);
- switch(pthread_create(thr, NULL, (void *(*)(void *))func, arg)) {
+ cookie = malloc(sizeof(*cookie));
+ if (cookie == NULL)
+ return thrd_nomem;
+
+ cookie->func = func;
+ cookie->arg = arg;
+
+ switch(pthread_create(thr, NULL, __thrd_create_tramp, cookie)) {
case 0:
return thrd_success;
- case EAGAIN:
- return thrd_nomem;
+ case ENOMEM:
+ error = thrd_nomem;
+ break;
default:
- return thrd_error;
+ error = thrd_error;
}
+
+ free(cookie);
+
+ return error;
}
thrd_t
Home |
Main Index |
Thread Index |
Old Index