delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2026/09/19/23:35:59

DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 68K3ZwW3729118
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 68K3ZwW3729118
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=EQ5j3oZ5
X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EE8884BA902B
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1789875341;
bh=QqgmmOrLKZ82BpiL7CWuL7DdFe1h+P8OSfawWLipfnk=;
h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post:
List-Help:List-Subscribe:From:Reply-To:From;
b=EQ5j3oZ5cNM7kLtRe0jvM98h2DiUMYPrWVSyiiutaw5Ng+Z2A8VRLmHBjsCGzOBu3
hrgvKUUGXbHVSasXA9BN8dILirt3xwWgpXED/sNPPQdF2qNTTEhGoKCDrzvxEf8iEh
MGJC45oLeSUiI6v2ay5bh6VFlVudeSRWpcIMnQsM=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3BB8C4BA9028
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3BB8C4BA9028
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789875293; cv=none;
b=JrZMNvjvdQhOLVf2BDNCC0UIki6nZozhrVGv8GmNeY7uhxWxhrq4EI287A80avcIhPW3p5xK2qH5OFT8LaxweFZ/+D6vBymzgwNtWst0HqIHKWof8pgG81RtYVUe+R7pOo1jGvv35umCWep876PZirvNsIhW9VelhA18ED+dxT0=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
t=1789875293; c=relaxed/simple;
bh=TZde5pp/aLs0MqTStCdwOkJ9WNfIIoy8k/ILHF/6YWI=;
h=DKIM-Signature:Message-Id:From:To:Subject:Date:MIME-Version;
b=Nww2BvMhsJgJRXj4ahwvhHiz2k66nA8soyzjXSd+IR5KloikBmWWgmMJJizhbTACn1v9ubHepCyp9lyGcrvp+V0LYhtERsrAGj43nvIOW7OsoHqBhdDahVVXNXMzAmqQofxVqhHltCWzTo2R7V81oHqKf+8N1v8pTyMd2REzow0=
ARC-Authentication-Results: i=1; sourceware.org;
dkim=pass (2048-bit key, unprotected)
header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104
header.b=MeegWUk9
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3BB8C4BA9028
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1789875292; x=1790480092;
h=mime-version:user-agent:date:subject:to:from:message-id
:content-type:x-gm-gg:x-gm-message-state:from:to:cc:subject:date
:message-id:reply-to:content-type;
bh=+acruNVHBNRSG8HlKWo4BHSVfwvpiRQMCIWK9ZCfQ1Y=;
b=hHqpYZItoOk2z3zs1eZpvxbu/EIIgewpArQTNViqapwceFbBfgx+DBZjNE4lpNKuG6
3KTZI0ukOhFXY1062HQJryhvdh+M/zbk/7ImKRu7r3JhnNMRoNYokL7i1Fomo/5uLk16
ZdaD0b7Ovmc9kw9+v67WsWHRtBd9QnJj/f6Q6WseTSDp9CnJgN3IL/TLyXsH02fKsm56
S3ak98uSaFfVRvzJfxHzb1U7jCus5xARNnrKyYn5Kv8Y0zeftk8a4ZxumrZ0GBoGkFx1
DIam5AdlPZEQmVKdIrrcjrdHNj8jd4QMx3314t+sfM3cWGaAUstkpFnSQ83T3T2da9rs
220A==
X-Gm-Message-State: AFuF++mdNL4iQbCcvSHOX6ehzvGZiklP7XFwj08sfngDBguOBLYFfeKX
VmanCXFa+5uoJO2giKtHRj+Nh3FTRNbbHafOhSJ5rVDp3kAVjnwxhSszcxihcg==
X-Gm-Gg: AYBFou0SABPwMQGFx1f7DnjCdma2VQpZCr3YLCfWDLNTageG66vvrGnqTTCLeSwwuqv
nLRu9b3ZJSTca4rzHAC/PRmA8MROAcCrC3islLZIzR+6w/qnBaff+cLe8ms+oggh2d41bV9u6om
cXA1RE13iov6MbEXBBhuCMnptU5vjzR5Sra0F7b1Zxp9DihjeMA602Tlh8cart5gZEdHJNLcqDx
TcJahxUzUx2rf8g0b8moByM5FcX2zm2gvmUS7DvRHftUrUDkRbiqIHNv8Y9IUvyNO6uvliCPRss
fIotdotwvPno9t8JPWwHqwQZNQdlhx9/odU26zT06QqNNv3PVKBZom/0wKo6axSJXp6ofoNHaPN
hCqzzNLtvD/huUejjESh6Q1bZVmP8/8kYn0OvUBAnXO/GbXCMyvzLSWTOR5C73F6PiDk4ejAedQ
EE6SjbAlh77An6nzKuIbhQ5Qv9Fwe10GIXFk110nHC6W74705bacmZoZWnDW2NttdTZuCmRUH5R
20Vt7Y8kNYR
X-Received: by 2002:a05:6a00:2ea4:b0:878:3519:aff8 with SMTP id
d2e1a72fcca58-8783528fd86mr3137906b3a.55.1789875291978;
Sat, 19 Sep 2026 20:34:51 -0700 (PDT)
Message-Id: <1789871270892.1574718721.2143240182@gmail.com>
To: cygwin AT cygwin DOT com
Subject: Race during the first use of pthread_rwlock_{rd,wr}lock from
PTHREAD_RWLOCK_INITIALIZER
Date: Sun, 20 Sep 2026 03:34:48 +0000
X-Mailer: Vivaldi Mail
User-Agent: Vivaldi Mail/8.2.4133.52
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-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: kikairoya via Cygwin <cygwin AT cygwin DOT com>
Reply-To: kikairoya <kikairoya AT gmail DOT com>
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com>

