xref: /freebsd/lib/libthr/libthr.3 (revision 663222b9f65416465eab4161ebeaa3f675032387)
15335c6bcSRobert Watson.\" Copyright (c) 2005 Robert N. M. Watson
22f02abc1SKonstantin Belousov.\" Copyright (c) 2014 The FreeBSD Foundation, Inc.
35335c6bcSRobert Watson.\" All rights reserved.
45335c6bcSRobert Watson.\"
52f02abc1SKonstantin Belousov.\" Part of this documentation was written by
62f02abc1SKonstantin Belousov.\" Konstantin Belousov <kib@FreeBSD.org> under sponsorship
72f02abc1SKonstantin Belousov.\" from the FreeBSD Foundation.
82f02abc1SKonstantin Belousov.\"
95335c6bcSRobert Watson.\" Redistribution and use in source and binary forms, with or without
105335c6bcSRobert Watson.\" modification, are permitted provided that the following conditions
115335c6bcSRobert Watson.\" are met:
125335c6bcSRobert Watson.\" 1. Redistributions of source code must retain the above copyright
135335c6bcSRobert Watson.\"    notice, this list of conditions and the following disclaimer.
145335c6bcSRobert Watson.\" 2. Redistributions in binary form must reproduce the above copyright
155335c6bcSRobert Watson.\"    notice, this list of conditions and the following disclaimer in the
165335c6bcSRobert Watson.\"    documentation and/or other materials provided with the distribution.
175335c6bcSRobert Watson.\"
185335c6bcSRobert Watson.\" THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
195335c6bcSRobert Watson.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
205335c6bcSRobert Watson.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
215335c6bcSRobert Watson.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
225335c6bcSRobert Watson.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
235335c6bcSRobert Watson.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
245335c6bcSRobert Watson.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
255335c6bcSRobert Watson.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
265335c6bcSRobert Watson.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
275335c6bcSRobert Watson.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
285335c6bcSRobert Watson.\" SUCH DAMAGE.
295335c6bcSRobert Watson.\"
305335c6bcSRobert Watson.\" $FreeBSD$
315335c6bcSRobert Watson.\"
32*663222b9SSergey Kandaurov.Dd September 26, 2014
335335c6bcSRobert Watson.Dt LIBTHR 3
34aa12cea2SUlrich Spörlein.Os
355335c6bcSRobert Watson.Sh NAME
365335c6bcSRobert Watson.Nm libthr
37886659feSRuslan Ermilov.Nd "1:1 POSIX threads library"
385335c6bcSRobert Watson.Sh LIBRARY
395335c6bcSRobert Watson.Lb libthr
405335c6bcSRobert Watson.Sh SYNOPSIS
415335c6bcSRobert Watson.In pthread.h
425335c6bcSRobert Watson.Sh DESCRIPTION
435335c6bcSRobert WatsonThe
445335c6bcSRobert Watson.Nm
45886659feSRuslan Ermilovlibrary provides a 1:1 implementation of the
465335c6bcSRobert Watson.Xr pthread 3
475335c6bcSRobert Watsonlibrary interfaces for application threading.
48886659feSRuslan ErmilovIt
495335c6bcSRobert Watsonhas been optimized for use by applications expecting system scope thread
50886659feSRuslan Ermilovsemantics, and can provide significant performance improvements
51886659feSRuslan Ermilovcompared to
52886659feSRuslan Ermilov.Lb libkse .
532f02abc1SKonstantin Belousov.Pp
542f02abc1SKonstantin BelousovThe library is tightly integrated with the run-time link editor
552f02abc1SKonstantin Belousov.Xr ld-elf.so.1 1
562f02abc1SKonstantin Belousovand
572f02abc1SKonstantin Belousov.Lb libc ;
582f02abc1SKonstantin Belousovall three components must be built from the same source tree.
592f02abc1SKonstantin BelousovMixing
602f02abc1SKonstantin Belousov.Li libc
612f02abc1SKonstantin Belousovand
622f02abc1SKonstantin Belousov.Nm
632f02abc1SKonstantin Belousovlibraries from different versions of
642f02abc1SKonstantin Belousov.Fx
652f02abc1SKonstantin Belousovis not supported.
662f02abc1SKonstantin BelousovThe run-time linker
672f02abc1SKonstantin Belousov.Xr ld-elf.so.1 1
682f02abc1SKonstantin Belousovhas some code to ensure backward-compatibility with older versions of
692f02abc1SKonstantin Belousov.Nm .
702f02abc1SKonstantin Belousov.Pp
712f02abc1SKonstantin BelousovThe man page documents the quirks and tunables of the
722f02abc1SKonstantin Belousov.Nm .
732f02abc1SKonstantin BelousovWhen linking with
742f02abc1SKonstantin Belousov.Li -lpthread ,
752f02abc1SKonstantin Belousovthe run-time dependency
762f02abc1SKonstantin Belousov.Li libthr.so.3
772f02abc1SKonstantin Belousovis recorded in the produced object.
782f02abc1SKonstantin Belousov.Sh MUTEX ACQUISITION
792f02abc1SKonstantin BelousovA locked mutex (see
802f02abc1SKonstantin Belousov.Xr pthread_mutex_lock 3 )
812f02abc1SKonstantin Belousovis represented by a volatile variable of type
822f02abc1SKonstantin Belousov.Dv lwpid_t ,
832f02abc1SKonstantin Belousovwhich records the global system identifier of the thread
842f02abc1SKonstantin Belousovowning the lock.
852f02abc1SKonstantin Belousov.Nm
862f02abc1SKonstantin Belousovperforms a contested mutex acquisition in three stages, each of which
872f02abc1SKonstantin Belousovis more resource-consuming than the previous.
88*663222b9SSergey KandaurovThe first two stages are only applied for a mutex of
89*663222b9SSergey Kandaurov.Dv PTHREAD_MUTEX_ADAPTIVE_NP
90*663222b9SSergey Kandaurovtype and
91*663222b9SSergey Kandaurov.Dv PTHREAD_PRIO_NONE
92*663222b9SSergey Kandaurovprotocol (see
93*663222b9SSergey Kandaurov.Xr pthread_mutexattr 3 ) .
942f02abc1SKonstantin Belousov.Pp
95*663222b9SSergey KandaurovFirst, on SMP systems, a spin loop
962f02abc1SKonstantin Belousovis performed, where the library attempts to acquire the lock by
972f02abc1SKonstantin Belousov.Xr atomic 9
982f02abc1SKonstantin Belousovoperations.
992f02abc1SKonstantin BelousovThe loop count is controlled by the
1002f02abc1SKonstantin Belousov.Ev LIBPTHREAD_SPINLOOPS
1012f02abc1SKonstantin Belousovenvironment variable, with a default value of 2000.
1022f02abc1SKonstantin Belousov.Pp
1032f02abc1SKonstantin BelousovIf the spin loop
1042f02abc1SKonstantin Belousovwas unable to acquire the mutex, a yield loop
1052f02abc1SKonstantin Belousovis executed, performing the same
1062f02abc1SKonstantin Belousov.Xr atomic 9
1072f02abc1SKonstantin Belousovacquisition attempts as the spin loop,
1082f02abc1SKonstantin Belousovbut each attempt is followed by a yield of the CPU time
1092f02abc1SKonstantin Belousovof the thread using the
1102f02abc1SKonstantin Belousov.Xr sched_yield 2
1112f02abc1SKonstantin Belousovsyscall.
1122f02abc1SKonstantin BelousovBy default, the yield loop
1132f02abc1SKonstantin Belousovis not executed.
1142f02abc1SKonstantin BelousovThis is controlled by the
1152f02abc1SKonstantin Belousov.Ev LIBPTHREAD_YIELDLOOPS
1162f02abc1SKonstantin Belousovenvironment variable.
1172f02abc1SKonstantin Belousov.Pp
1182f02abc1SKonstantin BelousovIf both the spin and yield loops
1192f02abc1SKonstantin Belousovfailed to acquire the lock, the thread is taken off the CPU and
1202f02abc1SKonstantin Belousovput to sleep in the kernel with the
1212f02abc1SKonstantin Belousov.Xr umtx 2
1222f02abc1SKonstantin Belousovsyscall.
1232f02abc1SKonstantin BelousovThe kernel wakes up a thread and hands the ownership of the lock to
1242f02abc1SKonstantin Belousovthe woken thread when the lock becomes available.
1252f02abc1SKonstantin Belousov.Sh THREAD STACKS
1262f02abc1SKonstantin BelousovEach thread is provided with a private user-mode stack area
1272f02abc1SKonstantin Belousovused by the C runtime.
1282f02abc1SKonstantin BelousovThe size of the main (initial) thread stack is set by the kernel, and is
1292f02abc1SKonstantin Belousovcontrolled by the
1302f02abc1SKonstantin Belousov.Dv RLIMIT_STACK
1312f02abc1SKonstantin Belousovprocess resource limit (see
1322f02abc1SKonstantin Belousov.Xr getrlimit 2 ) .
1332f02abc1SKonstantin Belousov.Pp
1342f02abc1SKonstantin BelousovBy default, the main thread's stack size is equal to the value of
1352f02abc1SKonstantin Belousov.Dv RLIMIT_STACK
1362f02abc1SKonstantin Belousovfor the process.
1372f02abc1SKonstantin BelousovIf the
1382f02abc1SKonstantin Belousov.Ev LIBPTHREAD_SPLITSTACK_MAIN
1392f02abc1SKonstantin Belousovenvironment variable is present in the process environment
1402f02abc1SKonstantin Belousov(its value does not matter),
1412f02abc1SKonstantin Belousovthe main thread's stack is reduced to 4MB on 64bit architectures, and to
1422f02abc1SKonstantin Belousov2MB on 32bit architectures, when the threading library is initialized.
1432f02abc1SKonstantin BelousovThe rest of the address space area which has been reserved by the
1442f02abc1SKonstantin Belousovkernel for the initial process stack is used for non-initial thread stacks
1452f02abc1SKonstantin Belousovin this case.
1462f02abc1SKonstantin BelousovThe presence of the
1472f02abc1SKonstantin Belousov.Ev LIBPTHREAD_BIGSTACK_MAIN
1482f02abc1SKonstantin Belousovenvironment variable overrides
1492f02abc1SKonstantin Belousov.Ev LIBPTHREAD_SPLITSTACK_MAIN ;
1502f02abc1SKonstantin Belousovit is kept for backward-compatibility.
1512f02abc1SKonstantin Belousov.Pp
1522f02abc1SKonstantin BelousovThe size of stacks for threads created by the process at run-time
1532f02abc1SKonstantin Belousovwith the
1542f02abc1SKonstantin Belousov.Xr pthread_create 3
1552f02abc1SKonstantin Belousovcall is controlled by thread attributes: see
1562f02abc1SKonstantin Belousov.Xr pthread_attr 3 ,
1572f02abc1SKonstantin Belousovin particular, the
1582f02abc1SKonstantin Belousov.Xr pthread_attr_setstacksize 3 ,
1592f02abc1SKonstantin Belousov.Xr pthread_attr_setguardsize 3
1602f02abc1SKonstantin Belousovand
1612f02abc1SKonstantin Belousov.Xr pthread_attr_setstackaddr 3
1622f02abc1SKonstantin Belousovfunctions.
1632f02abc1SKonstantin BelousovIf no attributes for the thread stack size are specified, the default
1642f02abc1SKonstantin Belousovnon-initial thread stack size is 2MB for 64bit architectures, and 1MB
1652f02abc1SKonstantin Belousovfor 32bit architectures.
1662f02abc1SKonstantin Belousov.Sh RUN-TIME SETTINGS
1672f02abc1SKonstantin BelousovThe following environment variables are recognized by
1682f02abc1SKonstantin Belousov.Nm
1692f02abc1SKonstantin Belousovand adjust the operation of the library at run-time:
1702f02abc1SKonstantin Belousov.Bl -tag -width LIBPTHREAD_SPLITSTACK_MAIN
1712f02abc1SKonstantin Belousov.It Ev LIBPTHREAD_BIGSTACK_MAIN
1722f02abc1SKonstantin BelousovDisables the reduction of the initial thread stack enabled by
1732f02abc1SKonstantin Belousov.Ev LIBPTHREAD_SPLITSTACK_MAIN .
1742f02abc1SKonstantin Belousov.It Ev LIBPTHREAD_SPLITSTACK_MAIN
1752f02abc1SKonstantin BelousovCauses a reduction of the initial thread stack, as described in the
1762f02abc1SKonstantin Belousovsection
1772f02abc1SKonstantin Belousov.Sx THREAD STACKS .
1782f02abc1SKonstantin BelousovThis was the default behaviour of
1792f02abc1SKonstantin Belousov.Nm
1802f02abc1SKonstantin Belousovbefore
1812f02abc1SKonstantin Belousov.Fx 11.0 .
1822f02abc1SKonstantin Belousov.It Ev LIBPTHREAD_SPINLOOPS
1832f02abc1SKonstantin BelousovThe integer value of the variable overrides the default count of
1842f02abc1SKonstantin Belousoviterations in the
1852f02abc1SKonstantin Belousov.Li spin loop
1862f02abc1SKonstantin Belousovof the mutex acquisition.
1872f02abc1SKonstantin BelousovThe default count is 2000, set by the
1882f02abc1SKonstantin Belousov.Dv MUTEX_ADAPTIVE_SPINS
1892f02abc1SKonstantin Belousovconstant in the
1902f02abc1SKonstantin Belousov.Nm
1912f02abc1SKonstantin Belousovsources.
1922f02abc1SKonstantin Belousov.It Ev LIBPTHREAD_YIELDLOOPS
1932f02abc1SKonstantin BelousovA non-zero integer value enables the yield loop
1942f02abc1SKonstantin Belousovin the process of the mutex acquisition.
1952f02abc1SKonstantin BelousovThe value is the count of loop operations.
1962f02abc1SKonstantin Belousov.It Ev LIBPTHREAD_QUEUE_FIFO
1972f02abc1SKonstantin BelousovThe integer value of the variable specifies how often blocked
1982f02abc1SKonstantin Belousovthreads are inserted at the head of the sleep queue, instead of its tail.
1992f02abc1SKonstantin BelousovBigger values reduce the frequency of the FIFO discipline.
2002f02abc1SKonstantin BelousovThe value must be between 0 and 255.
2012f02abc1SKonstantin Belousov.El
2022f02abc1SKonstantin Belousov.Sh INTERACTION WITH RUN-TIME LINKER
2032f02abc1SKonstantin BelousovThe
2042f02abc1SKonstantin Belousov.Nm
2052f02abc1SKonstantin Belousovlibrary must appear before
2062f02abc1SKonstantin Belousov.Li libc
2072f02abc1SKonstantin Belousovin the global order of depended objects.
2082f02abc1SKonstantin Belousov.Pp
2092f02abc1SKonstantin BelousovLoading
2102f02abc1SKonstantin Belousov.Nm
2112f02abc1SKonstantin Belousovwith the
2122f02abc1SKonstantin Belousov.Xr dlopen 3
2132f02abc1SKonstantin Belousovcall in the process after the program binary is activated
2142f02abc1SKonstantin Belousovis not supported, and causes miscellaneous and hard-to-diagnose misbehaviour.
2152f02abc1SKonstantin BelousovThis is due to
2162f02abc1SKonstantin Belousov.Nm
2172f02abc1SKonstantin Belousovinterposing several important
2182f02abc1SKonstantin Belousov.Li libc
2192f02abc1SKonstantin Belousovsymbols to provide thread-safe services.
2202f02abc1SKonstantin BelousovIn particular,
2212f02abc1SKonstantin Belousov.Dv errno
2222f02abc1SKonstantin Belousovand the locking stubs from
2232f02abc1SKonstantin Belousov.Li libc
2242f02abc1SKonstantin Belousovare affected.
2252f02abc1SKonstantin BelousovThis requirement is currently not enforced.
2262f02abc1SKonstantin Belousov.Pp
2272f02abc1SKonstantin BelousovIf the program loads any modules at run-time, and those modules may require
2282f02abc1SKonstantin Belousovthreading services, the main program binary must be linked with
2292f02abc1SKonstantin Belousov.Li libpthread ,
2302f02abc1SKonstantin Belousoveven if it does not require any services from the library.
2312f02abc1SKonstantin Belousov.Pp
2322f02abc1SKonstantin Belousov.Nm
2332f02abc1SKonstantin Belousovcannot be unloaded; the
2342f02abc1SKonstantin Belousov.Xr dlclose 3
2352f02abc1SKonstantin Belousovfunction does not perform any action when called with a handle for
2362f02abc1SKonstantin Belousov.Nm .
2372f02abc1SKonstantin BelousovOne of the reasons is that the interposing of
2382f02abc1SKonstantin Belousov.Li libc
2392f02abc1SKonstantin Belousovfunctions cannot be undone.
2402f02abc1SKonstantin Belousov.Sh SIGNALS
2412f02abc1SKonstantin BelousovThe implementation also interposes the user-installed
2422f02abc1SKonstantin Belousov.Xr signal 3
2432f02abc1SKonstantin Belousovhandlers.
2442f02abc1SKonstantin BelousovThis interposing is done to postpone signal delivery to threads which
2452f02abc1SKonstantin Belousoventered (libthr-internal) critical sections, where the calling
2462f02abc1SKonstantin Belousovof the user-provided signal handler is unsafe.
2472f02abc1SKonstantin BelousovAn example of such a situation is owning the internal library lock.
2482f02abc1SKonstantin BelousovWhen a signal is delivered while the signal handler cannot be safely
2492f02abc1SKonstantin Belousovcalled, the call is postponed and performed until after the exit from
2502f02abc1SKonstantin Belousovthe critical section.
2512f02abc1SKonstantin BelousovThis should be taken into account when interpreting
2522f02abc1SKonstantin Belousov.Xr ktrace 1
2532f02abc1SKonstantin Belousovlogs.
2545335c6bcSRobert Watson.Sh SEE ALSO
2552f02abc1SKonstantin Belousov.Xr ktrace 1 ,
2562f02abc1SKonstantin Belousov.Xr ld-elf.so.1 1 ,
2572f02abc1SKonstantin Belousov.Xr getrlimit 2 ,
2582f02abc1SKonstantin Belousov.Xr umtx 2 ,
2592f02abc1SKonstantin Belousov.Xr dlclose 3 ,
2602f02abc1SKonstantin Belousov.Xr dlopen 3 ,
2612f02abc1SKonstantin Belousov.Xr errno 3 ,
2622f02abc1SKonstantin Belousov.Xr getenv 3 ,
2632f02abc1SKonstantin Belousov.Xr libc 3 ,
2642f02abc1SKonstantin Belousov.Xr pthread_attr 3 ,
2652f02abc1SKonstantin Belousov.Xr pthread_attr_setstacksize 3 ,
2662f02abc1SKonstantin Belousov.Xr pthread_create 3 ,
2672f02abc1SKonstantin Belousov.Xr signal 3 ,
2682f02abc1SKonstantin Belousov.Xr atomic 9
2695335c6bcSRobert Watson.Sh AUTHORS
2705335c6bcSRobert Watson.An -nosplit
27148856b00SRuslan ErmilovThe
2725335c6bcSRobert Watson.Nm
27348856b00SRuslan Ermilovlibrary
2745335c6bcSRobert Watsonwas originally created by
2752b7af31cSBaptiste Daroussin.An Jeff Roberson Aq Mt jeff@FreeBSD.org ,
2765335c6bcSRobert Watsonand enhanced by
2772b7af31cSBaptiste Daroussin.An Jonathan Mini Aq Mt mini@FreeBSD.org
2785335c6bcSRobert Watsonand
2792b7af31cSBaptiste Daroussin.An Mike Makonnen Aq Mt mtm@FreeBSD.org .
2805335c6bcSRobert WatsonIt has been substantially rewritten and optimized by
2812b7af31cSBaptiste Daroussin.An David Xu Aq Mt davidxu@FreeBSD.org .
282