From d32818a16ceb352b8889f95ec7ff65575ad2e4f2 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Thu, 14 Jul 2022 09:52:34 -0400 Subject: [PATCH] Another POSIX thread destructor fix (Issue #293) --- CHANGES.md | 3 +-- NOTICE | 2 +- README.md | 2 +- mxml-private.c | 12 ++++++++---- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 2c5d532c..7e9a7bb0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,7 +1,6 @@ # Changes in Mini-XML 3.3.1 -- Fixed a POSIX thread cleanup bug on macOS - per-thread data destructor called - twice. +- Fixed POSIX thread cleanup bugs (Issue #293) # Changes in Mini-XML 3.3 diff --git a/NOTICE b/NOTICE index b6bfd1f3..e788dab7 100644 --- a/NOTICE +++ b/NOTICE @@ -1,6 +1,6 @@ Mini-XML -Copyright © 2003-2020 by Michael R Sweet +Copyright © 2003-2022 by Michael R Sweet (Optional) Exceptions to the Apache 2.0 License: diff --git a/README.md b/README.md index 34531707..bb62ce80 100644 --- a/README.md +++ b/README.md @@ -199,7 +199,7 @@ current version of this software, documentation, and Github issue tracking page. Legal Stuff ----------- -Copyright © 2003-2021 by Michael R Sweet +Copyright © 2003-2022 by Michael R Sweet The Mini-XML library is licensed under the Apache License Version 2.0 with an *optional* exception to allow linking against GPL2/LGPL2-only software. See the diff --git a/mxml-private.c b/mxml-private.c index 3f3ab6ca..ec4f0def 100644 --- a/mxml-private.c +++ b/mxml-private.c @@ -3,7 +3,7 @@ * * https://www.msweet.org/mxml * - * Copyright © 2003-2021 by Michael R Sweet. + * Copyright © 2003-2022 by Michael R Sweet. * * Licensed under Apache License v2.0. See the file "LICENSE" for more * information. @@ -26,7 +26,7 @@ * be unloaded safely, although since there is no standard way to do so I * can't even provide any guarantees that you can do it safely on all platforms. * - * This code currently supports AIX, HP-UX, Linux, Mac OS X, Solaris, and + * This code currently supports AIX, HP-UX, Linux, macOS, Solaris, and * Windows. It might work on the BSDs and IRIX, but I haven't tested that. */ @@ -36,7 +36,7 @@ #elif defined(__hpux) # pragma FINI _mxml_fini # define _MXML_FINI _mxml_fini -#elif defined(__GNUC__) /* Linux and Mac OS X */ +#elif defined(__GNUC__) /* Linux and macOS */ # define _MXML_FINI __attribute((destructor)) _mxml_fini #else # define _MXML_FINI _fini @@ -140,6 +140,8 @@ mxml_real_cb(mxml_node_t *node) /* I - Current node */ #ifdef HAVE_PTHREAD_H /**** POSIX threading ****/ # include +static int _mxml_initialized = 0; + /* Have we been initialized? */ static pthread_key_t _mxml_key; /* Thread local storage key */ static pthread_once_t _mxml_key_once = PTHREAD_ONCE_INIT; /* One-time initialization object */ @@ -165,7 +167,8 @@ _mxml_destructor(void *g) /* I - Global data */ static void _MXML_FINI(void) { - pthread_key_delete(_mxml_key); + if (_mxml_initialized) + pthread_key_delete(_mxml_key); } @@ -202,6 +205,7 @@ _mxml_global(void) static void _mxml_init(void) { + _mxml_initialized = 1; pthread_key_create(&_mxml_key, _mxml_destructor); }