diff --git a/data_structures/list/lockfree_list2.hpp b/data_structures/list/lockfree_list2.hpp deleted file mode 100644 index a9d762cff..000000000 --- a/data_structures/list/lockfree_list2.hpp +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef MEMGRAPH_DATA_STRUCTURES_LOCKFREE_LIST2_HPP -#define MEMGRAPH_DATA_STRUCTURES_LOCKFREE_LIST2_HPP - -#include - -#include "threading/sync/spinlock.hpp" -#include "threading/sync/lockable.hpp" - -namespace lockfree -{ - -template -class List2 : Lockable -{ - struct Node { T value; std::atomic next; }; - - class iterator - { - iterator(Node* node) : node(node) {} - - T& operator*() const { return *node; } - T* operator->() const { return node; } - - bool end() const - { - return node == nullptr; - } - - iterator& operator++() - { - node = node->next.load(std::memory_order_relaxed); - return *this; - } - - iterator& operator++(int) - { - return operator++(); - } - - protected: - Node* node; - }; - -public: - - ~List2() - { - Node* next, node = head.load(std::memory_order_relaxed); - - for(; node != nullptr; node = next) - { - next = node->next; - delete node; - } - } - - void insert(T value) - { - auto guard = acquire(); - head.store(new Node { value, head }); - } - - iterator begin() { return iterator(head.load()); } - -private: - std::atomic head; -}; - -} - -#endif diff --git a/promise.cpp b/promise.cpp new file mode 100644 index 000000000..8df711aef --- /dev/null +++ b/promise.cpp @@ -0,0 +1,115 @@ +#include +#include + +#include "server/uv/uv.hpp" +#include "server/http/http.hpp" + +template +class Promise +{ +public: + Promise(std::function f, Args&&... args) + { + result = f(std::forward(args)...); + } + + template + Promise then(std::function f) + { + return Promise(f, std::forward(result)); + } + + T get() + { + return result; + } + +private: + + + T result; + std::atomic completed; +}; + +class TaskPool +{ + template + class Task + { + public: + using task_t = Task; + using work_f = std::function; + using after_work_f = std::function; + + Task(work_f work, after_work_f callback) + : work(work), callback(callback) + { + req.data = this; + } + + void launch(uv::UvLoop& loop) + { + uv_queue_work(loop, &req, work_cb, after_work_cb); + } + + private: + std::function work; + std::function callback; + + R result; + + uv_work_t req; + + static void work_cb(uv_work_t* req) + { + auto& task = *reinterpret_cast(req->data); + } + + static void after_work_cb(uv_work_t* req, int) + { + auto task = reinterpret_cast(req->data); + delete task; + } + }; + +public: + TaskPool(uv::UvLoop& loop) : loop(loop) {} + + template + void launch(std::function func, + std::function callback) + { + auto task = new Task(func, callback); + task->launch(loop); + } + +private: + uv::UvLoop& loop; +}; + + + +int main(void) +{ + uv::UvLoop loop; + TaskPool tp(loop); + + tp.launch([](void) -> int { return 3 }, + [](int x) -> void { std::cout << x << std::endl; }); + +// http::HttpServer server(loop); +// +// http::Ipv4 ip("0.0.0.0", 3400); +// +// server.listen(ip, [](http::Request& req, http::Response& res) { +// +// +// +// res.send(req.url); +// }); +// +// loop.run(uv::UvLoop::Mode::Default); + + + return 0; +} diff --git a/threading/sync/timed_spinlock.hpp b/threading/sync/timed_spinlock.hpp index 5758843b6..77eb6aa3d 100644 --- a/threading/sync/timed_spinlock.hpp +++ b/threading/sync/timed_spinlock.hpp @@ -12,6 +12,7 @@ class LockExpiredError : public std::runtime_error using runtime_error::runtime_error; }; +template class TimedSpinLock { public: @@ -36,7 +37,7 @@ public: if(clock::now() - start > expiration) throw LockExpiredError("This lock has expired"); - usleep(250); + usleep(microseconds); } } diff --git a/threading/task.hpp b/threading/task.hpp deleted file mode 100644 index d5be264c3..000000000 --- a/threading/task.hpp +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef MEMGRAPH_THREADING_TASK_HPP -#define MEMGRAPH_THREADING_TASK_HPP - -class Task -{ -public: - -private: - //std::function< -}; - -#endif diff --git a/utils/mark_ref.hpp b/utils/mark_ref.hpp new file mode 100644 index 000000000..b98d5c731 --- /dev/null +++ b/utils/mark_ref.hpp @@ -0,0 +1,45 @@ +#ifndef MEMGRAPH_UTILS_MARK_REF_HPP +#define MEMGRAPH_UTILS_MARK_REF_HPP + +#include + +template +struct MarkRef +{ + MarkRef() = default; + MarkRef(MarkRef&) = default; + MarkRef(MarkRef&&) = default; + + bool is_marked() const + { + return ptr & 0x1L; + } + + bool set_mark() + { + return ptr |= 0x1L; + } + + bool clear_mark() + { + return ptr &= ~0x1L; + } + + T* get() const + { + return reinterpret_cast(ptr & ~0x1L); + } + + T& operator*() { return *get(); } + T* operator->() { return get(); } + + uintptr_t ptr; +}; + +//template +//MarkRef make_markref(Args&&... args) +//{ +// +//} + +#endif