10Sstevel@tonic-gate# 20Sstevel@tonic-gate# CDDL HEADER START 30Sstevel@tonic-gate# 40Sstevel@tonic-gate# The contents of this file are subject to the terms of the 5*2248Sraf# Common Development and Distribution License (the "License"). 6*2248Sraf# You may not use this file except in compliance with the License. 70Sstevel@tonic-gate# 80Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate# See the License for the specific language governing permissions 110Sstevel@tonic-gate# and limitations under the License. 120Sstevel@tonic-gate# 130Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate# 190Sstevel@tonic-gate# CDDL HEADER END 200Sstevel@tonic-gate# 210Sstevel@tonic-gate# 22*2248Sraf# Copyright 2006 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate# Use is subject to license terms. 240Sstevel@tonic-gate# 250Sstevel@tonic-gate# ident "%Z%%M% %I% %E% SMI" 260Sstevel@tonic-gate# 270Sstevel@tonic-gate 280Sstevel@tonic-gateThe Solaris Process Model Unification project: 290Sstevel@tonic-gate PSARC/2002/117 Solaris Process Model Unification 300Sstevel@tonic-gate 4470917 Solaris Process Model Unification 310Sstevel@tonic-gatefolded libthread into libc and has led to some fundamental changes 320Sstevel@tonic-gatein the rules by which code in libc must be developed and maintained. 330Sstevel@tonic-gate 340Sstevel@tonic-gateAll code in libc must be both MT-Safe and Fork-Safe 350Sstevel@tonic-gateand where possible (almost everywhere), Async-Signal-Safe. 360Sstevel@tonic-gate 370Sstevel@tonic-gateTo this end, the following rules should be followed: 380Sstevel@tonic-gate 390Sstevel@tonic-gateAlmost all internal libc locks (mutexes and read-write locks) 400Sstevel@tonic-gateshould be acquired and released via these interfaces: 410Sstevel@tonic-gate 420Sstevel@tonic-gate mutex_t some_lock = DEFAULTMUTEX; 430Sstevel@tonic-gate 440Sstevel@tonic-gate lmutex_lock(&some_lock); 450Sstevel@tonic-gate ... do something critical ... 460Sstevel@tonic-gate lmutex_unlock(&some_lock); 470Sstevel@tonic-gate 480Sstevel@tonic-gate rwlock_t some_rw_lock = DEFAULTRWLOCK; 490Sstevel@tonic-gate 500Sstevel@tonic-gate lrw_rdlock(&some_rw_lock); 510Sstevel@tonic-gate ... multiple threads can do something ... 520Sstevel@tonic-gate lrw_unlock(&some_rw_lock); 530Sstevel@tonic-gate 540Sstevel@tonic-gate lrw_wrlock(&some_rw_lock); 550Sstevel@tonic-gate ... only one thread can do something ... 560Sstevel@tonic-gate lrw_unlock(&some_rw_lock); 570Sstevel@tonic-gate 580Sstevel@tonic-gateThe above l* versions of the mutex and rwlock interfaces do more 590Sstevel@tonic-gatethan the ordinary interfaces: They define critical regions in 600Sstevel@tonic-gatewhich the calling thread cannot be suspended (making the region 610Sstevel@tonic-gatefork-safe) and in which the calling thread has all signals deferred 620Sstevel@tonic-gate(making the region async-signal-safe). 630Sstevel@tonic-gate 640Sstevel@tonic-gateHowever, certain rules apply to the code within these critical regions: 650Sstevel@tonic-gate 66*2248Sraf - The code must be of guaranteed short duration; no calls 67*2248Sraf to interfaces that might block indefinitely are allowed. 68*2248Sraf This means no calls into stdio or syslog() and no calls 69*2248Sraf to cond_wait() unless there is a guarantee of an almost- 70*2248Sraf immediate call to cond_signal() or cond_broadcast() 71*2248Sraf from elsewhere. 720Sstevel@tonic-gate 730Sstevel@tonic-gate - The code cannot call any non-l* synchronization 740Sstevel@tonic-gate primitives (mutex_lock(), _private_mutex_lock(), 750Sstevel@tonic-gate rw_wrlock(), rw_rdlock(), sema_wait(), etc.) 760Sstevel@tonic-gate 770Sstevel@tonic-gate - The code cannot call any functions outside of libc, 780Sstevel@tonic-gate including application callbacks and functions from 790Sstevel@tonic-gate dlopen()ed objects, such as those in the I18N code. 800Sstevel@tonic-gate 810Sstevel@tonic-gate - Because malloc(), calloc(), realloc(), and free() 820Sstevel@tonic-gate are designed to be interposed upon, they fall into 830Sstevel@tonic-gate the previous case of prohibition. None of these can 840Sstevel@tonic-gate be called by a thread while in a critical region. 850Sstevel@tonic-gate 860Sstevel@tonic-gateThere is a private memory allocator for use internally to libc. 870Sstevel@tonic-gateIt cannot be interposed upon and it is safe to use while in 880Sstevel@tonic-gatea critical region (or for that matter while not in a critical 890Sstevel@tonic-gateregion; it is async-signal-safe and fork-safe): 900Sstevel@tonic-gate 910Sstevel@tonic-gate void *lmalloc(size_t); 920Sstevel@tonic-gate void lfree(void *, size_t); 930Sstevel@tonic-gate 940Sstevel@tonic-gate void *libc_malloc(size_t); 950Sstevel@tonic-gate void *libc_realloc(void *, size_t); 960Sstevel@tonic-gate char *libc_strdup(const char *); 970Sstevel@tonic-gate void libc_free(void *); 980Sstevel@tonic-gate 990Sstevel@tonic-gatelmalloc() and lfree() are the basic interfaces. The libc_*() 1000Sstevel@tonic-gatevariants are built on top of lmalloc()/lfree() but they have 1010Sstevel@tonic-gatethe same interface signatures as the corresponding functions 1020Sstevel@tonic-gatewithout the 'libc_' prefix. lmalloc() and libc_malloc() 1030Sstevel@tonic-gatereturn zeroed memory blocks. Note that lmalloc()/lfree() 1040Sstevel@tonic-gaterequire the caller to remember the size parameter passed 1050Sstevel@tonic-gateto lmalloc() and to pass the same value to lfree(). 1060Sstevel@tonic-gate 1070Sstevel@tonic-gateMemory allocated by lmalloc() can only be freed by lfree(). 1080Sstevel@tonic-gateMemory allocated by libc_malloc(), libc_realloc(), or libc_strdup() 1090Sstevel@tonic-gatecan only be freed by libc_free(). Never pass such allocated 1100Sstevel@tonic-gatememory out of libc if the caller of libc is expected to free it. 1110Sstevel@tonic-gate 1120Sstevel@tonic-gatelmalloc()/lfree() is a small and simple power of two allocator. 1130Sstevel@tonic-gateDo not use it as a general-purpose allocator. Be kind to it. 1140Sstevel@tonic-gate 1150Sstevel@tonic-gateThere is a special mutual exclusion interface that exists for 1160Sstevel@tonic-gatecases, like code in the I18N interfaces, where mutual exclusion 1170Sstevel@tonic-gateis required but the above rules cannot be followed: 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate int fork_lock_enter(const char *); 1200Sstevel@tonic-gate void fork_lock_exit(void); 1210Sstevel@tonic-gate 1220Sstevel@tonic-gatefork_lock_enter() does triple-duty. Not only does it serialize 1230Sstevel@tonic-gatecalls to fork() and forkall(), but it also serializes calls to 1240Sstevel@tonic-gatethr_suspend() (fork() and forkall() also suspend other threads), 1250Sstevel@tonic-gateand furthermore it serializes I18N calls to functions in other 1260Sstevel@tonic-gatedlopen()ed L10N objects that might be calling malloc()/free(). 1270Sstevel@tonic-gateUse it in general like this: 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate (void) fork_lock_enter(NULL); 1300Sstevel@tonic-gate ... serialized; do something that might call malloc ... 1310Sstevel@tonic-gate fork_lock_exit(); 1320Sstevel@tonic-gate 1330Sstevel@tonic-gateThe 'const char *' argument to fork_lock_enter() should always 1340Sstevel@tonic-gatebe NULL except for two special cases: 1350Sstevel@tonic-gate - When called from fork() or forkall() 1360Sstevel@tonic-gate - When called from pthread_atfork() 1370Sstevel@tonic-gateThis enforces the prohibition against calling fork() or pthread_atfork() 1380Sstevel@tonic-gatefrom a pthread_atfork()-registered fork handler function while a fork() 1390Sstevel@tonic-gateprologue or epilogue is in progress. If _THREAD_ERROR_DETECTION is set 1400Sstevel@tonic-gateto 1 or 2 in the environment, such cases will draw a nasty message and 1410Sstevel@tonic-gatewill dump core if _THREAD_ERROR_DETECTION=2. fork_lock_enter() returns 1420Sstevel@tonic-gatenon-zero only if it is called from a fork handler. This is of interest 1430Sstevel@tonic-gateonly to callers that have to do something about this condition; the 1440Sstevel@tonic-gatereturn value should be ignored in all other cases (fork_lock_enter() 1450Sstevel@tonic-gatenever actually fails). 1460Sstevel@tonic-gate 1470Sstevel@tonic-gateIt is an error to call fork_lock_enter() while in a critical region 1480Sstevel@tonic-gate(that is, while holding any internal libc lock). 1490Sstevel@tonic-gate 1500Sstevel@tonic-gateOn return from fork_lock_enter(), no internal libc locks are held 1510Sstevel@tonic-gatebut a flag has been set to cause other callers of fork_lock_enter() 1520Sstevel@tonic-gateto delay (via _cond_wait()) until fork_lock_exit() is called. 1530Sstevel@tonic-gate 1540Sstevel@tonic-gateThese are the rules to follow for memory allocation: 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate - If a function acquires an internal libc lock or is called while 1570Sstevel@tonic-gate an internal libc lock is held: 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate * The malloc family cannot be used. 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate * lmalloc or libc_malloc should be used. The memory must 1620Sstevel@tonic-gate be released by lfree or libc_free, respectively. 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate * lfree takes an argument to tell the size of the releasing 1650Sstevel@tonic-gate memory. If the function does not know the size at the 1660Sstevel@tonic-gate releasing point, libc_malloc and libc_free should be used. 1670Sstevel@tonic-gate 1680Sstevel@tonic-gate * As the memory allocated by lmalloc or libc_malloc needs 1690Sstevel@tonic-gate to be released by lfree or libc_free and these are internal 1700Sstevel@tonic-gate to libc, they cannot be used to allocate memory that might 1710Sstevel@tonic-gate be released by application code outside libc. 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate * If the memory allocation by malloc() cannot be avoided and 1740Sstevel@tonic-gate the scalability of the function does not matter much, the 1750Sstevel@tonic-gate function can be serialized with fork_lock_enter() instead 1760Sstevel@tonic-gate of lmutex_lock(). 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate * If the memory allocation by malloc() cannot be avoided and 1790Sstevel@tonic-gate the scalability of the function does matter, another 1800Sstevel@tonic-gate implementation of the function will be necessary. 1810Sstevel@tonic-gate 1820Sstevel@tonic-gateIn a DEBUG build of libc: 1830Sstevel@tonic-gate make THREAD_DEBUG=-DTHREAD_DEBUG install 1840Sstevel@tonic-gatemany of these rules are enforced by ASSERT() statements scattered about 1850Sstevel@tonic-gatein the libc sources. This is the default mode for building libc when 1860Sstevel@tonic-gatea DEBUG nightly build is performed. 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate----- 1890Sstevel@tonic-gate 1900Sstevel@tonic-gateSome i18n code cannot be distributed as open source. To enable the rest of 1910Sstevel@tonic-gatelibc to be distributed as open source, those i18n files now live in a 1920Sstevel@tonic-gateseparate libc_i18n directory. Those source files are position-independently 1930Sstevel@tonic-gatecompiled and are archived into the libc_i18n.a library. The libc_i18n.a 1940Sstevel@tonic-gatearchive library is installed into the $(ROOTFS_LIBDIR) and 1950Sstevel@tonic-gate$(ROOTFS_LIBDIR64) directories. During link phase, libc.so.1 links with 1960Sstevel@tonic-gatelibc_i18n.a in the proto area. Therefore, the build of the libc_i18n tree 1970Sstevel@tonic-gateneeds to be done before the build of the libc tree. Also the compilation 1980Sstevel@tonic-gateconditions such as the setting of CFLAGS and CPPFLAGS for the libc_i18n 1990Sstevel@tonic-gatestuff need to be compatible with the ones for the libc stuff. Whenever 2000Sstevel@tonic-gatechanges that affect the compilation conditions of libc occur, the changes 2010Sstevel@tonic-gateshould be propagated to libc_i18n. 202*2248Sraf 203*2248Sraf----- 204*2248Sraf 205*2248SrafThe putback of the project: 206*2248Sraf 6416832 libaio and librt can and should be folded into libc 207*2248Srafintroduced several libc-private locking interfaces: 208*2248Sraf void sig_mutex_lock(mutex_t *); 209*2248Sraf void sig_mutex_unlock(mutex_t *); 210*2248Sraf int sig_mutex_trylock(mutex_t *); 211*2248Sraf int sig_cond_wait(cond_t *, mutex_t *); 212*2248Sraf int sig_cond_reltimedwait(cond_t *, mutex_t *, const timespec_t *); 213*2248Srafwhich are declared in both "thr_uberdata.h" and "mtlib.h". 214*2248Sraf 215*2248SrafThey are used in specialized code in libc, like the asynchronous i/o code. 216*2248SrafUnlike the lmutex_lock() and lmutex_unlock() interfaces described above, 217*2248Srafthese interfaces do not define critical regions, but signals are 218*2248Srafdeferred while locks acquired by these functions are held, making 219*2248Sraftheir use be async-signal safe. Calls to malloc(), calloc(), realloc(), 220*2248Srafand free() are permissible while holding such locks. 221*2248Sraf 222*2248SrafThese interfaces were brought over from code in the former libaio 223*2248Srafand librt and are necessary because, where they are used, the code 224*2248Srafmust execute potentially long-term waits and must be cancelable. 225*2248Srafsig_cond_wait() and sig_cond_reltimedwait() are cancellation points. 226*2248Sraf 227*2248SrafThese interfaces are available for other uses inside libc, as 228*2248Srafthe need arises. (There is no need if the code does not perform 229*2248Sraflong-term waits.) Just follow a few rules to be self-consistent: 230*2248Sraf - Don't mix calls to mutex_[un]lock(), lmutex_[un]lock() and 231*2248Sraf sig_mutex_[un]lock() on the same mutex. 232*2248Sraf - Don't call cond_wait() with a mutex acquired by sig_mutex_lock(); 233*2248Sraf call sig_cond_wait() or sig_cond_reltimedwait(). 234*2248Sraf - Use pthread_cleanup_push() and pthread_cleanup_pop() to make 235*2248Sraf your code cancellation-safe. 236*2248Sraf - The sig_*() interfaces are not in themselves fork-safe. 237*2248Sraf You have to employ other logic to make your code fork-safe. 238*2248Sraf See the tail of postfork1_child() for examples. 239