new_thread.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. /*
  3. * Copyright 2018-present Facebook, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <folly/experimental/pushmi/executor.h>
  18. #include <folly/experimental/pushmi/trampoline.h>
  19. namespace pushmi {
  20. // very poor perf example executor.
  21. //
  22. struct new_thread_executor {
  23. using properties = property_set<
  24. is_sender<>,
  25. is_executor<>,
  26. is_never_blocking<>,
  27. is_concurrent_sequence<>,
  28. is_single<>>;
  29. new_thread_executor executor() {
  30. return {};
  31. }
  32. PUSHMI_TEMPLATE(class Out)
  33. (requires Receiver<Out>)
  34. void submit(Out out) {
  35. std::thread t{[out = std::move(out)]() mutable {
  36. auto tr = ::pushmi::trampoline();
  37. ::pushmi::submit(tr, std::move(out));
  38. }};
  39. // pass ownership of thread to out
  40. t.detach();
  41. }
  42. };
  43. inline new_thread_executor new_thread() {
  44. return {};
  45. }
  46. } // namespace pushmi