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 may have been 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 #ifndef LTHREAD_INT_H 62 #include <lthread_api.h> 63 #define LTHREAD_INT_H 64 65 #include <stdint.h> 66 #include <sys/time.h> 67 #include <sys/types.h> 68 #include <errno.h> 69 #include <pthread.h> 70 #include <time.h> 71 72 #include <rte_cycles.h> 73 #include <rte_per_lcore.h> 74 #include <rte_timer.h> 75 #include <rte_ring.h> 76 #include <rte_atomic_64.h> 77 #include <rte_spinlock.h> 78 #include <ctx.h> 79 80 #include <lthread_api.h> 81 #include "lthread.h" 82 #include "lthread_diag.h" 83 #include "lthread_tls.h" 84 85 struct lthread; 86 struct lthread_sched; 87 struct lthread_cond; 88 struct lthread_mutex; 89 struct lthread_key; 90 91 struct key_pool; 92 struct qnode; 93 struct qnode_pool; 94 struct lthread_sched; 95 struct lthread_tls; 96 97 98 #define BIT(x) (1 << (x)) 99 #define CLEARBIT(x) ~(1 << (x)) 100 101 #define POSIX_ERRNO(x) (x) 102 103 #define MAX_LTHREAD_NAME_SIZE 64 104 105 #define RTE_LOGTYPE_LTHREAD RTE_LOGTYPE_USER1 106 107 108 /* define some shorthand for current scheduler and current thread */ 109 #define THIS_SCHED RTE_PER_LCORE(this_sched) 110 #define THIS_LTHREAD RTE_PER_LCORE(this_sched)->current_lthread 111 112 /* 113 * Definition of an scheduler struct 114 */ 115 struct lthread_sched { 116 struct ctx ctx; /* cpu context */ 117 uint64_t birth; /* time created */ 118 struct lthread *current_lthread; /* running thread */ 119 unsigned lcore_id; /* this sched lcore */ 120 int run_flag; /* sched shutdown */ 121 uint64_t nb_blocked_threads; /* blocked threads */ 122 struct lthread_queue *ready; /* local ready queue */ 123 struct lthread_queue *pready; /* peer ready queue */ 124 struct lthread_objcache *lthread_cache; /* free lthreads */ 125 struct lthread_objcache *stack_cache; /* free stacks */ 126 struct lthread_objcache *per_lthread_cache; /* free per lthread */ 127 struct lthread_objcache *tls_cache; /* free TLS */ 128 struct lthread_objcache *cond_cache; /* free cond vars */ 129 struct lthread_objcache *mutex_cache; /* free mutexes */ 130 struct qnode_pool *qnode_pool; /* pool of queue nodes */ 131 struct key_pool *key_pool; /* pool of free TLS keys */ 132 size_t stack_size; 133 uint64_t diag_ref; /* diag ref */ 134 } __rte_cache_aligned; 135 136 RTE_DECLARE_PER_LCORE(struct lthread_sched *, this_sched); 137 138 139 /* 140 * State for an lthread 141 */ 142 enum lthread_st { 143 ST_LT_INIT, /* initial state */ 144 ST_LT_READY, /* lthread is ready to run */ 145 ST_LT_SLEEPING, /* lthread is sleeping */ 146 ST_LT_EXPIRED, /* lthread timeout has expired */ 147 ST_LT_EXITED, /* lthread has exited and needs cleanup */ 148 ST_LT_DETACH, /* lthread frees on exit*/ 149 ST_LT_CANCELLED, /* lthread has been cancelled */ 150 }; 151 152 /* 153 * lthread sub states for exit/join 154 */ 155 enum join_st { 156 LT_JOIN_INITIAL, /* initial state */ 157 LT_JOIN_EXITING, /* thread is exiting */ 158 LT_JOIN_THREAD_SET, /* joining thread has been set */ 159 LT_JOIN_EXIT_VAL_SET, /* exiting thread has set ret val */ 160 LT_JOIN_EXIT_VAL_READ, /* joining thread has collected ret val */ 161 }; 162 163 /* defnition of an lthread stack object */ 164 struct lthread_stack { 165 uint8_t stack[LTHREAD_MAX_STACK_SIZE]; 166 size_t stack_size; 167 struct lthread_sched *root_sched; 168 } __rte_cache_aligned; 169 170 /* 171 * Definition of an lthread 172 */ 173 struct lthread { 174 struct ctx ctx; /* cpu context */ 175 176 uint64_t state; /* current lthread state */ 177 178 struct lthread_sched *sched; /* current scheduler */ 179 void *stack; /* ptr to actual stack */ 180 size_t stack_size; /* current stack_size */ 181 size_t last_stack_size; /* last yield stack_size */ 182 lthread_func_t fun; /* func ctx is running */ 183 void *arg; /* func args passed to func */ 184 void *per_lthread_data; /* per lthread user data */ 185 lthread_exit_func exit_handler; /* called when thread exits */ 186 uint64_t birth; /* time lthread was born */ 187 struct lthread_queue *pending_wr_queue; /* deferred queue to write */ 188 struct lthread *lt_join; /* lthread to join on */ 189 uint64_t join; /* state for joining */ 190 void **lt_exit_ptr; /* exit ptr for lthread_join */ 191 struct lthread_sched *root_sched; /* thread was created here*/ 192 struct queue_node *qnode; /* node when in a queue */ 193 struct rte_timer tim; /* sleep timer */ 194 struct lthread_tls *tls; /* keys in use by the thread */ 195 struct lthread_stack *stack_container; /* stack */ 196 char funcname[MAX_LTHREAD_NAME_SIZE]; /* thread func name */ 197 uint64_t diag_ref; /* ref to user diag data */ 198 } __rte_cache_aligned; 199 200 #endif /* LTHREAD_INT_H */ 201