1 /*
2 * Copyright (c) 2015-2016 Intel Corporation, Inc.  All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses.  You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * BSD license below:
9 *
10 *     Redistribution and use in source and binary forms, with or
11 *     without modification, are permitted provided that the following
12 *     conditions are met:
13 *
14 *      - Redistributions of source code must retain the above
15 *        copyright notice, this list of conditions and the following
16 *        disclaimer.
17 *
18 *      - Redistributions in binary form must reproduce the above
19 *        copyright notice, this list of conditions and the following
20 *        disclaimer in the documentation and/or other materials
21 *        provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32 
33 #ifndef _FI_NETDIR_QUEUE_H_
34 #define _FI_NETDIR_QUEUE_H_
35 
36 #include <windows.h>
37 
38 #include "rdma/fabric.h"
39 
40 #include "ofi.h"
41 #include "ofi_osd.h"
42 
43 #include "netdir.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif /* __cplusplus */
48 
49 struct nd_queue_item {
50 	struct nd_queue_item	*next;
51 };
52 
53 __declspec(align(16)) struct nd_queue_queue {
54 	union {
55 		struct {
56 			struct nd_queue_item	*head;
57 			struct nd_queue_item	*tail;
58 		};
59 		volatile LONG64 exchange[2];
60 	};
61 };
62 
63 /* push front call is non-blocking thread safe */
ofi_nd_queue_push_front(struct nd_queue_queue * queue,struct nd_queue_item * item)64 static inline void ofi_nd_queue_push_front(struct nd_queue_queue *queue,
65 	struct nd_queue_item *item)
66 {
67 	assert(queue);
68 
69 	item->next = 0;
70 	BOOLEAN success;
71 
72 	struct {
73 		struct nd_queue_item *head;
74 		struct nd_queue_item *tail;
75 	} src;
76 
77 	do {
78 		src.head = queue->head;
79 		src.tail = queue->tail;
80 
81 		LONG64 head = (LONG64)(src.head ? src.head : item);
82 		LONG64 tail = (LONG64)item;
83 		__declspec(align(16)) LONG64 compare[2] = { (LONG64)src.head, (LONG64)src.tail };
84 		success = InterlockedCompareExchange128(
85 			queue->exchange, tail, head, compare);
86 	} while (!success);
87 
88 	if (src.tail) {
89 		item->next = src.head;
90 		src.head = item;
91 		WakeByAddressAll(&src.head);
92 	}
93 }
94 
95 /* push call is non-blocking thread safe */
ofi_nd_queue_push(struct nd_queue_queue * queue,struct nd_queue_item * item)96 static inline void ofi_nd_queue_push(struct nd_queue_queue *queue,
97 				     struct nd_queue_item *item)
98 {
99 	assert(queue);
100 
101 	item->next = 0;
102 	BOOLEAN success;
103 
104 	struct {
105 		struct nd_queue_item *head;
106 		struct nd_queue_item *tail;
107 	} src;
108 
109 	do {
110 		src.head = queue->head;
111 		src.tail = queue->tail;
112 
113 		LONG64 head = (LONG64)(src.head ? src.head : item);
114 		LONG64 tail = (LONG64)item;
115 		__declspec(align(16)) LONG64 compare[2] = {(LONG64)src.head, (LONG64)src.tail};
116 		success = InterlockedCompareExchange128(
117 			queue->exchange, tail, head, compare);
118 	} while (!success);
119 
120 	if (src.tail) {
121 		src.tail->next = item;
122 		WakeByAddressAll(&src.tail->next);
123 	}
124 }
125 
126 /* pop call is NOT thread safe, it allows only one consumer, but it is
127    safe to be used with push operation without locks */
ofi_nd_queue_pop(struct nd_queue_queue * queue,struct nd_queue_item ** item)128 static inline int ofi_nd_queue_pop(struct nd_queue_queue *queue,
129 				   struct nd_queue_item **item)
130 {
131 	assert(queue);
132 	assert(item);
133 
134 	BOOLEAN success;
135 	struct {
136 		struct nd_queue_item *head;
137 		struct nd_queue_item *tail;
138 	} src;
139 
140 	do {
141 		src.head = queue->head;
142 		src.tail = queue->tail;
143 
144 		if (!src.head)
145 			return 0;
146 
147 		/* here is potential thread race: object located at src.head
148 		   may be destroyed while we're waiting. that is why pop
149 		   operation is not thread safe */
150 		if (src.head != src.tail) {
151 			/* in case if head and tail are not same - ensure that
152 			   head->next element is not NULL */
153 			void *zero = NULL;
154 			while (!src.head->next) {
155 				WaitOnAddress(&src.head->next, &zero, sizeof(zero), INFINITE);
156 			}
157 		}
158 
159 		LONG64 head = (LONG64)src.head->next;
160 		LONG64 tail = (LONG64)(src.head != src.tail ? src.tail : NULL);
161 		__declspec(align(16)) LONG64 compare[2] = {(LONG64)src.head, (LONG64)src.tail};
162 		success = InterlockedCompareExchange128(
163 			queue->exchange, tail, head, compare);
164 	} while (!success);
165 
166 	*item = src.head;
167 
168 	return (*item) != NULL;
169 }
170 
171 /* peek call is NOT thread safe, it allows only one consumer */
ofi_nd_queue_peek(struct nd_queue_queue * queue,struct nd_queue_item ** item)172 static inline int ofi_nd_queue_peek(struct nd_queue_queue *queue,
173 				    struct nd_queue_item **item)
174 {
175 	assert(queue);
176 	assert(item);
177 
178 	*item = queue->head;
179 	return (*item) != 0;
180 }
181 
182 #ifdef __cplusplus
183 }
184 #endif /* __cplusplus */
185 
186 #endif /* _FI_NETDIR_QUEUE_H_ */
187 
188