Refactor nanosleep(2) calls into sleep_ms() function

This commit is contained in:
Andrew Bettison 2012-08-21 13:34:29 +09:30
parent 83cb027484
commit d8fd9fa411
4 changed files with 25 additions and 23 deletions

View File

@ -700,7 +700,6 @@ int app_server_stop(int argc, const char *const *argv, struct command_line_optio
int pid, tries, running;
const char *instancepath;
time_ms_t timeout;
struct timespec delay;
if (cli_arg(argc, argv, o, "instance path", &instancepath, cli_absolute_path, NULL) == -1)
return WHY("Unable to determine instance path");
@ -750,11 +749,9 @@ int app_server_stop(int argc, const char *const *argv, struct command_line_optio
}
/* Allow a few seconds for the process to die. */
timeout = gettime_ms() + 2000;
do {
delay.tv_sec = 0;
delay.tv_nsec = 200000000; // 200 ms = 5 Hz
nanosleep(&delay, NULL);
} while ((running = server_pid()) == pid && gettime_ms() < timeout);
do
sleep_ms(200); // 5 Hz
while ((running = server_pid()) == pid && gettime_ms() < timeout);
}
server_remove_stopfile();
cli_puts("tries");

View File

@ -251,12 +251,12 @@ int sqlite_exec_void_loglevel(int log_level, const char *sqlformat, ...)
/*
Same as sqlite_exec_void() but if the statement cannot be executed because the database is locked
for updates, then will retry for at most the given number of milliseconds 'timeout_ms', sleeping
'sleep_ms' between tries (if sleep_ms == 0 then uses a default of 250ms).
'sleeptime_ms' between tries (if sleeptime_ms == 0 then uses a default of 250ms).
Returns -1 on error (logged as error), returns 0 on success, returns 1 if the database is still
locked after all retries (logged as error).
@author Andrew Bettison <andrew@servalproject.com>
*/
int sqlite_exec_void_retry(int timeout_ms, int sleep_ms, const char *sqlformat, ...)
int sqlite_exec_void_retry(int timeout_ms, int sleeptime_ms, const char *sqlformat, ...)
{
strbuf stmt = strbuf_alloca(8192);
strbuf_va_printf(stmt, sqlformat);
@ -264,8 +264,8 @@ int sqlite_exec_void_retry(int timeout_ms, int sleep_ms, const char *sqlformat,
if (!statement)
return -1;
int ret;
if (sleep_ms <= 0)
sleep_ms = 250;
if (sleeptime_ms <= 0)
sleeptime_ms = 250;
unsigned tries = 1;
time_ms_t start = gettime_ms();
while ((ret = sqlite_exec_void_prepared_loglevel(LOG_LEVEL_SILENT, statement, 1)) == 1) {
@ -283,10 +283,7 @@ int sqlite_exec_void_retry(int timeout_ms, int sleep_ms, const char *sqlformat,
INFOF("database locked on try %u after %.3f seconds: %s",
tries, (now - start) / 1e3, sqlite3_sql(statement)
);
struct timespec delay;
delay.tv_sec = sleep_ms / 1000;
delay.tv_nsec = (sleep_ms % 1000) * 1000000;
nanosleep(&delay, NULL);
sleep_ms(sleeptime_ms);
++tries;
}
if (tries > 1) {

View File

@ -549,6 +549,7 @@ int respondSimple(keyring_identity *id,
unsigned char *transaction_id,int recvttl,
struct sockaddr *recvaddr,int cryptoFlags);
time_ms_t gettime_ms();
time_ms_t sleep_ms(time_ms_t milliseconds);
int server_pid();
void server_save_argv(int argc, const char *const *argv);
int server(char *backing_file);

View File

@ -123,6 +123,20 @@ time_ms_t gettime_ms()
return nowtv.tv_sec * 1000LL + nowtv.tv_usec / 1000;
}
// Returns sleep time remaining.
time_ms_t sleep_ms(time_ms_t milliseconds)
{
if (milliseconds <= 0)
return 0;
struct timespec delay;
struct timespec remain;
delay.tv_sec = milliseconds / 1000;
delay.tv_nsec = (milliseconds % 1000) * 1000000;
if (nanosleep(&delay, &remain) == -1 && errno != EINTR)
FATALF_perror("nanosleep(tv_sec=%ld, tv_nsec=%ld)", delay.tv_sec, delay.tv_nsec);
return remain.tv_sec * 1000 + remain.tv_nsec / 1000000;
}
/** Return the PID of the currently running server process, return 0 if there is none.
*/
int server_pid()
@ -168,15 +182,8 @@ int server(char *backing_file)
process, for example to check that the start/stop logic is robust.
*/
const char *delay = getenv("SERVALD_SERVER_START_DELAY");
if (delay) {
long ms = atoi(delay);
if (ms > 0) {
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
nanosleep(&ts, NULL);
}
}
if (delay)
sleep_ms(atoi(delay));
serverMode = 1;
serverRespawnOnCrash = confValueGetBoolean("server.respawn_on_crash", 0);