Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-10 Thread LIU Hao

在 2026-9-10 21:10, Kirill Makurin 写道:

+static int WinpthreadsNormalMutexInit (WinpthreadsMutex **wMutex, const 
pthread_mutexattr_t *attr) {
+  /**
+   * Using `RTL_SIZEOF_THROUGH_FIELD` for allocation size may result in
+   * `-Walloc-size` diagnositcs.
+   */
+#if defined(__GNUC__) || defined(__clang__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Walloc-size"
+#endif
+
+  *wMutex = malloc (RTL_SIZEOF_THROUGH_FIELD (WinpthreadsMutex, Mutex.Normal));
+
+#if defined(__GNUC__) || defined(__clang__)
+#pragma GCC diagnostic pop
+#endif
+
+  /**
+   * The pthread_mutex_init() function shall fail if:
+   *
+   * [ENOMEM]
+   *   Insufficient memory exists to initialize the mutex.
+   */
+  if (*wMutex == NULL) {
+return ENOMEM;
+  }
+
+  /**
+   * Direct access through `(*wMutex)->Mutex` may result in `-Warray-bounds`
+   * diagnostics.
+   */
+  WinpthreadsNormalMutex *mutex = &(*wMutex)->Mutex.Normal;


While there are certainly no issues in this code, the pragmas don't look quite neat. Using the COM-flavor 
approach which I suggested earlier, it could be


   struct WinpthreadsMutex
   {
 const WinpthreadsMutexVtable *vtable;
   };

   struct WinpthreadsNormalMutex
   {
 const WinpthreadsMutexVtable *vtable;
 HANDLE Event;
 LONG LockState;
   };

   static int WinpthreadsNormalMutexInit (WinpthreadsMutex **wMutex, const 
pthread_mutexattr_t *attr) {
 WinpthreadsNormalMutex *mutex = malloc (sizeof (WinpthreadsNormalMutex));
 *wMutex = (WinpthreadsMutex *) mutex;
 if (!mutex) {
   return ENOMEM;
 }

 // ...
   }



+
+  mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
+
+  /**
+   * The pthread_mutex_init() function shall fail if:
+   *
+   * [EAGAIN]
+   *   The system lacked the necessary resources (other than memory) to
+   *   initialize another mutex.
+   */
+  if (mutex->Event == NULL) {
+free (wMutex);
+return EAGAIN;
+  }



This should be `free (*wMutex)`, and likewise somewhere else:


diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c 
b/mingw-w64-libraries/winpthreads/src/mutex.c
index be950ee2b..0a58a753d 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -380,7 +380,7 @@ static int WinpthreadsNormalMutexInit (WinpthreadsMutex 
**wMutex, const pthread_
*   initialize another mutex.
*/
   if (mutex->Event == NULL) {
-free (wMutex);
+free (*wMutex);
 return EAGAIN;
   }

@@ -503,7 +503,7 @@ static int WinpthreadsErrorCheckMutexInit (WinpthreadsMutex 
**wMutex, const pthr
*   initialize another mutex.
*/
   if (mutex->Event == NULL) {
-free (wMutex);
+free (*wMutex);
 return EAGAIN;
   }

@@ -650,7 +650,7 @@ static int WinpthreadsRecursiveMutexInit (WinpthreadsMutex 
**wMutex, const pthre
*   initialize another mutex.
*/
   if (mutex->Event == NULL) {
-free (wMutex);
+free (*wMutex);
 return EAGAIN;
   }


--
Best regards,
LIU Hao


OpenPGP_signature.asc
Description: OpenPGP digital signature
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-10 Thread Kirill Makurin
LIU Hao wrote:

> While there are certainly no issues in this code, the pragmas don't look 
> quite neat.

I agree that having a pragma around single function call indeed feels a bit off.

> Using the COM-flavor approach which I suggested earlier, it could be
>
> struct WinpthreadsMutex
> {
>   const WinpthreadsMutexVtable *vtable;
> };
>
> struct WinpthreadsNormalMutex
> {
>   const WinpthreadsMutexVtable *vtable;
>   HANDLE Event;
>   LONG LockState;
> };
>
> static int WinpthreadsNormalMutexInit (WinpthreadsMutex **wMutex, const 
> pthread_mutexattr_t *attr) {
>   WinpthreadsNormalMutex *mutex = malloc (sizeof 
> (WinpthreadsNormalMutex));
>   *wMutex = (WinpthreadsMutex *) mutex;
>   if (!mutex) {
> return ENOMEM;
>   }
>
>   // ...
> }

Yes, this approach does seem reasonable now. In the original version of changes 
I sent, `WinpthreadsMutex` simply contained two pointers: one to static 
"vtable" and second to allocated mutex-type specific structure. Now that we no 
longer have the latter, it does make sense to store "vtable" directly in 
mutex-type specific structure.

I'll take care of this tomorrow. Hopefully, it will be the last revision.

>> +
>> +  mutex->Event = CreateEventW (NULL, FALSE, FALSE, NULL);
>> +
>> +  /**
>> +   * The pthread_mutex_init() function shall fail if:
>> +   *
>> +   * [EAGAIN]
>> +   *   The system lacked the necessary resources (other than memory) to
>> +   *   initialize another mutex.
>> +   */
>> +  if (mutex->Event == NULL) {
>> +free (wMutex);
>> +return EAGAIN;
>> +  }
>
> This should be `free (*wMutex)`, and likewise somewhere else:

Nice catch! I simply replaced `mutex` with `wMutex` and overlooked different 
indirection levels.

- Kirill Makurin

___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-10 Thread Kirill Makurin
I wrote:

> I'll push new version for CI run later today, and if it passes, I'll send it 
> to the list.

Most of CI passes except one issue when compiling winpthreads with clang; see 
https://github.com/maiddaisuki/mingw-w64/actions/runs/34466771691/job/102837072791.

Using `RTL_SIZEOF_THROUGH_FIELD` for allocation size results in a -Walloc-size 
diagnostic from clang; we should be able to silence it with a pragma.

- Kirill Makurin


___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-10 Thread LIU Hao

在 2026-9-10 20:27, Kirill Makurin 写道:

I wrote:


I'll push new version for CI run later today, and if it passes, I'll send it to 
the list.


Most of CI passes except one issue when compiling winpthreads with clang; see https://github.com/ 
maiddaisuki/mingw-w64/actions/runs/34466771691/job/102837072791 .


Using `RTL_SIZEOF_THROUGH_FIELD` for allocation size results in a -Walloc-size diagnostic from clang; we 
should be able to silence it with a pragma.


Try this without a union, which is rather COM-ish:

   struct WinpthreadsMutexBase
   {
 const WinpthreadsMutexVtable *vtable;
   };

   struct WinpthreadsNormalMutex
   {
 const WinpthreadsMutexVtable *vtable;
 HANDLE Event;
 LONG LockState;
   };

   struct WinpthreadsErrorCheckMutex
   {
 const WinpthreadsMutexVtable *vtable;
 HANDLE Event;
 LONG LockState;
 DWORD Owner;
   };


And, does the recursive mutex really require a 64-bit recursion count? A 32-bit 
count is usually sufficient.


--
Best regards,
LIU Hao


OpenPGP_signature.asc
Description: OpenPGP digital signature
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-10 Thread Kirill Makurin
LIU Hao wrote:

> 在 2026-9-9 19:11, Kirill Makurin 写道:
>> I really don't like dealing with code like this:
>>
>> 1. We will "signal" before second thread calls `pthread_mutex_lock` again, 
>> the one which deadlocks
>> 2. Once "signaled", main thread calls `pthread_mutex_unlock` so that second 
>> thread can grab lock again
>> and continue execution
>>
>> It is possible, though unlikely, that main will unlock the mutex before 
>> second thread deadlocks by
>> entering the wait state; that would result in a deadlock in main. I wrote 
>> code that accounts for it, that
>> is no problem, although I don't like how unnecessary messier it gets.
>>
>> I thought maybe it would be acceptable to simply return 77 (report test as 
>> skipped), instead of
>> assert'ing, if call to `pthread_mutex_trylock` does not return EBUSY? I did 
>> not see original version to
>> assert in either local testing or CI.
>
> How about this?
>
> while (pthread_mutex_trylock (&mutex) == 0) {
>   pthread_mutex_unlock (&mutex);
>   Sleep(100);
>}

I'll go with my change for now. I'll push new version for CI run later today, 
and if it passes, I'll send it to the list.

- Kirill Makurin

___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-09 Thread LIU Hao

在 2026-9-9 19:11, Kirill Makurin 写道:

I really don't like dealing with code like this:

1. We will "signal" before second thread calls `pthread_mutex_lock` again, the 
one which deadlocks
2. Once "signaled", main thread calls `pthread_mutex_unlock` so that second thread can grab lock again 
and continue execution


It is possible, though unlikely, that main will unlock the mutex before second thread deadlocks by 
entering the wait state; that would result in a deadlock in main. I wrote code that accounts for it, that 
is no problem, although I don't like how unnecessary messier it gets.


I thought maybe it would be acceptable to simply return 77 (report test as skipped), instead of 
assert'ing, if call to `pthread_mutex_trylock` does not return EBUSY? I did not see original version to 
assert in either local testing or CI.


How about this?

   while (pthread_mutex_trylock (&mutex) == 0) {
 pthread_mutex_unlock (&mutex);
 Sleep(100);
   }


--
Best regards,
LIU Hao


OpenPGP_signature.asc
Description: OpenPGP digital signature
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-09 Thread Kirill Makurin
LIU Hao wrote:

>>  > 0023 has this:
>>  >
>>  > assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
>>  > Sleep(100);
>>  > /**
>>  >  * Ensure we do not dead lock if thread did not lock mutex fast 
>> enough.
>>  >  */
>>  > assert(pthread_mutex_trylock(&mutex) == EBUSY);
>>  >
>>  > I think this is a bit fragile; you can use a barrier here.
>>
>> Yeah, I had mixing feeling about this. Will it be ok to loop with 
>> `InterlockedExchange` in main until
>> thread sets a variable? I hesitate to use `pthread_barrier_t` because its 
>> implementation uses
>> `pthread_mutex_t`, so it would be a bit awkward.
>
> Right; probably you want a Windows event which will be set after the child 
> thread has acquired the mutex.

I really don't like dealing with code like this:

1. We will "signal" before second thread calls `pthread_mutex_lock` again, the 
one which deadlocks
2. Once "signaled", main thread calls `pthread_mutex_unlock` so that second 
thread can grab lock again and continue execution

It is possible, though unlikely, that main will unlock the mutex before second 
thread deadlocks by entering the wait state; that would result in a deadlock in 
main. I wrote code that accounts for it, that is no problem, although I don't 
like how unnecessary messier it gets.

I thought maybe it would be acceptable to simply return 77 (report test as 
skipped), instead of assert'ing, if call to `pthread_mutex_trylock` does not 
return EBUSY? I did not see original version to assert in either local testing 
or CI.

- Kirill Makurin

___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-09 Thread Kirill Makurin
LIU Hao wrote:

> 在 2026-9-9 00:06, Kirill Makurin 写道:
>> However, if you insist on keeping tests for `pthread_mutexattr_gettype` for 
>> now, I can change 0019.txt,
>> 0020.txt and 0021.txt to preserve it. What you say?
>
> It should be preferred to keep existing tests.

I think my reason for removing these `pthread_mutexattr_gettype` check was to 
make general logic in all tests as similar as possible, and other tests which 
use `pthread_mutexattr_settype` did not call `pthread_mutexattr_gettype`.

Worth to mention that some tests still use static initializer instead of 
calling `pthread_mutex_init`. The mutex3*.c tests are an example.

- Kirill Makurin

___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-08 Thread LIU Hao

在 2026-9-9 00:06, Kirill Makurin 写道:

 > 0019, 0020, 0021 delete tests for `pthread_mutexattr_gettype()`.

I mentioned that I want to add support for POSIX robust mutexes next, and this would require adding 
`pthread_mutexattr_{set,get}robust`. As a preparation step, I want to update `pthread_mutexattr_*` 
functions and add tests for each of them.


However, if you insist on keeping tests for `pthread_mutexattr_gettype` for now, I can change 0019.txt, 
0020.txt and 0021.txt to preserve it. What you say?


It should be preferred to keep existing tests.



 > 0023 has this:
 >
 >     assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
 >     Sleep(100);
 >     /**
 >      * Ensure we do not dead lock if thread did not lock mutex fast enough.
 >      */
 >     assert(pthread_mutex_trylock(&mutex) == EBUSY);
 >
 > I think this is a bit fragile; you can use a barrier here.

Yeah, I had mixing feeling about this. Will it be ok to loop with `InterlockedExchange` in main until 
thread sets a variable? I hesitate to use `pthread_barrier_t` because its implementation uses 
`pthread_mutex_t`, so it would be a bit awkward.


Right; probably you want a Windows event which will be set after the child 
thread has acquired the mutex.




--
Best regards,
LIU Hao


OpenPGP_signature.asc
Description: OpenPGP digital signature
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-08 Thread Kirill Makurin
LIU Hao wrote:

> 0010:
>
> If I understand correctly, `WinpthreadsMutexImpl` is a vtable, so maybe 
> `WinpthreadsMutexVtable` is a
> better name? `xxImpl` reminds me about a derived class.

Sure, I don't mind changing the name. I wasn't sure what naming convention to 
stick to, so I chose one similar to one I use in my posix32 library.

> And the union of pointers in `WinpthreadsMutex` makes less sense now; the 
> overhead and complexity of an
> extra allocation may cancel any benefits of a union. How about
>
> ```
> struct WinpthreadsMutex {
>   const WinpthreadsMutexVtable *Vtable;
>   union {
> WinpthreadsNormalMutex Normal;
> WinpthreadsErrorCheckMutex ErrorCheck;
> WinpthreadsRecursiveMutex Recursive;
>   } Mutex;
> };
>
> // ...
> WinpthreadsRecursiveMutex *mutex = malloc (
> RTL_SIZEOF_THROUGH_FIELD (WinpthreadsRecursiveMutex, Recursive));
>
>```

I just learnt something new! That's a very clever approach. I'll send updated 
version.

> 0019, 0020, 0021 delete tests for `pthread_mutexattr_gettype()`.

I mentioned that I want to add support for POSIX robust mutexes next, and this 
would require adding `pthread_mutexattr_{set,get}robust`. As a preparation 
step, I want to update `pthread_mutexattr_*` functions and add tests for each 
of them.

However, if you insist on keeping tests for `pthread_mutexattr_gettype` for 
now, I can change 0019.txt, 0020.txt and 0021.txt to preserve it. What you say?

> 0023 has this:
>
> assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
> Sleep(100);
> /**
>  * Ensure we do not dead lock if thread did not lock mutex fast enough.
>  */
> assert(pthread_mutex_trylock(&mutex) == EBUSY);
>
> I think this is a bit fragile; you can use a barrier here.

Yeah, I had mixing feeling about this. Will it be ok to loop with 
`InterlockedExchange` in main until thread sets a variable? I hesitate to use 
`pthread_barrier_t` because its implementation uses `pthread_mutex_t`, so it 
would be a bit awkward.

- Kirill Makurin

___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public


Re: [Mingw-w64-public] winpthreads: update mutex implementation [V2]

2026-09-08 Thread LIU Hao

在 2026-9-6 21:27, Kirill Makurin 写道:

Hello,

This is updated version of the mutex changes.
GitHub CI run: https://github.com/maiddaisuki/mingw-w64/actions/runs/34029615558 


The changes are:

- patch "winpthreads: mutex.c: simplify `pthread_mutex_*lock` functions'" was 
dropped
- patch "winpthreads: mutex.c: improve `pthread_mutex_destroy`" was updated
- patch "winpthreads: mutex.c: refactor implementation" was updated


0010:

If I understand correctly, `WinpthreadsMutexImpl` is a vtable, so maybe `WinpthreadsMutexVtable` is a 
better name? `xxImpl` reminds me about a derived class.


And the union of pointers in `WinpthreadsMutex` makes less sense now; the overhead and complexity of an 
extra allocation may cancel any benefits of a union. How about


   ```
   struct WinpthreadsMutex {
 const WinpthreadsMutexVtable *Vtable;
 union {
   WinpthreadsNormalMutex Normal;
   WinpthreadsErrorCheckMutex ErrorCheck;
   WinpthreadsRecursiveMutex Recursive;
 } Mutex;
   };

   // ...
   WinpthreadsRecursiveMutex *mutex = malloc (
   RTL_SIZEOF_THROUGH_FIELD (WinpthreadsRecursiveMutex, Recursive));

   ```

---

0019, 0020, 0021 delete tests for `pthread_mutexattr_gettype()`.

---

0023 has this:

   assert(pthread_create(&thread, NULL, ThreadA, &mutex) == 0);
   Sleep(100);
   /**
* Ensure we do not dead lock if thread did not lock mutex fast enough.
*/
   assert(pthread_mutex_trylock(&mutex) == EBUSY);

I think this is a bit fragile; you can use a barrier here.


--
Best regards,
LIU Hao


OpenPGP_signature.asc
Description: OpenPGP digital signature
___
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public