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

10 messages from 2019-05-21 to 2019-05-28. Participants: Jeff Hostetler via GitGitGadget, Jeff King, Jeff Hostetler, Duy Nguyen, Junio C Hamano.
Thread: https://gitlist.dev/t/51144

## Jeff Hostetler via GitGitGadget, 2019-05-21 19:33

Subject: [PATCH 0/1] trace2: fix tracing when NO_PTHREADS is defined
Message-ID: <pull.222.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/pull.222.git.gitgitgadget%40gmail.com

```
This commit addresses the problem reported in:
https://public-inbox.org/git/92cfdf43-8841-9c5a-7838-dda995038908@jeffhostetler.com/T/#mbaf8069f6d1bc18d5a02d3682a1f9282f5547ea9

As Duy suggested, pthread_getspecific() just returns NULL when NO_PTHREADS
is defined. And pthread_setspecific() silently does not nothing. So this
problem was hidden from view.

I have to wonder if we should update pthread_*specific() to call BUG() when
NO_PTHREADS is defined as a way to catch unguarded usages easier or make
this issue more clear.

Jeff Hostetler (1):
  trace2: fix tracing when NO_PTHREADS is defined

 trace2/tr2_tls.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)


base-commit: aa25c82427ae70aebf3b8f970f2afd54e9a2a8c6
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-222%2Fjeffhostetler%2Ftrace2-no-pthread-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-222/jeffhostetler/trace2-no-pthread-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/222
-- 
gitgitgadget

```

## Jeff Hostetler via GitGitGadget, 2019-05-21 19:33

Subject: [PATCH 1/1] trace2: fix tracing when NO_PTHREADS is defined
Message-ID: <c195394c86c1ee599945cd4ea65633a72a3fd30e.1558467237.git.gitgitgadget@gmail.com>
URL: https://gitlist.dev/e/c195394c86c1ee599945cd4ea65633a72a3fd30e.1558467237.git.gitgitgadget%40gmail.com
In-Reply-To: <pull.222.git.gitgitgadget@gmail.com>

```
From: Jeff Hostetler <jeffhost@microsoft.com>

Teach trace2 TLS code to not rely on pthread_getspecific() when NO_PTHREADS
is defined.  Instead, always assume the context data of the main thread.

Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
---
 trace2/tr2_tls.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/trace2/tr2_tls.c b/trace2/tr2_tls.c
index e76d8c5d92..067c23755f 100644
--- a/trace2/tr2_tls.c
+++ b/trace2/tr2_tls.c
@@ -61,7 +61,12 @@ struct tr2tls_thread_ctx *tr2tls_create_self(const char *thread_name,
 
 struct tr2tls_thread_ctx *tr2tls_get_self(void)
 {
-	struct tr2tls_thread_ctx *ctx = pthread_getspecific(tr2tls_key);
+	struct tr2tls_thread_ctx *ctx;
+
+	if (!HAVE_THREADS)
+		return tr2tls_thread_main;
+
+	ctx = pthread_getspecific(tr2tls_key);
 
 	/*
 	 * If the thread-proc did not call trace2_thread_start(), we won't
@@ -76,9 +81,10 @@ struct tr2tls_thread_ctx *tr2tls_get_self(void)
 
 int tr2tls_is_main_thread(void)
 {
-	struct tr2tls_thread_ctx *ctx = pthread_getspecific(tr2tls_key);
+	if (!HAVE_THREADS)
+		return 1;
 
-	return ctx == tr2tls_thread_main;
+	return pthread_getspecific(tr2tls_key) == tr2tls_thread_main;
 }
 
 void tr2tls_unset_self(void)
-- 
gitgitgadget

```

## Jeff King, 2019-05-21 21:27

Subject: Re: [PATCH 0/1] trace2: fix tracing when NO_PTHREADS is defined
Message-ID: <20190521212744.GC14807@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20190521212744.GC14807%40sigill.intra.peff.net
In-Reply-To: <pull.222.git.gitgitgadget@gmail.com>

```
On Tue, May 21, 2019 at 12:33:58PM -0700, Jeff Hostetler via GitGitGadget wrote:

> As Duy suggested, pthread_getspecific() just returns NULL when NO_PTHREADS
> is defined. And pthread_setspecific() silently does not nothing. So this
> problem was hidden from view.
> 
> I have to wonder if we should update pthread_*specific() to call BUG() when
> NO_PTHREADS is defined as a way to catch unguarded usages easier or make
> this issue more clear.

I think it should actually store the data asked for by the caller, as if
we were the single thread running. We discussed this as the time of
refactoring NO_PTHREADS, but there was only one caller that would have
benefited. Now there are two. ;)

Discussion in the subthread of this patch:

  https://public-inbox.org/git/20181027071003.1347-2-pclouds@gmail.com/

-Peff

```

## Jeff Hostetler, 2019-05-22 13:23

