Re: Cleaning up threading code
Thomas Munro <thomas.munro@gmail.com>
From: Thomas Munro <thomas.munro@gmail.com>
To: Heikki Linnakangas <hlinnaka@iki.fi>
Cc: Peter Eisentraut <peter@eisentraut.org>,
Andres Freund <andres@anarazel.de>, pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2024-08-21T12:51:53Z
Lists: pgsql-hackers
Attachments
- v3-0001-Add-port-pg_threads.h-for-a-common-threading-API.patch (text/x-patch) patch v3-0001
On Sun, Apr 14, 2024 at 3:16 PM Thomas Munro <thomas.munro@gmail.com> wrote: > So I'll go ahead and add the storage class to the next version, and > contemplate a couple of different options for the tss stuff, including > perhaps leaving it out if that seems doable. Here is a new attempt at pg_threads.h. Still work in progress, but passing tests, with storage class and working TSS, showing various users. I eventually understood first that my TSS destructor problems on Windows came from mismatched calling conventions, and that you can't really trampoline your way around that, at least not without doing some pretty unreasonable things, and that is why nobody can emulate either tss_create() or pthread_key_create() directly with Windows' FlsAlloc(), so everybody who tries finishes up building their own infrastructure to track destructors, or in ECPG's case just leaks all the memory instead. Here's the simplest implementation I could come up with so far. I don't have Windows so I made it possible to use emulated TSS destructors on my local machine with a special macro for testing, and then argued with CI for a while until the other machines agreed. Otherwise, it's all a fairly thin wrapper and hopefully not suprising. In one place, an ECPG thread-local variable has no destructor, so we can use it as the first example of the new pg_thread_local storage class. One thing this would need to be complete, at least the way I've implemented it, is memory barriers, for non-TSO hardware, which would require lifting the ban on atomics.h in frontend code, or at least parts of it. Only 64 bit emulation is actually tied to the backend now (because it calls spinlock stuff, that itself is backend-only, but also it doesn't actually need to be either). Or maybe I can figure out a different scheme that doesn't need that. Or something... WIP patch attached.
Commits
-
Doc: Adjust libpq docs about thread safety.
- ce0b0fa3e792 17.0 landed
-
Remove --disable-thread-safety and related code.
- 68a4b58eca03 17.0 landed
-
Rename port/thread.c to port/user.c.
- 89333db963af 17.0 landed
-
Remove obsolete comment and code from fe-auth.c.
- 1aadc504bf47 17.0 landed