| 1 |
148 |
jeremybenn |
/* 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 _INTERNALS_H
|
| 16 |
|
|
#define _INTERNALS_H 1
|
| 17 |
|
|
|
| 18 |
|
|
/* Internal data structures */
|
| 19 |
|
|
|
| 20 |
|
|
/* Includes */
|
| 21 |
|
|
|
| 22 |
|
|
#include <limits.h>
|
| 23 |
|
|
#include <resolv.h>
|
| 24 |
|
|
#include <setjmp.h>
|
| 25 |
|
|
#include <signal.h>
|
| 26 |
|
|
#include <unistd.h>
|
| 27 |
|
|
#include <stackinfo.h>
|
| 28 |
|
|
#include <sys/types.h>
|
| 29 |
|
|
#include <reent.h>
|
| 30 |
|
|
#include <bits/libc-tsd.h> /* for _LIBC_TSD_KEY_N */
|
| 31 |
|
|
|
| 32 |
|
|
extern long int testandset (int *spinlock);
|
| 33 |
|
|
extern int __compare_and_swap (long int *p, long int oldval, long int newval);
|
| 34 |
|
|
|
| 35 |
|
|
#include "libc-symbols.h"
|
| 36 |
|
|
#include "pt-machine.h"
|
| 37 |
|
|
#include "semaphore.h"
|
| 38 |
|
|
#include "thread_dbP.h"
|
| 39 |
|
|
#include <hp-timing.h>
|
| 40 |
|
|
|
| 41 |
|
|
#ifndef THREAD_GETMEM
|
| 42 |
|
|
# define THREAD_GETMEM(descr, member) descr->member
|
| 43 |
|
|
#endif
|
| 44 |
|
|
#ifndef THREAD_GETMEM_NC
|
| 45 |
|
|
# define THREAD_GETMEM_NC(descr, member) descr->member
|
| 46 |
|
|
#endif
|
| 47 |
|
|
#ifndef THREAD_SETMEM
|
| 48 |
|
|
# define THREAD_SETMEM(descr, member, value) descr->member = (value)
|
| 49 |
|
|
#endif
|
| 50 |
|
|
#ifndef THREAD_SETMEM_NC
|
| 51 |
|
|
# define THREAD_SETMEM_NC(descr, member, value) descr->member = (value)
|
| 52 |
|
|
#endif
|
| 53 |
|
|
|
| 54 |
|
|
/* Arguments passed to thread creation routine */
|
| 55 |
|
|
|
| 56 |
|
|
struct pthread_start_args {
|
| 57 |
|
|
void * (*start_routine)(void *); /* function to run */
|
| 58 |
|
|
void * arg; /* its argument */
|
| 59 |
|
|
sigset_t mask; /* initial signal mask for thread */
|
| 60 |
|
|
int schedpolicy; /* initial scheduling policy (if any) */
|
| 61 |
|
|
struct __sched_param schedparam; /* initial scheduling parameters (if any) */
|
| 62 |
|
|
};
|
| 63 |
|
|
|
| 64 |
|
|
|
| 65 |
|
|
/* We keep thread specific data in a special data structure, a two-level
|
| 66 |
|
|
array. The top-level array contains pointers to dynamically allocated
|
| 67 |
|
|
arrays of a certain number of data pointers. So we can implement a
|
| 68 |
|
|
sparse array. Each dynamic second-level array has
|
| 69 |
|
|
PTHREAD_KEY_2NDLEVEL_SIZE
|
| 70 |
|
|
entries. This value shouldn't be too large. */
|
| 71 |
|
|
#define PTHREAD_KEY_2NDLEVEL_SIZE 32
|
| 72 |
|
|
|
| 73 |
|
|
/* We need to address PTHREAD_KEYS_MAX key with PTHREAD_KEY_2NDLEVEL_SIZE
|
| 74 |
|
|
keys in each subarray. */
|
| 75 |
|
|
#define PTHREAD_KEY_1STLEVEL_SIZE \
|
| 76 |
|
|
((PTHREAD_KEYS_MAX + PTHREAD_KEY_2NDLEVEL_SIZE - 1) \
|
| 77 |
|
|
/ PTHREAD_KEY_2NDLEVEL_SIZE)
|
| 78 |
|
|
|
| 79 |
|
|
typedef void (*destr_function)(void *);
|
| 80 |
|
|
|
| 81 |
|
|
struct pthread_key_struct {
|
| 82 |
|
|
int in_use; /* already allocated? */
|
| 83 |
|
|
destr_function destr; /* destruction routine */
|
| 84 |
|
|
};
|
| 85 |
|
|
|
| 86 |
|
|
|
| 87 |
|
|
#define PTHREAD_START_ARGS_INITIALIZER(fct) \
|
| 88 |
|
|
{ (void *(*) (void *)) fct, NULL, {{0, }}, 0, { 0 } }
|
| 89 |
|
|
|
| 90 |
|
|
/* The type of thread descriptors */
|
| 91 |
|
|
|
| 92 |
|
|
typedef struct _pthread_descr_struct * pthread_descr;
|
| 93 |
|
|
|
| 94 |
|
|
/* Callback interface for removing the thread from waiting on an
|
| 95 |
|
|
object if it is cancelled while waiting or about to wait.
|
| 96 |
|
|
This hold a pointer to the object, and a pointer to a function
|
| 97 |
|
|
which ``extricates'' the thread from its enqueued state.
|
| 98 |
|
|
The function takes two arguments: pointer to the wait object,
|
| 99 |
|
|
and a pointer to the thread. It returns 1 if an extrication
|
| 100 |
|
|
actually occured, and hence the thread must also be signalled.
|
| 101 |
|
|
It returns 0 if the thread had already been extricated. */
|
| 102 |
|
|
|
| 103 |
|
|
typedef struct _pthread_extricate_struct {
|
| 104 |
|
|
void *pu_object;
|
| 105 |
|
|
int (*pu_extricate_func)(void *, pthread_descr);
|
| 106 |
|
|
} pthread_extricate_if;
|
| 107 |
|
|
|
| 108 |
|
|
/* Atomic counter made possible by compare_and_swap */
|
| 109 |
|
|
|
| 110 |
|
|
struct pthread_atomic {
|
| 111 |
|
|
long p_count;
|
| 112 |
|
|
int p_spinlock;
|
| 113 |
|
|
};
|
| 114 |
|
|
|
| 115 |
|
|
/* Context info for read write locks. The pthread_rwlock_info structure
|
| 116 |
|
|
is information about a lock that has been read-locked by the thread
|
| 117 |
|
|
in whose list this structure appears. The pthread_rwlock_context
|
| 118 |
|
|
is embedded in the thread context and contains a pointer to the
|
| 119 |
|
|
head of the list of lock info structures, as well as a count of
|
| 120 |
|
|
read locks that are untracked, because no info structure could be
|
| 121 |
|
|
allocated for them. */
|
| 122 |
|
|
|
| 123 |
|
|
struct _pthread_rwlock_t;
|
| 124 |
|
|
|
| 125 |
|
|
typedef struct _pthread_rwlock_info {
|
| 126 |
|
|
struct _pthread_rwlock_info *pr_next;
|
| 127 |
|
|
struct _pthread_rwlock_t *pr_lock;
|
| 128 |
|
|
int pr_lock_count;
|
| 129 |
|
|
} pthread_readlock_info;
|
| 130 |
|
|
|
| 131 |
|
|
struct _pthread_descr_struct {
|
| 132 |
|
|
union {
|
| 133 |
|
|
struct {
|
| 134 |
|
|
pthread_descr self; /* Pointer to this structure */
|
| 135 |
|
|
} data;
|
| 136 |
|
|
void *__padding[16];
|
| 137 |
|
|
} p_header;
|
| 138 |
|
|
pthread_descr p_nextlive, p_prevlive;
|
| 139 |
|
|
/* Double chaining of active threads */
|
| 140 |
|
|
pthread_descr p_nextwaiting; /* Next element in the queue holding the thr */
|
| 141 |
|
|
pthread_descr p_nextlock; /* can be on a queue and waiting on a lock */
|
| 142 |
|
|
pthread_t p_tid; /* Thread identifier */
|
| 143 |
|
|
int p_pid; /* PID of Unix process */
|
| 144 |
|
|
int p_priority; /* Thread priority (== 0 if not realtime) */
|
| 145 |
|
|
struct _pthread_fastlock * p_lock; /* Spinlock for synchronized accesses */
|
| 146 |
|
|
int p_signal; /* last signal received */
|
| 147 |
|
|
sigjmp_buf * p_signal_jmp; /* where to siglongjmp on a signal or NULL */
|
| 148 |
|
|
sigjmp_buf * p_cancel_jmp; /* where to siglongjmp on a cancel or NULL */
|
| 149 |
|
|
char p_terminated; /* true if terminated e.g. by pthread_exit */
|
| 150 |
|
|
char p_detached; /* true if detached */
|
| 151 |
|
|
char p_exited; /* true if the assoc. process terminated */
|
| 152 |
|
|
void * p_retval; /* placeholder for return value */
|
| 153 |
|
|
int p_retcode; /* placeholder for return code */
|
| 154 |
|
|
pthread_descr p_joining; /* thread joining on that thread or NULL */
|
| 155 |
|
|
struct _pthread_cleanup_buffer * p_cleanup; /* cleanup functions */
|
| 156 |
|
|
char p_cancelstate; /* cancellation state */
|
| 157 |
|
|
char p_canceltype; /* cancellation type (deferred/async) */
|
| 158 |
|
|
char p_canceled; /* cancellation request pending */
|
| 159 |
|
|
struct _reent * p_reentp; /* pointer to reent struct */
|
| 160 |
|
|
struct _reent p_reent; /* reentrant structure for newlib */
|
| 161 |
|
|
int * p_h_errnop; /* pointer to used h_errno variable */
|
| 162 |
|
|
int p_h_errno; /* error returned by last netdb function */
|
| 163 |
|
|
char * p_in_sighandler; /* stack address of sighandler, or NULL */
|
| 164 |
|
|
char p_sigwaiting; /* true if a sigwait() is in progress */
|
| 165 |
|
|
struct pthread_start_args p_start_args; /* arguments for thread creation */
|
| 166 |
|
|
void ** p_specific[PTHREAD_KEY_1STLEVEL_SIZE]; /* thread-specific data */
|
| 167 |
|
|
void * p_libc_specific[_LIBC_TSD_KEY_N]; /* thread-specific data for libc */
|
| 168 |
|
|
int p_userstack; /* nonzero if the user provided the stack */
|
| 169 |
|
|
void *p_guardaddr; /* address of guard area or NULL */
|
| 170 |
|
|
size_t p_guardsize; /* size of guard area */
|
| 171 |
|
|
int p_nr; /* Index of descriptor in __pthread_handles */
|
| 172 |
|
|
int p_report_events; /* Nonzero if events must be reported. */
|
| 173 |
|
|
td_eventbuf_t p_eventbuf; /* Data for event. */
|
| 174 |
|
|
struct pthread_atomic p_resume_count; /* number of times restart() was
|
| 175 |
|
|
called on thread */
|
| 176 |
|
|
char p_woken_by_cancel; /* cancellation performed wakeup */
|
| 177 |
|
|
char p_condvar_avail; /* flag if conditional variable became avail */
|
| 178 |
|
|
char p_sem_avail; /* flag if semaphore became available */
|
| 179 |
|
|
pthread_extricate_if *p_extricate; /* See above */
|
| 180 |
|
|
pthread_readlock_info *p_readlock_list; /* List of readlock info structs */
|
| 181 |
|
|
pthread_readlock_info *p_readlock_free; /* Free list of structs */
|
| 182 |
|
|
int p_untracked_readlock_count; /* Readlocks not tracked by list */
|
| 183 |
|
|
struct __res_state *p_resp; /* Pointer to resolver state */
|
| 184 |
|
|
struct __res_state p_res; /* per-thread resolver state */
|
| 185 |
|
|
int p_inheritsched; /* copied from the thread attribute */
|
| 186 |
|
|
#if HP_TIMING_AVAIL
|
| 187 |
|
|
hp_timing_t p_cpuclock_offset; /* Initial CPU clock for thread. */
|
| 188 |
|
|
#endif
|
| 189 |
|
|
/* New elements must be added at the end. */
|
| 190 |
|
|
} __attribute__ ((aligned(32))); /* We need to align the structure so that
|
| 191 |
|
|
doubles are aligned properly. This is 8
|
| 192 |
|
|
bytes on MIPS and 16 bytes on MIPS64.
|
| 193 |
|
|
32 bytes might give better cache
|
| 194 |
|
|
utilization. */
|
| 195 |
|
|
|
| 196 |
|
|
|
| 197 |
|
|
/* The type of thread handles. */
|
| 198 |
|
|
|
| 199 |
|
|
typedef struct pthread_handle_struct * pthread_handle;
|
| 200 |
|
|
|
| 201 |
|
|
struct pthread_handle_struct {
|
| 202 |
|
|
struct _pthread_fastlock h_lock; /* Fast lock for sychronized access */
|
| 203 |
|
|
pthread_descr h_descr; /* Thread descriptor or NULL if invalid */
|
| 204 |
|
|
char * h_bottom; /* Lowest address in the stack thread */
|
| 205 |
|
|
};
|
| 206 |
|
|
|
| 207 |
|
|
/* The type of messages sent to the thread manager thread */
|
| 208 |
|
|
|
| 209 |
|
|
struct pthread_request {
|
| 210 |
|
|
pthread_descr req_thread; /* Thread doing the request */
|
| 211 |
|
|
enum { /* Request kind */
|
| 212 |
|
|
REQ_CREATE, REQ_FREE, REQ_PROCESS_EXIT, REQ_MAIN_THREAD_EXIT,
|
| 213 |
|
|
REQ_POST, REQ_DEBUG, REQ_KICK, REQ_FOR_EACH_THREAD
|
| 214 |
|
|
} req_kind;
|
| 215 |
|
|
union { /* Arguments for request */
|
| 216 |
|
|
struct { /* For REQ_CREATE: */
|
| 217 |
|
|
const pthread_attr_t * attr; /* thread attributes */
|
| 218 |
|
|
void * (*fn)(void *); /* start function */
|
| 219 |
|
|
void * arg; /* argument to start function */
|
| 220 |
|
|
sigset_t mask; /* signal mask */
|
| 221 |
|
|
} create;
|
| 222 |
|
|
struct { /* For REQ_FREE: */
|
| 223 |
|
|
pthread_t thread_id; /* identifier of thread to free */
|
| 224 |
|
|
} free;
|
| 225 |
|
|
struct { /* For REQ_PROCESS_EXIT: */
|
| 226 |
|
|
int code; /* exit status */
|
| 227 |
|
|
} exit;
|
| 228 |
|
|
void * post; /* For REQ_POST: the semaphore */
|
| 229 |
|
|
struct { /* For REQ_FOR_EACH_THREAD: callback */
|
| 230 |
|
|
void (*fn)(void *, pthread_descr);
|
| 231 |
|
|
void *arg;
|
| 232 |
|
|
} for_each;
|
| 233 |
|
|
} req_args;
|
| 234 |
|
|
};
|
| 235 |
|
|
|
| 236 |
|
|
|
| 237 |
|
|
/* Signals used for suspend/restart and for cancellation notification. */
|
| 238 |
|
|
|
| 239 |
|
|
extern int __pthread_sig_restart;
|
| 240 |
|
|
extern int __pthread_sig_cancel;
|
| 241 |
|
|
|
| 242 |
|
|
/* Signal used for interfacing with gdb */
|
| 243 |
|
|
|
| 244 |
|
|
extern int __pthread_sig_debug;
|
| 245 |
|
|
|
| 246 |
|
|
/* Global array of thread handles, used for validating a thread id
|
| 247 |
|
|
and retrieving the corresponding thread descriptor. Also used for
|
| 248 |
|
|
mapping the available stack segments. */
|
| 249 |
|
|
|
| 250 |
|
|
extern struct pthread_handle_struct __pthread_handles[PTHREAD_THREADS_MAX];
|
| 251 |
|
|
|
| 252 |
|
|
/* Descriptor of the initial thread */
|
| 253 |
|
|
|
| 254 |
|
|
extern struct _pthread_descr_struct __pthread_initial_thread;
|
| 255 |
|
|
|
| 256 |
|
|
/* Descriptor of the manager thread */
|
| 257 |
|
|
|
| 258 |
|
|
extern struct _pthread_descr_struct __pthread_manager_thread;
|
| 259 |
|
|
|
| 260 |
|
|
/* Descriptor of the main thread */
|
| 261 |
|
|
|
| 262 |
|
|
extern pthread_descr __pthread_main_thread;
|
| 263 |
|
|
|
| 264 |
|
|
/* Limit between the stack of the initial thread (above) and the
|
| 265 |
|
|
stacks of other threads (below). Aligned on a STACK_SIZE boundary.
|
| 266 |
|
|
Initially 0, meaning that the current thread is (by definition)
|
| 267 |
|
|
the initial thread. */
|
| 268 |
|
|
|
| 269 |
|
|
extern char *__pthread_initial_thread_bos;
|
| 270 |
|
|
|
| 271 |
|
|
/* Indicate whether at least one thread has a user-defined stack (if 1),
|
| 272 |
|
|
or all threads have stacks supplied by LinuxThreads (if 0). */
|
| 273 |
|
|
|
| 274 |
|
|
extern int __pthread_nonstandard_stacks;
|
| 275 |
|
|
|
| 276 |
|
|
/* File descriptor for sending requests to the thread manager.
|
| 277 |
|
|
Initially -1, meaning that __pthread_initialize_manager must be called. */
|
| 278 |
|
|
|
| 279 |
|
|
extern int __pthread_manager_request;
|
| 280 |
|
|
|
| 281 |
|
|
/* Other end of the pipe for sending requests to the thread manager. */
|
| 282 |
|
|
|
| 283 |
|
|
extern int __pthread_manager_reader;
|
| 284 |
|
|
|
| 285 |
|
|
/* Limits of the thread manager stack. */
|
| 286 |
|
|
|
| 287 |
|
|
extern char *__pthread_manager_thread_bos;
|
| 288 |
|
|
extern char *__pthread_manager_thread_tos;
|
| 289 |
|
|
|
| 290 |
|
|
#ifdef FLOATING_STACKS
|
| 291 |
|
|
/* Maximum stack size. */
|
| 292 |
|
|
extern size_t __pthread_max_stacksize;
|
| 293 |
|
|
#endif
|
| 294 |
|
|
|
| 295 |
|
|
/* Pending request for a process-wide exit */
|
| 296 |
|
|
|
| 297 |
|
|
extern int __pthread_exit_requested, __pthread_exit_code;
|
| 298 |
|
|
|
| 299 |
|
|
/* Set to 1 by gdb if we're debugging */
|
| 300 |
|
|
|
| 301 |
|
|
extern volatile int __pthread_threads_debug;
|
| 302 |
|
|
|
| 303 |
|
|
/* Globally enabled events. */
|
| 304 |
|
|
extern volatile td_thr_events_t __pthread_threads_events;
|
| 305 |
|
|
|
| 306 |
|
|
/* Pointer to descriptor of thread with last event. */
|
| 307 |
|
|
extern volatile pthread_descr __pthread_last_event;
|
| 308 |
|
|
|
| 309 |
|
|
/* Flag which tells whether we are executing on SMP kernel. */
|
| 310 |
|
|
extern int __pthread_smp_kernel;
|
| 311 |
|
|
|
| 312 |
|
|
/* Return the handle corresponding to a thread id */
|
| 313 |
|
|
|
| 314 |
|
|
static inline pthread_handle thread_handle(pthread_t id)
|
| 315 |
|
|
{
|
| 316 |
|
|
return &__pthread_handles[id % PTHREAD_THREADS_MAX];
|
| 317 |
|
|
}
|
| 318 |
|
|
|
| 319 |
|
|
/* Validate a thread handle. Must have acquired h->h_spinlock before. */
|
| 320 |
|
|
|
| 321 |
|
|
static inline int invalid_handle(pthread_handle h, pthread_t id)
|
| 322 |
|
|
{
|
| 323 |
|
|
return h->h_descr == NULL || h->h_descr->p_tid != id || h->h_descr->p_terminated;
|
| 324 |
|
|
}
|
| 325 |
|
|
|
| 326 |
|
|
static inline int nonexisting_handle(pthread_handle h, pthread_t id)
|
| 327 |
|
|
{
|
| 328 |
|
|
return h->h_descr == NULL || h->h_descr->p_tid != id;
|
| 329 |
|
|
}
|
| 330 |
|
|
|
| 331 |
|
|
/* Fill in defaults left unspecified by pt-machine.h. */
|
| 332 |
|
|
|
| 333 |
|
|
/* We round up a value with page size. */
|
| 334 |
|
|
#ifndef page_roundup
|
| 335 |
|
|
#define page_roundup(v,p) ((((size_t) (v)) + (p) - 1) & ~((p) - 1))
|
| 336 |
|
|
#endif
|
| 337 |
|
|
|
| 338 |
|
|
/* The page size we can get from the system. This should likely not be
|
| 339 |
|
|
changed by the machine file but, you never know. */
|
| 340 |
|
|
#ifndef PAGE_SIZE
|
| 341 |
|
|
#define PAGE_SIZE (sysconf (_SC_PAGE_SIZE))
|
| 342 |
|
|
#endif
|
| 343 |
|
|
|
| 344 |
|
|
/* The max size of the thread stack segments. If the default
|
| 345 |
|
|
THREAD_SELF implementation is used, this must be a power of two and
|
| 346 |
|
|
a multiple of PAGE_SIZE. */
|
| 347 |
|
|
#ifndef STACK_SIZE
|
| 348 |
|
|
#define STACK_SIZE (2 * 1024 * 1024)
|
| 349 |
|
|
#endif
|
| 350 |
|
|
|
| 351 |
|
|
/* The initial size of the thread stack. Must be a multiple of PAGE_SIZE. */
|
| 352 |
|
|
#ifndef INITIAL_STACK_SIZE
|
| 353 |
|
|
#define INITIAL_STACK_SIZE (4 * PAGE_SIZE)
|
| 354 |
|
|
#endif
|
| 355 |
|
|
|
| 356 |
|
|
/* Size of the thread manager stack. The "- 32" avoids wasting space
|
| 357 |
|
|
with some malloc() implementations. */
|
| 358 |
|
|
#ifndef THREAD_MANAGER_STACK_SIZE
|
| 359 |
|
|
#define THREAD_MANAGER_STACK_SIZE (2 * PAGE_SIZE - 32)
|
| 360 |
|
|
#endif
|
| 361 |
|
|
|
| 362 |
|
|
/* The base of the "array" of thread stacks. The array will grow down from
|
| 363 |
|
|
here. Defaults to the calculated bottom of the initial application
|
| 364 |
|
|
stack. */
|
| 365 |
|
|
#ifndef THREAD_STACK_START_ADDRESS
|
| 366 |
|
|
#define THREAD_STACK_START_ADDRESS __pthread_initial_thread_bos
|
| 367 |
|
|
#endif
|
| 368 |
|
|
|
| 369 |
|
|
/* Get some notion of the current stack. Need not be exactly the top
|
| 370 |
|
|
of the stack, just something somewhere in the current frame. */
|
| 371 |
|
|
#ifndef CURRENT_STACK_FRAME
|
| 372 |
|
|
#define CURRENT_STACK_FRAME ({ char __csf; &__csf; })
|
| 373 |
|
|
#endif
|
| 374 |
|
|
|
| 375 |
|
|
/* Recover thread descriptor for the current thread */
|
| 376 |
|
|
|
| 377 |
|
|
extern pthread_descr __pthread_find_self (void) __attribute__ ((const));
|
| 378 |
|
|
|
| 379 |
|
|
static inline pthread_descr thread_self (void) __attribute__ ((const));
|
| 380 |
|
|
static inline pthread_descr thread_self (void)
|
| 381 |
|
|
{
|
| 382 |
|
|
#ifdef THREAD_SELF
|
| 383 |
|
|
return THREAD_SELF;
|
| 384 |
|
|
#else
|
| 385 |
|
|
char *sp = CURRENT_STACK_FRAME;
|
| 386 |
|
|
if (sp >= __pthread_initial_thread_bos)
|
| 387 |
|
|
return &__pthread_initial_thread;
|
| 388 |
|
|
else if (sp >= __pthread_manager_thread_bos
|
| 389 |
|
|
&& sp < __pthread_manager_thread_tos)
|
| 390 |
|
|
return &__pthread_manager_thread;
|
| 391 |
|
|
else if (__pthread_nonstandard_stacks)
|
| 392 |
|
|
return __pthread_find_self();
|
| 393 |
|
|
else
|
| 394 |
|
|
#ifdef _STACK_GROWS_DOWN
|
| 395 |
|
|
return (pthread_descr)(((unsigned long)sp | (STACK_SIZE-1))+1) - 1;
|
| 396 |
|
|
#else
|
| 397 |
|
|
return (pthread_descr)((unsigned long)sp &~ (STACK_SIZE-1));
|
| 398 |
|
|
#endif
|
| 399 |
|
|
#endif
|
| 400 |
|
|
}
|
| 401 |
|
|
|
| 402 |
|
|
/* If MEMORY_BARRIER isn't defined in pt-machine.h, assume the architecture
|
| 403 |
|
|
doesn't need a memory barrier instruction (e.g. Intel x86). Still we
|
| 404 |
|
|
need the compiler to respect the barrier and emit all outstanding
|
| 405 |
|
|
operations which modify memory. Some architectures distinguish between
|
| 406 |
|
|
full, read and write barriers. */
|
| 407 |
|
|
|
| 408 |
|
|
#ifndef MEMORY_BARRIER
|
| 409 |
|
|
#define MEMORY_BARRIER() asm ("" : : : "memory")
|
| 410 |
|
|
#endif
|
| 411 |
|
|
#ifndef READ_MEMORY_BARRIER
|
| 412 |
|
|
#define READ_MEMORY_BARRIER() MEMORY_BARRIER()
|
| 413 |
|
|
#endif
|
| 414 |
|
|
#ifndef WRITE_MEMORY_BARRIER
|
| 415 |
|
|
#define WRITE_MEMORY_BARRIER() MEMORY_BARRIER()
|
| 416 |
|
|
#endif
|
| 417 |
|
|
|
| 418 |
|
|
/* Max number of times we must spin on a spinlock calling sched_yield().
|
| 419 |
|
|
After MAX_SPIN_COUNT iterations, we put the calling thread to sleep. */
|
| 420 |
|
|
|
| 421 |
|
|
#ifndef MAX_SPIN_COUNT
|
| 422 |
|
|
#define MAX_SPIN_COUNT 50
|
| 423 |
|
|
#endif
|
| 424 |
|
|
|
| 425 |
|
|
/* Max number of times the spinlock in the adaptive mutex implementation
|
| 426 |
|
|
spins actively on SMP systems. */
|
| 427 |
|
|
|
| 428 |
|
|
#ifndef MAX_ADAPTIVE_SPIN_COUNT
|
| 429 |
|
|
#define MAX_ADAPTIVE_SPIN_COUNT 100
|
| 430 |
|
|
#endif
|
| 431 |
|
|
|
| 432 |
|
|
/* Duration of sleep (in nanoseconds) when we can't acquire a spinlock
|
| 433 |
|
|
after MAX_SPIN_COUNT iterations of sched_yield().
|
| 434 |
|
|
With the 2.0 and 2.1 kernels, this MUST BE > 2ms.
|
| 435 |
|
|
(Otherwise the kernel does busy-waiting for realtime threads,
|
| 436 |
|
|
giving other threads no chance to run.) */
|
| 437 |
|
|
|
| 438 |
|
|
#ifndef SPIN_SLEEP_DURATION
|
| 439 |
|
|
#define SPIN_SLEEP_DURATION 2000001
|
| 440 |
|
|
#endif
|
| 441 |
|
|
|
| 442 |
|
|
/* Debugging */
|
| 443 |
|
|
|
| 444 |
|
|
#ifdef DEBUG
|
| 445 |
|
|
#include <assert.h>
|
| 446 |
|
|
#define ASSERT assert
|
| 447 |
|
|
#define MSG __pthread_message
|
| 448 |
|
|
#else
|
| 449 |
|
|
#define ASSERT(x)
|
| 450 |
|
|
#define MSG(msg,arg...)
|
| 451 |
|
|
#endif
|
| 452 |
|
|
|
| 453 |
|
|
/* Internal global functions */
|
| 454 |
|
|
|
| 455 |
|
|
extern void __pthread_do_exit (void *retval, char *currentframe)
|
| 456 |
|
|
__attribute__ ((__noreturn__));
|
| 457 |
|
|
extern void __pthread_destroy_specifics (void);
|
| 458 |
|
|
extern void __pthread_perform_cleanup (char *currentframe);
|
| 459 |
|
|
extern void __pthread_init_max_stacksize (void);
|
| 460 |
|
|
extern int __pthread_initialize_manager (void);
|
| 461 |
|
|
extern void __pthread_message (char * fmt, ...);
|
| 462 |
|
|
extern int __pthread_manager (void *reqfd);
|
| 463 |
|
|
extern int __pthread_manager_event (void *reqfd);
|
| 464 |
|
|
extern void __pthread_manager_sighandler (int sig);
|
| 465 |
|
|
extern void __pthread_reset_main_thread (void);
|
| 466 |
|
|
extern void __pthread_once_fork_prepare (void);
|
| 467 |
|
|
extern void __pthread_once_fork_parent (void);
|
| 468 |
|
|
extern void __pthread_once_fork_child (void);
|
| 469 |
|
|
extern void __flockfilelist (void);
|
| 470 |
|
|
extern void __funlockfilelist (void);
|
| 471 |
|
|
extern void __fresetlockfiles (void);
|
| 472 |
|
|
extern void __pthread_manager_adjust_prio (int thread_prio);
|
| 473 |
|
|
extern void __pthread_initialize_minimal (void);
|
| 474 |
|
|
|
| 475 |
|
|
extern int __pthread_attr_setguardsize (pthread_attr_t *__attr,
|
| 476 |
|
|
size_t __guardsize);
|
| 477 |
|
|
extern int __pthread_attr_getguardsize (const pthread_attr_t *__attr,
|
| 478 |
|
|
size_t *__guardsize);
|
| 479 |
|
|
extern int __pthread_attr_setstackaddr (pthread_attr_t *__attr,
|
| 480 |
|
|
void *__stackaddr);
|
| 481 |
|
|
extern int __pthread_attr_getstackaddr (const pthread_attr_t *__attr,
|
| 482 |
|
|
void **__stackaddr);
|
| 483 |
|
|
extern int __pthread_attr_setstacksize (pthread_attr_t *__attr,
|
| 484 |
|
|
size_t __stacksize);
|
| 485 |
|
|
extern int __pthread_attr_getstacksize (const pthread_attr_t *__attr,
|
| 486 |
|
|
size_t *__stacksize);
|
| 487 |
|
|
extern int __pthread_attr_setstack (pthread_attr_t *__attr, void *__stackaddr,
|
| 488 |
|
|
size_t __stacksize);
|
| 489 |
|
|
extern int __pthread_attr_getstack (const pthread_attr_t *__attr, void **__stackaddr,
|
| 490 |
|
|
size_t *__stacksize);
|
| 491 |
|
|
extern int __pthread_getconcurrency (void);
|
| 492 |
|
|
extern int __pthread_setconcurrency (int __level);
|
| 493 |
|
|
extern int __pthread_mutex_timedlock (pthread_mutex_t *__mutex,
|
| 494 |
|
|
const struct timespec *__abstime);
|
| 495 |
|
|
extern int __pthread_mutexattr_getpshared (const pthread_mutexattr_t *__attr,
|
| 496 |
|
|
int *__pshared);
|
| 497 |
|
|
extern int __pthread_mutexattr_setpshared (pthread_mutexattr_t *__attr,
|
| 498 |
|
|
int __pshared);
|
| 499 |
|
|
extern int __pthread_mutexattr_gettype (const pthread_mutexattr_t *__attr,
|
| 500 |
|
|
int *__kind);
|
| 501 |
|
|
extern void __pthread_kill_other_threads_np (void);
|
| 502 |
|
|
|
| 503 |
|
|
extern void __pthread_restart_old(pthread_descr th);
|
| 504 |
|
|
extern void __pthread_suspend_old(pthread_descr self);
|
| 505 |
|
|
extern int __pthread_timedsuspend_old(pthread_descr self, const struct timespec *abs);
|
| 506 |
|
|
|
| 507 |
|
|
extern void __pthread_restart_new(pthread_descr th);
|
| 508 |
|
|
extern void __pthread_suspend_new(pthread_descr self);
|
| 509 |
|
|
extern int __pthread_timedsuspend_new(pthread_descr self, const struct timespec *abs);
|
| 510 |
|
|
|
| 511 |
|
|
extern void __pthread_wait_for_restart_signal(pthread_descr self);
|
| 512 |
|
|
|
| 513 |
|
|
extern int __pthread_yield (void);
|
| 514 |
|
|
|
| 515 |
|
|
extern int __pthread_rwlock_timedrdlock (pthread_rwlock_t *__restrict __rwlock,
|
| 516 |
|
|
__const struct timespec *__restrict
|
| 517 |
|
|
__abstime);
|
| 518 |
|
|
extern int __pthread_rwlock_timedwrlock (pthread_rwlock_t *__restrict __rwlock,
|
| 519 |
|
|
__const struct timespec *__restrict
|
| 520 |
|
|
__abstime);
|
| 521 |
|
|
extern int __pthread_rwlockattr_destroy (pthread_rwlockattr_t *__attr);
|
| 522 |
|
|
|
| 523 |
|
|
extern int __pthread_barrierattr_getpshared (__const pthread_barrierattr_t *
|
| 524 |
|
|
__restrict __attr,
|
| 525 |
|
|
int *__restrict __pshared);
|
| 526 |
|
|
|
| 527 |
|
|
extern int __pthread_spin_lock (pthread_spinlock_t *__lock);
|
| 528 |
|
|
extern int __pthread_spin_trylock (pthread_spinlock_t *__lock);
|
| 529 |
|
|
extern int __pthread_spin_unlock (pthread_spinlock_t *__lock);
|
| 530 |
|
|
extern int __pthread_spin_init (pthread_spinlock_t *__lock, int __pshared);
|
| 531 |
|
|
extern int __pthread_spin_destroy (pthread_spinlock_t *__lock);
|
| 532 |
|
|
|
| 533 |
|
|
extern int __pthread_clock_gettime (hp_timing_t freq, struct timespec *tp);
|
| 534 |
|
|
extern void __pthread_clock_settime (hp_timing_t offset);
|
| 535 |
|
|
|
| 536 |
|
|
|
| 537 |
|
|
/* Global pointers to old or new suspend functions */
|
| 538 |
|
|
|
| 539 |
|
|
extern void (*__pthread_restart)(pthread_descr);
|
| 540 |
|
|
extern void (*__pthread_suspend)(pthread_descr);
|
| 541 |
|
|
extern int (*__pthread_timedsuspend)(pthread_descr, const struct timespec *);
|
| 542 |
|
|
|
| 543 |
|
|
/* Prototypes for the function without cancelation support when the
|
| 544 |
|
|
normal version has it. */
|
| 545 |
|
|
extern int __libc_close (int fd);
|
| 546 |
|
|
extern int __libc_nanosleep (const struct timespec *requested_time,
|
| 547 |
|
|
struct timespec *remaining);
|
| 548 |
|
|
/* Prototypes for some of the new semaphore functions. */
|
| 549 |
|
|
extern int __new_sem_post (sem_t * sem);
|
| 550 |
|
|
extern int __new_sem_init (sem_t *__sem, int __pshared, unsigned int __value);
|
| 551 |
|
|
extern int __new_sem_wait (sem_t *__sem);
|
| 552 |
|
|
extern int __new_sem_trywait (sem_t *__sem);
|
| 553 |
|
|
extern int __new_sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval);
|
| 554 |
|
|
extern int __new_sem_destroy (sem_t *__sem);
|
| 555 |
|
|
|
| 556 |
|
|
/* Prototypes for compatibility functions. */
|
| 557 |
|
|
extern int __pthread_attr_init_2_1 (pthread_attr_t *__attr);
|
| 558 |
|
|
extern int __pthread_attr_init_2_0 (pthread_attr_t *__attr);
|
| 559 |
|
|
extern int __pthread_create_2_1 (pthread_t *__restrict __thread1,
|
| 560 |
|
|
const pthread_attr_t *__attr,
|
| 561 |
|
|
void *(*__start_routine) (void *),
|
| 562 |
|
|
void *__restrict __arg);
|
| 563 |
|
|
extern int __pthread_create_2_0 (pthread_t *__restrict __thread1,
|
| 564 |
|
|
const pthread_attr_t *__attr,
|
| 565 |
|
|
void *(*__start_routine) (void *),
|
| 566 |
|
|
void *__restrict arg);
|
| 567 |
|
|
|
| 568 |
|
|
/* The functions called the signal events. */
|
| 569 |
|
|
extern void __linuxthreads_create_event (void);
|
| 570 |
|
|
extern void __linuxthreads_death_event (void);
|
| 571 |
|
|
extern void __linuxthreads_reap_event (void);
|
| 572 |
|
|
|
| 573 |
|
|
/* This function is called to initialize the pthread library. */
|
| 574 |
|
|
extern void __pthread_initialize (void);
|
| 575 |
|
|
|
| 576 |
|
|
#endif /* internals.h */
|