Skip to content

Commit

Permalink
Update README to use unsafe blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
syg committed Oct 1, 2024
1 parent 61a03b4 commit 38b83c0
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ shared struct SharedBox {
let sharedBox = new SharedBox();
let sharedBox2 = new SharedBox();

sharedBox.x = 42; // x is declared and rhs is primitive
sharedBox.x = sharedBox2; // x is declared and rhs is shared
assertThrows(() => { sharedBox.x = {}; }) // rhs is not a shared struct
unsafe {
sharedBox.x = 42; // x is declared and rhs is primitive
sharedBox.x = sharedBox2; // x is declared and rhs is shared
assertThrows(() => { sharedBox.x = {}; }) // rhs is not a shared struct
}

// can programmatically test if a value can be shared
assert(Reflect.canBeShared(sharedBox2));
Expand All @@ -110,16 +112,20 @@ assert(!Reflect.canBeShared({}));
let worker = new Worker('worker.js');
worker.postMessage({ sharedBox });

sharedBox.x = "main"; // x is declared and rhs is primitive
console.log(sharedBox.x);
unsafe {
sharedBox.x = "main"; // x is declared and rhs is primitive
console.log(sharedBox.x);
}
```

```javascript
// worker.js
onmessage = function(e) {
let sharedBox = e.data.sharedBox;
sharedBox.x = "worker"; // x is declared and rhs is primitive
console.log(sharedBox.x);
unsafe {
sharedBox.x = "worker"; // x is declared and rhs is primitive
console.log(sharedBox.x);
}
};
```

Expand All @@ -135,25 +141,31 @@ Shared Arrays are a fixed-length arrays that may be shared across agents. They a

```javascript
let sharedArray = new SharedArray(100);
assert(sharedArray.length === 100);
for (i = 0; i < sharedArray.length; i++) {
// like shared structs, all elements are initialized to undefined
assert(sharedArray[i] === undefined);
unsafe {
assert(sharedArray.length === 100);
for (i = 0; i < sharedArray.length; i++) {
// like shared structs, all elements are initialized to undefined
assert(sharedArray[i] === undefined);
}
}

let worker = new Worker('worker_array.js');
worker.postMessage({ sharedArray });

sharedArray[0] = "main";
console.log(sharedArray[0]);
unsafe {
sharedArray[0] = "main";
console.log(sharedArray[0]);
}
```

```javascript
// worker_array.js
onmessage = function(e) {
let sharedArray = e.data.sharedArray;
sharedArray[0] = "worker";
console.log(sharedArray[0]);
unsafe {
sharedArray[0] = "worker";
console.log(sharedArray[0]);
}
};
```

Expand Down Expand Up @@ -281,13 +293,13 @@ let worker = new Worker('worker_mutex.js');
worker.postMessage({ point });

// assume this agent can block
{
unsafe {
using lock = Atomics.Mutex.lock(point.mutex);
point.x = "main";
point.y = "main";
}

{
unsafe {
using lock = Atomics.Mutex.lock(point.mutex);
console.log(point.x, point.y);
}
Expand All @@ -297,7 +309,7 @@ worker.postMessage({ point });
// worker_mutex.js
onmessage = function(e) {
let point = e.data.point;
{
unsafe {
using lock = Atomics.Mutex.lock(point.mutex);
point.x = "worker";
point.y = "worker";
Expand Down

0 comments on commit 38b83c0

Please sign in to comment.