2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
24 #include "thread_info.h"
25 #include "os/threading.h"
29 #include <boost/thread/tss.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <boost/thread/lock_guard.hpp>
32 #include <boost/lexical_cast.hpp>
36 class enumerable_thread_infos
39 std::map<void*, std::weak_ptr<thread_info>> enumerable_;
40 boost::thread_specific_ptr<std::shared_ptr<thread_info>> infos_;
42 static enumerable_thread_infos& get_instance()
44 static enumerable_thread_infos instance;
49 std::vector<spl::shared_ptr<thread_info>> get_thread_infos()
51 boost::lock_guard<boost::mutex> lock(mutex_);
53 std::vector<spl::shared_ptr<thread_info>> result;
54 result.reserve(enumerable_.size());
56 for (auto it = enumerable_.begin(); it != enumerable_.end();)
58 auto strong = it->second.lock();
62 result.push_back(spl::make_shared_ptr(strong));
67 it = enumerable_.erase(it);
71 std::sort(result.begin(), result.end(), [](const spl::shared_ptr<thread_info>& lhs, const spl::shared_ptr<thread_info>& rhs) { return lhs->native_id < rhs->native_id; });
76 thread_info& get_thread_info()
78 auto local = infos_.get();
82 std::unique_ptr<thread_info> p(new thread_info);
83 local = new std::shared_ptr<thread_info>(p.get(), [this](thread_info* p)
85 boost::lock_guard<boost::mutex> lock(mutex_);
91 boost::lock_guard<boost::mutex> lock(mutex_);
92 enumerable_.insert(std::make_pair(local->get(), *local));
99 thread_info::thread_info()
100 : native_id(get_current_thread_id())
104 thread_info& get_thread_info()
106 return enumerable_thread_infos::get_instance().get_thread_info();
109 std::vector<spl::shared_ptr<thread_info>> get_thread_infos()
111 return enumerable_thread_infos::get_instance().get_thread_infos();