Skip to content

Commit

Permalink
core: make send functions use message nodes
Browse files Browse the repository at this point in the history
this is the first step to decoupling message endpoints from threads.

Includes making messaging optional for threads.
  • Loading branch information
kaspar030 committed Aug 21, 2014
1 parent dcb66e5 commit 358fc06
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 106 deletions.
2 changes: 2 additions & 0 deletions core/include/flags.h
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2014 Kaspar Schleiser
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
Expand Down Expand Up @@ -28,6 +29,7 @@
#define CREATE_WOUT_YIELD (4) /**< do not automatically call thread_yield() after creation */
#define CREATE_STACKTEST (8) /**< write markers into the thread's stack to measure stack
usage (for debugging) */
#define CREATE_NOMSG (16) /**< Create thread without messaging capabilities */
/** @} */

#endif /* _FLAGS_H */
Expand Down
45 changes: 32 additions & 13 deletions core/include/msg.h
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
* Copyright (C) 2014 Kaspar Schleiser
*
* This file subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
Expand Down Expand Up @@ -34,6 +35,7 @@
#include <stdint.h>
#include <stdbool.h>
#include "kernel_types.h"
#include "queue.h"

#define MESSAGE_PROCESS_NOT_WAITING 0
#define MESSAGE_SENT 1
Expand All @@ -49,6 +51,23 @@
#define MSG_BLOCK (4) /**< The message was sent in blocking mode */
/** @} */

/**
* @brief Describes a message node object
*
* These messages nodes are used as messaging endpoints
* by the messaging API.
*
*/
typedef struct msg_node {
struct tcb *owner; /**< thread that owns this message node */
void *wait_data; /**< holding current receive data */
queue_node_t msg_waiters; /**< threads waiting to send to this node */

#ifdef MODULE_MSG_QUEUE
struct msg_queue *msg_queue; /**< ptr to this node's msg queue */
#endif
} msg_node_t;

/**
* @brief Describes a message header object that contains auxillary message data
*
Expand All @@ -57,14 +76,14 @@
* This is also used internally my RIOT's msg subsystem.
*/
typedef struct msg_hdr {
unsigned int sender_pid; /**< PID of thread that sent the msg */
char *payload; /**< Pointer to message send buffer (same as supplied to
msg_send()) */
uint16_t size; /**< Receive buffer size. */
uint16_t flags; /**< Bitfield to hold several flags */
char *rbuf; /**< Pointer to receive message buffer (same as supplied to
msg_receive()) */
uint16_t rsize; /**< size of rbuf */
struct msg_node *node; /**< Reference to sender message node */
char *payload; /**< Pointer to message send buffer (same as supplied to
msg_send()) */
uint16_t size; /**< Receive buffer size. */
uint16_t flags; /**< Bitfield to hold several flags */
char *rbuf; /**< Pointer to receive message buffer (same as supplied to
msg_receive()) */
uint16_t rsize; /**< size of rbuf */
} msg_hdr_t;

/**
Expand All @@ -84,14 +103,14 @@ typedef struct msg_hdr {
* The amount of data is the minimum of @c size and the target thread's message
* buffer as supplied to msg_(try)receive.
*
* @param target_pid PID of target thread
* @param target reference to target message node
* @param buf Pointer to buffer that should be sent
* @param size Size of buffer (must be >= sizeof(msg_pulse_t))
*
* @return >0 amount of data that was actually sent.
* @return -1 on error (invalid PID)
* @return -1 on error (invalid target node)
*/
int msg_send(unsigned int target_pid, char *buf, uint16_t size);
int msg_send(msg_node_t *target, char *buf, uint16_t size);

/**
* @brief Send a message. (non-blocking)
Expand All @@ -107,7 +126,7 @@ int msg_send(unsigned int target_pid, char *buf, uint16_t size);
* @return 0 if receiver is not waiting or has a full message queue
* @return -1 on error (invalid PID)
*/
int msg_try_send(unsigned int target_pid, char *buf, uint16_t size);
int msg_try_send(msg_node_t *target, char *buf, uint16_t size);

/**
* @brief Send a message from interrupt context. (non-blocking)
Expand All @@ -123,7 +142,7 @@ int msg_try_send(unsigned int target_pid, char *buf, uint16_t size);
* @return 0 if receiver is not waiting or has a full message queue
* @return -1 on error (invalid PID)
*/
int msg_send_int(unsigned int target_pid, char *buf, uint16_t size);
int msg_send_int(msg_node_t *target, char *buf, uint16_t size);

/**
* @brief Receive a message. (blocking)
Expand Down
14 changes: 3 additions & 11 deletions core/include/tcb.h
Expand Up @@ -23,10 +23,7 @@
#include <stdint.h>
#include "priority_queue.h"
#include "clist.h"

#ifdef MODULE_MSG_QUEUE
#include "msg_queue.h"
#endif
#include "msg.h"

/**
* @brief Thread status list
Expand Down Expand Up @@ -57,7 +54,7 @@
/**
* @brief @c tcb_t holds thread's context data.
*/
typedef struct tcb_t {
typedef struct tcb {
char *sp; /**< thread's stack pointer */
uint16_t status; /**< thread's status */

Expand All @@ -66,12 +63,7 @@ typedef struct tcb_t {

clist_node_t rq_entry; /**< run queue entry */

void *wait_data; /**< holding messages */
priority_queue_t msg_waiters; /**< threads waiting on message */

#ifdef MODULE_MSG_QUEUE
msg_queue_t *msg_queue; /**< ptr to thread's msg queue */
#endif
msg_node_t *msg_node; /**< optional ptr to message node */

const char *name; /**< thread's name */
char *stack_start; /**< thread's stack start address */
Expand Down
2 changes: 2 additions & 0 deletions core/include/thread.h
Expand Up @@ -169,5 +169,7 @@ int thread_measure_stack_free(char *stack);
int thread_set_msg_queue(int pid, msg_queue_t* msg_queue);
#endif

msg_node_t *thread_get_msg_node(int pid);

/* @} */
#endif /* __THREAD_H */

0 comments on commit 358fc06

Please sign in to comment.