-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.c
86 lines (69 loc) · 1.84 KB
/
screen.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
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "screen.h"
#include "list.h"
#include "listmanager.h"
#include "shutdownmanager.h"
/*
Screen thread
Receieve messages from receiver
Prints messages
*/
static pthread_t thread;
static pthread_cond_t screenCondVar = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t screenMutex = PTHREAD_MUTEX_INITIALIZER;
static List * outputList;
static char * message = NULL;
void * screenThread(void* unused) {
while (!ShutdownManager_isShuttingDown()) {
pthread_mutex_lock(&screenMutex);
{
pthread_cond_wait(&screenCondVar, &screenMutex);
}
pthread_mutex_unlock(&screenMutex);
if (ShutdownManager_isShuttingDown()) {
break;
}
ListManager_lockOutputList();
message = List_trim(outputList);
ListManager_unlockOutputList();
if (message == NULL) {
puts("Screen: message is NULL");
}
fputs("Receiver: ", stdout);
fputs(message, stdout);
if (strlen(message) == 2 && message[0] == '!') {
ShutdownManager_triggerShutdown();
break;
}
if (message != NULL) {
free(message);
message = NULL;
}
}
return NULL;
}
void Screen_signalNextMessage() {
pthread_mutex_lock(&screenMutex);
{
pthread_cond_signal(&screenCondVar);
}
pthread_mutex_unlock(&screenMutex);
}
void Screen_init() {
outputList = ListManager_getOutputList();
pthread_create(&thread, NULL, screenThread, NULL);
}
void Screen_waitForShutdown() {
pthread_join(thread, NULL);
}
void Screen_clean() {
pthread_cancel(thread);
pthread_mutex_destroy(&screenMutex);
pthread_cond_destroy(&screenCondVar);
if (message != NULL) {
free(message);
}
}