1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2015 Intel Corporation. All rights reserved. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 /* 35 * Some portions of this software is derived from the 36 * https://github.com/halayli/lthread which carrys the following license. 37 * 38 * Copyright (C) 2012, Hasan Alayli <[email protected]> 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 49 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 */ 61 62 #ifndef LTHREAD_SCHED_H_ 63 #define LTHREAD_SCHED_H_ 64 65 #ifdef __cplusplus 66 extern "C" { 67 #endif 68 69 #include "lthread_int.h" 70 #include "lthread_queue.h" 71 #include "lthread_objcache.h" 72 #include "lthread_diag.h" 73 #include "ctx.h" 74 75 /* 76 * insert an lthread into a queue 77 */ 78 static inline void 79 _ready_queue_insert(struct lthread_sched *sched, struct lthread *lt) 80 { 81 if (sched == THIS_SCHED) 82 _lthread_queue_insert_sp((THIS_SCHED)->ready, lt); 83 else 84 _lthread_queue_insert_mp(sched->pready, lt); 85 } 86 87 /* 88 * remove an lthread from a queue 89 */ 90 static inline struct lthread *_ready_queue_remove(struct lthread_queue *q) 91 { 92 return _lthread_queue_remove(q); 93 } 94 95 /** 96 * Return true if the ready queue is empty 97 */ 98 static inline int _ready_queue_empty(struct lthread_queue *q) 99 { 100 return _lthread_queue_empty(q); 101 } 102 103 static inline uint64_t _sched_now(void) 104 { 105 uint64_t now = rte_rdtsc(); 106 107 if (now > (THIS_SCHED)->birth) 108 return now - (THIS_SCHED)->birth; 109 if (now < (THIS_SCHED)->birth) 110 return (THIS_SCHED)->birth - now; 111 /* never return 0 because this means sleep forever */ 112 return 1; 113 } 114 115 static __rte_always_inline void 116 _affinitize(void); 117 static inline void 118 _affinitize(void) 119 { 120 struct lthread *lt = THIS_LTHREAD; 121 122 DIAG_EVENT(lt, LT_DIAG_LTHREAD_SUSPENDED, 0, 0); 123 ctx_switch(&(THIS_SCHED)->ctx, <->ctx); 124 } 125 126 static __rte_always_inline void 127 _suspend(void); 128 static inline void 129 _suspend(void) 130 { 131 struct lthread *lt = THIS_LTHREAD; 132 133 (THIS_SCHED)->nb_blocked_threads++; 134 DIAG_EVENT(lt, LT_DIAG_LTHREAD_SUSPENDED, 0, 0); 135 ctx_switch(&(THIS_SCHED)->ctx, <->ctx); 136 (THIS_SCHED)->nb_blocked_threads--; 137 } 138 139 static __rte_always_inline void 140 _reschedule(void); 141 static inline void 142 _reschedule(void) 143 { 144 struct lthread *lt = THIS_LTHREAD; 145 146 DIAG_EVENT(lt, LT_DIAG_LTHREAD_RESCHEDULED, 0, 0); 147 _ready_queue_insert(THIS_SCHED, lt); 148 ctx_switch(&(THIS_SCHED)->ctx, <->ctx); 149 } 150 151 extern struct lthread_sched *schedcore[]; 152 void _sched_timer_cb(struct rte_timer *tim, void *arg); 153 void _sched_shutdown(__rte_unused void *arg); 154 155 #ifdef __cplusplus 156 } 157 #endif 158 159 #endif /* LTHREAD_SCHED_H_ */ 160