tx_sync.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Otto-von-Guericke-Universität Magdeburg
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
21 #ifndef NET_GNRC_TX_SYNC_H
22 #define NET_GNRC_TX_SYNC_H
23 
24 #include <errno.h>
25 #include <stdint.h>
26 
27 #include "mutex.h"
28 #include "net/gnrc/pktbuf.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
36 typedef struct {
39 
43 static inline gnrc_tx_sync_t gnrc_tx_sync_init(void)
44 {
45  gnrc_tx_sync_t result = { .signal = MUTEX_INIT_LOCKED };
46  return result;
47 }
48 
60 {
61  *tx_sync = gnrc_tx_sync_init();
62  gnrc_pktsnip_t *snip = gnrc_pktbuf_add(NULL, NULL, 0, GNRC_NETTYPE_TX_SYNC);
63  if (!snip) {
64  return NULL;
65  }
66  snip->data = tx_sync;
67  return snip;
68 }
69 
81 static inline int gnrc_tx_sync_append(gnrc_pktsnip_t *pkt,
82  gnrc_tx_sync_t *tx_sync)
83 {
84  gnrc_pktsnip_t *snip = gnrc_tx_sync_build(tx_sync);
85  if (!snip) {
86  return -ENOMEM;
87  }
88  gnrc_pkt_append(pkt, snip);
89  return 0;
90 }
91 
99 
108 static inline void gnrc_tx_complete(gnrc_pktsnip_t *pkt)
109 {
110  assert(IS_USED(MODULE_GNRC_TX_SYNC) && (pkt->type == GNRC_NETTYPE_TX_SYNC));
111  gnrc_tx_sync_t *sync = (gnrc_tx_sync_t*)pkt->data;
112  mutex_unlock(&sync->signal);
113 }
114 
129 static inline void gnrc_tx_sync(gnrc_tx_sync_t *sync)
130 {
131  mutex_lock(&sync->signal);
132 }
133 
134 #ifdef __cplusplus
135 }
136 #endif
137 
138 #endif /* NET_GNRC_TX_SYNC_H */
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:136
void mutex_unlock(mutex_t *mutex)
Unlocks the mutex.
#define MUTEX_INIT_LOCKED
Static initializer for mutex_t with a locked mutex.
Definition: mutex.h:229
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
Definition: mutex.h:312
#define ENOMEM
Not enough space.
Definition: errno.h:118
@ GNRC_NETTYPE_TX_SYNC
TX synchronization data for passing up error data or auxiliary data.
Definition: nettype.h:56
static gnrc_pktsnip_t * gnrc_pkt_append(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Appends a snip to a packet.
Definition: pkt.h:172
gnrc_pktsnip_t * gnrc_pktbuf_add(gnrc_pktsnip_t *next, const void *data, size_t size, gnrc_nettype_t type)
Adds a new gnrc_pktsnip_t and its packet to the packet buffer.
static gnrc_tx_sync_t gnrc_tx_sync_init(void)
Helper to initialize a gnrc_tx_sync_t structure.
Definition: tx_sync.h:43
static gnrc_pktsnip_t * gnrc_tx_sync_build(gnrc_tx_sync_t *tx_sync)
Build a TX sync snip.
Definition: tx_sync.h:59
static void gnrc_tx_complete(gnrc_pktsnip_t *pkt)
Signal TX completion via the given tx sync packet snip.
Definition: tx_sync.h:108
static int gnrc_tx_sync_append(gnrc_pktsnip_t *pkt, gnrc_tx_sync_t *tx_sync)
Appends a newly allocated tx sync pktsnip to the end of the packet.
Definition: tx_sync.h:81
gnrc_pktsnip_t * gnrc_tx_sync_split(gnrc_pktsnip_t *pkt)
Split off the TX sync snip and return it.
static void gnrc_tx_sync(gnrc_tx_sync_t *sync)
Block until transmission of the corresponding packet has completed or failed.
Definition: tx_sync.h:129
#define IS_USED(module)
Checks whether a module is being used or not.
Definition: modules.h:71
Mutex for thread synchronization.
Interface definition for the global network buffer.
Type to represent parts (either headers or payload) of a packet, called snips.
Definition: pkt.h:108
void * data
pointer to the data of the snip
Definition: pkt.h:111
gnrc_nettype_t type
protocol of the packet snip
Definition: pkt.h:118
TX synchronization data.
Definition: tx_sync.h:36
mutex_t signal
Mutex used for synchronization.
Definition: tx_sync.h:37
Mutex structure.
Definition: mutex.h:146