Skip to content
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

[wip] Added watching and compare-and-set! for atoms #532

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions pixie/stdlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -3110,3 +3110,15 @@ ex: (vary-meta x assoc :foo 42)"
:added "0.1"}
[f x]
(->Iterate f x))

(defn compare-and-set!
{:doc "Atomically sets the value of the atom to newval iff the current value of atom is oldval.
Returns true if reset happens, else false."
:signatures [[atom oldval newval]]
:added "0.2"}
[atom oldval newval]
(if (= @atom oldval)
(do
(reset! atom newval)
true)
false)
21 changes: 18 additions & 3 deletions pixie/vm/atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,28 @@ def with_meta(self, meta):
def meta(self):
return self._meta

def __init__(self, boxed_value, meta=nil):
def with_watch(self, watch_key, watch_fn):
return Atom(self._boxed_value,
self._meta,
watch_key,
watch_fn)

def __init__(self, boxed_value,
meta=nil,
watch_key=nil, watch_fn=nil):
self._boxed_value = boxed_value
self._meta = meta

self._watch_key = watch_key
self._watch_fn = watch_fn

@extend(proto._reset_BANG_, Atom)
def _reset(self, v):
assert isinstance(self, Atom)
if self._watch_fn is not nil:
self._watch_fn.invoke([self._watch_key, self, self._boxed_value, v])
self._boxed_value = v
return v


@extend(proto._deref, Atom)
def _deref(self):
assert isinstance(self, Atom)
Expand All @@ -40,6 +50,11 @@ def _with_meta(self, meta):
assert isinstance(self, Atom)
return self.with_meta(meta)

@extend(proto._with_watch, Atom)
def _with_watch(self, watch_key, watch_fn):
assert isinstance(self, Atom)
return self.with_watch(watch_key, watch_fn)

@as_var("atom")
def atom(val=nil):
return Atom(val)
7 changes: 7 additions & 0 deletions pixie/vm/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@

defprotocol("pixie.stdlib", "IMessageObject", ["-call-method", "-get-attr"])

defprotocol("pixie.stdlib", "IWatch", ["-with-watch"])
defprotocol("pixie.stdlib", "IValidate", ["-with-validator"])

def maybe_mark_finalizer(self, tp):
if self is _finalize_BANG_:
print "MARKING ", tp
Expand Down Expand Up @@ -278,6 +281,10 @@ def __meta(a):
def __with_meta(a, b):
return rt._with_meta(a, b)

@as_var("with-watch")
def __with_watch(a, b, c):
return rt._with_watch(a, b, c)

@returns(bool)
@as_var("has-meta?")
def __has_meta(a):
Expand Down