TAS
TCP Acceleration as an OS Service
utils_nbqueue.h
1 /*
2  * Copyright 2019 University of Washington, Max Planck Institute for
3  * Software Systems, and The University of Texas at Austin
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef UTILS_NBQUEUE_H_
26 #define UTILS_NBQUEUE_H_
27 
28 #include <assert.h>
29 #include <pthread.h>
30 
31 struct nbqueue_el {
32  struct nbqueue_el *next;
33 };
34 
35 struct nbqueue {
36  struct nbqueue_el *head;
37  pthread_mutex_t mutex;
38 };
39 
40 static inline void nbqueue_init(struct nbqueue *nbq)
41 {
42  nbq->head = NULL;
43  pthread_mutex_init(&nbq->mutex, NULL);
44 }
45 
46 static inline void nbqueue_enq(struct nbqueue *nbq, struct nbqueue_el *el)
47 {
48  pthread_mutex_lock(&nbq->mutex);
49  el->next = nbq->head;
50  nbq->head = el;
51  pthread_mutex_unlock(&nbq->mutex);
52 }
53 
54 static inline void *nbqueue_deq(struct nbqueue *nbq)
55 {
56  struct nbqueue_el *el, *el_p;
57  if (nbq->head == NULL) {
58  return NULL;
59  }
60 
61  pthread_mutex_lock(&nbq->mutex);
62 
63  for (el = nbq->head, el_p = NULL; el != NULL && el->next != NULL;
64  el = el->next)
65  {
66  el_p = el;
67  }
68  assert(el->next == NULL);
69 
70  if (el != NULL) {
71  if (el_p != NULL) {
72  el_p->next = NULL;
73  } else {
74  nbq->head = NULL;
75  }
76  }
77 
78  pthread_mutex_unlock(&nbq->mutex);
79 
80  return el;
81 }
82 
83 #endif /* ndef UTILS_NBQUEUE_H_ */