ajout static, renames, pere fouettard tweaks

This commit is contained in:
Hugo LAMY
2022-01-26 16:31:36 +01:00
parent 6d3280631c
commit 77f2e6b978
9 changed files with 130 additions and 120 deletions

View File

@@ -3,39 +3,58 @@
/* ::: :::::::: */
/* generic.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* By: hulamy <hulamy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/26 15:30:19 by hulamy #+# #+# */
/* Updated: 2022/01/26 15:31:15 by hulamy ### ########.fr */
/* Created: 2022/01/26 15:30:54 by hulamy #+# #+# */
/* Updated: 2022/01/26 16:29:10 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int ft_atoi(const char *str)
void init_time(t_philo *philo)
{
int i;
int result;
int is_negative;
struct timeval stime;
is_negative = 0;
result = 0;
i = 0;
while (str[i] == ' ' || (str[i] >= 9 && str[i] <= 13))
i++;
if (str[i] == '+')
i++;
else if (str[i] == '-')
if (philo->global->t_start.ts == 0)
{
is_negative = 1;
i++;
gettimeofday(&stime, NULL);
philo->global->t_start.ts = stime.tv_sec;
philo->global->t_start.tu = stime.tv_usec;
}
while (str[i] >= '0' && str[i] <= '9')
{
result = (result * 10) + (str[i] - '0');
i++;
}
if (is_negative)
result = result * -1;
return (result);
philo->t_last_meal.ts = philo->global->t_start.ts;
philo->t_last_meal.tu = philo->global->t_start.tu;
}
void update_time(t_philo *philo)
{
struct timeval stime;
gettimeofday(&stime, NULL);
philo->t_last_meal.ts = stime.tv_sec;
philo->t_last_meal.tu = stime.tv_usec;
}
int diff_time(t_time *old, struct timeval *new)
{
return ((new->tv_sec - old->ts) * 1000 + (new->tv_usec - old->tu) / 1000);
}
int print_message(t_philo *philo, char *clr, char *msg)
{
long int time_stamp;
struct timeval stime;
pthread_mutex_lock(&(philo->global->m_print));
if (philo->global->stop)
{
pthread_mutex_unlock(&(philo->global->m_print));
return (1);
}
gettimeofday(&stime, NULL);
time_stamp = (stime.tv_sec - philo->global->t_start.ts) * 1000;
time_stamp += (stime.tv_usec - philo->global->t_start.tu) / 1000;
printf("%s%li %i %s%s\n", clr, time_stamp, philo->p_nbr, msg, RESET);
pthread_mutex_unlock(&(philo->global->m_print));
return (0);
}