Junxiao Shi | 0b5fbbb | 2014-02-20 15:54:03 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "limited-io.hpp" |
| 8 | |
| 9 | namespace nfd { |
| 10 | |
| 11 | const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max(); |
| 12 | const time::Duration LimitedIo::UNLIMITED_TIME = time::nanoseconds(-1); |
| 13 | |
| 14 | LimitedIo::LimitedIo() |
| 15 | : m_isRunning(false) |
| 16 | , m_nOpsRemaining(0) |
| 17 | { |
| 18 | resetGlobalIoService(); |
| 19 | } |
| 20 | |
| 21 | LimitedIo::StopReason |
| 22 | LimitedIo::run(int nOpsLimit, time::Duration nTimeLimit) |
| 23 | { |
| 24 | BOOST_ASSERT(!m_isRunning); |
| 25 | m_isRunning = true; |
| 26 | |
| 27 | m_reason = NO_WORK; |
| 28 | m_nOpsRemaining = nOpsLimit; |
| 29 | if (nTimeLimit != UNLIMITED_TIME) { |
| 30 | m_timeout = scheduler::schedule(nTimeLimit, bind(&LimitedIo::afterTimeout, this)); |
| 31 | } |
| 32 | |
| 33 | getGlobalIoService().run(); |
| 34 | |
| 35 | getGlobalIoService().reset(); |
| 36 | scheduler::cancel(m_timeout); |
| 37 | m_isRunning = false; |
| 38 | return m_reason; |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | LimitedIo::afterOp() |
| 43 | { |
| 44 | --m_nOpsRemaining; |
| 45 | if (m_nOpsRemaining <= 0) { |
| 46 | m_reason = EXCEED_OPS; |
| 47 | getGlobalIoService().stop(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | LimitedIo::afterTimeout() |
| 53 | { |
| 54 | m_reason = EXCEED_TIME; |
| 55 | getGlobalIoService().stop(); |
| 56 | } |
| 57 | |
| 58 | } // namespace nfd |