// Matrix Construct // // Copyright (C) Matrix Construct Developers, Authors & Contributors // Copyright (C) 2016-2019 Jason Volk // // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. #pragma once #define HAVE_IRCD_CTX_CONCURRENT_FOR_EACH_H namespace ircd::ctx { template struct concurrent_for_each; } template struct ircd::ctx::concurrent_for_each { using function = std::function; using initializer_list = std::initializer_list; size_t snd {0}; size_t rcv {0}; size_t fin {0}; std::exception_ptr eptr; concurrent_for_each(pool &p, const vector_view &, const function &); concurrent_for_each(pool &p, const function &, const initializer_list &); }; template ircd::ctx::concurrent_for_each::concurrent_for_each(pool &p, const function &func, const initializer_list &list) :concurrent_for_each { p, list, func } {} template ircd::ctx::concurrent_for_each::concurrent_for_each(pool &p, const vector_view &list, const function &func) { const uninterruptible::nothrow ui; latch latch(list.size()); for(auto it(begin(list)); it != end(list) && !eptr; ++it) { ++snd; p([this, &p, &list, &func, &latch] { ++rcv; try { func(list[rcv - 1]); } catch(...) { eptr = std::current_exception(); } ++fin; latch.count_down(); }); } latch.wait(); if(eptr) std::rethrow_exception(eptr); }