My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
thread_message.c
Go to the documentation of this file.
1 /*
2 START OF LICENSE STUB
3  DeDOS: Declarative Dispersion-Oriented Software
4  Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 END OF LICENSE STUB
19 */
26 #include "thread_message.h"
27 #include "logging.h"
28 
29 int enqueue_thread_msg(struct thread_msg *thread_msg, struct msg_queue *queue) {
30  struct dedos_msg *msg = malloc(sizeof(*msg));
31  if (msg == NULL) {
32  log_error("Error allocating dedos_msg for thread_msg");
33  return -1;
34  }
35  msg->data_size = sizeof(*thread_msg);
36  msg->type = THREAD_MSG;
37  msg->data = thread_msg;
38  int rtn = enqueue_msg(queue, msg);
39  if (rtn < 0) {
40  log_error("Error enqueueing message on thread message queue");
41  return -1;
42  }
43  log(LOG_THREAD_MESSAGES, "Enqueued thread message %p on queue %p",
44  thread_msg, queue);
45  return 0;
46 }
47 
48 struct thread_msg *dequeue_thread_msg(struct msg_queue *queue) {
49  struct dedos_msg *msg = dequeue_msg(queue);
50  if (msg == NULL) {
51  return NULL;
52  }
53  if (msg->data_size != sizeof(struct thread_msg)) {
54  log_error("Attempted to dequeue non-thread msg from queue");
55  return NULL;
56  }
57 
58  struct thread_msg *thread_msg = msg->data;
59  log(LOG_THREAD_MESSAGES, "Dequeued thread message %p from queue %p",
60  thread_msg, queue);
61  free(msg);
62  return thread_msg;
63 }
64 
65 
67  ssize_t data_size, void *data) {
68  struct thread_msg *msg = calloc(1, sizeof(*msg));
69  if (msg == NULL) {
70  log_error("Error allocating thread message");
71  return NULL;
72  }
73  msg->type = type;
74  msg->data_size = data_size;
75  msg->data = data;
76  log(LOG_THREAD_MESSAGES, "Constructed thread message %p of size %d",
77  msg, (int)data_size);
78  return msg;
79 }
80 
81 void destroy_thread_msg(struct thread_msg *msg) {
82  log(LOG_THREAD_MESSAGES, "Freeing thread message %p",
83  msg);
84  free(msg);
85 }
86 
88  unsigned int target_id,
89  size_t data_len,
90  void *data) {
91  struct send_to_peer_msg *msg = malloc(sizeof(*msg));
92  if (msg == NULL) {
93  log_error("Error allocating send_to_runtime_thread_msg");
94  return NULL;
95  }
96  msg->hdr.type = RT_FWD_TO_MSU;
97  msg->hdr.target = target_id;
98  msg->hdr.payload_size = data_len;
99  msg->runtime_id = runtime_id;
100  msg->data = data;
101 
103  sizeof(*msg), msg);
104  if (thread_msg == NULL) {
105  log_error("Error creating thread message for send-to-runtime message");
106  return NULL;
107  }
108  return thread_msg;
109 }
110 
Messages to be delivered to dedos_threads.
thread_msg_type
All messages that can be received by output thread or workers.
For delivery to the output monitor thread, a message to be sent to a peer runtime.
struct dedos_msg * dequeue_msg(struct msg_queue *q)
Dequeues the first available message from q.
Definition: message_queue.c:87
Container for linked list message queue.
Definition: message_queue.h:56
Logging of status messages to the terminal.
int enqueue_thread_msg(struct thread_msg *thread_msg, struct msg_queue *queue)
Enqueues a dedos_msg with a thread_msg as the payload to the appropriate queue.
void destroy_thread_msg(struct thread_msg *msg)
Frees a thread message.
enum thread_msg_type type
struct thread_msg * dequeue_thread_msg(struct msg_queue *queue)
Dequeues a thread_msg from the message queue.
A linked-list entry containing a message.
Definition: message_queue.h:42
enum dedos_msg_type type
Target of delivery: runtime, thread, or MSU.
Definition: message_queue.h:46
struct thread_msg * construct_thread_msg(enum thread_msg_type type, ssize_t data_size, void *data)
Allocates and initializes a thread message with the provided options.
#define log_error(fmt,...)
Definition: logging.h:101
A message to be delivered to a dedos_thread.
Paylaod type: output of serialize_msu_msg.
int enqueue_msg(struct msg_queue *q, struct dedos_msg *msg)
Enqueues a message to be delivered as soon as possible.
Definition: message_queue.c:34
struct inter_runtime_msg_hdr hdr
unsigned int target
MSU ID or thread ID depending on message type.
ssize_t data_size
struct thread_msg * init_send_thread_msg(unsigned int runtime_id, unsigned int target_id, size_t data_len, void *data)
Initializes a send_to_peer message (SEND_TO_PEER)
void * data
Payload.
Definition: message_queue.h:48
static int runtime_id(int runtime_fd)
#define log(level, fmt,...)
Log at a custom level.
Definition: logging.h:147
enum inter_runtime_msg_type type
payload: send_to_runtime_msg (below)
ssize_t data_size
Size of payload.
Definition: message_queue.h:50
unsigned int runtime_id
The runtime ID to which the message is delivered.