mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] Suggestion for thread safety
@ 2022-02-21 11:36 Lee Shallis
  2022-02-21 17:42 ` Markus Wichmann
  2022-02-23  1:19 ` Rich Felker
  0 siblings, 2 replies; 16+ messages in thread
From: Lee Shallis @ 2022-02-21 11:36 UTC (permalink / raw)
  To: musl

First I'll start with the code snippets I've copied from my own code:

/* 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 const char* LOCK;
typedef struct _GRIP { LOCK _; LOCK *_lock; struct _GRIP *_grip; } GRIP;
BASIC void    LockStdErr( LOCK *ud );
BASIC void    LockSysErr( LOCK *ud );
BASIC void    LockSiData( LOCK **shared, LOCK *ud );
BASIC void    LockMiData( GRIP *shared, GRIP *ud );
BASIC dint    FreeStdErr( LOCK *ud );
BASIC dint    FreeSysErr( LOCK *ud );
BASIC dint    FreeSiData( LOCK **shared, LOCK *ud );
BASIC void    FreeMiData( GRIP *shared, GRIP *ud );

#define LockErrors( ud ) LockStdErr( ud ); LockSysErr( ud )
#define FreeErrors( ud ) FreeSysErr( ud ); FreeStdErr( ud )

#define DO_LOCK( X ) { LOCK _lock = ""; X }
#define DO_GRIP( X ) { GRIP _grip = {NULL}; X }

#define LOCK_SIDATA( SID, X ) \
    DO_LOCK( LockSiData( SID, &_lock ); X *SID = NULL; )

#define LOCK_MIDATA( MID, X ) \
    DO_GRIP( LockMiData( MID, &_grip ); X FreeMiData( MID, &_grip ); )

#define LOCK_STDERR( X ) \
    DO_LOCK( LockStdErr( &_lock ); X FreeStdErr( &_lock ); )

#define LOCK_SYSERR( X ) \
    DO_LOCK( LockSysErr( &_lock ); X FreeSysErr( &_lock ); )

#define LOCK_ERRORS( X ) \
    DO_LOCK( LockErrors( &_lock ); X FreeErrors( &_lock ); )

#define LOCK_SIDATA_AND_ERRORS( SID, X ) \
    DO_LOCK \
    ( \
        LockSiData( SID, &_lock ); \
        LockErrors( &_lock ); \
        X \
        FreeErrors( &_lock ); \
        *SID = NULL; \
    )
...
SHARED_EXP void NoPause() {}

LOCK *stderr_lock = NULL;
LOCK *syserr_lock = NULL;
allot_cb AllotCB = Allot;
pause_cb pauseCB = NoPause;

void LockStdErr( LOCK *ud ) { LockSiData( &stderr_lock, ud ); }
dint FreeStdErr( LOCK *ud ) { return FreeSiData( &stderr_lock, ud ); }

void LockSysErr( LOCK *ud ) { LockSiData( &syserr_lock, ud ); }
dint FreeSysErr( LOCK *ud ) { return FreeSiData( &syserr_lock, ud ); }
...
SHARED_EXP dint    Allot( void *ud, void **data, size_t size )
{
    (void)ud;
    if ( size )
    {
        dint err;
        LOCK_ERRORS
        (
            errno = 0;
            *data = *data ? realloc( *data, size ) : malloc( size );
            err = *data ? 0 : errno;
        );
        return err;
    }
    else if ( *data )
    {
        free( *data );
        *data = NULL;
    }
    return 0;
}
SHARED_EXP void LockSiData( LOCK **shared, LOCK *ptr )
{
    while ( *shared != ptr )
    {
        if ( !(*shared) )
            *shared = ptr;
        pauseCB( ptr );
    }
}

SHARED_EXP dint FreeSiData( LOCK **shared, LOCK *ud )
{
    if ( *shared == ud )
    {
        *shared = NULL;
        return 0;
    }
    return EPERM;
}
/* These 2 are untested */
SHARED_EXP void LockMiData( GRIP *shared, GRIP *ud )
{
    while ( shared->_grip != ud )
    {
        while ( shared->_grip )
            shared = shared->_grip;
        shared->_grip = ud;
        pauseCB();
    }
}
SHARED_EXP void    FreeMiData( GRIP *shared, GRIP *ud )
{
    LOCK _ = NULL;
    LockSiData( &(shared->_lock), &_ );
    LockSiData( &(ud->_lock), &_ );
    while ( shared->_grip && shared->_grip != ud )
        shared = shared->_grip;
    if ( shared->_grip == ud )
    {
        shared->_grip = ud->_grip;
        ud->_grip = NULL;
    }
    (void)FreeSiData( &(ud->_lock), &_ );
    (void)FreeSiData( &(shared->_lock), &_ );
}

Take what you will of the above, the Allot function was included just
to give you an idea of how the locks are made, as for pauseCB that
should be re-mapped to a variant that calls something like
pthread_yield(), it's what I tested with, anyways the general idea is
that the shared lock defaults to NULL when not locked, when you want
to lock it you 1st wait for it to be NULL then as soon as it's empty
you fill it with your own pointer then wait for other threads who
detected the same situation to fill there pointers into the shared
pointer (assuming any other threads were trying to lock the pointer),
when the wait is over you check again if the pointer is not matching
your own, if not then you failed to lock and should wait longer, using
pointers lends itself well to threading as the pointer being used to
take the shared pointer will normally be unique to the thread (because
rarely will anyone try to use the same pointer in multiple threads for
this process that has a clear example in it's declaration), using
const char * for the LOCK typedef means a thread can give more
information about itself or the pointer it locked with if it so
chooses.

GRIP on the other hand is meant for multi-threaded data, for example
assigning parts of a buffer, the owning thread should fill in the '_'
member after acquiring the grip, if the previous grip doesn't have
info about the section of the buffer it took then it should wait until
it does, when it does it is then free to take it's own chunk after
analysing what has already been taken by other grips linked to the
shared grip, this method would lend itself well to graphics generation
for example as a thread for each column could be launched in ascending
order (so that the row contents are process from left to right), at
least that's the theory on the grip one, requires testing which I'm
sure you can do better than I can at the moment.

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

end of thread, other threads:[~2022-03-02  1:49 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-21 11:36 [musl] Suggestion for thread safety 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
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

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