git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC][PATCH] t-prio-queue: simplify using compound literals

From
Jeff King <peff@peff.net>
Date
Apr 5, 2024, 19:17 UTC
Message-ID
<20240405191714.GA2561807@coredump.intra.peff.net>
In-Reply-To
<c6cb255a-72f0-4ac2-81a2-1d8e95570a81@web.de>
On Fri, Apr 05, 2024 at 07:44:59PM +0200, René Scharfe wrote:
Show 20 quoted lines
> Am 02.04.24 um 22:41 schrieb Jeff King:
> >
> > I don't have a
> > strong opinion on compound literals in general, but if we did allow
> > them, we could clean up the horrible non-reentrant DATE_MODE().
> 
> We can easily make them reentrant without compound literals.  It just
> requires simple changes to lots of places.  Patch below.
> 
> --- >8 ---
> Subject: [PATCH] date: make DATE_MODE thread-safe
> 
> date_mode_from_type() modifies a static variable and returns a pointer
> to it.  This is not thread-safe.  Most callers of date_mode_from_type()
> use it via the macro DATE_MODE and pass its result on to functions like
> show_date(), which take a const pointer and don't modify the struct.
> 
> Avoid the static storage by putting the variable on the stack and
> returning the whole struct date_mode.  Change functions that take a
> constant pointer to expect the whole struct instead.

Yeah, this seems pretty reasonable. I think we've traditionally been hesitant to pass or return structs by value, but that's mostly superstition. IIRC it was illegal before C89, but that's long enough ago that we can presumably ignore it.

-Peff
Previous: René ScharfeNext: Junio C Hamano
Message 5 of 11 in “t-prio-queue: simplify using compound literals”
  1. René ScharfeApr 2, 2024
  2. Junio C HamanoApr 2, 2024
  3. Jeff KingApr 2, 2024
  4. René ScharfeApr 5, 2024
  5. Jeff KingApr 5, 2024
  6. Junio C HamanoApr 5, 2024
  7. René ScharfeApr 6, 2024
  8. Jeff KingApr 7, 2024
  9. Junio C HamanoApr 8, 2024
  10. Jeff KingApr 8, 2024
  11. Josh SteadmonApr 11, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.