source: source/ariba/utility/transport/asio/asio_io_service.cpp@ 5705

Last change on this file since 5705 was 5705, checked in by mies, 15 years ago
File size: 2.2 KB
Line 
1// Internal version: Please do not publish!
2// (... until released under FreeBSD-like license *g*)
3// Code: Sebastian Mies <mies@tm.uka.de>
4
5#include "asio_io_service.h"
6
7#include <boost/asio.hpp>
8#include <boost/thread.hpp>
9
10namespace ariba {
11namespace transport {
12namespace detail {
13
14using namespace boost::asio;
15using namespace std;
16
17asio_io_service* asio_io_service::singleton = NULL;
18
19//#define DBG(x) cout << x << endl;
20#define DBG(x)
21
22void asio_io_service::operator ()() {
23 running = true;
24 DBG("io_service started");
25 boost::asio::io_service::work work(*service);
26 service->run();
27 DBG("io_service stopped");
28 if (destroy) {
29 delete singleton;
30 singleton = NULL;
31 DBG(cout << "asio io_service singleton destroyed" << endl);
32 }
33 running = false;
34}
35
36asio_io_service::asio_io_service() :
37 references(1), running(false), destroy(false), thread(NULL), service(NULL) {
38 service = new io_service();
39}
40
41asio_io_service::~asio_io_service() {
42 if (running) {
43 service->stop();
44 thread->join();
45 }
46 if (thread != NULL) delete thread;
47 if (service != NULL) delete service;
48 thread = NULL;
49 service = NULL;
50}
51
52void asio_io_service::internal_start() {
53 if (!running) {
54 if (thread != NULL) delete thread;
55 thread = new boost::thread(boost::ref(*this));
56 }
57}
58
59void asio_io_service::internal_stop() {
60 service->stop();
61 singleton->running = false;
62}
63
64io_service& asio_io_service::alloc() {
65 if (singleton != NULL) {
66 DBG("new asio io_service reference");
67 singleton->references++;
68 return *singleton->service;
69 } else {
70 DBG("creating new asio io_service singleton");
71 singleton = new asio_io_service();
72 return *singleton->service;
73 }
74}
75
76void asio_io_service::free() {
77 if (singleton != NULL) {
78 DBG("decreasing asio io_service reference");
79 singleton->references--;
80 if (singleton->references == 0) {
81 DBG("request asio io_service destruction");
82 if (singleton->running == false) {
83 delete singleton;
84 singleton = NULL;
85 DBG("asio io_service singleton destroyed");
86 } else {
87 singleton->destroy = true;
88 singleton->service->stop();
89 }
90 }
91 }
92}
93
94void asio_io_service::start() {
95 singleton->internal_start();
96}
97
98void asio_io_service::stop() {
99 singleton->internal_stop();
100}
101
102
103}}} // namespace ariba::transport::detail
Note: See TracBrowser for help on using the repository browser.