Subject: Re: [PATCH 0/1] trace2: fix tracing when NO_PTHREADS is defined
Message-ID: <97796007-db6e-f2ea-91ae-3113b74e4ae9@jeffhostetler.com>
URL: https://gitlist.dev/e/97796007-db6e-f2ea-91ae-3113b74e4ae9%40jeffhostetler.com
In-Reply-To: <20190521212744.GC14807@sigill.intra.peff.net>

```


On 5/21/2019 5:27 PM, Jeff King wrote:
> On Tue, May 21, 2019 at 12:33:58PM -0700, Jeff Hostetler via GitGitGadget wrote:
> 
>> As Duy suggested, pthread_getspecific() just returns NULL when NO_PTHREADS
>> is defined. And pthread_setspecific() silently does not nothing. So this
>> problem was hidden from view.
>>
>> I have to wonder if we should update pthread_*specific() to call BUG() when
>> NO_PTHREADS is defined as a way to catch unguarded usages easier or make
>> this issue more clear.
> 
> I think it should actually store the data asked for by the caller, as if
> we were the single thread running. We discussed this as the time of
> refactoring NO_PTHREADS, but there was only one caller that would have
> benefited. Now there are two. ;)
> 
> Discussion in the subthread of this patch:
> 
>    https://public-inbox.org/git/20181027071003.1347-2-pclouds@gmail.com/
> 
> -Peff
> 

I was wondering about that too as the proper long term solution.
We would need (as the discussion suggests [1]) to properly
respect/represent the pthread_key_t argument.

For now, I've guarded my usage of pthread_getspecific() in the trace2
(similar to what index-pack does), so its not urgent that we update it.
And I'd rather we take this simple trace2 fix now and not try to combine
it with fixes for the pthread macros.  Especially now as we're in the RC
cycle for 2.22.

I'll make a note to revisit the pthread code after 2.22.

Thanks
Jeff


[1] 
https://public-inbox.org/git/CACsJy8DLW_smOJd6aCoRcJZxQ2Lzut5US=sPadj7=fhne0UHGg@mail.gmail.com/


```

## Jeff King, 2019-05-23 05:51

Subject: Re: [PATCH 0/1] trace2: fix tracing when NO_PTHREADS is defined
Message-ID: <20190523055108.GA20871@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20190523055108.GA20871%40sigill.intra.peff.net
In-Reply-To: <97796007-db6e-f2ea-91ae-3113b74e4ae9@jeffhostetler.com>

```
On Wed, May 22, 2019 at 09:23:39AM -0400, Jeff Hostetler wrote:

> I was wondering about that too as the proper long term solution.
> We would need (as the discussion suggests [1]) to properly
> respect/represent the pthread_key_t argument.
> 
> For now, I've guarded my usage of pthread_getspecific() in the trace2
> (similar to what index-pack does), so its not urgent that we update it.
> And I'd rather we take this simple trace2 fix now and not try to combine
> it with fixes for the pthread macros.  Especially now as we're in the RC
> cycle for 2.22.

Yeah, I think that makes sense.

> I'll make a note to revisit the pthread code after 2.22.

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;
+} 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);

```

## Jeff Hostetler, 2019-05-23 13:55

Subject: Re: [PATCH 0/1] trace2: fix tracing when NO_PTHREADS is defined
Message-ID: <50badedb-7856-374e-b14c-1eb1951c63f0@jeffhostetler.com>
URL: https://gitlist.dev/e/50badedb-7856-374e-b14c-1eb1951c63f0%40jeffhostetler.com
In-Reply-To: <20190523055108.GA20871@sigill.intra.peff.net>

```


On 5/23/2019 1:51 AM, Jeff King wrote:
> On Wed, May 22, 2019 at 09:23:39AM -0400, Jeff Hostetler wrote:
> 
>> I was wondering about that too as the proper long term solution.
>> We would need (as the discussion suggests [1]) to properly
>> respect/represent the pthread_key_t argument.
>>
>> For now, I've guarded my usage of pthread_getspecific() in the trace2
>> (similar to what index-pack does), so its not urgent that we update it.
>> And I'd rather we take this simple trace2 fix now and not try to combine
>> it with fixes for the pthread macros.  Especially now as we're in the RC
>> cycle for 2.22.
> 
> Yeah, I think that makes sense.
> 
>> I'll make a note to revisit the pthread code after 2.22.
> 
> 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;
> +} 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);
> 

Thanks!  I'll play with this and submit something after 2.22 and
things slow down a bit.

Jeff


```

## Duy Nguyen, 2019-05-25 10:43

Subject: Re: [PATCH 0/1] trace2: fix tracing when NO_PTHREADS is defined
Message-ID: <CACsJy8D7w4sC_tchx-Q80PCiu+2hYfkZo22_Vb3vgJ+xvgUAAA@mail.gmail.com>
URL: https://gitlist.dev/e/CACsJy8D7w4sC_tchx-Q80PCiu%2B2hYfkZo22_Vb3vgJ%2BxvgUAAA%40mail.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:
> 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.

