| Line | |
|---|
| 1 | // =========================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : arb_sleep.h // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in May 2013 // |
|---|
| 7 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // =========================================================== // |
|---|
| 11 | |
|---|
| 12 | #ifndef ARB_SLEEP_H |
|---|
| 13 | #define ARB_SLEEP_H |
|---|
| 14 | |
|---|
| 15 | #ifndef _UNISTD_H |
|---|
| 16 | #include <unistd.h> |
|---|
| 17 | #endif |
|---|
| 18 | #ifndef _GLIBCXX_ALGORITHM |
|---|
| 19 | #include <algorithm> |
|---|
| 20 | #endif |
|---|
| 21 | |
|---|
| 22 | enum TimeUnit { USEC = 1, MS = 1000, SEC = 1000*MS }; |
|---|
| 23 | |
|---|
| 24 | inline void GB_sleep(int amount, TimeUnit tu) { // @@@ rename into ARB_sleep |
|---|
| 25 | usleep(amount*tu); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | class ARB_inc_sleep { |
|---|
| 29 | useconds_t curr_wait, max_wait, inc; |
|---|
| 30 | |
|---|
| 31 | void slowdown() { curr_wait = std::min(max_wait, curr_wait+inc); } |
|---|
| 32 | public: |
|---|
| 33 | ARB_inc_sleep(int min_amount, int max_amount, TimeUnit tu, int increment) |
|---|
| 34 | : curr_wait(min_amount*tu), |
|---|
| 35 | max_wait(max_amount*tu), |
|---|
| 36 | inc(increment*tu) |
|---|
| 37 | {} |
|---|
| 38 | |
|---|
| 39 | void sleep() { |
|---|
| 40 | fprintf(stderr, "pid %i waits %lu usec\n", getpid(), (unsigned long)curr_wait); |
|---|
| 41 | usleep(curr_wait); |
|---|
| 42 | slowdown(); |
|---|
| 43 | } |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | #else |
|---|
| 47 | #error arb_sleep.h included twice |
|---|
| 48 | #endif // ARB_SLEEP_H |
|---|
Note: See
TracBrowser
for help on using the repository browser.