intra to gitea
This commit is contained in:
+104
@@ -0,0 +1,104 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/21 17:11:19 by lfirmin #+# #+# */
|
||||
/* Updated: 2025/03/17 04:00:00 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/philo.h"
|
||||
|
||||
t_data *init_data(void)
|
||||
{
|
||||
t_data *data;
|
||||
|
||||
data = malloc(sizeof(t_data));
|
||||
if (!data)
|
||||
return (NULL);
|
||||
data->nb_philo = 0;
|
||||
data->t_die = 0;
|
||||
data->t_eat = 0;
|
||||
data->t_sleep = 0;
|
||||
data->must_eat = -1;
|
||||
data->dead_flag = 0;
|
||||
data->forks = NULL;
|
||||
data->philos = NULL;
|
||||
data->start_time = 0;
|
||||
if (pthread_mutex_init(&data->print, NULL))
|
||||
return (free(data), NULL);
|
||||
if (pthread_mutex_init(&data->dead_mutex, NULL))
|
||||
return (free(data), NULL);
|
||||
return (data);
|
||||
}
|
||||
|
||||
void free_data(t_data *data)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (data)
|
||||
{
|
||||
if (data->forks)
|
||||
{
|
||||
i = -1;
|
||||
while (++i < data->nb_philo)
|
||||
{
|
||||
pthread_mutex_destroy(&data->forks[i]);
|
||||
pthread_mutex_destroy(&data->philos[i].meal_lock);
|
||||
}
|
||||
free(data->forks);
|
||||
}
|
||||
if (data->philos)
|
||||
free(data->philos);
|
||||
pthread_mutex_destroy(&data->print);
|
||||
pthread_mutex_destroy(&data->dead_mutex);
|
||||
free(data);
|
||||
}
|
||||
}
|
||||
|
||||
long long ft_atoll(const char *str)
|
||||
{
|
||||
long long result;
|
||||
int sign;
|
||||
|
||||
result = 0;
|
||||
sign = 1;
|
||||
while (*str == ' ' || (*str >= 9 && *str <= 13))
|
||||
str++;
|
||||
if (*str == '-' || *str == '+')
|
||||
{
|
||||
if (*str == '-')
|
||||
sign = -1;
|
||||
str++;
|
||||
}
|
||||
while (*str >= '0' && *str <= '9')
|
||||
{
|
||||
if (result > INT_MAX || result < INT_MIN)
|
||||
return (2147483648);
|
||||
result = result * 10 + (*str - '0');
|
||||
str++;
|
||||
}
|
||||
return (result * sign);
|
||||
}
|
||||
|
||||
int check_if_dead(t_philo *philo)
|
||||
{
|
||||
int result;
|
||||
|
||||
pthread_mutex_lock(&philo->data->dead_mutex);
|
||||
result = philo->data->dead_flag;
|
||||
pthread_mutex_unlock(&philo->data->dead_mutex);
|
||||
return (result);
|
||||
}
|
||||
|
||||
int check_if_dead_data(t_data *data)
|
||||
{
|
||||
int result;
|
||||
|
||||
pthread_mutex_lock(&data->dead_mutex);
|
||||
result = data->dead_flag;
|
||||
pthread_mutex_unlock(&data->dead_mutex);
|
||||
return (result);
|
||||
}
|
||||
Reference in New Issue
Block a user