summaryrefslogtreecommitdiff
path: root/src/init.c
diff options
context:
space:
mode:
authoryctct <yctct@yctct.com>2026-09-13 18:58:14 +0200
committeryctct <yctct@yctct.com>2026-09-13 18:58:14 +0200
commit47beba686ff9d2aab63738fba7176a5235703d05 (patch)
treef839ed36a9e9d4476fcdc59379b17903abfa35c8 /src/init.c
Add all files, first commit
Diffstat (limited to 'src/init.c')
-rw-r--r--src/init.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/init.c b/src/init.c
new file mode 100644
index 0000000..623a7bb
--- /dev/null
+++ b/src/init.c
@@ -0,0 +1,66 @@
+#include "codexion.h"
+
+static void init_mutex_philos(t_sim *sim)
+{
+ t_philo **philos;
+ t_philo *philo;
+ int number_of_philos;
+
+ number_of_philos = sim->number_of_philos;
+ philos = sim->philos;
+ while (number_of_philos-- > 0)
+ {
+ philo = *philos++;
+ (void)pthread_mutex_init(&philo->mutex_number_of_eats_done, NULL);
+ (void)pthread_mutex_init(&philo->mutex_beginning_last_eat_time, NULL);
+ (void)pthread_mutex_init(&philo->mutex_id, NULL);
+ (void)pthread_mutex_init(&philo->mutex_sim, NULL);
+ }
+}
+
+static void init_mutex_forks(t_sim *sim)
+{
+ t_fork **forks;
+ t_fork *fork;
+ int number_of_philos;
+
+ number_of_philos = sim->number_of_philos;
+ forks = sim->forks;
+ while (number_of_philos-- > 0)
+ {
+ fork = *forks++;
+ (void)pthread_mutex_init(&fork->mutex, NULL);
+ (void)pthread_mutex_init(&fork->mutex_last_released, NULL);
+ (void)pthread_mutex_init(&fork->mutex_left_philo, NULL);
+ (void)pthread_mutex_init(&fork->mutex_right_philo, NULL);
+ (void)pthread_mutex_init(&fork->mutex_ticket_next, NULL);
+ (void)pthread_mutex_init(&fork->mutex_cond, NULL);
+ (void)pthread_mutex_init(&fork->mutex_sim, NULL);
+ (void)pthread_cond_init(&fork->cond_signal, NULL);
+ (void)pthread_mutex_init(&fork->mutex_pq, NULL);
+ }
+}
+
+static void init_mutex_sim(t_sim *sim)
+{
+ (void)pthread_mutex_init(&sim->mutex_philo_starved, NULL);
+ (void)pthread_mutex_init(&sim->mutex_print_log, NULL);
+ (void)pthread_mutex_init(&sim->mutex_cooldown, NULL);
+ (void)pthread_mutex_init(&sim->mutex_start_time, NULL);
+ (void)pthread_mutex_init(&sim->mutex_number_of_eat_required, NULL);
+ (void)pthread_mutex_init(&sim->mutex_philos, NULL);
+ (void)pthread_mutex_init(&sim->mutex_number_of_philos, NULL);
+ (void)pthread_mutex_init(&sim->mutex_time_to_starve, NULL);
+ (void)pthread_mutex_init(&sim->mutex_time_to_think, NULL);
+ (void)pthread_mutex_init(&sim->mutex_time_to_reflect, NULL);
+ (void)pthread_mutex_init(&sim->mutex_time_to_eat, NULL);
+ (void)pthread_mutex_init(&sim->mutex_scheduler, NULL);
+ (void)pthread_mutex_init(&sim->mutex_forks, NULL);
+}
+
+void init_mutex(t_sim *sim)
+{
+ init_mutex_philos(sim);
+ init_mutex_forks(sim);
+ init_mutex_sim(sim);
+}