Commit Graph

2 Commits

Author SHA1 Message Date
Valeriy Van
0826d47a8b Fix bug in internal func _tryLock in OpenBSDImpl.swift (#84619)
`pthread_mutex_trylock` return `0` on success.

```
NAME
     pthread_mutex_trylock – attempt to lock a mutex without blocking

SYNOPSIS
     #include <pthread.h>

     int
     pthread_mutex_trylock(pthread_mutex_t *mutex);

DESCRIPTION
     The pthread_mutex_trylock() function locks mutex.  If the mutex is already locked,
     pthread_mutex_trylock() will not block waiting for the mutex, but will return an error
     condition.

RETURN VALUES
     If successful, pthread_mutex_trylock() will return zero, otherwise an error number will
     be returned to indicate the error.

ERRORS
     The pthread_mutex_trylock() function will fail if:

     [EINVAL]           The value specified by mutex is invalid.

     [EBUSY]            Mutex is already locked.
```
2025-10-29 00:50:47 +05:30
3405691582
3bb369cd91 Use libpthread primitives for OpenBSD mutexes.
There has been some discussion as to whether this is the right approach,
centered around internal pointers in the underlying mutex structure.
After much thought, let's commit this for now. The argument:

* While it is true that OpenBSD has futex(2) and in fact is used in
  the underlying implementations in Dispatch. I'm not confident right now
  creating robust locking primitives by hand from futexes.
  It would be ideal to just delegate all of this to Dispatch, but
  Dispatch doesn't expose a ready-use API for mutexes. Cobbling one
  together might be possible, but that would take some time and finesse.

* While recent versions of C and C++ have language support for mutexes,
  they are implemented in many cases by libpthread anyway, so there
  is nothing inherently gained right now by not using the pthread API
  other than portability.

* While the concern for internal pointers for the pthread API is
  important, the OpenBSD is mitigated by the fact that pthread_mutex_t is
  pointer-valued on the platform. The concern that this might not always
  be the case is mitigated by the fact that this commit does not attempt
  to use the pthread implementation as a catch-all for all platforms.

* The alternative is to use MutexUnavailable, but given Foundation has
  some dependencies on Mutex, this may cause problems elsewhere
  throughout the port.
2025-06-21 10:26:39 -04:00