]> git.sesse.net Git - casparcg/blob - common/thread_info.cpp
Remove most of boost::lexical_cast.
[casparcg] / common / thread_info.cpp
1 /*
2 * Copyright 2013 Sveriges Television AB http://casparcg.com/
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "stdafx.h"
23
24 #include "thread_info.h"
25 #include "os/threading.h"
26
27 #include <map>
28 #include <mutex>
29
30 #include <boost/thread/tss.hpp>
31
32 namespace caspar {
33
34 class enumerable_thread_infos
35 {
36         std::mutex                                                                                              mutex_;
37         std::map<void*, std::weak_ptr<thread_info>>                                     enumerable_;
38         boost::thread_specific_ptr<std::shared_ptr<thread_info>>        infos_;
39 public:
40         static enumerable_thread_infos& get_instance()
41         {
42                 static enumerable_thread_infos instance;
43
44                 return instance;
45         }
46
47         std::vector<spl::shared_ptr<thread_info>> get_thread_infos()
48         {
49                 std::lock_guard<std::mutex> lock(mutex_);
50
51                 std::vector<spl::shared_ptr<thread_info>> result;
52                 result.reserve(enumerable_.size());
53
54                 for (auto it = enumerable_.begin(); it != enumerable_.end();)
55                 {
56                         auto strong = it->second.lock();
57
58                         if (strong)
59                         {
60                                 result.push_back(spl::make_shared_ptr(strong));
61                                 ++it;
62                         }
63                         else
64                         {
65                                 it = enumerable_.erase(it);
66                         }
67                 }
68
69                 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; });
70
71                 return result;
72         }
73
74         thread_info& get_thread_info()
75         {
76                 auto local = infos_.get();
77
78                 if (!local)
79                 {
80                         std::unique_ptr<thread_info> p(new thread_info);
81                         local = new std::shared_ptr<thread_info>(p.get(), [this](thread_info* p)
82                         {
83                                 std::lock_guard<std::mutex> lock(mutex_);
84                                 enumerable_.erase(p);
85                                 delete p;
86                         });
87                         p.release();
88                         infos_.reset(local);
89                         std::lock_guard<std::mutex> lock(mutex_);
90                         enumerable_.insert(std::make_pair(local->get(), *local));
91                 }
92
93                 return **local;
94         }
95 };
96
97 thread_info::thread_info()
98         : native_id(get_current_thread_id())
99 {
100 }
101
102 thread_info& get_thread_info()
103 {
104         return enumerable_thread_infos::get_instance().get_thread_info();
105 }
106
107 std::vector<spl::shared_ptr<thread_info>> get_thread_infos()
108 {
109         return enumerable_thread_infos::get_instance().get_thread_infos();
110 }
111
112 }