From: Maxim Mamontov Date: Tue, 22 Mar 2011 15:01:42 +0000 (+0200) Subject: Change some usleep() to nanosleep() `cause usleep() is obsolete since X-Git-Tag: 2.407-rc3~114 X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/e1c08c2c174c75d7c5cd2ec5d0b974c6f240ba7e Change some usleep() to nanosleep() `cause usleep() is obsolete since POSIX.2001 --- diff --git a/projects/stargazer/main.cpp b/projects/stargazer/main.cpp index 63d5a6b0..2c789ff6 100644 --- a/projects/stargazer/main.cpp +++ b/projects/stargazer/main.cpp @@ -386,6 +386,7 @@ switch (stgChildPid) break; default: + struct timespec ts = {0, 200000000}; for (int i = 0; i < 120 * 5; i++) { if (access(startFile.c_str(), F_OK) == 0) @@ -399,7 +400,7 @@ switch (stgChildPid) unlink(startFile.c_str()); exit(1); } - usleep(200000); + nanosleep(&ts, NULL); } unlink(startFile.c_str()); exit(1); diff --git a/projects/stargazer/stg_timer.cpp b/projects/stargazer/stg_timer.cpp index 7903c38d..b17153cf 100644 --- a/projects/stargazer/stg_timer.cpp +++ b/projects/stargazer/stg_timer.cpp @@ -1,6 +1,6 @@ -#include #include +#include #include #include "common.h" @@ -61,11 +61,15 @@ isTimerRunning = true; while (nonstop) { #ifdef STG_TIMER_DEBUG - usleep(1000000 / TIME_SPEED); + struct timespec ts = {0, 1000000000 / TIME_SPEED}; + nanosleep(&ts); + //usleep(1000000 / TIME_SPEED); stgTime++; #else + struct timespec ts = {0, 500000000}; + nanosleep(&ts); + //usleep(500000); stgTime = time(NULL); - usleep(500000); #endif } isTimerRunning = false; @@ -104,9 +108,13 @@ return isTimerRunning; int stgUsleep(unsigned long t) { #ifdef STG_TIMER_DEBUG -return usleep(t / TIME_SPEED); +struct timespec ts = {(t / TIME_SPEED) / 1000000, ((t / TIME_SPEED) % 1000000) * 1000}; +return nanosleep(&ts); +//return usleep(t / TIME_SPEED); #else -return usleep(t); +struct timespec ts = {t / 1000000, (t % 1000000) * 1000}; +return nanosleep(&ts); +//return usleep(t); #endif } //----------------------------------------------------------------------------- @@ -116,5 +124,3 @@ void WaitTimer() stgUsleep(200000); } //----------------------------------------------------------------------------- - - diff --git a/projects/stargazer/traffcounter.cpp b/projects/stargazer/traffcounter.cpp index 9697cde0..dd672f37 100644 --- a/projects/stargazer/traffcounter.cpp +++ b/projects/stargazer/traffcounter.cpp @@ -139,9 +139,10 @@ while (users->SearchNext(h, &u) == 0) users->CloseSearch(h); //5 seconds to thread stops itself +struct timespec ts = {0, 200000000}; for (int i = 0; i < 25 && !stopped; i++) { - usleep(200000); + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now kill it @@ -166,10 +167,10 @@ tc->stopped = false; int c = 0; time_t touchTime = stgTime - MONITOR_TIME_DELAY_SEC; - +struct timespec ts = {0, 500000000}; while (tc->running) { - usleep(500000); + nanosleep(&ts, 0); if (!tc->running) { tc->FlushAndRemove(); diff --git a/projects/stargazer/users_impl.cpp b/projects/stargazer/users_impl.cpp index e58e7224..24955187 100644 --- a/projects/stargazer/users_impl.cpp +++ b/projects/stargazer/users_impl.cpp @@ -467,13 +467,13 @@ if (!isRunning) nonstop = false; //5 seconds to thread stops itself -unsigned i; -for (i = 0; i < 25 * (users.size() / 50 + 1); i++) +struct timespec ts = {0, 200000000}; +for (size_t i = 0; i < 25 * (users.size() / 50 + 1); i++) { if (!isRunning) break; - usleep(200000); + nanosleep(&ts, NULL); } //after 5 seconds waiting thread still running. now kill it