Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions standard/conversions.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ An implicit conversion exists from the `null` literal to any reference type or n
The implicit reference conversions are:

- A null literal conversion ([§10.2.7](conversions.md#1027-null-literal-conversions)) to any *reference_type*.
- From any *reference_type* to `object` and `dynamic`.
- From any *class_type* `S` to any *class_type* `T`, provided `S` is derived from `T`.
- From any *class_type* `S` to any *interface_type* `T`, provided `S` implements `T`.
- From any *reference_type* to `object` and `dynamic`. A warning shall be issued when *reference_type* is `System.Threading.Lock`.
- From any *class_type* `S` to any *class_type* `T`, provided `S` is derived from `T`. A warning shall be issued when `S` is `System.Threading.Lock`.
- From any *class_type* `S` to any *interface_type* `T`, provided `S` implements `T`. A warning shall be issued when `S` is `System.Threading.Lock`.
- From any *interface_type* `S` to any *interface_type* `T`, provided `S` is derived from `T`.
- From an *array_type* `S` with an element type `Sᵢ` to an *array_type* `T` with an element type `Tᵢ`, provided all of the following are true:
- `S` and `T` differ only in element type. In other words, `S` and `T` have the same number of dimensions.
Expand Down
12 changes: 12 additions & 0 deletions standard/standard-library.md
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,16 @@ namespace System.Threading
{
public bool IsCancellationRequested { get; }
}

public sealed class Lock
{
public Lock();
public System.Threading.Lock.Scope EnterScope();
public ref struct Lock.Scope
{
public void Dispose();
}
}
}

namespace System.Threading.Tasks
Expand Down Expand Up @@ -1560,6 +1570,8 @@ The following library types are referenced in this specification. The full names
- `global::System.Runtime.CompilerServices.ValueTaskAwaiter<TResult>`
- `global::System.Runtime.CompilerServices.Unsafe`
- `global::System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute`
- `global::System.Threading.Lock`
- `global::System.Threading.Lock.Scope`
- `global::System.Threading.Monitor`
- `global::System.Threading.Tasks.Task`
- `global::System.Threading.Tasks.Task<TResult>`
Expand Down
19 changes: 18 additions & 1 deletion standard/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,7 @@ A `lock` statement of the form

`lock (x)` …

where `x` is an expression of a *reference_type*, is precisely equivalent to:
where `x` is an expression of a *reference_type* other than `System.Threading.Lock`, is precisely equivalent to:

```csharp
bool __lockWasTaken = false;
Expand All @@ -2043,8 +2043,25 @@ finally

except that `x` is only evaluated once.

A `lock` statement of the form

`lock (x)` …

where `x` is an expression of type `System.Threading.Lock`, is precisely equivalent to:

```csharp
using (x.EnterScope())
{
}
```

Note carefully that when `x` is an expression of type `System.Threading.Lock`, the statements `lock (x) …` and `lock ((object)x) …` are treated quite differently. The former uses the `Lock`/`Scope` approach described above while the latter uses the `Monitor.Enter`/`Exit` approach described earlier, as the expression does not have type `System.Threading.Lock` in the latter case, and a warning to that effect is reported.

While a mutual-exclusion lock is held, code executing in the same execution thread can also obtain and release the lock. However, code executing in other threads is blocked from obtaining the lock until the lock is released.

It is a compile-time error to use a lock statement on a value of type 'System.Threading.Lock' in an async method or async lambda expression.

## 13.14 The using statement

### 13.14.1 General
Expand Down
Loading