Free (GPLv2) TCP/IP stack developed by TASS Belgium

Dependents:   lpc1768-picotcp-demo ZeroMQ_PicoTCP_Publisher_demo TCPSocket_HelloWorld_PicoTCP Pico_TCP_UDP_Test ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pico_queue.h Source File

pico_queue.h

00001 /*********************************************************************
00002    PicoTCP. Copyright (c) 2012-2015 Altran Intelligent Systems. Some rights reserved.
00003    See LICENSE and COPYING for usage.
00004 
00005  *********************************************************************/
00006 #ifndef INCLUDE_PICO_QUEUE
00007 #define INCLUDE_PICO_QUEUE
00008 #include "pico_config.h"
00009 #include "pico_frame.h"
00010 
00011 #define Q_LIMIT 0
00012 
00013 #ifndef NULL
00014 #define NULL ((void *)0)
00015 #endif
00016 
00017 void *pico_mutex_init(void);
00018 void pico_mutex_deinit(void *mutex);
00019 void pico_mutex_lock(void *mutex);
00020 int pico_mutex_lock_timeout(void *mutex, int timeout);
00021 void pico_mutex_unlock(void *mutex);
00022 void pico_mutex_unlock_ISR(void *mutex);
00023 
00024 struct pico_queue {
00025     uint32_t frames;
00026     uint32_t size;
00027     uint32_t max_frames;
00028     uint32_t max_size;
00029     struct pico_frame *head;
00030     struct pico_frame *tail;
00031 #ifdef PICO_SUPPORT_MUTEX
00032     void *mutex;
00033 #endif
00034     uint8_t shared;
00035     uint16_t overhead;
00036 };
00037 
00038 #ifdef PICO_SUPPORT_MUTEX
00039 #define PICOTCP_MUTEX_LOCK(x) { \
00040         if (x == NULL) \
00041             x = pico_mutex_init(); \
00042         pico_mutex_lock(x); \
00043 }
00044 #define PICOTCP_MUTEX_UNLOCK(x) pico_mutex_unlock(x)
00045 #define PICOTCP_MUTEX_DEL(x) pico_mutex_deinit(x)
00046 
00047 #else
00048 #define PICOTCP_MUTEX_LOCK(x) do {} while(0)
00049 #define PICOTCP_MUTEX_UNLOCK(x) do {} while(0)
00050 #define PICOTCP_MUTEX_DEL(x) do {} while(0)
00051 #endif
00052 
00053 #ifdef PICO_SUPPORT_DEBUG_TOOLS
00054 static void debug_q(struct pico_queue *q)
00055 {
00056     struct pico_frame *p = q->head;
00057     dbg("%d: ", q->frames);
00058     while(p) {
00059         dbg("(%p)-->", p);
00060         p = p->next;
00061     }
00062     dbg("X\n");
00063 }
00064 
00065 #else
00066 
00067 #define debug_q(x) do {} while(0)
00068 #endif
00069 
00070 static inline int32_t pico_enqueue(struct pico_queue *q, struct pico_frame *p)
00071 {
00072     if ((q->max_frames) && (q->max_frames <= q->frames))
00073         return -1;
00074 
00075 #if (Q_LIMIT != 0)
00076     if ((Q_LIMIT < p->buffer_len + q->size))
00077         return -1;
00078 
00079 #endif
00080 
00081     if ((q->max_size) && (q->max_size < (p->buffer_len + q->size)))
00082         return -1;
00083 
00084     if (q->shared)
00085         PICOTCP_MUTEX_LOCK(q->mutex);
00086 
00087     p->next = NULL;
00088     if (!q->head) {
00089         q->head = p;
00090         q->tail = p;
00091         q->size = 0;
00092         q->frames = 0;
00093     } else {
00094         q->tail->next = p;
00095         q->tail = p;
00096     }
00097 
00098     q->size += p->buffer_len + q->overhead;
00099     q->frames++;
00100     debug_q(q);
00101 
00102     if (q->shared)
00103         PICOTCP_MUTEX_UNLOCK(q->mutex);
00104 
00105     return (int32_t)q->size;
00106 }
00107 
00108 static inline struct pico_frame *pico_dequeue(struct pico_queue *q)
00109 {
00110     struct pico_frame *p = q->head;
00111     if (!p)
00112         return NULL;
00113 
00114     if (q->frames < 1)
00115         return NULL;
00116 
00117     if (q->shared)
00118         PICOTCP_MUTEX_LOCK(q->mutex);
00119 
00120     q->head = p->next;
00121     q->frames--;
00122     q->size -= p->buffer_len - q->overhead;
00123     if (q->head == NULL)
00124         q->tail = NULL;
00125 
00126     debug_q(q);
00127 
00128     p->next = NULL;
00129     if (q->shared)
00130         PICOTCP_MUTEX_UNLOCK(q->mutex);
00131 
00132     return p;
00133 }
00134 
00135 static inline struct pico_frame *pico_queue_peek(struct pico_queue *q)
00136 {
00137     struct pico_frame *p = q->head;
00138     if (q->frames < 1)
00139         return NULL;
00140 
00141     debug_q(q);
00142     return p;
00143 }
00144 
00145 static inline void pico_queue_deinit(struct pico_queue *q)
00146 {
00147     if (q->shared) {
00148         PICOTCP_MUTEX_DEL(q->mutex);
00149     }
00150 }
00151 
00152 static inline void pico_queue_empty(struct pico_queue *q)
00153 {
00154     struct pico_frame *p = pico_dequeue(q);
00155     while(p) {
00156         pico_frame_discard(p);
00157         p = pico_dequeue(q);
00158     }
00159 }
00160 
00161 static inline void pico_queue_protect(struct pico_queue *q)
00162 {
00163     q->shared = 1;
00164 }
00165 
00166 #endif