------sinikael-?=_1-17898752886680.4958541757434958
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

Hello,

I've found a race during the first use of pthread_rwlock_{rd,wr}lock
(reproducer attached; requires -lsynchronization to build).

Concurrent calls to pthread_rwlock_{rd,wr}lock on a rwlock initialized
with PTHREAD_RWLOCK_INITIALIZER can cause multiple internal
initializations, resulting in some locks being lost.

It seems that pthread_rwlock::init (cygwin/thread.cc:2441) needs to
check whether the rwlock is still PTHREAD_RWLOCK_INITIALIZER while
holding rwlock_initialization_lock, as pthread_mutex::init does.

pthread_cond::init appears to need the same fix as well.

Regards,

-- 
Tomohiro Kashiwada (@kikairoya)

------sinikael-?=_1-17898752886680.4958541757434958
Content-Type: text/plain
Content-Disposition: attachment; filename=repro-rwlock-init.c
Content-Transfer-Encoding: quoted-printable

// gcc repro-rwlock-init.c -lsynchronization

#include <pthread.h>
#include <windows.h>
#include <assert.h>
#include <stdio.h>

#define USE_RWLOCK
//#define INIT_LOCK_EXPLICITLY

int fu =3D 0;
pthread_mutex_t mut =3D PTHREAD_MUTEX_INITIALIZER;
pthread_rwlock_t rwl =3D PTHREAD_RWLOCK_INITIALIZER;

void wait_for_start() {
    while (fu =3D=3D 0) {
        int l =3D 0;
        WaitOnAddress(&fu, &l, 4, INFINITE);
    }
}

void work() {
    pthread_t self =3D pthread_self();
    printf("thread %p: starting some work exclusively\n", self);
    Sleep(100);
    printf("thread %p: finish\n", self);
}

void *fn_mutex(void *) {
    wait_for_start();
    int ret;
    ret =3D pthread_mutex_lock(&mut);
    assert(ret =3D=3D 0 && "pthread_mutex_lock failed");

    work();

    ret =3D pthread_mutex_unlock(&mut);
    assert(ret =3D=3D 0 && "pthread_mutex_unlock failed");
    return NULL;
}

void *fn_rwlock(void *) {
    wait_for_start();
    int ret;
    ret =3D pthread_rwlock_wrlock(&rwl);
    assert(ret =3D=3D 0 && "pthread_rwlock_wrlock failed");

    work();

    ret =3D pthread_rwlock_unlock(&rwl);
    assert(ret =3D=3D 0 && "pthread_rwlock_unlock failed");
    return NULL;
}

#define NTHR 4
int main() {
    pthread_t thrs[NTHR] =3D {0};

#ifdef INIT_LOCK_EXPLICITLY
    pthread_mutex_init(&mut, NULL);
    pthread_rwlock_init(&rwl, NULL);
#endif

    for (int i =3D 0; i < NTHR; ++i) {
#ifdef USE_RWLOCK
        pthread_create(&thrs[i], NULL, &fn_rwlock, NULL);
#else
        pthread_create(&thrs[i], NULL, &fn_mutex, NULL);
#endif
    }

    fu =3D 1;
    WakeByAddressAll(&fu);

    for (int i =3D 0; i < NTHR; ++i) {
        void *r;
        pthread_join(thrs[i], &r);
    }
    return 0;
}

------sinikael-?=_1-17898752886680.4958541757434958
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


-- 
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

------sinikael-?=_1-17898752886680.4958541757434958--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2026   by DJ Delorie     Updated Sep 20 2026