Skip to content

Commit

Permalink
concurrent: port ES JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Giannini authored and Matthew Giannini committed Aug 14, 2023
1 parent 5f26340 commit 8281dce
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/concurrent/es/Actor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Copyright (c) 2009, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 02 Jun 09 Andy Frank Creation
// 13 May 10 Andy Frank Move from sys to concurrent
// 22 Jun 23 Matthew Giannini Refactor for ES
//

/**
* Actor.
*/
class Actor extends sys.Obj {
constructor() { super(); }

typeof$() { return Actor.type$; }

static #locals;

static locals() {
if (!Actor.#locals) {
const k = sys.Str.type$;
const v = sys.Obj.type$.toNonNullable();
Actor.#locals = sys.Map.make(k, v);
}
return Actor.#locals;
}
}
17 changes: 17 additions & 0 deletions src/concurrent/es/ActorFuture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Copyright (c) 2023, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 17 Mar 2023 Matthew Giannini Creation
// 22 Jun 2023 Matthew Giannini Refactor for JS
//

/**
* ActorFuture.
*/
class ActorFuture extends Future {
constructor() { super(); }

typeof$() { return ActorFuture.type$; }
}
38 changes: 38 additions & 0 deletions src/concurrent/es/ActorPool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Copyright (c) 2010, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 15 Mar 10 Andy Frank Creation
// 13 May 10 Andy Frank Move from sys to concurrent
// 22 Jun 23 Matthew Giannini Refactor for ES
//

