NDN-DPDK
High-Speed Named Data Networking Forwarder
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
thread.h
Go to the documentation of this file.
1 #ifndef NDNDPDK_DPDK_THREAD_H
2 #define NDNDPDK_DPDK_THREAD_H
3 
6 #include "../core/common.h"
7 #include "thread-enum.h"
8 
10 typedef struct ThreadCtrl {
11  uint64_t nPolls[2]; // [0] empty polls; [1] valid polls
12  uint64_t items;
13  uint32_t sleepFor;
14  atomic_bool stop;
16 
17 static __rte_always_inline void
18 ThreadCtrl_Sleep(ThreadCtrl* ctrl) {
19 #ifdef NDNDPDK_THREADSLEEP
20  if (unlikely(ctrl->nPolls[0] % ThreadCtrl_SleepAdjustEvery == 0) &&
21  unlikely(ctrl->sleepFor < ThreadCtrl_SleepMax)) {
24  UINT32_MAX,
25  "");
27  static_assert(ThreadCtrl_SleepDivide > 0, "");
28  static_assert(ThreadCtrl_SleepAdd >= 0, "");
29  uint64_t sleepFor =
31  if (ThreadCtrl_SleepAdd == 0) {
32  sleepFor = RTE_MAX(sleepFor, (uint64_t)ctrl->sleepFor + 1);
33  }
34  ctrl->sleepFor = RTE_MIN(ThreadCtrl_SleepMax, (uint32_t)sleepFor + ThreadCtrl_SleepAdd);
35  }
36  struct timespec req = {.tv_sec = 0, .tv_nsec = ctrl->sleepFor};
37  nanosleep(&req, NULL);
38 #else
39  rte_pause();
40 #endif // NDNDPDK_THREADSLEEP
41 }
42 
43 static __rte_always_inline void
44 ThreadCtrl_SleepReset(ThreadCtrl* ctrl) {
45 #ifdef NDNDPDK_THREADSLEEP
47 #endif // NDNDPDK_THREADSLEEP
48 }
49 
58 #define ThreadCtrl_Continue(ctrl, count) \
59  __extension__({ \
60  ++(ctrl).nPolls[(int)((count) > 0)]; \
61  (ctrl).items += (count); \
62  if (count == 0) { \
63  ThreadCtrl_Sleep(&(ctrl)); \
64  } else { \
65  ThreadCtrl_SleepReset(&(ctrl)); \
66  } \
67  (count) = 0; \
68  atomic_load_explicit(&(ctrl).stop, memory_order_acquire); \
69  })
70 
71 __attribute__((nonnull)) static inline void
72 ThreadCtrl_Init(ThreadCtrl* ctrl) {
74  atomic_init(&ctrl->stop, true);
75 }
76 
77 __attribute__((nonnull)) static inline void
78 ThreadCtrl_RequestStop(ThreadCtrl* ctrl) {
79  atomic_store_explicit(&ctrl->stop, false, memory_order_release);
80 }
81 
82 __attribute__((nonnull)) static inline void
83 ThreadCtrl_FinishStop(ThreadCtrl* ctrl) {
84  atomic_store_explicit(&ctrl->stop, true, memory_order_release);
85 }
86 
87 #endif // NDNDPDK_DPDK_THREAD_H
Thread load stats and stop flag.
Definition: thread.h:10
atomic_bool stop
Definition: thread.h:14
uint32_t sleepFor
Definition: thread.h:13
uint64_t items
Definition: thread.h:12
uint64_t nPolls[2]
Definition: thread.h:11
@ ThreadCtrl_SleepAdd
Definition: thread-enum.h:12
@ ThreadCtrl_SleepMax
Definition: thread-enum.h:8
@ ThreadCtrl_SleepMultiply
Definition: thread-enum.h:10
@ ThreadCtrl_SleepMin
Definition: thread-enum.h:7
@ ThreadCtrl_SleepAdjustEvery
Definition: thread-enum.h:9
@ ThreadCtrl_SleepDivide
Definition: thread-enum.h:11
struct ThreadCtrl ThreadCtrl
Thread load stats and stop flag.