This file is indexed.

/usr/include/pion/scheduler.hpp is in libpion-dev 5.0.4+dfsg-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// ---------------------------------------------------------------------
// pion:  a Boost C++ framework for building lightweight HTTP interfaces
// ---------------------------------------------------------------------
// Copyright (C) 2007-2012 Cloudmeter, Inc.  (http://www.cloudmeter.com)
//
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
//

#ifndef __PION_SCHEDULER_HEADER__
#define __PION_SCHEDULER_HEADER__

#include <vector>
#include <boost/asio.hpp>
#include <boost/assert.hpp>
#include <boost/bind.hpp>
#include <boost/function/function0.hpp>
#include <boost/cstdint.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/thread/condition.hpp>
#include <pion/config.hpp>
#include <pion/logger.hpp>


namespace pion {    // begin namespace pion

///
/// scheduler: combines Boost.ASIO with a managed thread pool for scheduling
/// 
class PION_API scheduler :
    private boost::noncopyable
{
public:

    /// constructs a new scheduler
    scheduler(void)
        : m_logger(PION_GET_LOGGER("pion.scheduler")),
        m_num_threads(DEFAULT_NUM_THREADS), m_active_users(0), m_is_running(false)
    {}
    
    /// virtual destructor
    virtual ~scheduler() {}

    /// Starts the thread scheduler (this is called automatically when necessary)
    virtual void startup(void) {}
    
    /// Stops the thread scheduler (this is called automatically when the program exits)
    virtual void shutdown(void);

    /// the calling thread will sleep until the scheduler has stopped
    void join(void);
    
    /// registers an active user with the thread scheduler.  Shutdown of the
    /// scheduler is deferred until there are no more active users.  This
    /// ensures that any work queued will not reference destructed objects
    void add_active_user(void);

    /// unregisters an active user with the thread scheduler
    void remove_active_user(void);
    
    /// returns true if the scheduler is running
    inline bool is_running(void) const { return m_is_running; }
    
    /// sets the number of threads to be used (these are shared by all servers)
    inline void set_num_threads(const boost::uint32_t n) { m_num_threads = n; }
    
    /// returns the number of threads currently in use
    inline boost::uint32_t get_num_threads(void) const { return m_num_threads; }

    /// sets the logger to be used
    inline void set_logger(logger log_ptr) { m_logger = log_ptr; }

    /// returns the logger currently in use
    inline logger get_logger(void) { return m_logger; }
    
    /// returns an async I/O service used to schedule work
    virtual boost::asio::io_service& get_io_service(void) = 0;
    
    /**
     * schedules work to be performed by one of the pooled threads
     *
     * @param work_func work function to be executed
     */
    virtual void post(boost::function0<void> work_func) {
        get_io_service().post(work_func);
    }
    
    /**
     * thread function used to keep the io_service running
     *
     * @param my_service IO service used to re-schedule keep_running()
     * @param my_timer deadline timer used to keep the IO service active while running
     */
    void keep_running(boost::asio::io_service& my_service,
                     boost::asio::deadline_timer& my_timer);
    
    /**
     * puts the current thread to sleep for a specific period of time
     *
     * @param sleep_sec number of entire seconds to sleep for
     * @param sleep_nsec number of nanoseconds to sleep for (10^-9 in 1 second)
     */
    inline static void sleep(boost::uint32_t sleep_sec, boost::uint32_t sleep_nsec) {
        boost::system_time wakeup_time(get_wakeup_time(sleep_sec, sleep_nsec));
        boost::thread::sleep(wakeup_time);
    }

    /**
     * puts the current thread to sleep for a specific period of time, or until
     * a wakeup condition is signaled
     *
     * @param wakeup_condition if signaled, the condition will wakeup the thread early
     * @param wakeup_lock scoped lock protecting the wakeup condition
     * @param sleep_sec number of entire seconds to sleep for
     * @param sleep_nsec number of nanoseconds to sleep for (10^-9 in 1 second)
     */
    template <typename ConditionType, typename LockType>
    inline static void sleep(ConditionType& wakeup_condition, LockType& wakeup_lock,
                             boost::uint32_t sleep_sec, boost::uint32_t sleep_nsec)
    {
        boost::system_time wakeup_time(get_wakeup_time(sleep_sec, sleep_nsec));
        wakeup_condition.timed_wait(wakeup_lock, wakeup_time);
    }
    
    
    /// processes work passed to the asio service & handles uncaught exceptions
    void process_service_work(boost::asio::io_service& service);


protected:

    /**
     * calculates a wakeup time in boost::system_time format
     *
     * @param sleep_sec number of seconds to sleep for
     * @param sleep_nsec number of nanoseconds to sleep for
     *
     * @return boost::system_time time to wake up from sleep
     */
    static boost::system_time get_wakeup_time(boost::uint32_t sleep_sec,
        boost::uint32_t sleep_nsec);

    /// stops all services used to schedule work
    virtual void stop_services(void) {}
    
    /// stops all threads used to perform work
    virtual void stop_threads(void) {}

    /// finishes all services used to schedule work
    virtual void finish_services(void) {}

    /// finishes all threads used to perform work
    virtual void finish_threads(void) {}
    
    
    /// default number of worker threads in the thread pool
    static const boost::uint32_t    DEFAULT_NUM_THREADS;

    /// number of nanoseconds in one full second (10 ^ 9)
    static const boost::uint32_t    NSEC_IN_SECOND;

    /// number of microseconds in one full second (10 ^ 6)
    static const boost::uint32_t    MICROSEC_IN_SECOND;
    
    /// number of seconds a timer should wait for to keep the IO services running
    static const boost::uint32_t    KEEP_RUNNING_TIMER_SECONDS;


    /// mutex to make class thread-safe
    boost::mutex                    m_mutex;
    
    /// primary logging interface used by this class
    logger                          m_logger;

    /// condition triggered when there are no more active users
    boost::condition                m_no_more_active_users;

    /// condition triggered when the scheduler has stopped
    boost::condition                m_scheduler_has_stopped;

    /// total number of worker threads in the pool
    boost::uint32_t                 m_num_threads;

    /// the scheduler will not shutdown until there are no more active users
    boost::uint32_t                 m_active_users;

    /// true if the thread scheduler is running
    bool                            m_is_running;
};

    
///
/// multi_thread_scheduler: uses a pool of threads to perform work
/// 
class PION_API multi_thread_scheduler :
    public scheduler
{
public:
    
    /// constructs a new single_service_scheduler
    multi_thread_scheduler(void) {}
    
    /// virtual destructor
    virtual ~multi_thread_scheduler() {}

    
protected:
    
    /// stops all threads used to perform work
    virtual void stop_threads(void) {
        if (! m_thread_pool.empty()) {
            PION_LOG_DEBUG(m_logger, "Waiting for threads to shutdown");
            
            // wait until all threads in the pool have stopped
            boost::thread current_thread;
            for (ThreadPool::iterator i = m_thread_pool.begin();
                 i != m_thread_pool.end(); ++i)
            {
                // make sure we do not call join() for the current thread,
                // since this may yield "undefined behavior"
                if (**i != current_thread) (*i)->join();
            }
        }
    }
    
    /// finishes all threads used to perform work
    virtual void finish_threads(void) { m_thread_pool.clear(); }

    
    /// typedef for a pool of worker threads
    typedef std::vector<boost::shared_ptr<boost::thread> >  ThreadPool;
    
    
    /// pool of threads used to perform work
    ThreadPool              m_thread_pool;
};
    
    
///
/// single_service_scheduler: uses a single IO service to schedule work
/// 
class PION_API single_service_scheduler :
    public multi_thread_scheduler
{
public:
    
    /// constructs a new single_service_scheduler
    single_service_scheduler(void)
        : m_service(), m_timer(m_service)
    {}
    
    /// virtual destructor
    virtual ~single_service_scheduler() { shutdown(); }
    
    /// returns an async I/O service used to schedule work
    virtual boost::asio::io_service& get_io_service(void) { return m_service; }
    
    /// Starts the thread scheduler (this is called automatically when necessary)
    virtual void startup(void);
        
    
protected:
    
    /// stops all services used to schedule work
    virtual void stop_services(void) { m_service.stop(); }
    
    /// finishes all services used to schedule work
    virtual void finish_services(void) { m_service.reset(); }

    
    /// service used to manage async I/O events
    boost::asio::io_service         m_service;
    
    /// timer used to periodically check for shutdown
    boost::asio::deadline_timer     m_timer;
};
    

///
/// one_to_one_scheduler: uses a single IO service for each thread
/// 
class PION_API one_to_one_scheduler :
    public multi_thread_scheduler
{
public:
    
    /// constructs a new one_to_one_scheduler
    one_to_one_scheduler(void)
        : m_service_pool(), m_next_service(0)
    {}
    
    /// virtual destructor
    virtual ~one_to_one_scheduler() { shutdown(); }
    
    /// returns an async I/O service used to schedule work
    virtual boost::asio::io_service& get_io_service(void) {
        boost::mutex::scoped_lock scheduler_lock(m_mutex);
        while (m_service_pool.size() < m_num_threads) {
            boost::shared_ptr<service_pair_type>  service_ptr(new service_pair_type());
            m_service_pool.push_back(service_ptr);
        }
        if (++m_next_service >= m_num_threads)
            m_next_service = 0;
        BOOST_ASSERT(m_next_service < m_num_threads);
        return m_service_pool[m_next_service]->first;
    }
    
    /**
     * returns an async I/O service used to schedule work (provides direct
     * access to avoid locking when possible)
     *
     * @param n integer number representing the service object
     */
    virtual boost::asio::io_service& get_io_service(boost::uint32_t n) {
        BOOST_ASSERT(n < m_num_threads);
        BOOST_ASSERT(n < m_service_pool.size());
        return m_service_pool[n]->first;
    }

    /// Starts the thread scheduler (this is called automatically when necessary)
    virtual void startup(void);
    
    
protected:
    
    /// stops all services used to schedule work
    virtual void stop_services(void) {
        for (service_pool_type::iterator i = m_service_pool.begin(); i != m_service_pool.end(); ++i) {
            (*i)->first.stop();
        }
    }
        
    /// finishes all services used to schedule work
    virtual void finish_services(void) { m_service_pool.clear(); }
    

    /// typedef for a pair object where first is an IO service and second is a deadline timer
    struct service_pair_type {
        service_pair_type(void) : first(), second(first) {}
        boost::asio::io_service         first;
        boost::asio::deadline_timer     second;
    };
    
    /// typedef for a pool of IO services
    typedef std::vector<boost::shared_ptr<service_pair_type> >        service_pool_type;

    
    /// pool of IO services used to schedule work
    service_pool_type   m_service_pool;

    /// the next service to use for scheduling work
    boost::uint32_t     m_next_service;
};
    
    
}   // end namespace pion

#endif