FireBreath  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
TimerService.cpp
1 /**********************************************************\
2 Original Author: Mital Vora <mital.d.vora@gmail.com>
3 
4 Created: Mar 26, 2011
5 License: Dual license model; choose one of two:
6  New BSD License
7  http://www.opensource.org/licenses/bsd-license.php
8  - or -
9  GNU Lesser General Public License, version 2.1
10  http://www.gnu.org/licenses/lgpl-2.1.html
11 
12 \**********************************************************/
13 
14 #include "win_targetver.h"
15 #include "precompiled_headers.h" // On windows, everything above this line in PCH
16 #include "TimerService.h"
17 #include <boost/asio.hpp>
18 
19 using namespace FB;
20 
21 namespace FB {
22  class TimerServicePimpl {
23  public:
24  TimerServicePimpl() :
25  io_service(new boost::asio::io_service()),
26  io_idlework(new boost::asio::io_service::work(*io_service)),
27  io_thread(NULL) {}
28 
29  ~TimerServicePimpl() {
30  io_service->stop();
31  io_thread->join();
32  io_idlework.reset();
33  io_thread.reset();
34  io_service.reset();
35  }
36 
37  boost::scoped_ptr<boost::asio::io_service> io_service;
38  boost::scoped_ptr<boost::asio::io_service::work> io_idlework;
39  boost::scoped_ptr<boost::thread> io_thread;
40  };
41 };
42 
43 TimerServiceWeakPtr TimerService::inst;
44 boost::mutex TimerService::instance_mutex;
45 
46 TimerServicePtr TimerService::instance()
47 {
48  boost::mutex::scoped_lock lock(instance_mutex);
49  TimerServicePtr service(inst.lock());
50  if(!service)
51  {
52  service = TimerServicePtr(new TimerService());
53  inst = service;
54  }
55  return service;
56 }
57 
58 TimerService::TimerService() : pimpl(new TimerServicePimpl)
59 {
60  pimpl->io_thread.reset(new boost::thread(
61  boost::bind(&boost::asio::io_service::run, pimpl->io_service.get())));
62 }
63 
64 TimerService::~TimerService()
65 {
66 }
67 
68 boost::asio::io_service* TimerService::getIOService()
69 {
70  return pimpl->io_service.get();
71 }
TimerService Utility.
Definition: TimerService.h:38