mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Spurious wake up in musl
@ 2022-07-02 16:38 William Tang
  2022-07-02 17:56 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: William Tang @ 2022-07-02 16:38 UTC (permalink / raw)
  To: 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 <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <pthread.h>

#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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [musl] Spurious wake up in musl
  2022-07-02 16:38 [musl] Spurious wake up in musl William Tang
@ 2022-07-02 17:56 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2022-07-02 17:56 UTC (permalink / raw)
  To: William Tang; +Cc: musl

On Sun, Jul 03, 2022 at 12:38:05AM +0800, William Tang wrote:
> Hi,
> 
> According to the about page, the spurious wake up should not be possible:

That is not a correct reading. Spurious wakes are always 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:
> [...]
> 
> 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!

These aren't contradictory. The guarantee is that wakes won't be
missed due to a newly-arriving waiter stealing one that should have
been seen by an already-present waiter. You seem to be reading that in
a sort of converse-direction as implying a waiter won't wake
spuriously -- maybe assuming a spurious wake would be stealing the
wake from another waiter, but that's not what's happening. Spuriou
wakes are an inherent part of using condvars, and any logic that
depends on them not happening is missing the point of how condvars
work and how they're supposed to be used.

Rich

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-07-02 17:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-02 16:38 [musl] Spurious wake up in musl William Tang
2022-07-02 17:56 ` Rich Felker

Code repositories for project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).