www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2025/06/15/13:14:21

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: <de0e1b8a-5dba-46be-6b47-1214b523d5b9@jdrake.com>
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
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: Jeremy Drake via Cygwin <cygwin AT cygwin DOT com>
Reply-To: Jeremy Drake <cygwin AT jdrake DOT com>
Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com>

On Sat, 14 Jun 2025, Jeremy Drake via Cygwin wrote:

> Consider the following -std=c++20:
> #include <pthread.h>
>
> 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 <pthread.h>

#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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019