Commit Graph

51 Commits

Author SHA1 Message Date
Sarunas 21835cd4fa Drop enum for constants 2026-04-30 17:35:37 +02:00
Sarunas cbf27f497b Move docs, remove old _spin variable 2026-04-28 21:25:23 +03:00
Sarunas e4b1283837 clarify why attempting to do CAS after spin 2026-04-24 20:56:15 +02:00
Sarunas 62632bf9a7 PR feedback 2026-04-22 22:20:08 +03:00
Sarunas 8c1e266155 Restore some existing comments and style 2026-04-21 11:58:10 +03:00
Sarunas ac04173f5e Update 2026-04-20 16:09:40 +03:00
Sarunas 8705100af3 Adaptive Synchronization Linux spinning 2026-04-17 17:24:45 +03:00
Jonathan Grynspan 2ebb425e95 [SE-0512] Adjust the docs for Mutex.withLockIfAvailable() re: spurious failures. (#85497)
This PR adjusts the documentation for `Mutex.withLockIfAvailable()` to
clarify that it is not subject to spurious failures. The C11 and C++11
specs for their respective `tryLock()` APIs allow for spurious failures,
but our implementations and those of every other similar API I've found
don't use weak `cmpxchg`s and don't spuriously fail.

Implements
[SE-0512](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0512-withlockifavailable-cannot-spuriously-fail.md).
2026-03-09 15:37:33 -04:00
3405691582 6ffc91c488 Fix optionals usage. 2026-01-18 11:03:29 -05:00
3405691582 7e5387b2f9 Update stdlib/public/Synchronization/Mutex/OpenBSDImpl.swift
Co-authored-by: Jonathan Grynspan <grynspan@me.com>
2026-01-18 08:18:11 -05:00
3405691582 6e31926a11 Add unsafe annotations to OpenBSDImpl.
In #86276 strict memory safety becomes an error, necessitating adding
the following annotations and changes:

* in init(): check the return value of `pthread_mutex_init`; since this
  isn't a failable initializer, `fatalError` if this fails, to avoid
  silently leaving a zero-valued pointer,

* similarly check return values in `pthread_mutex_lock`,
  `pthread_mutex_unlock`, and `pthread_mutex_destroy`,

* the message in `fatalError` should use `strerror_r`, which necessitates
  `unsafe` markings since it initializes the array storage unsafely.

* marking the `value` as `@safe` since the underlying mutex type is an
  `OpaquePointer` and is unsafe and all access to value occur through
  a safe interface,

* thus, marking `_MutexHandle` `@safe`.
2026-01-17 22:04:47 -05:00
Evan Wilde d11891ff56 FreeBSD: Synchronization: Add missing unsafe flags
The FreeBSD Mutex implementation contained several un-annotated unsafe
pointer accesses resulting in build-failures after enabling strict
memory safety.

Fixes: #86289
2026-01-05 12:59:43 -08:00
Danny Canter 36b6b96a72 Synchronization: Add errno to some linux lock fatalErrors
In the comments it's stated that these errnos can't really occur for
the implementation, but unfortunately they have been seen in some
scenarios. To aide in debugging, it'd be nice to at least have the
errno in the error message.
2025-11-27 15:01:19 -08:00
Jonathan Grynspan 276706eceb Don't abort on Linux if withLockIfAvailable() is called recursively. (#85448)
On all platforms except Linux, calling `withLockIfAvailable()`
recursively just returns `nil`. However, our Linux implementation
chooses to abort the process instead. We don't need this
inconsistent/destructive behaviour and can just return `nil` as we do
elsewhere.
2025-11-11 23:34:12 -08:00
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
Alejandro Alonso 12a4415f28 Merge pull request #82392 from 3405691582/openbsd_mutex_pthread
Use libpthread primitives for OpenBSD mutexes.
2025-07-22 12:48:11 -07:00
Allan Shortlidge 008efc432f stdlib: Fix missing unsafe operators in more places.
Add `unsafe` where it is still missing. I missed these in previous passes due
to conditional compilation.
2025-06-22 18:46:57 -07:00
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
michael-yuji 9a948a9c3b Merge branch 'main' into mchiu/freebsd 2025-05-21 16:29:16 -07:00
Max Desiatov 99a4b887f9 NFC: fix comment in WasmImpl.swift (#80760)
Wasm is not an acronym, thus it's not uppercased [per the spec](https://webassembly.github.io/spec/core/intro/introduction.html#wasm).
2025-04-11 12:30:01 -07:00
Michael Chiu 0c47c45303 Adding FreeBSD support
This commit adds required conditional compilation blocks to enable bulding on
FreeBSD (tested on x86_64 FreeBSD 14.1-RELEASE-p6). Also implements FreeBSD
synchronization shims using `_umtx_op(2)`
2025-03-14 02:15:15 -04:00
Doug Gregor 1efb994375 Enable strict memory safety in the Synchronization module 2025-02-26 14:28:28 -08:00
Doug Gregor 8378562e12 Adopt @unsafe throughout the standard library
Annotate all of the `Unsafe*` types and `unsafe` functions in the standard
library (including concurrency, synchronization, etc.) as `@unsafe`. Add a
few tests to ensure that we detect uses of these types in clients that
have disabled unsafe code.
2024-08-19 14:33:09 -07:00
Evan Wilde cae88d3344 Fix add_swift_target_library for the static SDK
add_swift_target_library was missing pieces for passing sources and
flags to the static SDK build. As a result, the static SDK was missing
pieces (specifically Mutex).

Also adding the Musl import to the Linux Mutex implementation.
2024-08-13 15:59:31 -07:00
Alejandro Alonso 24a46c422b Merge pull request #74946 from Azoy/mutex-sending-inout
[stdlib] Adopt inout sending for Mutex
2024-07-17 10:56:57 -07:00
Alexander Cyon c21b1e68fd [stdlib] Fix typos 2024-07-06 13:09:57 +02:00
Alejandro Alonso 9cbf410220 Adopt inout sending for Mutex 2024-07-03 10:54:20 -07:00
Alejandro Alonso e3a53abe13 Don't use aarch64 hint for arm targets 2024-06-10 09:23:21 -07:00
Alejandro Alonso b1ceb9b947 Update MutexUnavailable.swift (#74182)
Fixes the availability syntax for minimal platforms.
Resolves: rdar://129345303
2024-06-06 12:46:28 -07:00
Alejandro Alonso 4f8181dde6 Merge pull request #74143 from Azoy/mutex-minimal-thing
[stdlib] Condition Mutex.swift on platform
2024-06-06 07:54:36 -07:00
Yuta Saito da6f015e3c Merge pull request #74171 from kateinoigakukun/yt/fix-mutex-wasm-test
[Synchronization] Fix wasm atomic intrinsic declarations
2024-06-06 16:58:32 +09:00
Alex Lorenz bffb878476 Merge pull request #74160 from hyp/eng/fix-android-synchro
[android] Fix Synchronization Android build
2024-06-05 23:38:47 -07:00
Yuta Saito 2dbbbcf646 [Synchronization] Skip atomic operations in single-threaded mode on WebAssembly
Use of atomics instructions requires the support of threads proposal and
it's not widely supported yet. So we should enable actual atomic
operations only when targeting wasm32-uknown-wasip1-threads.
2024-06-06 04:18:13 +00:00
Yuta Saito 0536c9ac65 [Synchronization] Fix wasm atomic intrinsic declarations
Otherwise, isel will not be able to select the desired atomic
instructions.
2024-06-06 04:18:13 +00:00
Alex Lorenz 74d2a66a27 [androd] Fix Synchronization Android build
Add Android import for Mutex implementation
2024-06-05 16:58:05 -07:00
Alejandro Alonso fcfbf068a5 Add a special file for mutex on unavailable platforms 2024-06-05 13:51:55 -07:00
Yuta Saito 3b1791a638 [Synchronization] Fix Wasm mutex build
This change fixes the following build error happening on Wasm:
```
error: referencing instance method '_wait(expected:)' on 'Atomic' requires the types '_MutexHandle.State' and 'UInt32' be equivalent
```
2024-06-05 17:15:43 +00:00
Alejandro Alonso cb3fc4b451 Adopt sending and movesAsLike 2024-06-04 09:49:21 -07:00
Alejandro Alonso 66ec7abd54 Underscore some things, add explicit internal
Fix some compile issues
2024-06-04 09:06:37 -07:00
Alejandro Alonso fbfb218d11 Update LinuxImpl.swift 2024-06-04 09:06:37 -07:00
Alejandro Alonso 3a115fc290 Properly handle the syscalls on Linux and do some spins
fix some stuff
2024-06-04 09:06:37 -07:00
Alejandro Alonso 9b389a3df5 Fix tests and toolchain build 2024-06-04 09:06:37 -07:00
Alejandro Alonso e99268864c Use Builtin.addressOfRawLayout 2024-06-04 09:06:37 -07:00
Alejandro Alonso 97072e5b79 Adopt transferring for Mutex init 2024-06-04 09:06:37 -07:00
Alejandro Alonso 89748d4229 Actually implement the API now 2024-06-04 09:06:36 -07:00
Alejandro Alonso 92e6a989b2 Cache TID in TLS on Linux 2024-06-04 09:06:36 -07:00
Alejandro Alonso 5b7e3d8ca2 Add a WasmMutex 2024-06-04 09:06:36 -07:00
Alejandro Alonso 1146b41d14 Add the underscored unsafe locking primitives 2024-06-04 09:06:36 -07:00
Alejandro Alonso 882568ca4d Directly call gettid syscall instead of library function 2024-06-04 09:06:36 -07:00
Alejandro Alonso 1f206ec58e Attempt to release the lock and wake when we can't 2024-06-04 09:06:36 -07:00