-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
311 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#pragma once | ||
|
||
namespace jank::runtime | ||
{ | ||
template <> | ||
struct static_object<object_type::atom> : gc | ||
{ | ||
static constexpr native_bool pointer_free{ false }; | ||
|
||
static_object() = default; | ||
static_object(object_ptr o); | ||
|
||
/* behavior::objectable */ | ||
native_bool equal(object const &) const; | ||
native_persistent_string to_string() const; | ||
void to_string(fmt::memory_buffer &buff) const; | ||
native_hash to_hash() const; | ||
|
||
/* behavior::derefable */ | ||
object_ptr deref() const; | ||
|
||
object_ptr reset(object_ptr o); | ||
obj::persistent_vector_ptr reset_vals(object_ptr o); | ||
|
||
object_ptr swap(object_ptr fn); | ||
object_ptr swap(object_ptr fn, object_ptr a1); | ||
object_ptr swap(object_ptr fn, object_ptr a1, object_ptr a2); | ||
object_ptr swap(object_ptr fn, object_ptr a1, object_ptr a2, object_ptr rest); | ||
|
||
obj::persistent_vector_ptr swap_vals(object_ptr fn); | ||
obj::persistent_vector_ptr swap_vals(object_ptr fn, object_ptr a1); | ||
obj::persistent_vector_ptr swap_vals(object_ptr fn, object_ptr a1, object_ptr a2); | ||
obj::persistent_vector_ptr | ||
swap_vals(object_ptr fn, object_ptr a1, object_ptr a2, object_ptr rest); | ||
|
||
object_ptr compare_and_set(object_ptr old_val, object_ptr new_val); | ||
|
||
object base{ object_type::atom }; | ||
std::atomic<object *> val{}; | ||
}; | ||
|
||
namespace obj | ||
{ | ||
using atom = static_object<object_type::atom>; | ||
using atom_ptr = native_box<atom>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
#include <jank/runtime/obj/atom.hpp> | ||
|
||
namespace jank::runtime | ||
{ | ||
obj::atom::static_object(object_ptr const o) | ||
: val{ o } | ||
{ | ||
assert(val); | ||
} | ||
|
||
native_bool obj::atom::equal(object const &o) const | ||
{ | ||
return &o == &base; | ||
} | ||
|
||
native_persistent_string obj::atom::to_string() const | ||
{ | ||
fmt::memory_buffer buff; | ||
to_string(buff); | ||
return native_persistent_string{ buff.data(), buff.size() }; | ||
} | ||
|
||
void obj::atom::to_string(fmt::memory_buffer &buff) const | ||
{ | ||
fmt::format_to(std::back_inserter(buff), | ||
"{}@{}", | ||
magic_enum::enum_name(base.type), | ||
fmt::ptr(&base)); | ||
} | ||
|
||
native_hash obj::atom::to_hash() const | ||
{ | ||
return static_cast<native_hash>(reinterpret_cast<uintptr_t>(this)); | ||
} | ||
|
||
object_ptr obj::atom::deref() const | ||
{ | ||
return val.load(); | ||
} | ||
|
||
object_ptr obj::atom::reset(object_ptr const o) | ||
{ | ||
assert(o); | ||
val = o; | ||
return o; | ||
} | ||
|
||
obj::persistent_vector_ptr obj::atom::reset_vals(object_ptr const o) | ||
{ | ||
while(true) | ||
{ | ||
auto v(val.load()); | ||
if(val.compare_exchange_weak(v, o)) | ||
{ | ||
return make_box<obj::persistent_vector>(std::in_place, v, o); | ||
} | ||
} | ||
} | ||
|
||
/* NOLINTNEXTLINE(cppcoreguidelines-noexcept-swap) */ | ||
object_ptr obj::atom::swap(object_ptr const fn) | ||
{ | ||
while(true) | ||
{ | ||
auto v(val.load()); | ||
auto const next(dynamic_call(fn, v)); | ||
if(val.compare_exchange_weak(v, next)) | ||
{ | ||
return next; | ||
} | ||
} | ||
} | ||
|
||
/* NOLINTNEXTLINE(cppcoreguidelines-noexcept-swap) */ | ||
object_ptr obj::atom::swap(object_ptr fn, object_ptr a1) | ||
{ | ||
while(true) | ||
{ | ||
auto v(val.load()); | ||
auto const next(dynamic_call(fn, v, a1)); | ||
if(val.compare_exchange_weak(v, next)) | ||
{ | ||
return next; | ||
} | ||
} | ||
} | ||
|
||
/* NOLINTNEXTLINE(cppcoreguidelines-noexcept-swap) */ | ||
object_ptr obj::atom::swap(object_ptr fn, object_ptr a1, object_ptr a2) | ||
{ | ||
while(true) | ||
{ | ||
auto v(val.load()); | ||
auto const next(dynamic_call(fn, v, a1, a2)); | ||
if(val.compare_exchange_weak(v, next)) | ||
{ | ||
return next; | ||
} | ||
} | ||
} | ||
|
||
/* NOLINTNEXTLINE(cppcoreguidelines-noexcept-swap) */ | ||
object_ptr obj::atom::swap(object_ptr fn, object_ptr a1, object_ptr a2, object_ptr rest) | ||
{ | ||
while(true) | ||
{ | ||
auto v(val.load()); | ||
auto const next(apply_to(fn, conj(a1, conj(a2, rest)))); | ||
if(val.compare_exchange_weak(v, next)) | ||
{ | ||
return next; | ||
} | ||
} | ||
} | ||
|
||
obj::persistent_vector_ptr obj::atom::swap_vals(object_ptr const fn) | ||
{ | ||
while(true) | ||
{ | ||
auto v(val.load()); | ||
auto const next(dynamic_call(fn, v)); | ||
if(val.compare_exchange_weak(v, next)) | ||
{ | ||
return make_box<obj::persistent_vector>(std::in_place, v, next); | ||
} | ||
} | ||
} | ||
|
||
obj::persistent_vector_ptr obj::atom::swap_vals(object_ptr fn, object_ptr a1) | ||
{ | ||
while(true) | ||
{ | ||
auto v(val.load()); | ||
auto const next(dynamic_call(fn, v, a1)); | ||
if(val.compare_exchange_weak(v, next)) | ||
{ | ||
return make_box<obj::persistent_vector>(std::in_place, v, next); | ||
} | ||
} | ||
} | ||
|
||
obj::persistent_vector_ptr obj::atom::swap_vals(object_ptr fn, object_ptr a1, object_ptr a2) | ||
{ | ||
while(true) | ||
{ | ||
auto v(val.load()); | ||
auto const next(dynamic_call(fn, v, a1, a2)); | ||
if(val.compare_exchange_weak(v, next)) | ||
{ | ||
return make_box<obj::persistent_vector>(std::in_place, v, next); | ||
} | ||
} | ||
} | ||
|
||
obj::persistent_vector_ptr | ||
obj::atom::swap_vals(object_ptr fn, object_ptr a1, object_ptr a2, object_ptr rest) | ||
{ | ||
while(true) | ||
{ | ||
auto v(val.load()); | ||
auto const next(apply_to(fn, conj(a1, conj(a2, rest)))); | ||
if(val.compare_exchange_weak(v, next)) | ||
{ | ||
return make_box<obj::persistent_vector>(std::in_place, v, next); | ||
} | ||
} | ||
} | ||
|
||
object_ptr obj::atom::compare_and_set(object_ptr old_val, object_ptr new_val) | ||
{ | ||
object *old{ old_val }; | ||
return make_box(val.compare_exchange_weak(old, new_val)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters