tx_sync.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2020 Otto-von-Guericke-Universität Magdeburg
3  * SPDX-License-Identifier: LGPL-2.1-only
4  */
5 
6 #pragma once
7 
21 #include <errno.h>
22 #include <stdint.h>
23 
24 #include "mutex.h"
25 #include "net/gnrc/pktbuf.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
33 typedef struct {
36 
40 static inline gnrc_tx_sync_t gnrc_tx_sync_init(void)
41 {
42  gnrc_tx_sync_t result = { .signal = MUTEX_INIT_LOCKED };
43  return result;
44 }
45 
57 {
58  *tx_sync = gnrc_tx_sync_init();
59  gnrc_pktsnip_t *snip = gnrc_pktbuf_add(NULL, NULL, 0, GNRC_NETTYPE_TX_SYNC);
60  if (!snip) {
61  return NULL;
62  }
63  snip->data = tx_sync;
64  return snip;
65 }
66 
78 static inline int gnrc_tx_sync_append(gnrc_pktsnip_t *pkt,
79  gnrc_tx_sync_t *tx_sync)
80 {
81  gnrc_pktsnip_t *snip = gnrc_tx_sync_build(tx_sync);
82  if (!snip) {
83  return -ENOMEM;
84  }
85  gnrc_pkt_append(pkt, snip);
86  return 0;
87 }
88 
96 
105 static inline void gnrc_tx_complete(gnrc_pktsnip_t *pkt)
106 {
107  assert(IS_USED(MODULE_GNRC_TX_SYNC) && (pkt->type == GNRC_NETTYPE_TX_SYNC));
108  gnrc_tx_sync_t *sync = (gnrc_tx_sync_t*)pkt->data;
109  mutex_unlock(&sync->signal);
110 }
111 
126 static inline void gnrc_tx_sync(gnrc_tx_sync_t *sync)
127 {
128  mutex_lock(&sync->signal);
129 }
130 
131 #ifdef __cplusplus
132 }
133 #endif
134 
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:143
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:119
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
Definition: mutex.h:202
#define ENOMEM
Not enough space.
Definition: errno.h:117
@ GNRC_NETTYPE_TX_SYNC
TX synchronization data for passing up error data or auxiliary data.
Definition: nettype.h:53
static gnrc_pktsnip_t * gnrc_pkt_append(gnrc_pktsnip_t *pkt, gnrc_pktsnip_t *snip)
Appends a snip to a packet.
Definition: pkt.h:169
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:40
static gnrc_pktsnip_t * gnrc_tx_sync_build(gnrc_tx_sync_t *tx_sync)
Build a TX sync snip.
Definition: tx_sync.h:56
static void gnrc_tx_complete(gnrc_pktsnip_t *pkt)
Signal TX completion via the given tx sync packet snip.
Definition: tx_sync.h:105
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:78
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:126
#define IS_USED(module)
Checks whether a module is being used or not.
Definition: modules.h:67
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:105
void * data
pointer to the data of the snip
Definition: pkt.h:108
gnrc_nettype_t type
protocol of the packet snip
Definition: pkt.h:115
TX synchronization data.
Definition: tx_sync.h:33
mutex_t signal
Mutex used for synchronization.
Definition: tx_sync.h:34
Mutex structure.
Definition: mutex.h:36