/**
* ActorPool.
*/
class ActorPool extends sys.Obj {
constructor() {
super();
}

#name = "ActorPool";
name$() { return this.#name; }
__name$(it) { this.#name = it; }

#maxThreads = 100;
maxThreads() { return this.#maxThreads; }
__maxThreads(it) { this.#maxThreads = it; }

typeof$() { return ActorPool.type$; }

static make(f) {
const self = new ActorPool();
ActorPool.make$(self, f);
return self;
}

static make$(self, f) {
if (f) f(self);
}
}
35 changes: 35 additions & 0 deletions src/concurrent/es/AtomicBoolPeer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Copyright (c) 2015, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 17 Aug 2015 Matthew Giannini Creation
// 22 Jun 2023 Matthew Giannini Refactor for ES
//

/**
* AtomicBoolPeer
*/
class AtomicBoolPeer extends sys.Obj {
constructor() { super(); }

#val = false;
val(self, it) {
if (it === undefined) return this.#val;
this.#val = it;
}

getAndSet(self, val) {
const old = this.#val;
this.#val = val;
return old;
}

compareAndSet(self, expect, update) {
if (this.#val == expect) {
this.#val = update;
return true;
}
return false;
}
}
60 changes: 60 additions & 0 deletions src/concurrent/es/AtomicIntPeer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// Copyright (c) 2015, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 17 Aug 2015 Matthew Giannini Creation
// 22 Jun 2023 Matthew Giannini Refactor for ES
//

/**
* AtomicIntPeer
*/
class AtomicIntPeer extends sys.Obj {
constructor() { super(); }

#val = 0;
val(self, it) {
if (it === undefined) return this.#val;
this.#val = it;
}

getAndSet(self, val) {
const old = this.#val;
this.#val = val;
return old;
}

compareAndSet(self, expect, update) {
if (this.#val == expect) {
this.#val = update;
return true;
}
return false;
}

getAndIncrement(self) { return this.getAndAdd(self, 1); }

getAndDecrement(self) { return this.getAndAdd(self, -1); }

getAndAdd(self, delta) {
const old = this.#val;
this.#val = old + delta;
return old;
}

incrementAndGet(self) { return this.addAndGet(self, 1); }

decrementAndGet(self) { return this.addAndGet(self, -1); }

addAndGet(self, delta) {
this.#val = this.#val + delta;
return this.#val;
}

increment(self) { this.add(self, 1); }

decrement(self) { this.add(self, -1); }

add(self, delta) { this.#val = this.#val + delta; }
}
36 changes: 36 additions & 0 deletions src/concurrent/es/AtomicRefPeer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Copyright (c) 2012, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 02 Nov 2012 Andy Frank Creation
// 22 Jun 2023 Matthew Giannini Refactor for ES
//

/**
* AtomicRefPeer.
*/
class AtomicRefPeer extends sys.Obj {
constructor() { super(); }

#val = null;
val(self, it) {
if (it === undefined) return this.#val;
if (!sys.ObjUtil.isImmutable(it)) throw sys.NotImmutableErr.make();
this.#val = it;
}

getAndSet(self, val) {
if (!sys.ObjUtil.isImmutable(val)) throw sys.NotImmutableErr.make();
const old = this.#val;
this.#val = val;
return old;
}

compareAndSet(self, expect, update) {
if (!sys.ObjUtil.isImmutable(update)) throw sys.NotImmutableErr.make();
if (this.#val != expect) return false;
this.#val = update;
return true;
}
}
89 changes: 89 additions & 0 deletions src/concurrent/es/ConcurrentMap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// Copyright (c) 2019, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 10 Jun 2019 Matthew Giannini Creation
// 22 Jun 2023 Matthew Giannini Refactor for ES
//

/**
* ConcurrentMap
*/
class ConcurrentMap extends sys.Obj {
constructor() {
super();
this.#map = sys.Map.make(sys.Obj.type$, sys.Obj.type$);
}

#map;

static make(capacity) {
const self = new ConcurrentMap();
return self;
}

typeof$() { return ConcurrentMap.type$; }

isEmpty() { return this.#map.isEmpty(); }

size() { return this.#map.size(); }

get(key) { return this.#map.get(key); }

set(key, val) { this.#map.set(key, this.#checkImmutable(val)); }

getAndSet(key, val) {
const old = this.get(key);
this.set(key, val);
return old;
}

add(key, val) {
if (this.containsKey(key)) throw sys.Err(`Key already mapped: ${key}`);
this.#map.add(key, this.#checkImmutable(val));
}

getOrAdd(key, defVal) {
let val = this.get(key);
if (val == null) this.add(key, val = defVal);
return val;
}

setAll(m) {
if (m.isImmutable()) this.#map.setAll(m);
else {
const vals = m.vals();
for (let i=0; i<vals.size(); ++i) { this.#checkImmutable(vals.get(i)); }
this.#map.setAll(m);
}
return this;
}

remove(key) { return this.#map.remove(key); }

clear() { this.#map.clear(); }

each(f) { this.#map.each(f); }

eachWhile(f) { return this.#map.eachWhile(f); }

containsKey(key) { return this.#map.containsKey(key); }

keys(of) {
const array = [];
this.#map.keys().each((key) => { array.push(key); });
return sys.List.make(of, array);
}

vals(of) {
const array = [];
this.#map.vals().each((val) => { array.push(val); });
return sys.List.make(of, array);
}

#checkImmutable(val) {
if (sys.ObjUtil.isImmutable(val)) return val;
throw sys.NotImmutableErr.make();
}
}
28 changes: 28 additions & 0 deletions src/concurrent/es/Future.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// Copyright (c) 2010, Brian Frank and Andy Frank
// Licensed under the Academic Free License version 3.0
//
// History:
// 15 Mar 2010 Andy Frank Creation
// 13 May 2010 Andy Frank Move from sys to concurrent
// 22 Jun 2023 Matthew Giannini Refactor for ES
//

/**
* Future.
*/
class Future extends sys.Obj {
constructor() { super(); }

typeof$() { return Future.type$; }

static makeCompletable() {
const self = new Future();
Future.make$(self);
return self;
}

static make$(self) {
}

}

0 comments on commit 8281dce

Please sign in to comment.