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 |
|
---|
10 | namespace ariba {
|
---|
11 | namespace transport {
|
---|
12 | namespace detail {
|
---|
13 |
|
---|
14 | using namespace boost::asio;
|
---|
15 | using namespace std;
|
---|
16 |
|
---|
17 | asio_io_service* asio_io_service::singleton = NULL;
|
---|
18 |
|
---|
19 | //#define DBG(x) cout << x << endl;
|
---|
20 | #define DBG(x)
|
---|
21 |
|
---|
22 | void 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 |
|
---|
36 | asio_io_service::asio_io_service() :
|
---|
37 | references(1), running(false), destroy(false), thread(NULL), service(NULL) {
|
---|
38 | service = new io_service();
|
---|
39 | }
|
---|
40 |
|
---|
41 | asio_io_service::~asio_io_service() {
|
---|
42 | if (thread != NULL) delete thread;
|
---|
43 | if (service != NULL) delete service;
|
---|
44 | thread = NULL;
|
---|
45 | service = NULL;
|
---|
46 | }
|
---|
47 |
|
---|
48 | void asio_io_service::internal_start() {
|
---|
49 | if (!running) {
|
---|
50 | if (thread != NULL) delete thread;
|
---|
51 | thread = new boost::thread(boost::ref(*this));
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | void asio_io_service::internal_stop() {
|
---|
56 | service->stop();
|
---|
57 | singleton->running = false;
|
---|
58 | }
|
---|
59 |
|
---|
60 | io_service& asio_io_service::alloc() {
|
---|
61 | if (singleton != NULL) {
|
---|
62 | DBG("new asio io_service reference");
|
---|
63 | singleton->references++;
|
---|
64 | return *singleton->service;
|
---|
65 | } else {
|
---|
66 | DBG("creating new asio io_service singleton");
|
---|
67 | singleton = new asio_io_service();
|
---|
68 | return *singleton->service;
|
---|
69 | }
|
---|
70 | }
|
---|
71 |
|
---|
72 | void asio_io_service::free() {
|
---|
73 | if (singleton != NULL) {
|
---|
74 | DBG("decreasing asio io_service reference");
|
---|
75 | singleton->references--;
|
---|
76 | if (singleton->references == 0) {
|
---|
77 | DBG("request asio io_service destruction");
|
---|
78 | if (singleton->running == false) {
|
---|
79 | delete singleton;
|
---|
80 | singleton = NULL;
|
---|
81 | DBG("asio io_service singleton destroyed");
|
---|
82 | } else {
|
---|
83 | singleton->destroy = true;
|
---|
84 | singleton->service->stop();
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void asio_io_service::start() {
|
---|
91 | singleton->internal_start();
|
---|
92 | }
|
---|
93 |
|
---|
94 | void asio_io_service::stop() {
|
---|
95 | singleton->internal_stop();
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | }}} // namespace ariba::transport::detail
|
---|