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

Re: [PATCH 0/1] trace2: fix tracing when NO_PTHREADS is defined

From
Duy Nguyen <pclouds@gmail.com>
Date
May 25, 2019, 10:43 UTC
Message-ID
<CACsJy8D7w4sC_tchx-Q80PCiu+2hYfkZo22_Vb3vgJ+xvgUAAA@mail.gmail.com>
In-Reply-To
<20190523055108.GA20871@sigill.intra.peff.net>
On Thu, May 23, 2019 at 12:51 PM Jeff King <peff@peff.net> wrote:
Show 40 quoted lines
> For fun, here's a constant-time zero-allocation implementation that I
> came up with. It passes t0211 with NO_PTHREADS, but I didn't test it
> beyond that.
>
> diff --git a/thread-utils.h b/thread-utils.h
> index 4961487ed9..f466215742 100644
> --- a/thread-utils.h
> +++ b/thread-utils.h
> @@ -18,7 +18,7 @@
>  #define pthread_t int
>  #define pthread_mutex_t int
>  #define pthread_cond_t int
> -#define pthread_key_t int
> +#define pthread_key_t git_pthread_key_t
>
>  #define pthread_mutex_init(mutex, attr) dummy_pthread_init(mutex)
>  #define pthread_mutex_lock(mutex)
> @@ -31,16 +31,49 @@
>  #define pthread_cond_broadcast(cond)
>  #define pthread_cond_destroy(cond)
>
> -#define pthread_key_create(key, attr) dummy_pthread_init(key)
> -#define pthread_key_delete(key)
> +#define pthread_key_create(key, destroy) git_pthread_key_create(key, destroy)
> +#define pthread_key_delete(key) git_pthread_key_delete(key)
>
>  #define pthread_create(thread, attr, fn, data) \
>         dummy_pthread_create(thread, attr, fn, data)
>  #define pthread_join(thread, retval) \
>         dummy_pthread_join(thread, retval)
>
> -#define pthread_setspecific(key, data)
> -#define pthread_getspecific(key) NULL
> +#define pthread_setspecific(key, data) git_pthread_setspecific(key, data)
> +#define pthread_getspecific(key) git_pthread_getspecific(key)
> +
> +typedef struct {
> +       void *data;
> +       /* extra indirection because setspecific is passed key by value */
> +       void **vdata;

Ha! I was thinking a separate key->value mapping which is complicated in C. But this works pretty well for a single thread, and it even supports multiple keys.

Show 31 quoted lines
> +} git_pthread_key_t;
> +
> +static inline int git_pthread_key_create(git_pthread_key_t *key,
> +                                        void (*destroy)(void *))
> +{
> +       key->data = NULL;
> +       key->vdata = &key->data;
> +       /* We don't use this; alternatively we could all via atexit(). */
> +       if (destroy)
> +               BUG("NO_PTHREADS does not support pthread key destructors");
> +       return 0;
> +}
> +
> +static inline int git_pthread_key_delete(git_pthread_key_t key)
> +{
> +       /* noop */
> +       return 0;
> +}
> +
> +static inline void git_pthread_setspecific(git_pthread_key_t key, void *data)
> +{
> +       *(key.vdata) = data;
> +}
> +
> +static inline void *git_pthread_getspecific(git_pthread_key_t key)
> +{
> +       return key.data;
> +}
>
>  int dummy_pthread_create(pthread_t *pthread, const void *attr,
>                          void *(*fn)(void *), void *data);
-- 
Duy
Previous: Jeff HostetlerNext: Jeff King
Message 7 of 10 in “trace2: fix tracing when NO_PTHREADS is defined”
  1. 0/1 trace2: fix tracing when NO_PTHREADS is definedJeff Hostetler via GitGitGadget, May 21, 2019
  2. 1/1 trace2: fix tracing when NO_PTHREADS is definedJeff Hostetler via GitGitGadget, May 21, 2019
  3. Jeff KingMay 21, 2019
  4. Jeff HostetlerMay 22, 2019
  5. Jeff KingMay 23, 2019
  6. Jeff HostetlerMay 23, 2019
  7. Duy NguyenMay 25, 2019
  8. Jeff KingMay 28, 2019
  9. Jeff HostetlerMay 28, 2019
  10. Junio C HamanoMay 28, 2019

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.