[5284] | 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() {
|
---|
[5705] | 42 | if (running) {
|
---|
| 43 | service->stop();
|
---|
| 44 | thread->join();
|
---|
| 45 | }
|
---|
[5284] | 46 | if (thread != NULL) delete thread;
|
---|
| 47 | if (service != NULL) delete service;
|
---|
| 48 | thread = NULL;
|
---|
| 49 | service = NULL;
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | void 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 |
|
---|
| 59 | void asio_io_service::internal_stop() {
|
---|
| 60 | service->stop();
|
---|
| 61 | singleton->running = false;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | io_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 |
|
---|
| 76 | void 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 |
|
---|
| 94 | void asio_io_service::start() {
|
---|
| 95 | singleton->internal_start();
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void asio_io_service::stop() {
|
---|
| 99 | singleton->internal_stop();
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | }}} // namespace ariba::transport::detail
|
---|