-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.c
106 lines (92 loc) · 2.91 KB
/
msg.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
/**
* Messages. Please #include this file in runtime.c
*/
const char* msg_show_label(struct msg_t* m) {
switch (m->label) {
case MSG_MSG : return "MSG";
case MSG_BRANCH : return "BRANCH";
case MSG_INIT : return "INIT";
case MSG_CLOSE : return "CLOSE";
case MSG_SYNC_INIT : return "SYNC_INIT";
case MSG_SYNC_CLOSE : return "SYNC_CLOSE";
case MSG_FWD_KEEP : return "KEEP";
case MSG_FWD_KILL : return "KILL";
default : panic("Unknown label.");
}
return NULL;
}
int msg_show_senders(struct msg_t* m, char* buffer) {
int i = 0;
for(unsigned int mask = 0x80; mask; mask >>= 1) {
i += sprintf(buffer+i, "%d", !!(mask & m->senders));
}
return i;
}
int msg_show_receivers(struct msg_t* m, char* buffer) {
int i = 0;
for(unsigned int mask = 0x80; mask; mask >>= 1) {
i += sprintf(buffer+i, "%d", !!(mask & m->receivers));
}
return i;
}
void msg_show(struct msg_t* m) {
msg_show_prefix(m, "\t");
}
void msg_show_prefix(struct msg_t* m, const char* prefix) {
char* label;
char buffer[100];
int i = sprintf(buffer, "%s", prefix);
i += sprintf(buffer+i, "[%s] [", msg_show_label(m));
i += msg_show_senders(m, buffer+i);
i += sprintf(buffer+i, "] [");
i += msg_show_receivers(m, buffer+i);
i += sprintf(buffer+i, "]");
switch (m->label) {
case MSG_FWD_KEEP:
case MSG_FWD_KILL:
i += sprintf(buffer+i, "[%s]", ((struct board_t*)m->payload)->id);
break;
case MSG_SYNC_INIT:
case MSG_SYNC_CLOSE:
i += sprintf(buffer+i, " [");
for(unsigned int mask = 0x80; mask; mask >>= 1) {
i += sprintf(buffer+i, "%d", !!(mask & (int32_t)m->payload));
}
sprintf(buffer+i, "]");
break;
}
log_debug("%s", buffer);
}
PRIVATE void msg_log(void* b, void* m, int rw) {
struct board_t* board = (struct board_t*)b;
struct msg_t* msg = (struct msg_t*)m;
char* action = rw == 0 ? "read from" : "written to";
switch (msg->label) {
case MSG_MSG : log_debug("Message %s %s %s", msg->payload, action, board->id); break;
case MSG_BRANCH : log_debug("Branch %s %s %s", (int)msg->payload == 0 ? "left" : "right", action, board->id); break;
case MSG_CLOSE : log_debug("Close %s %s", action, board->id); break;
case MSG_FWD_KEEP : log_debug("Keep %s %s %s", ((struct board_t*)msg->payload)->id, action, board->id); break;
case MSG_FWD_KILL : log_debug("Kill %s %s %s", ((struct board_t*)msg->payload)->id, action, board->id); break;
}
}
struct msg_t* msg_make(int label, int32_t senders, int32_t receivers, void* payload) {
struct msg_t* m = (struct msg_t*)malloc(sizeof(struct msg_t));
m->label = label;
m->senders = senders;
m->receivers = receivers;
m->payload = payload;
return m;
}
/**
* Free the message. If the payload is a blackboard, free the board as well.
*/
void msg_free(struct msg_t* msg) {
struct board_t* child;
switch (msg->label) {
case MSG_FWD_KILL:
case MSG_FWD_KEEP:
child = (struct board_t*)msg->payload;
board_free(child);
}
free(msg);
}