2020-06-28 14:58:08 +03:00
|
|
|
# V Work In Progress
|
|
|
|
|
|
|
|
***This document describes features that are not implemented, yet.
|
|
|
|
Please refer to [docs.md](https://github.com/vlang/v/blob/master/doc/docs.md)
|
|
|
|
for the current state of V***
|
|
|
|
|
|
|
|
## Table of Contents
|
|
|
|
|
|
|
|
* [Concurrency](#concurrency)
|
2020-07-11 00:45:03 +03:00
|
|
|
* [Variable Declarations](#variable-declarations)
|
2023-02-13 11:29:02 +03:00
|
|
|
* [Strengths](#strengths)
|
|
|
|
* [Weaknesses](#weaknesses)
|
|
|
|
* [Compatibility](#compatibility)
|
|
|
|
* [Automatic Lock](#automatic-lock)
|
|
|
|
* [Channels](#channels)
|
2020-06-28 14:58:08 +03:00
|
|
|
|
|
|
|
## Concurrency
|
|
|
|
|
|
|
|
### Variable Declarations
|
|
|
|
|
|
|
|
Objects that are supposed to be used to exchange data between
|
|
|
|
coroutines have to be declared with special care. Exactly one of the following
|
|
|
|
4 kinds of declaration has to be chosen:
|
|
|
|
|
2020-11-03 03:04:14 +03:00
|
|
|
```v ignore
|
2020-06-28 14:58:08 +03:00
|
|
|
a := ...
|
|
|
|
mut b := ...
|
|
|
|
shared c := ...
|
|
|
|
atomic d := ...
|
|
|
|
```
|
|
|
|
|
|
|
|
- `a` is declared as *constant* that can be passed to
|
|
|
|
other coroutines and read without limitations. However
|
|
|
|
it cannot be changed.
|
|
|
|
- `b` can be accessed reading and writing but only from one
|
|
|
|
coroutine. That coroutine *owns* the object. A `mut` variable can
|
|
|
|
be passed to another coroutine (as receiver or function argument in
|
|
|
|
the `go` statement or via a channel) but then ownership is passed,
|
|
|
|
too, and only the other coroutine can access the object.<sup>1</sup>
|
|
|
|
- `c` can be passed to coroutines an accessed
|
|
|
|
*concurrently*.<sup>2</sup> In order to avoid data races it has to
|
|
|
|
be locked before access can occur and unlocked to allow access to
|
2020-07-07 02:57:31 +03:00
|
|
|
other coroutines. This is done by one the following block structures:
|
2020-11-03 03:04:14 +03:00
|
|
|
```v ignore
|
2020-06-28 14:58:08 +03:00
|
|
|
lock c {
|
|
|
|
// read, modify, write c
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-11-03 03:04:14 +03:00
|
|
|
```v ignore
|
2020-07-07 02:57:31 +03:00
|
|
|
rlock c {
|
|
|
|
// read c
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
2020-06-28 14:58:08 +03:00
|
|
|
Several variables may be specified: `lock x, y, z { ... }`.
|
|
|
|
They are unlocked in the opposite order.
|
|
|
|
- `d` can be passed to coroutines and accessed *concurrently*,
|
|
|
|
too.<sup>3</sup> No lock is needed in this case, however
|
|
|
|
`atomic` variables can only be 32/64 bit integers (or pointers)
|
|
|
|
and access is limited to a small set of predefined idioms that have
|
|
|
|
native hardware support.
|
|
|
|
|
|
|
|
To help making the correct decision the following table summarizes the
|
|
|
|
different capabilities:
|
|
|
|
|
2023-02-13 11:29:02 +03:00
|
|
|
| | *default* | `mut` | `shared` | `atomic` |
|
|
|
|
|:-------------------------|:---------:|:-----:|:--------:|:--------:|
|
|
|
|
| write access | | + | + | + |
|
|
|
|
| concurrent access | + | | + | + |
|
|
|
|
| performance | ++ | ++ | | + |
|
|
|
|
| sophisticated operations | + | + | + | |
|
|
|
|
| structured data types | + | + | + | |
|
2020-06-28 14:58:08 +03:00
|
|
|
|
|
|
|
### Strengths
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2021-06-14 13:12:02 +03:00
|
|
|
**default**
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-06-28 14:58:08 +03:00
|
|
|
- very fast
|
|
|
|
- unlimited access from different coroutines
|
|
|
|
- easy to handle
|
|
|
|
|
2021-06-14 13:12:02 +03:00
|
|
|
**`mut`**
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-06-28 14:58:08 +03:00
|
|
|
- very fast
|
|
|
|
- easy to handle
|
|
|
|
|
2021-06-14 13:12:02 +03:00
|
|
|
**`shared`**
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-06-28 14:58:08 +03:00
|
|
|
- concurrent access from different coroutines
|
|
|
|
- data type may be complex structure
|
|
|
|
- sophisticated access possible (several statements within one `lock`
|
|
|
|
block)
|
|
|
|
|
2021-06-14 13:12:02 +03:00
|
|
|
**`atomic`**
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-06-28 14:58:08 +03:00
|
|
|
- concurrent access from different coroutines
|
|
|
|
- reasonably fast
|
|
|
|
|
|
|
|
### Weaknesses
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2021-06-14 13:12:02 +03:00
|
|
|
**default**
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-06-28 14:58:08 +03:00
|
|
|
- read only
|
|
|
|
|
2021-06-14 13:12:02 +03:00
|
|
|
**`mut`**
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-06-28 14:58:08 +03:00
|
|
|
- access only from one coroutine at a time
|
|
|
|
|
2021-06-14 13:12:02 +03:00
|
|
|
**`shared`**
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-06-28 14:58:08 +03:00
|
|
|
- lock/unlock are slow
|
|
|
|
- moderately difficult to handle (needs `lock` block)
|
|
|
|
|
2021-06-14 13:12:02 +03:00
|
|
|
**`atomic`**
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-06-28 14:58:08 +03:00
|
|
|
- limited to single (max. 64 bit) integers (and pointers)
|
|
|
|
- only a small set of predefined operations possible
|
|
|
|
- very difficult to handle correctly
|
|
|
|
|
|
|
|
<sup>1</sup> The owning coroutine will also free the memory space used
|
|
|
|
for the object when it is no longer needed.
|
|
|
|
<sup>2</sup> For `shared` objects the compiler adds code for reference
|
|
|
|
counting. Once the counter reaches 0 the object is automatically freed.
|
|
|
|
<sup>3</sup> Since an `atomic` variable is only a few bytes in size
|
|
|
|
allocation would be an unnecessary overhead. Instead the compiler
|
|
|
|
creates a global.
|
2020-07-11 00:45:03 +03:00
|
|
|
|
|
|
|
### Compatibility
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-07-11 00:45:03 +03:00
|
|
|
Outside of `lock`/`rlock` blocks function arguments must in general
|
|
|
|
match - with the familiar exception that objects declared `mut` can be
|
|
|
|
used to call functions expecting immutable arguments:
|
|
|
|
|
2020-11-03 03:04:14 +03:00
|
|
|
```v ignore
|
2020-07-11 00:45:03 +03:00
|
|
|
fn f(x St) {...}
|
|
|
|
fn g(mut x St) {...}
|
|
|
|
fn h(shared x St) {...}
|
|
|
|
fn i(atomic x u64) {...}
|
|
|
|
|
|
|
|
a := St{...}
|
|
|
|
f(a)
|
|
|
|
|
2021-02-01 23:43:45 +03:00
|
|
|
mut b := St{...}
|
2020-07-11 00:45:03 +03:00
|
|
|
f(b)
|
|
|
|
go g(mut b)
|
2022-10-28 22:27:38 +03:00
|
|
|
// `b` should not be accessed here anymore
|
2020-07-11 00:45:03 +03:00
|
|
|
|
2021-02-01 23:43:45 +03:00
|
|
|
shared c := St{...}
|
2020-07-11 00:45:03 +03:00
|
|
|
h(shared c)
|
|
|
|
|
|
|
|
atomic d &u64
|
|
|
|
i(atomic d)
|
|
|
|
```
|
|
|
|
|
|
|
|
Inside a `lock c {...}` block `c` behaves like a `mut`,
|
|
|
|
inside an `rlock c {...}` block like an immutable:
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-11-03 03:04:14 +03:00
|
|
|
```v ignore
|
2021-02-01 23:43:45 +03:00
|
|
|
shared c := St{...}
|
2020-07-11 00:45:03 +03:00
|
|
|
lock c {
|
|
|
|
g(mut c)
|
|
|
|
f(c)
|
|
|
|
// call to h() not allowed inside `lock` block
|
|
|
|
// since h() will lock `c` itself
|
|
|
|
}
|
|
|
|
rlock c {
|
|
|
|
f(c)
|
|
|
|
// call to g() or h() not allowed
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Automatic Lock
|
2023-02-13 11:29:02 +03:00
|
|
|
|
2020-07-11 00:45:03 +03:00
|
|
|
In general the compiler will generate an error message when a `shared`
|
|
|
|
object is accessed outside of any corresponding `lock`/`rlock`
|
|
|
|
block. However in simple and obvious cases the necessary lock/unlock
|
|
|
|
can be generated automatically for `array`/`map` operations:
|
|
|
|
|
2020-11-03 03:04:14 +03:00
|
|
|
```v ignore
|
2021-02-01 23:43:45 +03:00
|
|
|
shared a := []int{cap: 5}
|
2020-07-11 00:45:03 +03:00
|
|
|
go h2(shared a)
|
|
|
|
a << 3
|
|
|
|
// keep in mind that `h2()` could change `a` between these statements
|
|
|
|
a << 4
|
|
|
|
x := a[1] // not necessarily `4`
|
|
|
|
|
2021-02-01 23:43:45 +03:00
|
|
|
shared b := map[string]int{}
|
2020-07-11 00:45:03 +03:00
|
|
|
go h3(shared b)
|
|
|
|
b['apple'] = 3
|
|
|
|
c['plume'] = 7
|
|
|
|
y := b['apple'] // not necesarily `3`
|
|
|
|
|
|
|
|
// iteration over elements
|
|
|
|
for k, v in b {
|
2022-10-28 22:27:38 +03:00
|
|
|
// concurrently changed k/v pairs may or may not be included
|
2020-07-11 00:45:03 +03:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
This is handy, but since other coroutines might access the `array`/`map`
|
|
|
|
concurrently between the automatically locked statements, the results
|
|
|
|
are sometimes surprising. Each statement should be seen as a single
|
|
|
|
transaction that is unrelated to the previous or following
|
|
|
|
statement. Therefore - but also for performance reasons - it's often
|
|
|
|
better to group consecutive coherent statements in an explicit `lock` block.
|
2021-06-14 13:12:02 +03:00
|
|
|
|
|
|
|
### Channels
|