-
Notifications
You must be signed in to change notification settings - Fork 80
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
Contrib separation & ftl::Trampoline #69
Open
martty
wants to merge
2
commits into
RichieSams:master
Choose a base branch
from
martty:contrib-trampoline
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* FiberTaskingLib - A tasking library that uses fibers for efficient task switching | ||
* | ||
* This library was created as a proof of concept of the ideas presented by | ||
* Christian Gyrling in his 2015 GDC Talk 'Parallelizing the Naughty Dog Engine Using Fibers' | ||
* | ||
* http://gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine | ||
* | ||
* FiberTaskingLib is the legal property of Adrian Astley | ||
* Copyright Adrian Astley 2015 - 2018 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "ftl/task.h" | ||
|
||
#include <type_traits> | ||
#include <tuple> | ||
#include <functional> | ||
|
||
namespace ftl { | ||
class TaskScheduler; | ||
|
||
template<class F, class... Args> struct BoundTrampoline; | ||
|
||
template<class F> | ||
struct Trampoline { | ||
Trampoline(F&& f) : handler(std::move(f)) {} | ||
F handler; | ||
|
||
template<class... Args> | ||
BoundTrampoline<F, Args...>* bind(Args&&... args) { | ||
return new BoundTrampoline<F, Args...>(*this, std::forward_as_tuple(args...)); | ||
} | ||
}; | ||
|
||
template<class F> | ||
Trampoline<F> make_trampoline(F&& f) { | ||
Trampoline<F> t(std::forward<F>(f)); | ||
return t; | ||
} | ||
|
||
/* | ||
C++11 machinery to power std::apply | ||
*/ | ||
namespace detail { | ||
// based on http://stackoverflow.com/a/17426611/410767 by Xeo | ||
template <size_t... Ints> | ||
struct index_sequence { | ||
using type = index_sequence; | ||
using value_type = size_t; | ||
static constexpr std::size_t size() noexcept { return sizeof...(Ints); } | ||
}; | ||
|
||
// -------------------------------------------------------------- | ||
|
||
template <class Sequence1, class Sequence2> | ||
struct _merge_and_renumber; | ||
|
||
template <size_t... I1, size_t... I2> | ||
struct _merge_and_renumber<index_sequence<I1...>, index_sequence<I2...>> | ||
: index_sequence<I1..., (sizeof...(I1) + I2)...> {}; | ||
|
||
// -------------------------------------------------------------- | ||
|
||
template <size_t N> | ||
struct make_index_sequence | ||
: _merge_and_renumber<typename make_index_sequence<N / 2>::type, | ||
typename make_index_sequence<N - N / 2>::type> {}; | ||
|
||
template<> struct make_index_sequence<0> : index_sequence<> {}; | ||
template<> struct make_index_sequence<1> : index_sequence<0> {}; | ||
|
||
|
||
template<typename F, typename... Args> | ||
auto invoke(F f, Args&&... args) -> decltype(std::ref(f)(std::forward<Args>(args)...)) { | ||
return std::ref(f)(std::forward<Args>(args)...); | ||
} | ||
|
||
template <class F, class Tuple, std::size_t... I> | ||
auto apply_impl(F&& f, Tuple&& t, detail::index_sequence<I...>) -> decltype(std::ref(f)(std::get<I>(std::forward<Tuple>(t))...)) { | ||
return invoke(std::forward<F>(f), std::get<I>(std::forward<Tuple>(t))...); | ||
} | ||
|
||
} // namespace detail | ||
|
||
template<class F, class... Args> | ||
struct BoundTrampoline { | ||
Trampoline<F> tramp; | ||
std::tuple<Args...> args; | ||
|
||
friend struct Trampoline<F>; | ||
private: | ||
BoundTrampoline(Trampoline<F> func, std::tuple<Args...>&& args) : tramp(func), args(std::move(args)) {} | ||
public: | ||
BoundTrampoline(const BoundTrampoline&) = delete; | ||
BoundTrampoline& operator=(const BoundTrampoline&) = delete; | ||
|
||
BoundTrampoline(BoundTrampoline&&) = default; | ||
BoundTrampoline& operator=(BoundTrampoline&&) = default; | ||
|
||
// call task | ||
static void gencall(ftl::TaskScheduler* ts, void * arg) { | ||
auto t = static_cast<BoundTrampoline<F, Args...>*>(arg); | ||
detail::apply_impl(t->tramp.handler, t->args,detail::make_index_sequence<std::tuple_size<typename std::remove_reference<decltype(args)>::type>::value>{}); | ||
delete t; | ||
} | ||
|
||
operator Task() { | ||
return Task{ &gencall, static_cast<void*>(this) }; | ||
} | ||
|
||
~BoundTrampoline() = default; | ||
}; | ||
|
||
} // End of namespace ftl |
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 |
---|---|---|
|
@@ -152,5 +152,4 @@ void AtomicCounter::CheckWaitingFibers(uint value) { | |
} | ||
} | ||
|
||
|
||
} // End of namespace ftl |
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,61 @@ | ||
/** | ||
* FiberTaskingLib - A tasking library that uses fibers for efficient task switching | ||
* | ||
* This library was created as a proof of concept of the ideas presented by | ||
* Christian Gyrling in his 2015 GDC Talk 'Parallelizing the Naughty Dog Engine Using Fibers' | ||
* | ||
* http://gdcvault.com/play/1022186/Parallelizing-the-Naughty-Dog-Engine | ||
* | ||
* FiberTaskingLib is the legal property of Adrian Astley | ||
* Copyright Adrian Astley 2015 - 2018 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "ftl/atomic_counter.h" | ||
#include "ftl/task_scheduler.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "ftl/contrib/trampoline.h" | ||
|
||
struct Foo {}; | ||
|
||
/* | ||
Showcases Trampoline functionality | ||
|
||
Features: | ||
- automatic type safe interface and lambdas-as-tasks | ||
- error if mismatched type/count of arguments (if a bit cryptic) | ||
- no need to repeat types | ||
|
||
Glossary: | ||
- Trampoline: wrapper around a lambda / function | ||
- BoundTrampoline: Trampoline + arguments | ||
*/ | ||
|
||
void TrampolineMainTask(ftl::TaskScheduler *taskScheduler, void *arg) { | ||
Foo f; | ||
int a = 3, b = 4, d = 6; | ||
|
||
ftl::AtomicCounter counter(taskScheduler); | ||
taskScheduler->AddTask(*ftl::make_trampoline([](int& a, const int& b, const Foo& f, const int d) { a++; }).bind(a, b, f, d), &counter); | ||
taskScheduler->WaitForCounter(&counter, 0); | ||
// values are correctly captured and const is respected | ||
std::cout << a << '\n'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of the cout, can you do a GTEST_ASSERT_*() ? |
||
} | ||
|
||
TEST(ContribTests, Trampoline) { | ||
ftl::TaskScheduler taskScheduler; | ||
taskScheduler.Run(400, TrampolineMainTask); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline