48 lines
1.7 kB
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* state.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: mleitao <marvin@42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/07/07 01:07:20 by mleitao #+# #+# */
9/* Updated: 2026/07/11 22:58:31 by mleitao ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "codexion.h"
14#include "state.h"
15#include "timer.h"
16#include <stdio.h>
17
18int sim_stopped(t_sim *sim)
19{
20 unsigned int s;
21
22 pthread_mutex_lock(&sim->stop_lock);
23 s = sim->stop;
24 pthread_mutex_unlock(&sim->stop_lock);
25 return (s);
26}
27
28void sim_set_stop(t_sim *sim)
29{
30 pthread_mutex_lock(&sim->stop_lock);
31 sim->stop = 1;
32 pthread_mutex_unlock(&sim->stop_lock);
33}
34
35void log_state(t_sim *sim, unsigned int id, const char *msg)
36{
37 pthread_mutex_lock(&sim->log_lock);
38 if (!sim_stopped(sim))
39 printf("%ld %u %s\n", elapsed_ms(sim->start_ms), id, msg);
40 pthread_mutex_unlock(&sim->log_lock);
41}
42
43void log_burnout(t_sim *sim, unsigned int id)
44{
45 pthread_mutex_lock(&sim->log_lock);
46 printf("%ld %u %s\n", elapsed_ms(sim->start_ms), id, MSG_BURNOUT);
47 pthread_mutex_unlock(&sim->log_lock);
48}