1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-03 16:57:46 +02:00

dynamically adjust main loop sleep time according to recommended value

This commit is contained in:
Jfreegman 2014-08-01 13:56:49 -04:00
parent a223545853
commit c455e79604
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
2 changed files with 26 additions and 3 deletions

View File

@ -450,8 +450,6 @@ static void select_friend(ToxWindow *self, wint_t key, int *selected, int num)
static void delete_friend(Tox *m, int32_t f_num)
{
int i;
if (friends[f_num].chatwin >= 0) {
ToxWindow *toxwin = get_window_ptr(friends[f_num].chatwin);
@ -464,6 +462,8 @@ static void delete_friend(Tox *m, int32_t f_num)
tox_del_friend(m, f_num);
memset(&friends[f_num], 0, sizeof(ToxicFriend));
int i;
for (i = max_friends_index; i > 0; --i) {
if (friends[i - 1].active)
break;

View File

@ -594,6 +594,25 @@ static int init_data_files(void)
return config_err;
}
#define REC_TOX_DO_LOOPS_PER_SEC 25
/* Adjusts usleep value so that tox_do runs close to the recommended number of times per second */
useconds_t optimal_msleepval(uint64_t *looptimer, uint64_t *loopcount, uint64_t cur_time, useconds_t msleepval)
{
useconds_t new_sleep = msleepval;
++(*loopcount);
if (*looptimer == cur_time || *loopcount == REC_TOX_DO_LOOPS_PER_SEC)
return new_sleep;
double d = (double) *loopcount / REC_TOX_DO_LOOPS_PER_SEC;
new_sleep *= d;
*looptimer = cur_time;
*loopcount = 0;
return new_sleep;
}
int main(int argc, char *argv[])
{
parse_args(argc, argv);
@ -662,6 +681,9 @@ int main(int argc, char *argv[])
}
uint64_t last_save = (uint64_t) time(NULL);
uint64_t looptimer = last_save;
useconds_t msleepval = 40000;
uint64_t loopcount = 0;
while (true) {
update_unix_time();
@ -676,7 +698,8 @@ int main(int argc, char *argv[])
last_save = cur_time;
}
usleep(40000);
msleepval = optimal_msleepval(&looptimer, &loopcount, cur_time, msleepval);
usleep(msleepval);
}
return 0;