-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.c
103 lines (86 loc) · 1.85 KB
/
threads.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
#include "threads.h"
#define MAX_QUEUE_SIZE 10000
static void *queue[MAX_QUEUE_SIZE]; // Queue now stores `void *`
static int queue_size = 0;
static int active_tasks = 0;
static bool flag = false, done = false;
static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t queue_not_empty = PTHREAD_COND_INITIALIZER;
static pthread_cond_t queue_not_full = PTHREAD_COND_INITIALIZER;
void enqueue(void *item)
{
pthread_mutex_lock(&queue_mutex);
while (queue_size == MAX_QUEUE_SIZE)
{
pthread_cond_wait(&queue_not_full, &queue_mutex);
}
queue[queue_size++] = item;
active_tasks++;
pthread_cond_signal(&queue_not_empty);
pthread_mutex_unlock(&queue_mutex);
}
void *dequeue()
{
pthread_mutex_lock(&queue_mutex);
while (queue_size == 0 && !done)
{
pthread_cond_wait(&queue_not_empty, &queue_mutex);
}
if (done && queue_size == 0)
{
pthread_mutex_unlock(&queue_mutex);
return NULL;
}
void *item = queue[--queue_size];
queue[queue_size] = NULL;
pthread_cond_signal(&queue_not_full);
pthread_mutex_unlock(&queue_mutex);
return item;
}
// Flag and done setters and getters
void set_flag(bool value)
{
flag = value;
}
bool get_flag()
{
return flag;
}
void set_done(bool value)
{
done = value;
}
bool get_done()
{
return done;
}
// Queue size and active task management
int get_queue_size()
{
return queue_size;
}
void set_queue_size(int value)
{
queue_size = value;
}
int get_active_tasks()
{
return active_tasks;
}
void set_active_tasks(int value)
{
active_tasks = value;
}
// Synchronization primitives
pthread_mutex_t *get_queue_mutex()
{
return &queue_mutex;
}
pthread_cond_t *get_not_empty_condition()
{
return &queue_not_empty;
}
pthread_cond_t *get_not_full_condition()
{
return &queue_not_full;
}