blob: 5752685e7e1d50c55cc83e3ae38a9c0793ba3f64 [file] [log] [blame]
Junxiao Shi0b5fbbb2014-02-20 15:54:03 -07001/* -*- 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
9namespace nfd {
10
11const int LimitedIo::UNLIMITED_OPS = std::numeric_limits<int>::max();
12const time::Duration LimitedIo::UNLIMITED_TIME = time::nanoseconds(-1);
13
14LimitedIo::LimitedIo()
15 : m_isRunning(false)
16 , m_nOpsRemaining(0)
17{
18 resetGlobalIoService();
19}
20
21LimitedIo::StopReason
22LimitedIo::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
41void
42LimitedIo::afterOp()
43{
44 --m_nOpsRemaining;
45 if (m_nOpsRemaining <= 0) {
46 m_reason = EXCEED_OPS;
47 getGlobalIoService().stop();
48 }
49}
50
51void
52LimitedIo::afterTimeout()
53{
54 m_reason = EXCEED_TIME;
55 getGlobalIoService().stop();
56}
57
58} // namespace nfd