]> git.sesse.net Git - casparcg/blob - common/thread_info.cpp
[CHANGELOG] Updated
[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
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>
33
34 namespace caspar {
35
36 class enumerable_thread_infos
37 {
38         boost::mutex                                                                                            mutex_;
39         std::map<void*, std::weak_ptr<thread_info>>                                     enumerable_;
40         boost::thread_specific_ptr<std::shared_ptr<thread_info>>        infos_;
41 public:
42         static enumerable_thread_infos& get_instance()
43         {
44                 static enumerable_thread_infos instance;
45
46                 return instance;
47         }
48
49         std::vector<spl::shared_ptr<thread_info>> get_thread_infos()
50         {
51                 boost::lock_guard<boost::mutex> lock(mutex_);
52
53                 std::vector<spl::shared_ptr<thread_info>> result;
54                 result.reserve(enumerable_.size());
55
56                 for (auto it = enumerable_.begin(); it != enumerable_.end();)
57                 {
58                         auto strong = it->second.lock();
59
60                         if (strong)
61                         {
62                                 result.push_back(spl::make_shared_ptr(strong));
63                                 ++it;
64                         }
65                         else
66                         {
67                                 it = enumerable_.erase(it);
68                         }
69                 }
70
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; });
72
73                 return result;
74         }
75
76         thread_info& get_thread_info()
77         {
78                 auto local = infos_.get();
79
80                 if (!local)
81                 {
82                         std::unique_ptr<thread_info> p(new thread_info);
83                         local = new std::shared_ptr<thread_info>(p.get(), [this](thread_info* p)
84                         {
85                                 boost::lock_guard<boost::mutex> lock(mutex_);
86                                 enumerable_.erase(p);
87                                 delete p;
88                         });
89                         p.release();
90                         infos_.reset(local);
91                         boost::lock_guard<boost::mutex> lock(mutex_);
92                         enumerable_.insert(std::make_pair(local->get(), *local));
93                 }
94
95                 return **local;
96         }
97 };
98
99 thread_info::thread_info()
100         : native_id(get_current_thread_id())
101 {
102 }
103
104 thread_info& get_thread_info()
105 {
106         return enumerable_thread_infos::get_instance().get_thread_info();
107 }
108
109 std::vector<spl::shared_ptr<thread_info>> get_thread_infos()
110 {
111         return enumerable_thread_infos::get_instance().get_thread_infos();
112 }
113
114 }