On Sat, Jun 18, 2022 at 03:59:08PM +0300, Dmitry Kozlyuk wrote: > 2022-06-14 16:47 (UTC-0700), Tyler Retzlaff: > > On Windows, the function executed by a thread when the thread starts is > > represeneted by a function pointer of type DWORD (*func) (void*). > > On other platforms, the function pointer is a void* (*func) (void*). > > > > Performing a cast between these two types of function pointers to > > uniformize the API on all platforms may result in undefined behavior. > > TO fix this issue, a wrapper that respects the signature required by > > CreateThread() has been created on Windows. > > The interface issue is still there: > `rte_thread_func` allows the thread routine to have a pointer-sized result. > `rte_thread_join()` allows to obtain this value as `unsigned long`, > which is pointer-sized on 32-bit platforms > and less than that on 64-bit platforms. > This can lead to issues when developers assume they can return a pointer > and this works on 32 bits, but doesn't work on 64 bits. > If you want to keep API as close to phtread as possible, > the limitation must be at least clearly documented in Doxygen > (`rte_thread_func` is undocumented BTW). > I also suggest using `uint32_t` instead of `unsigned long` > precisely to avoid "is it pointer-sized?" doubts. > (I don't see much benefit in keeping pthread-like signature. > When moving from pthread to rte_thread, > it is as trivial to change the thread routine return type.)
i'll alter the rte api to use fixed width uint32_t for thread result. it seems like an unnecessary feature to return a pointer and it can be accomplished through void *arg if necessary. > > > +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? > > +int > > +rte_thread_create(rte_thread_t *thread_id, > > + const rte_thread_attr_t *thread_attr, > > + rte_thread_func thread_func, void *args) > > +{ > > + int ret = 0; > > + DWORD tid; > > + HANDLE thread_handle = NULL; > > + GROUP_AFFINITY thread_affinity; > > + struct thread_routine_ctx *ctx = NULL; > > + > > + ctx = calloc(1, sizeof(*ctx)); > > + if (ctx == NULL) { > > + RTE_LOG(DEBUG, EAL, "Insufficient memory for thread context > > allocations\n"); > > + ret = ENOMEM; > > + goto cleanup; > > + } > > + ctx->routine_args = args; > > + ctx->thread_func = thread_func; > > + > > + thread_handle = CreateThread(NULL, 0, thread_func_wrapper, ctx, > > + CREATE_SUSPENDED, &tid); > > + if (thread_handle == NULL) { > > + ret = thread_log_last_error("CreateThread()"); > > + free(ctx); > > + goto cleanup; > > Missing `free(ctx)` from other error paths below. beyond this point free(ctx) will happen in thread_func_wrapper. i will add a comment to make it clear. thanks.