DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 55FHEKEV3184136 Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 55FHEKEV3184136 Authentication-Results: delorie.com; dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=nHQPKQm6 X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 823993969166 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1750007658; bh=UB20AsKbJGqWMi9gBImo33+7ZdTxOHpr/qBpmJJjV/Q=; h=Date:To:Subject:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=nHQPKQm6SD8sovZ1SMWSwh32DTXlk0haJjY0Oda3THTaC0Je+8sm1LKshoPAm6oRY sphqRcpLkaGTmxYeYsh9A1JZpKQgr67mCadepcZuYzwIhCtrqQJ1Bcug018KbKnpqZ G+MVL+/uxt1dPEHabK/ZJZ0mQN9eUZfbpEpC738c= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C11533854A9A ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C11533854A9A ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750007589; cv=none; b=elu914zrz4LkIu/g0tEUmeY9Oj1++hHmZUUYaH4W5NPJzv1NlQzVNEtBSe2yIQy+ARKZa+2hrNCoPO3kGn1sF0UKYPumaPpjg0TyBb6GfqJSc0kjJB5T74KhydMgpIUSX6+4AWaMdDOLQ72n7EOxcmYe7LauNkr2fJX0EQl3F3Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1750007589; c=relaxed/simple; bh=nNwg3Lt9+Rx3UX9VtIAWJ5OOOKFIxNl7fbOegUj/qlE=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=Wb+7FruQsl30/Tv53TRlMS7ybGIRbDw/wjlcjGHQxI5UATBqGN1mYMvCKI0d/wCCBdfsQg6Q3Mu7eXbGS1HaKtfvrDkC/nESXvF5ExETadE+qkowpg7PvhybRszE5QOXVZARz0ta3aKkepmS3nXc7Th1fru+q7+8whCgqTko1BE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C11533854A9A Date: Sun, 15 Jun 2025 10:13:09 -0700 (PDT) X-X-Sender: jeremyd AT resin DOT csoft DOT net To: cygwin AT cygwin DOT com Subject: Re: PTHREAD_MUTEX_INITIALIZER and constinit In-Reply-To: <87a87123-9ecb-ac15-ddb4-4cdfdf9faee4@jdrake.com> Message-ID: References: <87a87123-9ecb-ac15-ddb4-4cdfdf9faee4 AT jdrake DOT com> MIME-Version: 1.0 X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Jeremy Drake via Cygwin Reply-To: Jeremy Drake Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" On Sat, 14 Jun 2025, Jeremy Drake via Cygwin wrote: > Consider the following -std=c++20: > #include > > constinit pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER; > > With g++ 12.4.0, this succeeds, but with clang 20.1.6 it fails: > pthreadtest.cpp:3:27: error: variable does not have a constant initializer > 3 | constinit pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER; > | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~ > pthreadtest.cpp:3:1: note: required by 'constinit' specifier here > 3 | constinit pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER; > | ^~~~~~~~~ > pthreadtest.cpp:3:33: note: cast that performs the conversions of a reinterpret_cast is not allowed in a constant expression > 3 | constinit pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER; > | ^ > /usr/include/pthread.h:49:35: note: expanded from macro 'PTHREAD_MUTEX_INITIALIZER' > 49 | #define PTHREAD_MUTEX_INITIALIZER PTHREAD_NORMAL_MUTEX_INITIALIZER_NP > | ^ > /usr/include/pthread.h:47:45: note: expanded from macro 'PTHREAD_NORMAL_MUTEX_INITIALIZER_NP' > 47 | #define PTHREAD_NORMAL_MUTEX_INITIALIZER_NP (pthread_mutex_t)19 > | ^ > 1 error generated. > > It seems the standard disallows this sort of cast pretty comprehensively. > Is there some way that Cygwin's pthread initializer macros can be made > compatible with constinit? I was thinking changing the typedef to be uintptr_t instead of a pointer would allow constinit initialization to a constant integer, and be the same ABI-wise, but it would probably change C++ mangled names for functions taking those types as parameters. What did work for me, and I'll admit it's pretty gross, is #include #ifdef __i386__ #define SYMPREFIX "_" #else #define SYMPREFIX "" #endif __asm__( SYMPREFIX "__pthread_normal_mutex_initializer_np=19"); extern "C" struct __pthread_mutex_t __pthread_normal_mutex_initializer_np; #undef PTHREAD_NORMAL_MUTEX_INITIALIZER_NP #define PTHREAD_NORMAL_MUTEX_INITIALIZER_NP (&__pthread_normal_mutex_initializer_np) constinit pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER; Basically, hiding the value from the compiler and making it think it is in fact dealing with a real pointer. -- Problem reports: https://cygwin.com/problems.html FAQ: https://cygwin.com/faq/ Documentation: https://cygwin.com/docs.html Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple