Commit Graph

38 Commits

Author SHA1 Message Date
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
Alejandro Alonso
2a60c88457 Implement Mutex in Synchronization 2024-06-04 09:06:34 -07:00