PageRenderTime 293ms CodeModel.GetById 121ms app.highlight 4ms RepoModel.GetById 107ms app.codeStats 0ms

/mordor/sleep.cpp

http://github.com/mozy/mordor
C++ | 48 lines | 39 code | 8 blank | 1 comment | 5 complexity | 7d1ed7dbc7173a14d043f09c146b1742 MD5 | raw file
 1// Copyright (c) 2009 - Mozy, Inc.
 2
 3#include <boost/bind.hpp>
 4
 5#include "sleep.h"
 6
 7#include "assert.h"
 8#include "fiber.h"
 9#include "scheduler.h"
10#include "timer.h"
11
12namespace Mordor {
13
14static void scheduleMe(Scheduler *scheduler, Fiber::ptr fiber)
15{
16    scheduler->schedule(fiber);
17}
18
19void
20sleep(TimerManager &timerManager, unsigned long long us)
21{
22    MORDOR_ASSERT(Scheduler::getThis());
23    timerManager.registerTimer(us,
24        boost::bind(&scheduleMe, Scheduler::getThis(), Fiber::getThis()));
25    Scheduler::yieldTo();
26}
27
28void
29sleep(unsigned long long us)
30{
31#ifdef WINDOWS
32    Sleep((DWORD)(us / 1000));
33#else
34    struct timespec ts;
35    ts.tv_sec = us / 1000000;
36    ts.tv_nsec = (us % 1000000) * 1000;
37    while (true) {
38        if (nanosleep(&ts, &ts) == -1) {
39            if (errno == EINTR)
40                continue;
41            MORDOR_THROW_EXCEPTION_FROM_LAST_ERROR_API("nanosleep");
42        }
43        break;
44    }
45#endif
46}
47
48}