"docs/vscode:/vscode.git/clone" did not exist on "456b390c60bdbb5d0b0e6e06fa03d7ccfdf4aa75"
Commit efb502bf authored by Brian Pickrell's avatar Brian Pickrell
Browse files

Added mutex locks in register_target.cpp and created a multithreading test. Fails.

parent ac310ae5
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include <mutex>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <migraphx/register_target.hpp> #include <migraphx/register_target.hpp>
...@@ -33,6 +34,9 @@ inline namespace MIGRAPHX_INLINE_NS { ...@@ -33,6 +34,9 @@ inline namespace MIGRAPHX_INLINE_NS {
void store_target_lib(const dynamic_loader& lib) void store_target_lib(const dynamic_loader& lib)
{ {
static std::vector<dynamic_loader> target_loader; static std::vector<dynamic_loader> target_loader;
std::mutex mutex;
std::unique_lock<std::mutex> lock(mutex);
target_loader.emplace_back(lib); target_loader.emplace_back(lib);
} }
...@@ -46,14 +50,28 @@ void register_target_init() { (void)target_map(); } ...@@ -46,14 +50,28 @@ void register_target_init() { (void)target_map(); }
void unregister_target(const std::string& name) void unregister_target(const std::string& name)
{ {
std::mutex mutex;
std::unique_lock<std::mutex> lock(mutex);
assert(target_map().count(name)); assert(target_map().count(name));
target_map().erase(name); target_map().erase(name);
} }
void register_target(const target& t) { target_map()[t.name()] = t; } void register_target(const target& t)
{
std::mutex mutex;
std::unique_lock<std::mutex> lock(mutex);
target_map()[t.name()] = t;
}
target make_target(const std::string& name) target make_target(const std::string& name)
{ {
// debug
for(auto pp : target_map())
std::cout << pp.first << "\n";
std::mutex mutex;
std::unique_lock<std::mutex> lock(mutex);
if(not contains(target_map(), name)) if(not contains(target_map(), name))
{ {
std::string target_name = "libmigraphx_" + name + ".so"; std::string target_name = "libmigraphx_" + name + ".so";
......
...@@ -23,6 +23,9 @@ ...@@ -23,6 +23,9 @@
*/ */
#include <migraphx/register_target.hpp> #include <migraphx/register_target.hpp>
#include <migraphx/target.hpp> #include <migraphx/target.hpp>
#include <migraphx/par_for.hpp>
#include <mutex>
#include <thread>
#include "test.hpp" #include "test.hpp"
TEST_CASE(make_target) TEST_CASE(make_target)
...@@ -50,4 +53,21 @@ TEST_CASE(targets) ...@@ -50,4 +53,21 @@ TEST_CASE(targets)
EXPECT(ts.size() >= 1); EXPECT(ts.size() >= 1);
} }
TEST_CASE(concurrent_targets)
{
std::vector<std::thread> threads;
for(auto i = 0u; i < 10000; i++)
{
auto thread_body = []() {
auto ref_target = migraphx::make_target("gpu");
migraphx::register_target(ref_target);
};
threads.emplace_back(thread_body);
}
for(auto& tt : threads)
tt.join();
}
int main(int argc, const char* argv[]) { test::run(argc, argv); } int main(int argc, const char* argv[]) { test::run(argc, argv); }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment