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=-3.1 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 23523 invoked from network); 27 Feb 2022 23:37:24 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 27 Feb 2022 23:37:24 -0000 Received: (qmail 20294 invoked by uid 550); 27 Feb 2022 23:37:22 -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 20259 invoked from network); 27 Feb 2022 23:37:21 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:reply-to:from:date:message-id :subject:to; bh=DgPj+pcgq/ETWDGC+Xg8F9SrmO7pq79AUWrlfSqEXDM=; b=SdGR4WmU5wiHaa+1DDkasTqV/FJsZHXOZVKJVjGxDGytauVAQPNyYYC3PWB3O81aYb l426WbXvrA13mFkT/61+wthV7HXtQAeLOZo3GqA9jvNvc/jgK3rEknrCIr5SL6twHAT2 GdcTmn9JIIr8A+3M/Ks5rqgpKrhsH3ummkGl/ctQuIGpls9AFS+DLttha6pwyeYEJUy3 S2idmv0QweIL9TXwsscdIUyUWaW7LsM3ZDPlpfBonO+xroFjEWuxiKHDtC4L69pnJiro Z9CG8I1Rb6q3EeI3NcXrXWvEjZcN9NPOaeDZER+qKg194Z68rmTkyuJtKs2NfOHbNRdx eE7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:reply-to :from:date:message-id:subject:to; bh=DgPj+pcgq/ETWDGC+Xg8F9SrmO7pq79AUWrlfSqEXDM=; b=XdyBm3K80ZzPBU955Q9c73QpEs4qyYdVNN1ubW+EclZofi1msmUYZh5xssKc2HYUK7 0Qd6JDZiYlLXJq56i/+4liYP8PQx+0SxjmPE9E6MK0phTbzHBmm3gPfLLIqtgNECnLNT ccVah28l5TVkh/Xk6kiOZbAgnHh+RbuBghkDgAGRZV417xzh/YjQ027WHqA+VaE01BtN dTkwqPySidLuLUWzB8wkSi3Qk91PMe7q3QmufXct62TOQSeBiSH8qqUJEYMZQnSXfBjc E+Hts+DLFDFEuoAg70FispH9mvAPvawA1Wd7kUfHgqxTm7GyHiDY/qABRWpPIGGvSBAW snzQ== X-Gm-Message-State: AOAM532O9bBsHpqiv5puFncSViIMjYMcSYoCvzgSQrrlHfwjPFHOjuuV q8kDtgN+QwYaLXZYE7HUGNl+TBAHIpvpYPJqDLT2KZeIQ5M= X-Google-Smtp-Source: ABdhPJyl0heX4R3KesathjO7q9aOkWYsV/PHvRf9vAFb649et43J11HP0CBIAVRAqRSIhcTg3UjF1Nnlds1w1t/vmow= X-Received: by 2002:ab0:2351:0:b0:347:2121:547a with SMTP id h17-20020ab02351000000b003472121547amr742833uao.15.1646005029357; Sun, 27 Feb 2022 15:37:09 -0800 (PST) MIME-Version: 1.0 References: <20220221174223.GA2079@voyager> <20220223185746.GB2079@voyager> <20220226123855.392c22acb208a966210c7af2@zhasha.com> In-Reply-To: <20220226123855.392c22acb208a966210c7af2@zhasha.com> From: Lee Shallis Date: Sun, 27 Feb 2022 23:32:47 +0000 Message-ID: To: musl@lists.openwall.com Content-Type: text/plain; charset="UTF-8" Subject: Re: [musl] Suggestion for thread safety 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 #include 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 wrote: > > On Sat, 26 Feb 2022 09:56:04 +0000, Lee Shallis wrote: > > On Wed, 23 Feb 2022 at 18:58, Markus Wichmann 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