2023-04-13 09:00:39 -04:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "./data.h"
|
|
|
|
#include "philo.h"
|
|
|
|
#include "philos.h"
|
|
|
|
#include "struct.h"
|
2023-04-19 07:53:50 -04:00
|
|
|
#include <string.h>
|
2023-04-13 09:00:39 -04:00
|
|
|
|
|
|
|
bool data_init(t_data *data)
|
|
|
|
{
|
|
|
|
data->forks = malloc(sizeof(bool) * data->nb_philos);
|
|
|
|
if (data->forks == NULL)
|
|
|
|
return (1);
|
2023-04-19 07:53:50 -04:00
|
|
|
memset(data->forks, 1, data->nb_philos);
|
2023-04-13 09:00:39 -04:00
|
|
|
data->philos = malloc(sizeof(t_philo) * data->nb_philos);
|
|
|
|
if (data->philos == NULL)
|
|
|
|
{
|
|
|
|
free(data->forks);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
data->threads = malloc(sizeof(pthread_t) * data->nb_philos);
|
|
|
|
if (data->forks == NULL)
|
|
|
|
{
|
|
|
|
free(data->philos);
|
|
|
|
free(data->forks);
|
|
|
|
return (1);
|
|
|
|
}
|
2023-04-19 07:53:50 -04:00
|
|
|
pthread_mutex_init(&data->forks_mutex, NULL);
|
|
|
|
pthread_mutex_init(&data->stop_mutex, NULL);
|
2023-04-13 09:00:39 -04:00
|
|
|
data->stop = 0;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void data_destroyer(t_data *data)
|
|
|
|
{
|
|
|
|
free(data->threads);
|
|
|
|
free(data->forks);
|
|
|
|
pthread_mutex_destroy(&data->forks_mutex);
|
|
|
|
pthread_mutex_destroy(&data->stop_mutex);
|
|
|
|
philos_destroyer(data);
|
|
|
|
}
|