[PATCH v2 2/6] eal: add thread lifetime management

Dmitry Kozlyuk dmitry.kozliuk at gmail.com
Wed Jun 22 00:24:14 CEST 2022


2022-06-21 14:28 (UTC-0700), Tyler Retzlaff:
> On Tue, Jun 21, 2022 at 10:44:21PM +0300, Dmitry Kozlyuk wrote:
> > 2022-06-21 11:51 (UTC-0700), Tyler Retzlaff:  
> > > > > +int
> > > > > +rte_thread_join(rte_thread_t thread_id, unsigned long *value_ptr)
> > > > > +{
> > > > > +	int ret = 0;
> > > > > +	void *res = NULL;
> > > > > +	void **pres = NULL;
> > > > > +
> > > > > +	if (value_ptr != NULL)
> > > > > +		pres = &res;
> > > > > +
> > > > > +	ret = pthread_join((pthread_t)thread_id.opaque_id, pres);
> > > > > +	if (ret != 0) {
> > > > > +		RTE_LOG(DEBUG, EAL, "pthread_join failed\n");
> > > > > +		return ret;
> > > > > +	}
> > > > > +
> > > > > +	if (value_ptr != NULL && *pres != NULL)
> > > > > +		*value_ptr = *(unsigned long *)(*pres);
> > > > > +
> > > > > +	return 0;
> > > > > +}    
> > > > 
> > > > What makes *pres == NULL special?    
> > > 
> > > it's not clear what you mean, can you explain? maybe there is some
> > > context i am missing from the original patch series?  
> > 
> > There's no previous context.
> > After ptread_join(), *pres holds the return value of the thread routine.
> > You only assign *value_ptr if value_ptr is not NULL (obviously correct)
> > and if *pres != NULL, that is, if the thread returned a non-NULL value.
> > But this value is opaque, why do you filter NULL?  
> 
> i don't think it is opaque here? unsigned long * value_ptr says we have
> to store an integer. which leads to a discussion of what should get
> stored at the value_ptr location if pthread_join() itself returns no
> result but the caller of rte_thread_join() requests the result.

There is no question. If `pthread_join()` fails, the function exits early
and `*value_ptr` remains unmodified. If `pthread_join()` succeeds
with a non-NULL second argument (`pres`), `*pres` aka `res` is always filled.
NULL can be placed there too if that's what the thread routine has returned.

> 
> > Perhaps you meant if (pres != NULL), no dereference?  
> 
> that i think is just a repeat of a test checking if the caller of
> rte_thread_join is interested in the result?
> i.e. value_ptr != NULL -> pres != NULL
> 
> both pres and *pres are dereferenced so it seems to track that prior to
> those dereferences they have to be validated as being non-NULL.
> i don't see how we could avoid dereferencing **pres to satisfy the
> calling contract when the result is requested.
> 
> now if value_ptr was unsigned long ** i guess i'd understand. i could
> always be reading the code wrong. but thinking about further there is
> another problem with this in that we really don't know what is being
> aliased in *pres when using the pthread implementation, since pthread
> could be returning a pointer to something narrow or with unknown layout
> where later dereferencing it as something wider or in this case
> specifically as unsigned long * would have horrible consequences.

Sorry, no, it's just all wrong.
We get a pointer from `pthread_join()` and store it in `res`,
which can also be accessed as `*pres`, because `pres == &res`.
Then we store the lower bits of `res` in an `*value_ptr`.
We don't care what `res` points to because it is never dereferenced.

> 
> i think this ends up semi-related to your other comment about what the
> result type from rte_thread_func is, we can discuss offline and post
> details back to the list.


More information about the dev mailing list