changeset 879:5dd01b60e33b

thread: use pthread_cleanup_{push,pop} Each xthr_create wraps the passed in user function in hopes of executing code after the function returns to clean up some libjeffpc-internal thread-local state. This needs to happen even if the thread called pthread_exit. The pthread_cleanup_{push,pop} functions are the POSIX-provided way to run such cleanup code. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 31 Jan 2024 13:50:28 -0500
parents 491f408f6294
children fe350b24465d
files thread.c
diffstat 1 files changed, 8 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/thread.c	Sat Apr 20 08:51:59 2024 -0400
+++ b/thread.c	Wed Jan 31 13:50:28 2024 -0500
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016-2021 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
+ * Copyright (c) 2016-2021,2024 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -57,6 +57,11 @@
 	return thread_name;
 }
 
+static void xthr_cleanups(void *unused)
+{
+	lockdep_no_locks();
+}
+
 static void *xthr_setup(void *_info)
 {
 	struct xthr_info info = *((struct xthr_info *) _info);
@@ -68,9 +73,9 @@
 	/* stash & set the thread name, but ignore errors */
 	xthr_set_name(info.name);
 
+	pthread_cleanup_push(xthr_cleanups, NULL);
 	ret = info.f(info.arg);
-
-	lockdep_no_locks();
+	pthread_cleanup_pop(1);
 
 	return ret;
 }