Skip to content

Lock and thread #2590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
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
Next Next commit
First draft of minimal-mt
raddad772 committed Dec 10, 2022
commit 3a0a96e2d513d623153c4c2018e513fb28eb4a74
1 change: 1 addition & 0 deletions cli/index.js
Original file line number Diff line number Diff line change
@@ -300,6 +300,7 @@ export async function main(argv, options) {
switch (opts.runtime) {
case "stub": runtime = 0; break;
case "minimal": runtime = 1; break;
case "minimal-mt": runtime = 4; break;
/* incremental */
default: runtime = 2; break;
}
5 changes: 3 additions & 2 deletions std/assembly/rt/index-incremental.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import "rt/tlsf";
import "rt/itcms";
import "rt/tlsf-base";
import "rt/tlsf-st";
import "rt/itcms";
3 changes: 3 additions & 0 deletions std/assembly/rt/index-minimal-mt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import "rt/tlsf-base";
import "rt/tlsf-mt";
import "rt/tcms";
5 changes: 3 additions & 2 deletions std/assembly/rt/index-minimal.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import "rt/tlsf";
import "rt/tcms";
import "rt/tlsf-base";
import "rt/tlsf-st";
import "rt/tcms";
1,156 changes: 567 additions & 589 deletions std/assembly/rt/tlsf.ts → std/assembly/rt/tlsf-base.ts

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions std/assembly/rt/tlsf-mt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {BLOCK_OVERHEAD} from "./common";
import {allocateBlock, freeBlock, reallocateBlock, ROOT, TLSFinitialize, moveBlock, checkUsedBlock} from "./tlsf-base";
import {TlsfMutex_lock, TlsfMutex_unlock} from './tlsf-mutex'

const mutex_ptr = memory.data(4, 16);

// @ts-ignore: decorator
@global @unsafe
export function __alloc(size: usize): usize {
TlsfMutex_lock(mutex_ptr);

if (!ROOT) TLSFinitialize();
let r: usize = changetype<usize>(allocateBlock(ROOT, size)) + BLOCK_OVERHEAD;

TlsfMutex_unlock(mutex_ptr);
return r;
}

// @ts-ignore: decorator
@global @unsafe
export function __realloc(ptr: usize, size: usize): usize {
TlsfMutex_lock(mutex_ptr);

if (!ROOT) TLSFinitialize();
let r: usize = (ptr < __heap_base
? changetype<usize>(moveBlock(ROOT, checkUsedBlock(ptr), size))
: changetype<usize>(reallocateBlock(ROOT, checkUsedBlock(ptr), size))
) + BLOCK_OVERHEAD;

TlsfMutex_unlock(mutex_ptr);
return r;
}

// @ts-ignore: decorator
@global @unsafe
export function __free(ptr: usize): void {
if (ptr < __heap_base) return;

TlsfMutex_lock(mutex_ptr);

if (!ROOT) TLSFinitialize();
freeBlock(ROOT, checkUsedBlock(ptr));

TlsfMutex_unlock(mutex_ptr);
}
30 changes: 30 additions & 0 deletions std/assembly/rt/tlsf-mutex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This just implements a super-simple lock for tlsf-mt.ts

enum TlsfMutexState {
unlocked,
locked
}

// Basic spinlock. Spinning is not a performance issue since this only takes as long as an allocation
// @ts-ignore: decorator
@inline
export function TlsfMutex_lock(mutex_ptr: usize): void {
for (; ;) {
// If we succesfully atomically compare and exchange unlocked for locked, we have the mutex
if (atomic.cmpxchg<i32>(mutex_ptr, TlsfMutexState.unlocked, TlsfMutexState.locked) === TlsfMutexState.unlocked)
return;
// Wait for unlocked state to try for locked
for (; ;) {
if (atomic.load<i32>(mutex_ptr) === TlsfMutexState.unlocked) break;
}
}
}

// @ts-ignore: decorator
@inline
export function TlsfMutex_unlock(mutex_ptr: usize): void {
if (atomic.cmpxchg<i32>(mutex_ptr, TlsfMutexState.locked, TlsfMutexState.unlocked) !== TlsfMutexState.locked) {
// This only happens if someone else unlocked our mutex, or we did it more than once...
throw new Error('Is this the right thing to do here? Mutex in inconsistent state');
}
}
28 changes: 28 additions & 0 deletions std/assembly/rt/tlsf-st.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {BLOCK_OVERHEAD} from "./common";
import {allocateBlock, freeBlock, reallocateBlock, ROOT, TLSFinitialize, moveBlock, checkUsedBlock} from "./tlsf-base";

// @ts-ignore: decorator
@global @unsafe
export function __alloc(size: usize): usize {
if (!ROOT) TLSFinitialize();

return changetype<usize>(allocateBlock(ROOT, size)) + BLOCK_OVERHEAD;
}

// @ts-ignore: decorator
@global @unsafe
export function __realloc(ptr: usize, size: usize): usize {
if (!ROOT) TLSFinitialize();
return (ptr < __heap_base
? changetype<usize>(moveBlock(ROOT, checkUsedBlock(ptr), size))
: changetype<usize>(reallocateBlock(ROOT, checkUsedBlock(ptr), size))
) + BLOCK_OVERHEAD;
}

// @ts-ignore: decorator
@global @unsafe
export function __free(ptr: usize): void {
if (ptr < __heap_base) return;
if (!ROOT) TLSFinitialize();
freeBlock(ROOT, checkUsedBlock(ptr));
}