Skip to content

Commit

Permalink
Improve visualization and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
benson1029 committed Apr 9, 2024
1 parent 9c4eb36 commit 2e927c5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 12 deletions.
87 changes: 75 additions & 12 deletions docs/docs/language-spec/concurrent.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,61 @@ sidebar_position: 4

# Concurrent Constructs

## Wait Groups

The `sync` package provides the `WaitGroup` type for synchronizing goroutines. The syntax for declaring a `WaitGroup` is as follows:

```go
var wg sync.WaitGroup
```

The `Add` method is used to increment the counter of the `WaitGroup`.

```go
wg.Add(1)
```

The `Done` method is used to decrement the counter of the `WaitGroup`.

```go
wg.Done()
```

The `Wait` method is used to block until the counter of the `WaitGroup` becomes zero.

```go
wg.Wait()
```

The following example demonstrates the use of a `WaitGroup` to synchronize goroutines.

```go
package main

import (
"fmt"
"sync"
)

func worker(id int32, wg sync.WaitGroup) {
fmt.Println("Worker", id, "starting")
wg.Done()
}

func main() {
var wg sync.WaitGroup
for i := 1; i <= 5; i++ {
wg.Add(1)
go worker(i, wg)
}
wg.Wait()
fmt.Println("All workers done")
}
```

## Mutex

The syntax for declaring a mutex is as follows:
The `sync` package also provides the `Mutex` type for synchronizing access to shared variables. The syntax for declaring a mutex is as follows:

```go
var m sync.Mutex
Expand Down Expand Up @@ -36,22 +88,26 @@ The following example demonstrates the use of a mutex to synchronize access to a
```go
package main

import "fmt"
import (
"fmt"
"sync"
)

var count int32
var wg sync.WaitGroup

func increment() {
for i := 0; i < 100; i++ {
count++
}
wg.Done()
}

func main() {
wg.Add(2)
go increment()
go increment()
for i := 0; i <= 1000; i++ {
// busy wait
}
wg.Wait()
fmt.Println(count) // prints 100
}
```
Expand All @@ -67,6 +123,7 @@ The following example demonstrates the use of a mutex to synchronize access to a
)
var count int32
var wg sync.WaitGroup
var m sync.Mutex
func increment() {
Expand All @@ -75,14 +132,14 @@ The following example demonstrates the use of a mutex to synchronize access to a
count++
m.Unlock()
}
wg.Done()
}
func main() {
wg.Add(2)
go increment()
go increment()
for i := 0; i <= 1000; i++ {
// busy wait
}
wg.Wait()
fmt.Println(count) // prints 200
}
```
Expand All @@ -108,7 +165,12 @@ The following example demonstrates the use of channels to synchronize goroutines
```go
package main
import "fmt"
import (
"fmt"
"sync"
)
var wg sync.WaitGroup
func producer(ch chan int32) {
for i := 0; i < 5; i++ {
Expand All @@ -118,21 +180,22 @@ func producer(ch chan int32) {
fmt.Println("Producer done") // this does not get printed
// making the channel buffered will allow the producer to continue
// by changing the channel declaration to "ch := make(chan int32, 1)"
wg.Done()
}
func consumer(ch chan int32) {
for i := 0; i < 4; i++ {
fmt.Println(<-ch)
}
fmt.Println("Consumer done")
wg.Done()
}
func main() {
wg.Add(2)
ch := make(chan int32)
go producer(ch)
go consumer(ch)
for i := 0; i <= 1000; i++ {
// busy wait
}
wg.Wait()
}
```
5 changes: 5 additions & 0 deletions src/go/heap/types/user/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ class UserChannel extends HeapObject {
result += this.isClosed() ? "closed" : "open";
return result;
}

public to_object(): any {
const buffer = this.buffer().to_object();
return "Channel [" + buffer.join(" ") + "]";
}
}

export { UserChannel };
13 changes: 13 additions & 0 deletions src/go/heap/types/user/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Heap } from "../../heap";
import { TAG_USER_slice } from "../tags";
import { HeapObject } from "../objects";
import { ComplexArray } from "../complex/array";
import { UserVariable } from "./variable";

class UserSlice extends HeapObject {
public get_length(): number {
Expand Down Expand Up @@ -46,6 +47,18 @@ class UserSlice extends HeapObject {
return "slice (" + this.get_length() + "/" + this.get_capacity() + ")"
+ " of " + this.get_underlying_array().stringify_i() + " at " + this.get_offset();
}

public to_object(): any {
let result = "(" + this.get_length() + "/" + this.get_capacity() + ") [";
for (let i = 0; i < this.get_length(); i++) {
result += (this.get_underlying_array().get_value_address(i + this.get_offset()) as UserVariable).get_value().to_object();
if (i + 1 < this.get_length()) {
result += " ";
}
}
result += "]";
return result;
}
}

export { UserSlice };

0 comments on commit 2e927c5

Please sign in to comment.