mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Lee Shallis <gb2985@gmail.com>
To: musl@lists.openwall.com
Subject: Re: [musl] Suggestion for thread safety
Date: Sun, 27 Feb 2022 23:32:47 +0000	[thread overview]
Message-ID: <CAOZ3c1p8A9UraG2gUuzXijgzPFtPjgMX8Yom41+Wet08Uw6ifg@mail.gmail.com> (raw)
In-Reply-To: <20220226123855.392c22acb208a966210c7af2@zhasha.com>

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:

#ifdef _WIN32
typedef DWORD WHICH;
#else
#include <dlfcn.h>
#include <pthread.h>
typedef pid_t WHICH;
#endif

/* Which Thread Id */
BASIC WHICH    Which();
/* Pause thread execution to yield resources */
BASIC void    Pause();

/* The error locks only work for conforming code, anything that doesn't will
 * corrupt the result if it tries to do something that would need them */
typedef struct _LOCK LOCK;
typedef struct _GRIP GRIP;
struct _LOCK { uint num; WHICH tid; };
struct _GRIP { uint num; WHICH tid; GRIP *next, *prev; void *ud; };
...
#ifdef _WIN32
SHARED_EXP WHICH    Which() { return GetCurrentThreadId(); }
SHARED_EXP void        Pause() { SwitchToThread(); }
#else
SHARED_EXP WHICH    Which() { return gettid(); }
SHARED_EXP void        Pause() { pthread_yield(); }
#endif
...
SHARED_EXP void LockSiData( LOCK *shared )
{
    WHICH tid = Which();
    while ( shared->tid != tid )
    {
        if ( !(shared->tid) )
            shared->tid = tid;
        Pause();
    }
    shared->num++;
}

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

I'm not a fan of plugging in APIs directly but in this case I
eventually decided I ought to make an exception like I did with dlopen
etc, the name NoPause() was supposed to clue you in that to switch to
threading you needed to change what pauseCB pointed to, I guess that
wasn't clear enough, anyways whether you stick to the original code or
dump my (copy-pasted) code from my library into the test and edit
whatever you deem necessary for a valid test in your eyes, as for why
NoPause was even necessary, it's because I'd planned on documenting
the library to say that by default it's only single threaded mode
compatible but with a simple change of callback from NoPause to a
developer wrapper for the equivalent of pthread_yield() it would
become multi-threaded safe. I switched to the object with a reference
count because I kept making the mistake of not thinking through how I
used it well enough causing me to eventually decide the method wasn't
complex enough under the hood, for the malloc example I gave before it
can be extended to support thread specific errno, all it takes is page
locks when connecting pages together, memory locks when taking a
section of said pages & then some #ifdef code that switches between:

a LOCK_ERRORS( errno = err; ); statement &
a plain errno = err; statement

Either way the function can be programmed the same right up until that
point (unless there's some way to detect in code which is suitable)

On Sat, 26 Feb 2022 at 11:39, Joakim Sindholt <opensource@zhasha.com> wrote:
>
> On Sat, 26 Feb 2022 09:56:04 +0000, Lee Shallis <gb2985@gmail.com> wrote:
> > On Wed, 23 Feb 2022 at 18:58, Markus Wichmann <nullplan@gmx.net> wrote:
> > >
> > > On Wed, Feb 23, 2022 at 12:30:43AM +0000, Lee Shallis wrote:
> > > > think of the lock as the same as a mutex, just simpler,
> > >
> > > It isn't really simpler than a fast mutex, but a lot buggier.
> > >
> > There are no bugs, I made sure to test this after all, I deliberately
> > created a situation where any bugs would show them selves, the only
> > thing that was a potential problem I've now fixed (after doing some
> > research to find out if 0 is ever a valid thread id), the concept
> > remains the same, just with support for developer calling LockSiData
> > twice:
>
> Did I use it wrong then?
>
> zhasha@wirbelwind /home/zhasha ; gcc -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 = 2, expected 1
> zhasha@wirbelwind /home/zhasha ; ./a.out
> var = 2, expected 1
> var = 1, expected 0
> zhasha@wirbelwind /home/zhasha ; ./a.out
> var = 1, expected 0

  reply	other threads:[~2022-02-27 23:37 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 [this message]
2022-02-28  0:15             ` Rich Felker
2022-02-28  8:48             ` Joakim Sindholt
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=CAOZ3c1p8A9UraG2gUuzXijgzPFtPjgMX8Yom41+Wet08Uw6ifg@mail.gmail.com \
    --to=gb2985@gmail.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).