From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 32475 invoked from network); 2 Jul 2022 16:38:34 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 2 Jul 2022 16:38:34 -0000 Received: (qmail 5772 invoked by uid 550); 2 Jul 2022 16:38:30 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 5733 invoked from network); 2 Jul 2022 16:38:30 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=PHd5OYBbqn3Y5x4mfHQL9uHVBSBG5gdaLcutLoh8ELE=; b=k7pf3l1OmbiM/EqePyS3O5Zn+rMOWvUqYjJvF7duWiF0cLLxFI2JOoyjosYlcdF4/Y Yxlw60kIZHZFd0kfPJHSVQlwE1xkL2dcqjtsWQDqY1xqNIFX9fSoGeRpukMvXeMobQNk RjOQ9VsFFexP4mzk1XGFF3l+QeevJzCAujd3g+nfakzJYBpI4i2vowss4meSbqu7sl+w ezMtlaZDqNWAQZXzO+oD2m/nwl7w1t0xHffj7bGmf/hhswainmObaxuge4GgxMVrfPVf jruYB/Hah1s7FqRxvvehEVjkSgU7aNDmnlTh7iMA5yF6VO/O6VHlm2SIhqo24vNKWoLt uoPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=PHd5OYBbqn3Y5x4mfHQL9uHVBSBG5gdaLcutLoh8ELE=; b=jXHeDsGx7Nx4v/7hCfS34ZbKNTzp0tquWU+x08E/KC7Jc5yCb1RgKz6irAWHJ0c5sq q7kd0Yy9G91PLlMnhhpGPT3c2DsHq3idfQ52Hhm3qGrR2bLWc6HtBODSul/ziqykezkQ L1NkSlhudBkEWWGFxP8Qp+raJmuOV7VjekaYwMUx3syRJmqilTNddLyE+iJdc4XLbxJ3 71u2/ViHuWqaMWVlcAcVCmynchU1u/4u4gu97iSpH8tEwffShTKnnu0KGqZaSnBlj97m GESwPubzmgD4olgEjMtjkiHspDUz5UxGJsy2FGTBg8rASJQX+PR7kFLA7d0Dbaos2Fai mssg== X-Gm-Message-State: AJIora/rUYKrNmDsSb0o5u9M9oK/7npKpJ0jlL1S9LavInR9cr4imIZg zpvAZt8ByA+m/QlQhsHI/Ecl4yfnt/c/mYEpava9+DimcrI= X-Google-Smtp-Source: AGRyM1toLa+nbieELiUfATuO0UEnAe1qwx58lT++/D0uSGfhFdIoPBeYafP6F0OQWY+/dLtfIA6zhUBo4ILb47EcVRk= X-Received: by 2002:a17:90a:d56:b0:1ef:68e8:2b7c with SMTP id 22-20020a17090a0d5600b001ef68e82b7cmr5604246pju.12.1656779897852; Sat, 02 Jul 2022 09:38:17 -0700 (PDT) MIME-Version: 1.0 From: William Tang Date: Sun, 3 Jul 2022 00:38:05 +0800 Message-ID: To: musl@lists.openwall.com Content-Type: text/plain; charset="UTF-8" Subject: [musl] Spurious wake up in musl Hi, According to the about page, the spurious wake up should not be possible: musl was the first Linux libc to have ..., the first to have condvars where newly-arrived waiters can't steal wake events from previous waiters However, when I use the following code to test spurious wake up: #include #include #include #include #include #define COUNT_MAX 1000000 static int64_t counter = 0; static pthread_cond_t cond_var = PTHREAD_COND_INITIALIZER; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static void *thread_func(void *arg) { while (true) { pthread_mutex_lock(&mutex); if (counter <= 0) pthread_cond_wait(&cond_var, &mutex); if (counter <= 0) { printf("[worker %p] Spurious wakeup occurred! Counter is %lld!\n", pthread_self(), counter); exit(0); } if (--counter == 0) printf("[worker %p] No more work need to be done!\n", pthread_self()); pthread_mutex_unlock(&mutex); } } int main() { pthread_t thread_1, thread_2; pthread_create(&thread_1, NULL, thread_func, NULL); pthread_create(&thread_2, NULL, thread_func, NULL); printf("[main] Started working threads: %p, %p\n", thread_1, thread_2); for (size_t i = 0; i < COUNT_MAX; ++i) { pthread_mutex_lock(&mutex); ++counter; pthread_mutex_unlock(&mutex); pthread_cond_signal(&cond_var); } printf("Finished counting to %u\n", COUNT_MAX); pthread_join(thread_1, NULL); pthread_join(thread_2, NULL); return 0; } And compile with command "musl-gcc -static main.c", it outputs: [main] Started working threads: 0x7efc7f212f38, 0x7efc7f1eff38 [worker 0x7efc7f212f38] No more work need to be done! [worker 0x7efc7f1eff38] No more work need to be done! [worker 0x7efc7f1eff38] No more work need to be done! [worker 0x7efc7f1eff38] No more work need to be done! [worker 0x7efc7f212f38] Spurious wakeup occurred! Counter is 0! William