在 2026-9-12 07:07, Kirill Makurin 写道:
LIU Hao wrote:> First, `LockCount` doesn't have to be 64-bit which would introduce some complexity for 32-bit targets; > and when using a 64-bit value, it's unnecessary to check for overflows which could take hundreds of years.I don't have strong opinion on this; if you think that using 64-bit lock count is unnecessary, then let's use 32-bit lock count. Can you apply the changes locally?
OK.
However, I'm not sure if in the current state winphtreads is even loadable on Win9x systems and static winpthreads would be broken on Win9x due to lack of support for TLS callbacks in EXEs (thanks to Pali for investigating TLS behavior across Windows versions). I want to add support for old NT versions as well as Win9x systems. I would prioritize adding support for older NT versions before Win9x.
Ah I see it now; `__dyn_tls_pthread` is not called from the DLL entry-point routine, but as a TLS callback.So I'm leaving `CreateEventW` alone. There's also one reference to `CreateEventA` in spinlock.c which you may fix later.
To give you an example, you can take look at this[1] source file in my posix32 library, the way it handles `OutputDebugString` functions. The idea is basically to have a function (e.g. `winpthreads_create_event`) which calls either ANSI or Unicode version of `CreateEvent`, depending on whether code is running on NT or Win9x. This would also apply to functions such as `CreateMutex` or `CreateSemaphore`.
`OutputDebugString` is an exception to the common guidance that W functions should be preferred to A ones. In reality, `OutputDebugStringW` converts its argument to an ANSI string and calls `OutputDebugStringA`, so it's always recommended to call `OutputDebugStringA` directly.
I have pushed this series patches with these changes:
diff --git a/mingw-w64-libraries/winpthreads/src/mutex.c
b/mingw-w64-libraries/winpthreads/src/mutex.c
index a2169efa0..3b3edf261 100644
--- a/mingw-w64-libraries/winpthreads/src/mutex.c
+++ b/mingw-w64-libraries/winpthreads/src/mutex.c
@@ -239,7 +239,7 @@ typedef struct {
* once lock count reaches zero, the owning thread releases the ownership of
* the mutex.
*/
- unsigned __int64 LockCount;
+ unsigned int LockCount;
} WinpthreadsRecursiveMutex;
WINPTHREADS_STATIC_ASSERT (offsetof (WinpthreadsMutexBase, Vtable) == offsetof
(WinpthreadsRecursiveMutex, Base.Vtable), "");
@@ -646,7 +646,7 @@ static int WinpthreadsRecursiveMutexLock (WinpthreadsMutex
*wMutex, const struct
* If calling thread already owns the mutex, simply increment the lock count.
*/
if (mutex->Owner == threadId) {
- if (unlikely (mutex->LockCount == _UI64_MAX)) {
+ if (unlikely (mutex->LockCount == UINT_MAX)) {
return EAGAIN;
}
@@ -668,8 +668,8 @@ static int WinpthreadsRecursiveMutexLock (WinpthreadsMutex
*wMutex, const struct
}
done:
- mutex->Owner = threadId;
- mutex->LockCount += 1;
+ mutex->Owner = threadId;
+ mutex->LockCount = 1;
return 0;
}
@@ -687,7 +687,7 @@ static int WinpthreadsRecursiveMutexTryLock
(WinpthreadsMutex *wMutex, BOOL excl
return EBUSY;
}
- if (unlikely (mutex->LockCount == _UI64_MAX)) {
+ if (unlikely (mutex->LockCount == UINT_MAX)) {
return EAGAIN;
}
@@ -699,8 +699,8 @@ static int WinpthreadsRecursiveMutexTryLock
(WinpthreadsMutex *wMutex, BOOL excl
return EBUSY;
}
- mutex->Owner = threadId;
- mutex->LockCount += 1;
+ mutex->Owner = threadId;
+ mutex->LockCount = 1;
return 0;
}
diff --git a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-lock-recursive.c
b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-lock-recursive.c
index 3ead86aea..4d97a50f6 100644 --- a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-lock-recursive.c +++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-lock-recursive.c @@ -38,7 +38,7 @@ * Thread A locks L. * * Thread A attempts to lock L again; the call to `pthread_mutex_lock` must - * fail with EBUSY. + * fail with EDEADLK. * * Thread A unlocks L. *diff --git a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-static-lock-recursive.c b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-static-lock-recursive.c
index ebfbb8be5..b0c70aff5 100644 --- a/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-static-lock-recursive.c +++ b/mingw-w64-libraries/winpthreads/tests/pthread_mutex/errorcheck-static-lock-recursive.c @@ -39,7 +39,7 @@ * Thread A locks L. * * Thread A attempts to lock L again; the call to `pthread_mutex_lock` must - * fail with EBUSY. + * fail with EDEADLK. * * Thread A unlocks L. * -- 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
