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