-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
75 lines (63 loc) · 2.08 KB
/
stack.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
#include <stdio.h>
#include "stack.h"
void stack_push(STACK *stack, void *element)
{
pthread_mutex_lock(&stack->mutex);
// printf("entered push\n");
while (stack->pointer == MaxStackSize - 1) // cant push on full stack
pthread_cond_wait(&stack->overCond, &stack->mutex);
stack->pointer++;
stack->elements[stack->pointer] = element;
// printf("Pushed: %s \n", (char *)stack->elements[stack->pointer]);
// printf("%d\n", stack->pointer);
pthread_mutex_unlock(&stack->mutex);
// printf("exit push\n");
pthread_cond_signal(&stack->underCond);
}
void *stack_pop(STACK *stack)
{
pthread_mutex_lock(&stack->mutex);
// printf("entered pop\n");
while (stack->pointer < 0 && stack->activePushers > 0) // cant pop from empty stack
{
// printf("Waiting on underCond\n");
pthread_cond_wait(&stack->underCond, &stack->mutex);
}
if (stack->pointer < 0 && stack->activePushers < 1) // stack empty an not gonna get more elements
{
// printf("no more elements on stack\n");
pthread_mutex_unlock(&stack->mutex);
return NULL;
}
void *tmp = stack->elements[stack->pointer];
stack->pointer--;
pthread_mutex_unlock(&stack->mutex);
pthread_cond_signal(&stack->overCond);
// printf("exit pop\n");
return tmp;
}
void stack_read(STACK *stack)
{
printf("Printing stack elements\n");
printf("%d\n", stack->pointer);
for (int i = 0; i <= stack->pointer; i++)
{
printf("%s \n", (char *)stack->elements[i]);
}
}
void stack_init(STACK *stack, int pushers)
{
stack->pointer = -1;
stack->activePushers = pushers;
pthread_mutex_init(&stack->mutex, NULL);
pthread_cond_init(&stack->overCond, NULL);
pthread_cond_init(&stack->underCond, NULL);
}
void stack_endData(STACK *stack)
{
// reduces a number of active pushers (prodeucers) by one. When the number reaches 0 all poping processes will receive null
pthread_mutex_lock(&stack->mutex);
stack->activePushers -= 1;
pthread_cond_broadcast(&stack->underCond);
pthread_mutex_unlock(&stack->mutex);
}