-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.c
113 lines (94 loc) · 1.93 KB
/
linked_list.c
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//======================================================
// linked_list.c
//
// COMP 3430 - Operating Systems - Assignment 2 Frogger
//
// Log adaptation of generic linked list data structure
//
// @author Andy Lun
// @date June 09, 2017
//======================================================
#include <stdlib.h>
#include "linked_list.h"
#include "log.h"
#include "threads.h"
//=====================================
// STRUCT
//=====================================
typedef struct node_t
{
log_t* log;
pthread_t thread;
struct node_t* prev;
struct node_t* next;
} node_t;
node_t* log_list;
//=====================================
// FUNCTIONS
//=====================================
void
add_log(log_t* log)
{
node_t* node = (node_t*) malloc(sizeof(node_t));
node->log = log;
node->thread = init_thread("log", animate_log, node->log);
lock_mutex(&mutex_logs);
node->prev = NULL;
node->next = log_list;
if (NULL != log_list)
{
log_list->prev = node;
}
log_list = node;
unlock_mutex(&mutex_logs);
}
bool
add_frog(int frog_tier, int frog_column)
{
bool valid_jump = false;
node_t* curr = log_list;
lock_mutex(&mutex_logs);
while (NULL != curr)
{
if (check_frog_on_log(frog_tier, frog_column, curr->log))
{
valid_jump = true;
}
curr = curr->next;
}
unlock_mutex(&mutex_logs);
return valid_jump;
}
void
remove_dead_logs()
{
lock_mutex(&mutex_logs);
if (NULL != log_list)
{
node_t* curr = log_list;
while (NULL != curr)
{
if (is_log_dead(curr->log))
{
node_t* next = curr->next;
if (NULL != curr->prev)
{
(curr->prev)->next = (NULL != curr->next) ? curr->next : NULL;
}
if (NULL != curr->next)
{
(curr->next)->prev = (NULL != curr->prev) ? curr->prev : NULL;
}
join_thread(curr->thread);
free(curr->log);
free(curr);
curr = next;
}
else
{
curr = curr->next;
}
}
}
unlock_mutex(&mutex_logs);
}