CNDP  22.08.0
cthread_sched.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2019-2022 Intel Corporation
3  */
4 
5 /*
6  * Some portions of this software is derived from the
7  * https://github.com/halayli/lthread which carrys the following license.
8  *
9  * Copyright (c) 2012, Hasan Alayli <halayli@gmail.com>
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef _CTHREAD_SCHED_H_
34 #define _CTHREAD_SCHED_H_
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
48 static inline void
49 _ready_queue_insert(struct cthread_sched *sched, struct cthread *ct)
50 {
51  if (sched == THIS_SCHED)
52  _cthread_queue_insert_sp((THIS_SCHED)->ready, ct);
53  else
54  _cthread_queue_insert_mp(sched->pready, ct);
55 }
56 
65 static inline struct cthread *
66 _ready_queue_remove(struct cthread_queue *q)
67 {
68  return _cthread_queue_remove(q);
69 }
70 
79 static inline int
80 _ready_queue_empty(struct cthread_queue *q)
81 {
82  return _cthread_queue_empty(q);
83 }
84 
89 static inline uint64_t
90 _sched_now(void)
91 {
92  uint64_t now = cne_rdtsc();
93 
94  if (now > (THIS_SCHED)->birth)
95  return now - (THIS_SCHED)->birth;
96 
97  if (now < (THIS_SCHED)->birth)
98  return (THIS_SCHED)->birth - now;
99 
100  /* never return 0 because this means sleep forever */
101  return 1;
102 }
103 
104 static inline void __attribute__((always_inline)) _affinitize(void)
105 {
106  struct cthread *ct = THIS_CTHREAD;
107 
108  cthread_switch(&(THIS_SCHED)->ctx, &ct->ctx);
109 }
110 
111 static inline void __attribute__((always_inline)) _suspend(void)
112 {
113  struct cthread *ct = THIS_CTHREAD;
114 
115  (THIS_SCHED)->nb_blocked_threads++;
116  cthread_switch(&(THIS_SCHED)->ctx, &ct->ctx);
117  (THIS_SCHED)->nb_blocked_threads--;
118 }
119 
120 static inline void __attribute__((always_inline)) _reschedule(void)
121 {
122  struct cthread *ct = THIS_CTHREAD;
123 
124  _ready_queue_insert(THIS_SCHED, ct);
125  cthread_switch(&(THIS_SCHED)->ctx, &ct->ctx);
126 }
127 
128 static inline int
129 is_sched_running(void)
130 {
131  struct cthread_sched *sched = THIS_SCHED;
132 
133  return sched->run_flag;
134 }
135 
136 void _sched_timer_cb(struct cne_timer *tim, void *arg);
137 void _sched_shutdown(void *arg);
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif /* _CTHREAD_SCHED_H_ */
static uint64_t cne_rdtsc(void)
Definition: cne_cycles.h:32
struct cthread_queue * pready
Definition: cthread_int.h:85
uint64_t birth
Definition: cthread_int.h:171
struct ctx ctx
Definition: cthread_int.h:160