# SE-0433: Synchronous Mutual Exclusion Lock

**URL:** <https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174>\
**Category:** Proposal Reviews\
**Created:** [April 10, 2024, 3:10pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174 "2024-04-10T15:10:26Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [April 12, 2024, 4:23pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/41 "2024-04-12T16:23:45Z")

</div>

> [@drewmccormack](#):
>
> This makes me reserved about it, because introducing it formally also endorses this approach in general, and I'm not sure that we should do that for 90% of app programming Swift developers. There are cases where you need this low-level high performance concurrency, but in my experience, in everyday app level development, they are uncommon.

In my opinion, the Swift standard library should not be limited by the considerations of one application domain. From [Swift.org](http://Swift.org):

> Swift is a **general-purpose** programming language that’s **approachable** for newcomers and **powerful** for experts.

There is a big effort to support Swift on servers, to build distributed systems, to build firmware for embedded systems, and everything else that a general-purpose programming language can do.

Every day, more of the Swift compiler is built in Swift. We know that Apple is using it in their Secure Enclave processors, the swift-collections package contains complex data structures written in Swift, Foundation is being rewritten in Swift, and personally I hope one day we'll see other major open-source projects (such as WebKit) begin a transition to Swift. Community member @LiarPrincess even wrote [a Python VM](https://github.com/LiarPrincess/Violet) in Swift.

These are all exciting projects, and conceivably, projects of this kind may have need of mutexes (I know I have in my projects - [one example](https://github.com/karwa/uniqueid/blob/0b0a36949d1ff5e02dc1c0aaee7461f12a768d07/Sources/UniqueID/UniqueID%2Bv6.swift#L22-L92)). They are a fairly standard construct in computing.

I don't believe that introducing a native `Mutex` type (or any API for that matter) implies any kind of endorsement outside the use-cases it is specifically designed to support.

It is worth noting that Apple platforms offer at least 3 mutex/mutex-like APIs already - `OS(Allocated)UnfairLock`, `std::mutex`, and `pthread_mutex` - so this doesn't fundamentally offer anything that developers don't _already_ have easy access to, and since it lives in a separate `Synchronization` module, developers will have to go looking for it explicitly (just as they do for the other types).

---

<div class="post-metadata">

**Author:** ![James\_Dempsey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/james_dempsey/32/14336_2.png) [@James\_Dempsey](https://forums.swift.org/u/James_Dempsey)\
**Post date:** [April 12, 2024, 4:40pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/42 "2024-04-12T16:40:01Z")

</div>

> [@Karl](#):
>
> > [@drewmccormack](#):
> >
> > This makes me reserved about it, because introducing it formally also endorses this approach in general, and I'm not sure that we should do that for 90% of app programming Swift developers. There are cases where you need this low-level high performance concurrency, but in my experience, in everyday app level development, they are uncommon.
> 
> In my opinion, the Swift standard library should not be limited by the considerations of one application domain. From [Swift.org](http://Swift.org):

I think that having `Mutex` as part the `Synchronization` module as proposed strikes a good balance.

The `Mutex` type is part of the standard library, but it takes that additional import statement to make it available.

This helps newcomers avoid being led towards using it by finding it accidentally with code completion, but makes it easily availability when needed.

---

<div class="post-metadata">

**Author:** ![tclementdev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tclementdev/32/13398_2.png) [@tclementdev](https://forums.swift.org/u/tclementdev)\
**Post date:** [April 12, 2024, 6:26pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/43 "2024-04-12T18:26:12Z")

</div>

Personally I have the opposite opinion. I think the lack of a lock alongside Swift Concurrency has been largely detrimental to the language. It has sent the wrong message that actors is the solution to all concurrency problems and the logical consequence has been an overuse of actors and asynchronicity. In this context, the main point of a lock to me is being able to remain synchronous, which is not something you could ever achieve with actors. Therefore, I can't wait for this type to be available to all :)

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [April 12, 2024, 7:08pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/44 "2024-04-12T19:08:39Z")

</div>

> [@wadetregaskis](#):
>
> Is it still the right mindset to have "sync" vs "async" here; can the `Lock` not support both simultaneously? i.e. have both sync and async versions of `withLock` etc.

Async locking (e.g. as performed by actors) is totally different at the implementation level from sync locking. Expecting a single lock to provide both sync and async locking would preclude using any existing lock implementations (which are usually very tightly optimized and kernel-integrated) and probably make it impossible to provide expected features like priority inversion avoidance for synchronous lockers.

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [April 16, 2024, 3:54pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/45 "2024-04-16T15:54:25Z")

</div>

This should now be resolved with [SILGen: Emit references to noncopyable global storage directly as a borrow. by jckarter · Pull Request #73041 · apple/swift · GitHub](https://github.com/apple/swift/pull/73041). I can try to regenerate toolchains so folks can try it out again.

Edit: Here they are which should includes the fixes:  
macOS: [https://ci.swift.org/job/swift-PR-toolchain-macos/1223/artifact/branch-main/swift-PR-71383-1223-osx.tar.gz](https://ci.swift.org/job/swift-PR-toolchain-macos/1223/artifact/branch-main/swift-PR-71383-1223-osx.tar.gz)  
Linux (x86\_64): [https://download.swift.org/tmp/pull-request/71383/792/ubuntu2004/PR-ubuntu2004.tar.gz](https://download.swift.org/tmp/pull-request/71383/792/ubuntu2004/PR-ubuntu2004.tar.gz)  
Windows: [https://ci-external.swift.org/job/swift-PR-build-toolchain-windows/1213/artifact/\*zip\*/archive.zip](https://ci-external.swift.org/job/swift-PR-build-toolchain-windows/1213/artifact/*zip*/archive.zip)

---

<div class="post-metadata">

**Author:** ![Datagram](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@Datagram](https://forums.swift.org/u/Datagram)\
**Post date:** [April 18, 2024, 2:53pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/46 "2024-04-18T14:53:21Z")

</div>

Using this new toolchain, I did some testing with Mutex with few types on top level on macOS and everything works great now.

Thanks !

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [April 18, 2024, 6:11pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/47 "2024-04-18T18:11:20Z")

</div>

Hi all, I've updated the proposal to include @FranzBusch's suggestion to mark the return type for the `withLock` and `withLockIfAvailable` as `transferring`. This means the return type no longer needs to `Sendable`. I've also added a snippet to the API documentation that attempting to reacquire the lock on the same thread who acquired will guarantee that it will not reacquire the lock and must either deadlock/panic/or some other behavior the platform decides (as long as it doesn't reacquire).

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [April 24, 2024, 7:27am UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/48 "2024-04-24T07:27:40Z")

</div>

Hello,

I've been playing with my own Mutex type, inspired from the one reviewed here, while migrating a code base to strict concurrency checking. I'm still in Swift 5.10, so I don't use non-copyable types and `transferring` parameters. Still, I find the narrow api surface of `Mutex` pretty limiting:

I have countless _tests_ that use mutexes in a pattern similar to the one below:

```swift
let valueMutex = Mutex(0)
await stuff { // Sendable closure
  valueMutex.withLock { $0 = 42 }
}
XCTAssertEqual(valueMutex.withLock { $0 }, 42)

```

This verbosity makes me beg for a convenience `value` property:

```swift
let valueMutex = Mutex(...)
await stuff {
  valueMutex.value = 42
}
XCTAssertEqual(valueMutex.value, 42)

```

But this property is dangerous, because it allows racy code such as `mutex.value += 1`.

I beg the community to explore compiler-level solutions that would make it possible to declare a convenience property that disallows the unsafe mutations:

```swift
mutex.value // OK
mutex.value = 42 // OK
mutex.value += 1 // Compiler error

```

This would greatly improve the ergonomics of this type.

Of course, making `mutex.value += 1` safe would be even better.

Maybe other solutions have been explored, that reduce the need for an explicit call to `withLock { ... }`? I'm curious.

---

<div class="post-metadata">

**Author:** ![MPLewis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mplewis/32/40019_2.png) [@MPLewis](https://forums.swift.org/u/MPLewis)\
**Post date:** [April 24, 2024, 7:37am UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/49 "2024-04-24T07:37:50Z")

</div>

I think that this would be a bad direction for the standard library to go in - you can add those convenience properties as an extension to/wrapper around `Mutex` for your own use, but completely hiding the fact that a synchronous lock needs to be acquired to perform an operation seems dangerous for the standard library to do, in the same vein as suspension points being made explicit at the source level and not done implicitly even when the compiler absolutely could do so.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [April 24, 2024, 7:46am UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/50 "2024-04-24T07:46:37Z")

</div>

I will sure do. I won't make a generality of my particular case, but if many developers start to add this unsafe property, it's reasonable to question whether the type fits its use cases. It is possible to ask this kind of question _right now_, even before the type is publicly released, before we have statistics about the behavior of the crowd, by sharing early experience, performing educated guesses, and using empathy. In my own experience, _tests_ are the context where I've needed the (currently dangerous) property the most.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [April 24, 2024, 8:03am UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/51 "2024-04-24T08:03:07Z")

</div>

The problem with adding a `.value` member is that it becomes tempting to write code such as:

```swift
if mutex.value > 5 {
  mutex.value = 0
}

```

But of course, since each call to `.value` acquires the lock, other code could run between those statements:

```swift
if mutex.value > 5 {
  // mutex.value may be <= 5 here!
  mutex.value = 0
}

```

By having the explicit `withLock` closure, the operation becomes _atomic_ - you don't give other threads a chance to read or modify the value until you are completely done with it.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [April 24, 2024, 8:39am UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/52 "2024-04-24T08:39:34Z")

</div>

Ah, you're right, @Karl

@jpsim, do you think it would be possible for a linter such as SwiftLint to detect such misuses? In which case, the standard lib could ship with the convenience property (along with a detailed documentation about wrong usage), and rely on linters for avoiding the mistakes?

This would be better than letting the devs exposed to the dangers of their own extension (which, I predict, will happen if the stdlib does not ship the property).

In my own experience in API design, I've seen myself hoping that users understand that if an obvious api is not there, there is a good reason for it - and they should not add it. In reality, that's not how users behave. They just add the dangerous api in their custom extension, and I stay there, helpless, watching them shot themselves in the foot. Surely I can say they're wrong. But I can also consider that my api might have an issue in need for a solution.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 24, 2024, 10:33am UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/53 "2024-04-24T10:33:28Z")

</div>

> [@Karl](#):
>
> By having the explicit `withLock` closure, the operation becomes _atomic_

It doesn't completely rule out the misuses though (just makes them less likely), as someone could still write:

```swift
if (withLock { value > 5 }) {
    withLock { value = 0 }
}

```

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [April 24, 2024, 12:16pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/54 "2024-04-24T12:16:30Z")

</div>

But in your example case, the critical sections are clearly visible, and so is the logic flaw. That's why that such a misuse does not need to be addressed. We only need to think about misuses that look like they are correct:

```swift
// Incorrect, but looks correct 👎
if mutex.value > 5 {
  mutex.value = 0
}

// Incorrect, and looks incorrect 👍
if (mutex.withLock { value > 5 }) {
    mutex.withLock { value = 0 }
}

```

That's why I'm conscious I'm asking for a dangerous api, why I'm trying to argue that people will add it if it is missing, why ignoring this prediction is creating problems, and why I'm suggesting to look for solutions.

This should not jeopardize the success of the proposal. I just wish to read _some acknowledment_ that an api limited to `withLock`, despite its high level of correctness, has ergonomics issues that have people look for incorrect solutions. The sooner this problem is noticed, the better.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 24, 2024, 12:31pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/55 "2024-04-24T12:31:47Z")

</div>

I see what you mean. If there's a fear that users will provide `.value` of their own maybe we could do it first, marking it with "unavailable", and having a failing implementation if users manage to call it somehow. Perhaps along with some `.nonAtomicTransactionallyValue` (bike-shed name) for those cases it's actually useful.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [April 24, 2024, 1:01pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/56 "2024-04-24T13:01:40Z")

</div>

😅 Imagination has no limit: if `value` is marked as unavailable, people will write `mutex.get()` and `mutex.set(...)` in order to avoid the proliferation of `withLock { $0 }` and `withLock { $0 = ... }`. I'd rather _ship_ the `Mutex.value` property, and rely on linters to detect the misuses (unless another solution is found).

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [April 24, 2024, 1:57pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/57 "2024-04-24T13:57:18Z")

</div>

It could be a warning – annoying enough to discourage people using it whilst "available" enough to demotivate people investing into their own alternatives.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [April 24, 2024, 2:24pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/58 "2024-04-24T14:24:17Z")

</div>

If we were to provide a "convenience" API, exposing the value as a property is definitely too abuse-prone, but maybe we could provide something that looks like the `Atomic` API, with methods like `mutex.load()`, `mutex.store(x)`, maybe even RMW math operations if the value conforms to the right properties, and so on. Someone might still be tempted to write `mutex.store(mutex.load() + 1)` but it's at least more apparent in that line of code that that will end up doing two separate mutex accesses (and it's actually longer than `mutex.withLock { $0 += 1 }`, so it's less "convenient" to do so overall).

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [April 24, 2024, 2:27pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/59 "2024-04-24T14:27:04Z")

</div>

> [@Joe\_Groff](#):
>
> If we were to provide a "convenience" API, exposing the value as a property is definitely too abuse-prone, but maybe we could provide something that looks like the `Atomic` API, with methods like `mutex.load()`, `mutex.store(x)`, maybe even RMW math operations if the value conforms to the right properties, and so on. Someone might still be tempted to write `mutex.store(mutex.load() + 1)` but it's at least more apparent in that line of code that that will end up doing two separate mutex accesses.

I would worry a little bit about the attractive nuisance of providing such API--both from a "making it easy to write incorrect code" perspective, but also from a "if that's what you're doing, you probably should be using an atomic instead" perspective.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [April 24, 2024, 2:35pm UTC](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174/60 "2024-04-24T14:35:18Z")

</div>

> [@scanon](#):
>
> I would worry a little bit about the attractive nuisance of providing such API--both from a "making it easy to write incorrect code" perspective, but also from a "if that's what you're doing, you probably should be using an atomic instead" perspective.

I'm not terribly convinced that convenience APIs are desirable either, but I think it is relatively common that reading a value is safe to do in a grab-and-go way, even though updating the value may require a longer transaction. There might be an argument there for providing `load` but not any corresponding mutation conveniences. There's also the fact that our `Atomic` type only strictly supports types that the platform can atomically update without locks, so there might be an argument there for `Mutex` providing a similar API so that it can be dropped in as a replacement for `Atomic` when locking is acceptable and a target platform doesn't have native atomic support for a type.

[Previous page](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174.md?page=2)

[Next page](https://forums.swift.org/t/se-0433-synchronous-mutual-exclusion-lock/71174.md?page=4)
