blob: 47a9acd9b119f93226838383e4924c8e875a8028 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* Linuxthreads - a simple clone()-based implementation of Posix */
2/* threads for Linux. */
3/* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
4/* */
5/* This program is free software; you can redistribute it and/or */
6/* modify it under the terms of the GNU Library General Public License */
7/* as published by the Free Software Foundation; either version 2 */
8/* of the License, or (at your option) any later version. */
9/* */
10/* This program is distributed in the hope that it will be useful, */
11/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13/* GNU Library General Public License for more details. */
14
15#ifndef _DESCR_H
16#define _DESCR_H 1
17
18#define __need_res_state
19#include <resolv.h>
20#include <sched.h>
21#include <setjmp.h>
22#include <signal.h>
23#include <stdint.h>
24#include <sys/types.h>
25#include <hp-timing.h>
26#ifdef USE_TLS
27#include <tls.h>
28#endif
29#include "uClibc-glue.h"
30
31/* Fast thread-specific data internal to libc. */
32enum __libc_tsd_key_t { _LIBC_TSD_KEY_MALLOC = 0,
33 _LIBC_TSD_KEY_DL_ERROR,
34 _LIBC_TSD_KEY_RPC_VARS,
35 _LIBC_TSD_KEY_LOCALE,
36 _LIBC_TSD_KEY_CTYPE_B,
37 _LIBC_TSD_KEY_CTYPE_TOLOWER,
38 _LIBC_TSD_KEY_CTYPE_TOUPPER,
39 _LIBC_TSD_KEY_N };
40
41/* The type of thread descriptors */
42typedef struct _pthread_descr_struct *pthread_descr;
43
44
45/* Some more includes. */
46#include <pt-machine.h>
47#include <linuxthreads_db/thread_dbP.h>
48
49
50/* Arguments passed to thread creation routine */
51struct pthread_start_args {
52 void *(*start_routine)(void *); /* function to run */
53 void *arg; /* its argument */
54 sigset_t mask; /* initial signal mask for thread */
55 int schedpolicy; /* initial scheduling policy (if any) */
56 struct sched_param schedparam; /* initial scheduling parameters (if any) */
57};
58
59
60/* Callback interface for removing the thread from waiting on an
61 object if it is cancelled while waiting or about to wait.
62 This hold a pointer to the object, and a pointer to a function
63 which ``extricates'' the thread from its enqueued state.
64 The function takes two arguments: pointer to the wait object,
65 and a pointer to the thread. It returns 1 if an extrication
66 actually occured, and hence the thread must also be signalled.
67 It returns 0 if the thread had already been extricated. */
68typedef struct _pthread_extricate_struct {
69 void *pu_object;
70 int (*pu_extricate_func)(void *, pthread_descr);
71} pthread_extricate_if;
72
73
74/* Atomic counter made possible by compare_and_swap */
75struct pthread_atomic {
76 long p_count;
77 int p_spinlock;
78};
79
80
81/* Context info for read write locks. The pthread_rwlock_info structure
82 is information about a lock that has been read-locked by the thread
83 in whose list this structure appears. The pthread_rwlock_context
84 is embedded in the thread context and contains a pointer to the
85 head of the list of lock info structures, as well as a count of
86 read locks that are untracked, because no info structure could be
87 allocated for them. */
88struct _pthread_rwlock_t;
89typedef struct _pthread_rwlock_info {
90 struct _pthread_rwlock_info *pr_next;
91 struct _pthread_rwlock_t *pr_lock;
92 int pr_lock_count;
93} pthread_readlock_info;
94
95
96/* We keep thread specific data in a special data structure, a two-level
97 array. The top-level array contains pointers to dynamically allocated
98 arrays of a certain number of data pointers. So we can implement a
99 sparse array. Each dynamic second-level array has
100 PTHREAD_KEY_2NDLEVEL_SIZE
101 entries. This value shouldn't be too large. */
102#define PTHREAD_KEY_2NDLEVEL_SIZE 32
103
104/* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
105 keys in each subarray. */
106#define PTHREAD_KEY_1STLEVEL_SIZE \
107 ((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
108 / PTHREAD_KEY_2NDLEVEL_SIZE)
109
110
111union dtv;
112
113struct _pthread_descr_struct
114{
115#if !defined USE_TLS || !TLS_DTV_AT_TP || INCLUDE_TLS_PADDING
116 /* This overlaps tcbhead_t (see tls.h), as used for TLS without threads. */
117 union
118 {
119 struct
120 {
121 void *tcb; /* Pointer to the TCB. This is not always
122 the address of this thread descriptor. */
123 union dtv *dtvp;
124 pthread_descr self; /* Pointer to this structure */
125 int multiple_threads;
126 uintptr_t sysinfo;
127 } data;
128 void *__padding[16];
129 } p_header;
130# define p_multiple_threads p_header.data.multiple_threads
131#elif defined TLS_MULTIPLE_THREADS_IN_TCB && TLS_MULTIPLE_THREADS_IN_TCB
132 int p_multiple_threads;
133#endif
134
135 pthread_descr p_nextlive, p_prevlive;
136 /* Double chaining of active threads */
137 pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
138 pthread_descr p_nextlock; /* can be on a queue and waiting on a lock */
139 pthread_t p_tid; /* Thread identifier */
140 int p_pid; /* PID of Unix process */
141 int p_priority; /* Thread priority (== 0 if not realtime) */
142 struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
143 int p_signal; /* last signal received */
144 sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
145 sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
146 char p_terminated; /* true if terminated e.g. by pthread_exit */
147 char p_detached; /* true if detached */
148 char p_exited; /* true if the assoc. process terminated */
149 void * p_retval; /* placeholder for return value */
150 int p_retcode; /* placeholder for return code */
151 pthread_descr p_joining; /* thread joining on that thread or NULL */
152 struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
153 char p_cancelstate; /* cancellation state */
154 char p_canceltype; /* cancellation type (deferred/async) */
155 char p_canceled; /* cancellation request pending */
156 char * p_in_sighandler; /* stack address of sighandler, or NULL */
157 char p_sigwaiting; /* true if a sigwait() is in progress */
158 struct pthread_start_args p_start_args; /* arguments for thread creation */
159 void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
160#if !(USE_TLS && HAVE___THREAD)
161 void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
162 int * p_errnop; /* pointer to used errno variable */
163 int p_errno; /* error returned by last system call */
164 int * p_h_errnop; /* pointer to used h_errno variable */
165 int p_h_errno; /* error returned by last netdb function */
166 struct __res_state *p_resp; /* Pointer to resolver state */
167#endif
168 struct __res_state p_res; /* per-thread resolver state */
169 int p_userstack; /* nonzero if the user provided the stack */
170 void *p_guardaddr; /* address of guard area or NULL */
171 size_t p_guardsize; /* size of guard area */
172 int p_nr; /* Index of descriptor in __pthread_handles */
173 int p_report_events; /* Nonzero if events must be reported. */
174 td_eventbuf_t p_eventbuf; /* Data for event. */
175 struct pthread_atomic p_resume_count; /* number of times restart() was
176 called on thread */
177 char p_woken_by_cancel; /* cancellation performed wakeup */
178 char p_condvar_avail; /* flag if conditional variable became avail */
179 char p_sem_avail; /* flag if semaphore became available */
180 pthread_extricate_if *p_extricate; /* See above */
181 pthread_readlock_info *p_readlock_list; /* List of readlock info structs */
182 pthread_readlock_info *p_readlock_free; /* Free list of structs */
183 int p_untracked_readlock_count; /* Readlocks not tracked by list */
184 int p_inheritsched; /* copied from the thread attribute */
185#if HP_TIMING_AVAIL
186 hp_timing_t p_cpuclock_offset; /* Initial CPU clock for thread. */
187#endif
188#ifdef USE_TLS
189 char *p_stackaddr; /* Stack address. */
190#endif
191 size_t p_alloca_cutoff; /* Maximum size which should be allocated
192 using alloca() instead of malloc(). */
193 /* New elements must be added at the end. */
194} __attribute__ ((aligned(32))); /* We need to align the structure so that
195 doubles are aligned properly. This is 8
196 bytes on MIPS and 16 bytes on MIPS64.
197 32 bytes might give better cache
198 utilization. */
199
200
201
202/* Limit between the stack of the initial thread (above) and the
203 stacks of other threads (below). Aligned on a STACK_SIZE boundary.
204 Initially 0, meaning that the current thread is (by definition)
205 the initial thread. */
206
207extern char *__pthread_initial_thread_bos;
208
209/* Descriptor of the initial thread */
210
211extern struct _pthread_descr_struct __pthread_initial_thread;
212
213/* Limits of the thread manager stack. */
214
215extern char *__pthread_manager_thread_bos;
216extern char *__pthread_manager_thread_tos;
217
218/* Descriptor of the manager thread */
219
220extern struct _pthread_descr_struct __pthread_manager_thread;
221extern pthread_descr __pthread_manager_threadp attribute_hidden;
222
223/* Indicate whether at least one thread has a user-defined stack (if 1),
224 or all threads have stacks supplied by LinuxThreads (if 0). */
225
226extern int __pthread_nonstandard_stacks;
227
228/* The max size of the thread stack segments. If the default
229 THREAD_SELF implementation is used, this must be a power of two and
230 a multiple of PAGE_SIZE. */
231#ifndef STACK_SIZE
232#define STACK_SIZE (2 * 1024 * 1024)
233#endif
234
235/* Get some notion of the current stack. Need not be exactly the top
236 of the stack, just something somewhere in the current frame. */
237#ifndef CURRENT_STACK_FRAME
238#define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
239#endif
240
241/* Recover thread descriptor for the current thread */
242
243extern pthread_descr __pthread_find_self (void) __attribute__ ((pure));
244
245static __inline__ pthread_descr thread_self (void) __attribute__ ((pure));
246static __inline__ pthread_descr thread_self (void)
247{
248#ifdef THREAD_SELF
249 return THREAD_SELF;
250#else
251 char *sp = CURRENT_STACK_FRAME;
252 if (sp >= __pthread_initial_thread_bos)
253 return &__pthread_initial_thread;
254 else if (sp >= __pthread_manager_thread_bos
255 && sp < __pthread_manager_thread_tos)
256 return &__pthread_manager_thread;
257 else if (__pthread_nonstandard_stacks)
258 return __pthread_find_self();
259 else
260#ifdef _STACK_GROWS_DOWN
261 return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
262#else
263 return (pthread_descr)((unsigned long)sp &~ (STACK_SIZE-1));
264#endif
265#endif
266}
267
268#endif /* descr.h */