mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Joakim Sindholt <opensource@zhasha.com>
To: musl@lists.openwall.com
Subject: Re: [musl] Suggestion for thread safety
Date: Mon, 28 Feb 2022 09:48:14 +0100	[thread overview]
Message-ID: <20220228094814.be34ddcf7be25724e8f8c21b@zhasha.com> (raw)
In-Reply-To: <CAOZ3c1p8A9UraG2gUuzXijgzPFtPjgMX8Yom41+Wet08Uw6ifg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 906 bytes --]

On Sun, 27 Feb 2022 23:32:47 +0000, Lee Shallis <gb2985@gmail.com> wrote:
> Yes, as I mentioned before, pauseCB is supposed to have it's pointer
> be changed by the developer, in other words you forgot to plugin a
> pthreads compatible call prior to your threads starting, considering
> you made that mistake I suppose it is a good thing I since switched to
> a non-redirectable pointer:

How many times do you want to do this?

zhasha@wirbelwind /home/zhasha ; gcc -D_GNU_SOURCE -lpthread buglock.c
zhasha@wirbelwind /home/zhasha ; ./a.out
var = 1, expected 0
zhasha@wirbelwind /home/zhasha ; ./a.out
var = 1, expected 0
zhasha@wirbelwind /home/zhasha ; ./a.out
var = 1, expected 0
zhasha@wirbelwind /home/zhasha ; ./a.out
var = 1, expected 0
zhasha@wirbelwind /home/zhasha ; ./a.out
var = 1, expected 0
var = 2, expected 1
zhasha@wirbelwind /home/zhasha ; ./a.out
var = 1, expected 0
var = 2, expected 1

[-- Attachment #2: buglock.c --]
[-- Type: text/plain, Size: 1475 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdint.h>
#include <errno.h>
#include <unistd.h>

typedef int dint;
typedef pid_t WHICH;
typedef struct _LOCK LOCK;
struct _LOCK { uint num; WHICH tid; };

WHICH Which() { return gettid(); }
void Pause() { pthread_yield(); }

void LockSiData( LOCK *shared )
{
    WHICH tid = Which();
    while ( shared->tid != tid )
    {
        if ( !(shared->tid) )
            shared->tid = tid;
        Pause();
    }
    shared->num++;
}

dint FreeSiData( LOCK *shared )
{
    if ( shared->tid == Which() )
    {
        shared->num--;
        if ( !(shared->num) )
            shared->tid = (WHICH)0;
        return 0;
    }
    return EACCES;
}

static LOCK l;
static volatile int var = 0;

static void *thread(void *arg)
{
    int i;
    while (1) {
        LockSiData(&l);
        i = var++;
        if (i != 0) {
            printf("var = %d, expected 0\n", i);
            exit(1);
        }
        clock_nanosleep(CLOCK_MONOTONIC, 0, &(struct timespec){.tv_sec = 0, .tv_nsec = 1}, 0);
        i = var--;
        if (i != 1) {
            printf("var = %d, expected 1\n", i);
            exit(1);
        }
        FreeSiData(&l);
    }
    return 0;
}

int main(void)
{
    pthread_t pt;
    int i;

    for (i = 0; i < 2; i++) {
        if ((errno = pthread_create(&pt, 0, thread, 0)) != 0) {
            printf("pthread_create failed: %m\n");
            return 1;
        }
    }
    pthread_exit(0);
}

  parent reply	other threads:[~2022-02-28  8:48 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-21 11:36 Lee Shallis
2022-02-21 17:42 ` Markus Wichmann
2022-02-23  0:30   ` Lee Shallis
2022-02-23 18:57     ` Markus Wichmann
2022-02-23 20:06       ` Rich Felker
2022-02-26  9:56       ` Lee Shallis
2022-02-26 11:38         ` Joakim Sindholt
2022-02-27 23:32           ` Lee Shallis
2022-02-28  0:15             ` Rich Felker
2022-02-28  8:48             ` Joakim Sindholt [this message]
2022-02-28 14:43               ` Lee Shallis
2022-02-28 15:19                 ` Rich Felker
2022-02-28 15:50                 ` Joakim Sindholt
2022-02-28 16:07                   ` Lee Shallis
2022-03-02  1:44                     ` Lee Shallis
2022-02-23  1:19 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220228094814.be34ddcf7be25724e8f8c21b@zhasha.com \
    --to=opensource@zhasha.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public 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).