Skip to content

Commit

Permalink
mapM_ (#36)
Browse files Browse the repository at this point in the history
* Fix RSE set

* Fix RSE set

* mapM_

* clean up

Co-authored-by: Bowen Fu <missing>
  • Loading branch information
BowenFu authored Jul 23, 2022
1 parent 31a2f51 commit 006289f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
13 changes: 13 additions & 0 deletions include/hspp.h
Original file line number Diff line number Diff line change
Expand Up @@ -4129,6 +4129,19 @@ constexpr auto apply = toGFunc<1> | [](auto p)

} // namespace parser

// For io
constexpr auto mapM_ = toGFunc<2> | [](auto func, auto lst)
{
return data::io([=]
{
for (auto e : lst)
{
(e >>= func).run();
}
return _o_;
});
};

} // namespace hspp

#endif // HSPP_PARSER_H
48 changes: 37 additions & 11 deletions test/hspp/stm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ struct TVar
IORef<std::vector<MVar<_O_>>> waitQueue;
};

template <typename A>
struct RSE
{
ID id;
Expand All @@ -428,6 +427,12 @@ struct RSE
IORef<std::vector<MVar<_O_>>> waitQueue;
};

bool operator<(RSE const& lhs, RSE const& rhs)
{
auto result = (lhs.id <compare> rhs.id);
return result == Ordering::kLT;
}

template <typename A>
struct WSE
{
Expand All @@ -443,15 +448,19 @@ constexpr auto toWSE = toGFunc<5> | [](Lock lock, IORef<Integer> writeStamp, aut
return WSE{lock, writeStamp, content, waitQueue, newValue};
};

// optimize me later
class ReadSet
{
using T = std::map<ID, std::any>;
using T = std::set<RSE>;
public:
std::shared_ptr<T> data = std::make_shared<T>();
};

using WriteSet = ReadSet;
class WriteSet
{
using T = std::map<ID, std::any>;
public:
std::shared_ptr<T> data = std::make_shared<T>();
};

using TId = std::thread::id;
using Stamp = Integer;
Expand All @@ -474,7 +483,8 @@ IORef<Integer> globalClock{initIORef<Integer>(1)};

constexpr auto readIORef = toGFunc<1> | [](auto const& ioRef)
{
return io([&ioRef]{
return io([&ioRef]
{
return ioRef.data->load();
});
};
Expand Down Expand Up @@ -583,7 +593,14 @@ constexpr auto putWS = toFunc<> | [](WriteSet ws, ID id, std::any ptr)
});
};

constexpr auto putRS = putWS;
constexpr auto putRS = toFunc<> | [](ReadSet rs, RSE entry)
{
return io([=]
{
rs.data->insert(entry);
return _o_;
});
};

constexpr auto lookUpWS = toFunc<> | [](WriteSet ws, ID id)
{
Expand Down Expand Up @@ -730,9 +747,15 @@ class MonadBase<STM>
namespace concurrent
{

constexpr auto myTId = io([]
{
return 2 * std::hash<std::thread::id>{}(std::this_thread::get_id());
});

constexpr auto newTState = io([]
{
return TState{std::this_thread::get_id(), {}, {}, {}};
auto const readStamp = readIORef(globalClock).run();
return TState{std::this_thread::get_id(), readStamp, {}, {}};
});

template <typename A, typename Func>
Expand All @@ -747,10 +770,13 @@ auto atomicallyImpl(STM<A, Func> const& stmac)
return std::visit(overload(
[=](Valid<A> const& v_) -> A
{
(void)v_;
// auto [nts, a] = v_;
// auto transid = nts.transId;
// auto writeSet = nts.writeSet;
auto [nts, a] = v_;
auto ti = myTId.run();
assert(ti == (nts.transId));
(void)ti;
(void)a;
// auto ws = nts.writeSet;
// tup = getLocks | nts.transId | wslist;
return A{};
},
[=](Retry const&)
Expand Down
17 changes: 17 additions & 0 deletions test/hspp/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1474,3 +1474,20 @@ TEST(do_, comprehension3)
auto const expected = std::vector<std::tuple<int, int, int>>{ { 3, 4, 5 }, { 6, 8, 10 }, { 5, 12, 13 }, { 9, 12, 15 }, { 8, 15, 17 } };
EXPECT_EQ(result, expected);
}

TEST(MapM_, IO)
{
testing::internal::CaptureStdout();

const auto lst = std::vector{ioData("3"s), ioData("4"s)};
const auto func = putStrLn;
const auto mapM_result = mapM_ | func | lst;

std::string output0 = testing::internal::GetCapturedStdout();
EXPECT_EQ(output0, "");

testing::internal::CaptureStdout();
EXPECT_EQ(mapM_result.run(), _o_);
std::string output1 = testing::internal::GetCapturedStdout();
EXPECT_EQ(output1, "3\n4\n");
}

0 comments on commit 006289f

Please sign in to comment.