Skip to content

Commit

Permalink
Add lock(rwlock) command
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Sep 28, 2024
1 parent 40e2653 commit d7eed3a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,22 @@ Unless otherwise specified, return the value before the memory operation
| stor | `m = n` |
| swpi | `m, mem[n] = mem[n], m` |
| swap | `m, n[i] = n[i], m` |
| lock | `rwlock(m, n, i)` |


Complex Commands
-------------------------------------------------------------------------------
- **rwlock(stat, mut, block)**:
- **block**: Block polling until locked
- **mut**: Acquire write lock or read lock
- **result**: Guard value, please subtract the value of guard when releasing the lock


Basic Usage Examples
-------------------------------------------------------------------------------

copy to multi processors

```
write 3 cell1 0
mematom add old cell1 0 4 0
Expand Down
2 changes: 1 addition & 1 deletion src/mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"displayName": "Logic-Atomics",
"author": "A4-Tacks",
"description": "Processor atomic operation extension.",
"version": "0.2.0",
"version": "0.2.1",
"minGameVersion": 136
}
40 changes: 36 additions & 4 deletions src/scripts/inst/memory-atomics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ const is_operable_memory = (exec, mem) => (
&& mem instanceof MemoryBlock.MemoryBuild
&& mem.team == exec.team);

const as_memory = (exec, mem) => is_operable_memory(exec, mem)
? mem.memory : undefined;
const as_memory = (exec, mem, index) => {
if (!is_operable_memory(exec, mem)) return undefined;

let arr = mem.memory;
if (index < 0 || index >= arr.length) return undefined;

return arr;
};

const check_num = (mem, idx) => {
let num = mem[idx];
Expand Down Expand Up @@ -215,7 +221,7 @@ const OPTIONS = {
},
run(exec, mem, index, args) {
let {oth, '#': idx} = args;
let other_mem = as_memory(exec, oth);
let other_mem = as_memory(exec, oth, idx);
if (other_mem === undefined) return undefined;

let old = mem[index];
Expand All @@ -224,6 +230,32 @@ const OPTIONS = {
return old;
},
},
lock: {
args: {
mut: ["0", "num"],
block: ["1", "num"],
},
run(exec, mem, index, args) {
let {mut, block} = args;
let stat = mem[index];
let guard = 0;

if (!mut && stat >= 0) {
guard = 1;
} else if (mut && !stat) {
guard = -1;
}

if (guard) {
// acquired
mem[index] += guard;
} else if (block) {
// poll
exec.counter.numval -= 1;
}
return guard;
},
},
};
const OPTIONS_KEYS = Object.keys(OPTIONS);

Expand Down Expand Up @@ -265,7 +297,7 @@ const MemoryAtomicsI = {
let mem, result;
let index = cvar(exec, 'numi', this.index);
let mem_building = cvar(exec, 'building', this.mem);
if ((mem = as_memory(exec, mem_building)) !== undefined) {
if ((mem = as_memory(exec, mem_building, index)) !== undefined) {
result = op.run(exec, mem, index, this.args); // running
check_num(mem, index);
}
Expand Down

0 comments on commit d7eed3a

Please sign in to comment.