]> git.sesse.net Git - casparcg/blob - protocol/asio/io_service_manager.cpp
Improved read_fps.
[casparcg] / protocol / asio / io_service_manager.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 "io_service_manager.h"
25
26 #include <memory>
27
28 #include <boost/asio/io_service.hpp>
29 #include <boost/thread/thread.hpp>
30
31 #include <common/exception/win32_exception.h>
32
33 namespace caspar { namespace protocol { namespace asio {
34
35 struct io_service_manager::impl
36 {
37         boost::asio::io_service service_;
38         // To keep the io_service::run() running although no pending async
39         // operations are posted.
40         std::unique_ptr<boost::asio::io_service::work> work_;
41         boost::thread thread_;
42
43         impl()
44                 : work_(new boost::asio::io_service::work(service_))
45                 , thread_([this] { run(); })
46         {
47         }
48
49         void run()
50         {
51                 win32_exception::ensure_handler_installed_for_thread("asio-thread");
52
53                 service_.run();
54         }
55
56         ~impl()
57         {
58                 work_.reset();
59                 service_.stop();
60                 thread_.join();
61         }
62 };
63
64 io_service_manager::io_service_manager()
65         : impl_(new impl)
66 {
67 }
68
69 io_service_manager::~io_service_manager()
70 {
71 }
72
73 boost::asio::io_service& io_service_manager::service()
74 {
75         return impl_->service_;
76 }
77
78 }}}