From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 68149A00C5; Tue, 21 Jun 2022 20:51:06 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2A73C4069C; Tue, 21 Jun 2022 20:51:06 +0200 (CEST) Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mails.dpdk.org (Postfix) with ESMTP id 8782440151 for ; Tue, 21 Jun 2022 20:51:04 +0200 (CEST) Received: by linux.microsoft.com (Postfix, from userid 1086) id B370020C5A64; Tue, 21 Jun 2022 11:51:03 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com B370020C5A64 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1655837463; bh=PeVlj0+otaKjqwmaVIlQW3pBIdnqxJ0G5rG+/bwYCjg=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Pp0aUPRPhmxfEaxBOZ0LjJBAI7TXd3X388GBZO3STXCvUWm+PKEGk5XbdpEsuoS4D 3ZcJ2ZhUEa5/TT+0trlHoVQwJGguz3adDRHbuQ1R1sUK1qWwX6T7cmkINmA71MMK8x fZ05SUxTc6FMOaHeEkiqNHmHKFaA0aqLLnpBUvEY= Date: Tue, 21 Jun 2022 11:51:03 -0700 From: Tyler Retzlaff To: Dmitry Kozlyuk Cc: dev@dpdk.org, thomas@monjalon.net, anatoly.burakov@intel.com, Narcisa Vasile Subject: Re: [PATCH v2 2/6] eal: add thread lifetime management Message-ID: <20220621185103.GC18214@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net> References: <1654783134-13303-1-git-send-email-roretzla@linux.microsoft.com> <1655250438-18044-1-git-send-email-roretzla@linux.microsoft.com> <1655250438-18044-3-git-send-email-roretzla@linux.microsoft.com> <20220618155908.70e822af@sovereign> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220618155908.70e822af@sovereign> User-Agent: Mutt/1.5.21 (2010-09-15) X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org 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.