-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
81 lines (72 loc) · 2.21 KB
/
utils.c
File metadata and controls
81 lines (72 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanmetol <sanmetol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/12 13:02:27 by sanmetol #+# #+# */
/* Updated: 2025/02/12 13:02:27 by sanmetol ### ########.fr */
/* */
/* ************************************************************************** */
#include "philo.h"
int ft_usleep(t_philo *philo, size_t time_to_)
{
size_t start;
start = get_current_time_ms();
while ((get_current_time_ms() - start) < time_to_)
{
pthread_mutex_lock(philo->mutex_dead);
if (*philo->dead)
{
pthread_mutex_unlock(philo->mutex_dead);
return (1);
}
pthread_mutex_unlock(philo->mutex_dead);
usleep(200);
}
return (0);
}
void cleanup(char *str, t_sim *simulation, pthread_mutex_t *forks)
{
int i;
i = 0;
if (str)
printf("%s\n", str);
pthread_mutex_destroy(&simulation->mutex_meal);
pthread_mutex_destroy(&simulation->mutex_log);
pthread_mutex_destroy(&simulation->mutex_dead);
while (i < simulation->philos[0].input.n_of_philos)
pthread_mutex_destroy(&forks[i++]);
}
size_t get_current_time_ms(void)
{
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000 + time.tv_usec / 1000);
}
long ft_atol(const char *str)
{
long n;
int i;
int neg;
i = 0;
n = 0;
neg = 1;
while ((str[i] == ' ' || str[i] == '\f' || str[i] == '\n'
|| str[i] == '\r' || str[i] == '\t' || str[i] == '\v')
&& str[i])
i++;
if (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
neg = -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
n = str[i] - '0' + n * 10;
i++;
}
return (n * neg);
}