> +} 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

```

## Jeff King, 2019-05-28 06:28

Subject: Re: [PATCH 0/1] trace2: fix tracing when NO_PTHREADS is defined
Message-ID: <20190528062804.GE7946@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20190528062804.GE7946%40sigill.intra.peff.net
In-Reply-To: <CACsJy8D7w4sC_tchx-Q80PCiu+2hYfkZo22_Vb3vgJ+xvgUAAA@mail.gmail.com>

```
On Sat, May 25, 2019 at 05:43:55PM +0700, Duy Nguyen wrote:

> > +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.

I really wish that all of the functions passed the pthread_key_t by
reference. That would make it possible to define the key as a single
pointer.

I'm not sure if pthread_key_t's are meant to be shallow-copyable. I.e.,
should this work:

  void foo(pthread_key_t *out)
  {
	pthread_key_t tmp;
	pthread_key_create(&tmp, NULL);
	*out = tmp;
  }
  ...
  pthread_key_t k;
  foo(&k);
  pthread_setspecific(k, some_ptr);

It does not with my proposed plan, because the pointer in tmp.data went
out of scope, leaving tmp.vdata (and thus k.vdata) as a dangling
pointer.

The code above seems like a vaguely crazy thing to do. But if we want to
be absolutely paranoid, we'd have to malloc() an extra pointer in the
create() function, instead of carrying it inside the key. Or just make a
global "void *thread_specific_data[PTHREAD_KEYS_MAX]" and make each key
an integer index into it.

It's pretty clear that they expect one of those two implementations,
given that POSIX says key creation can report either ENOMEM, or EAGAIN
if we exceed PTHREAD_KEYS_MAX. :)

-Peff

```

## Jeff Hostetler, 2019-05-28 14:57

Subject: Re: [PATCH 0/1] trace2: fix tracing when NO_PTHREADS is defined
Message-ID: <47db8f30-846b-12d0-250e-4bccf1e19c45@jeffhostetler.com>
URL: https://gitlist.dev/e/47db8f30-846b-12d0-250e-4bccf1e19c45%40jeffhostetler.com
In-Reply-To: <20190528062804.GE7946@sigill.intra.peff.net>

```


On 5/28/2019 2:28 AM, Jeff King wrote:
> On Sat, May 25, 2019 at 05:43:55PM +0700, Duy Nguyen wrote:
> 
>>> +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.
> 
> I really wish that all of the functions passed the pthread_key_t by
> reference. That would make it possible to define the key as a single
> pointer.
> 
> I'm not sure if pthread_key_t's are meant to be shallow-copyable. I.e.,
> should this work:
> 
>    void foo(pthread_key_t *out)
>    {
> 	pthread_key_t tmp;
> 	pthread_key_create(&tmp, NULL);
> 	*out = tmp;
>    }
>    ...
>    pthread_key_t k;
>    foo(&k);
>    pthread_setspecific(k, some_ptr);
> 
> It does not with my proposed plan, because the pointer in tmp.data went
> out of scope, leaving tmp.vdata (and thus k.vdata) as a dangling
> pointer.
> 
> The code above seems like a vaguely crazy thing to do. But if we want to
> be absolutely paranoid, we'd have to malloc() an extra pointer in the
> create() function, instead of carrying it inside the key. Or just make a
> global "void *thread_specific_data[PTHREAD_KEYS_MAX]" and make each key
> an integer index into it.
> 
> It's pretty clear that they expect one of those two implementations,
> given that POSIX says key creation can report either ENOMEM, or EAGAIN
> if we exceed PTHREAD_KEYS_MAX. :)
> 
> -Peff
> 


Yes, a fixed global void* array should be fine.
Besides there aren't that many calls to pthread_key_create()  (My use in
Trace2 is the second, right?), so just create an arbitrary value for
PTHREAD_KEYS_MAX and be done.


Jeff

```

## Junio C Hamano, 2019-05-28 17:14

Subject: Re: [PATCH 0/1] trace2: fix tracing when NO_PTHREADS is defined
Message-ID: <xmqqv9xu33j6.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqv9xu33j6.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <97796007-db6e-f2ea-91ae-3113b74e4ae9@jeffhostetler.com>

```
Jeff Hostetler <git@jeffhostetler.com> writes:

> For now, I've guarded my usage of pthread_getspecific() in the trace2
> (similar to what index-pack does), so its not urgent that we update it.
> And I'd rather we take this simple trace2 fix now and not try to combine
> it with fixes for the pthread macros.  Especially now as we're in the RC
> cycle for 2.22.
>
> I'll make a note to revisit the pthread code after 2.22.

Thanks, both.  The above direction makes sense for me, too.


```
