EM-ODP  3.7.0
Event Machine on ODP
env_bitmask.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Nokia Solutions and Networks
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 /**
32  * @file
33  * Env bit mask functions - don't include this file directly,
34  * instead include "environment.h"
35  */
36 
37 #ifndef _ENV_BITMASK_H_
38 #define _ENV_BITMASK_H_
39 
40 #pragma GCC visibility push(default)
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #include <stdio.h>
47 
48 /*
49  * Type for a bit mask.
50  */
51 typedef struct {
52  odp_cpumask_t odp_cpumask;
54 
55 /**
56  * Zero the whole mask.
57  *
58  * @param mask Bit mask
59  */
60 static inline void env_bitmask_zero(env_bitmask_t *mask)
61 {
62  odp_cpumask_zero(&mask->odp_cpumask);
63 }
64 
65 /**
66  * Set a bit in the mask.
67  *
68  * @param bit Bit id
69  * @param mask Bit mask
70  */
71 static inline void env_bitmask_set(int bit, env_bitmask_t *mask)
72 {
73  odp_cpumask_set(&mask->odp_cpumask, bit);
74 }
75 
76 /**
77  * Clear a bit in the mask.
78  *
79  * @param bit Bit id
80  * @param mask Bit mask
81  */
82 static inline void env_bitmask_clr(int bit, env_bitmask_t *mask)
83 {
84  odp_cpumask_clr(&mask->odp_cpumask, bit);
85 }
86 
87 /**
88  * Test if a bit is set in the mask.
89  *
90  * @param bit Bit id
91  * @param mask Bit mask
92  *
93  * @return Non-zero if bit id is set in the mask
94  */
95 static inline int env_bitmask_isset(int bit, const env_bitmask_t *mask)
96 {
97  return odp_cpumask_isset(&mask->odp_cpumask, bit);
98 }
99 
100 /**
101  * Test if the mask is all zero.
102  *
103  * @param mask Bit mask
104  *
105  * @return Non-zero if the mask is all zero
106  */
107 static inline int env_bitmask_iszero(const env_bitmask_t *mask)
108 {
109  odp_cpumask_t zero_mask;
110 
111  odp_cpumask_zero(&zero_mask);
112 
113  return odp_cpumask_equal(&zero_mask, &mask->odp_cpumask);
114 }
115 
116 /**
117  * Test if two masks are equal
118  *
119  * @param mask1 First bit mask
120  * @param mask2 Second bit mask
121  *
122  * @return Non-zero if the two masks are equal
123  */
124 static inline int env_bitmask_equal(const env_bitmask_t *mask1,
125  const env_bitmask_t *mask2)
126 {
127  return odp_cpumask_equal(&mask1->odp_cpumask, &mask2->odp_cpumask);
128 }
129 
130 /**
131  * Set a range (0...count-1) of bits in the mask.
132  *
133  * @param count Number of bits to set
134  * @param mask Bit mask
135  */
136 static inline void env_bitmask_set_count(int count, env_bitmask_t *mask)
137 {
138  for (int i = 0; i < count; i++)
139  odp_cpumask_set(&mask->odp_cpumask, i);
140 }
141 
142 /**
143  * Copy bit mask
144  *
145  * @param dst Destination bit mask
146  * @param src Source bit mask
147  */
148 static inline void env_bitmask_copy(env_bitmask_t *dst,
149  const env_bitmask_t *src)
150 {
151  odp_cpumask_copy(&dst->odp_cpumask, &src->odp_cpumask);
152 }
153 
154 /**
155  * Count the number of bits set in the mask.
156  *
157  * @param mask Bit mask
158  *
159  * @return Number of bits set
160  */
161 static inline int env_bitmask_count(const env_bitmask_t *mask)
162 {
163  return odp_cpumask_count(&mask->odp_cpumask);
164 }
165 
166 /**
167  * Set specified bits from 'bits[]' in bit mask.
168  *
169  * bit 0: bits[0] = 0x1 (len = 1)
170  * bit 1: bits[0] = 0x2 (len = 1)
171  * ...
172  * bit 64: bits[0] = 0x0, bits[1] = 0x1 (len = 2)
173  * bit 65: bits[0] = 0x0, bits[1] = 0x2 (len = 2)
174  * ...
175  * cores 0-127: bits[0]=0xffffffffffffffff, bits[1]=0xffffffffffffffff (len=2)
176  * ...
177  * @param bits[] array of uint64_t:s containing the bits to set in the bit mask
178  * @param len number of array elements in bits[].
179  * @param mask bit mask to set.
180  *
181  * @note bits ar 'or'ed into mask, so any previously set bits will remain set.
182  */
183 static inline void env_bitmask_set_bits(const uint64_t bits[], int len,
184  env_bitmask_t *mask)
185 {
186  (void)bits;
187  (void)len;
188  (void)mask;
189 
190  fprintf(stderr, "%s() function not implemented!\n", __func__);
191 }
192 
193 /**
194  * Get bit mask, stored in a uint64_t array for the user
195  *
196  * bit 0: bits[0] = 0x1 (len = 1)
197  * bit 1: bits[0] = 0x2 (len = 1)
198  * ...
199  * bit 64: bits[0] = 0x0, bits[1] = 0x1 (len = 2)
200  * bit 65: bits[0] = 0x0, bits[1] = 0x2 (len = 2)
201  * ...
202  * cores 0-127: bits[0]=0xffffffffffffffff, bits[1]=0xffffffffffffffff (len=2)
203  * ...
204  * @param[out] bits[] array of uint64_t:s that the bit mask will be stored in
205  * @param len number of array elements in bits[].
206  * @param mask bit mask to get bits from.
207  *
208  * @return The number of uint64_t:s written into bits[].
209  */
210 static inline int env_bitmask_get_bits(uint64_t bits[/*out*/], int len,
211  const env_bitmask_t *mask)
212 {
213  (void)bits;
214  (void)len;
215  (void)mask;
216 
217  fprintf(stderr, "%s() function not implemented!\n", __func__);
218 
219  return 0;
220 }
221 
222 /**
223  * Return the index (position) of the Nth set bit in the bit mask
224  *
225  * @param n Nth set bit, note n=1 means first set bit, n=[1...MaxCores]
226  * @param mask bit mask
227  *
228  * @return Index of the Nth set bit, <0 on error or if no such bit.
229  */
230 static inline int env_bitmask_idx(int n, const env_bitmask_t *mask)
231 {
232  if (unlikely((unsigned int)(n - 1) >= ODP_CPUMASK_SIZE))
233  return -1;
234 
235  int i = 1;
236  int cpu = odp_cpumask_first(&mask->odp_cpumask);
237 
238  while (cpu >= 0 && i < n) {
239  cpu = odp_cpumask_next(&mask->odp_cpumask, cpu);
240  i++;
241  }
242 
243  return cpu;
244 }
245 
246 /**
247  * Bitwise AND operation on two masks, store the result in 'dst'
248  *
249  * dst = src1 & src2
250  *
251  * @param dst destination bit mask, result is stored here
252  * @param src1 source mask #1
253  * @param scr2 source mask #2
254  */
255 static inline void env_bitmask_and(env_bitmask_t *dst,
256  const env_bitmask_t *src1,
257  const env_bitmask_t *src2)
258 {
259  odp_cpumask_and(&dst->odp_cpumask,
260  &src1->odp_cpumask, &src2->odp_cpumask);
261 }
262 
263 /**
264  * Bitwise OR operation on two masks, store the result in 'dst'
265  *
266  * dst = src1 | src2
267  *
268  * @param dst destination bit mask, result is stored here
269  * @param src1 source mask #1
270  * @param scr2 source mask #2
271  */
272 static inline void env_bitmask_or(env_bitmask_t *dst,
273  const env_bitmask_t *src1,
274  const env_bitmask_t *src2)
275 {
276  odp_cpumask_or(&dst->odp_cpumask,
277  &src1->odp_cpumask, &src2->odp_cpumask);
278 }
279 
280 /**
281  * Bitwise XOR operation on two masks, store the result in 'dst'
282  *
283  * dst = src1 ^ src2
284  *
285  * @param dst destination bit mask, result is stored here
286  * @param src1 source mask #1
287  * @param scr2 source mask #2
288  */
289 static inline void env_bitmask_xor(env_bitmask_t *dst,
290  const env_bitmask_t *src1,
291  const env_bitmask_t *src2)
292 {
293  odp_cpumask_xor(&dst->odp_cpumask,
294  &src1->odp_cpumask, &src2->odp_cpumask);
295 }
296 
297 #ifdef __cplusplus
298 }
299 #endif
300 
301 #pragma GCC visibility pop
302 #endif /* _ENV_BITMASK_H_ */
env_bitmask_t
Definition: env_bitmask.h:51