Commit Graph

240 Commits

Author SHA1 Message Date
Rich Felker 3990c5c6a4 avoid all malloc/free in timer creation/destruction
instead of allocating a userspace structure for signal-based timers,
simply use the kernel timer id. we use the fact that thread pointers
will always be zero in the low bit (actually more) to encode integer
timerid values as pointers.

also, this change ensures that the timer_destroy syscall has completed
before the library timer_destroy function returns, in case it matters.
2011-03-30 13:04:55 -04:00
Rich Felker b8be64c43d optimize timer creation and possibly protect against some minor races
the major idea of this patch is not to depend on having the timer
pointer delivered to the signal handler, and instead use the thread
pointer to get the callback function address and argument. this way,
the parent thread can make the timer_create syscall while the child
thread is starting, and it should never have to block waiting for the
barrier.
2011-03-30 12:06:39 -04:00
Rich Felker a1eb8cb5da avoid crash on stupid but allowable usage of pthread_mutex_unlock
unlocking an unlocked mutex is not UB for robust or error-checking
mutexes, so we must avoid calling __pthread_self (which might crash
due to lack of thread-register initialization) until after checking
that the mutex is locked.
2011-03-30 10:32:45 -04:00
Rich Felker 620a134638 rename __simple_malloc.c to lite_malloc.c - yes this affects behavior!
why does this affect behavior? well, the linker seems to traverse
archive files starting from its current position when resolving
symbols. since calloc.c comes alphabetically (and thus in sequence in
the archive file) between __simple_malloc.c and malloc.c, attempts to
resolve the "malloc" symbol for use by calloc.c were pulling in the
full malloc.c implementation rather than the __simple_malloc.c
implementation.

as of now, lite_malloc.c and malloc.c are adjacent in the archive and
in the correct order, so malloc.c should never be used to resolve
"malloc" unless it's already needed to resolve another symbol ("free"
or "realloc").
2011-03-30 09:29:49 -04:00
Rich Felker 02084109f0 streamline mutex unlock to remove a useless branch, use a_store to unlock
this roughly halves the cost of pthread_mutex_unlock, at least for
non-robust, normal-type mutexes.

the a_store change is in preparation for future support of archs which
require a memory barrier or special atomic store operation, and also
should prevent the possibility of the compiler misordering writes.
2011-03-30 09:06:00 -04:00
Rich Felker 124b4ebc8a cheap special-case optimization for normal mutexes
cycle-level benchmark on atom cpu showed typical pthread_mutex_lock
call dropping from ~120 cycles to ~90 cycles with this change. benefit
may vary with compiler options and version, but this optimization is
very cheap to make and should always help some.
2011-03-30 08:58:25 -04:00
Rich Felker 680630011d reorder timer initialization so that timer_create does not depend on free
this allows small programs which only create times, but never delete
them, to use simple_malloc instead of the full malloc.
2011-03-29 22:43:13 -04:00
Rich Felker 1c1aa32eea missing prototype for wcscoll (stub) 2011-03-29 18:30:27 -04:00
Rich Felker 8524d6536c revert mutex "optimization" that turned out to be worse 2011-03-29 15:11:25 -04:00
Rich Felker 80c4dcd253 implement POSIX timers
this implementation is superior to the glibc/nptl implementation, in
that it gives true realtime behavior. there is no risk of timer
expiration events being lost due to failed thread creation or failed
malloc, because the thread is created as time creation time, and
reused until the timer is deleted.
2011-03-29 13:01:25 -04:00
Rich Felker bf619d82c8 major improvements to cancellation handling
- there is no longer any risk of spoofing cancellation requests, since
  the cancel flag is set in pthread_cancel rather than in the signal
  handler.

- cancellation signal is no longer unblocked when running the
  cancellation handlers. instead, pthread_create will cause any new
  threads created from a cancellation handler to unblock their own
  cancellation signal.

- various tweaks in preparation for POSIX timer support.
2011-03-29 12:58:22 -04:00
Rich Felker 70c31c7bd7 some preliminaries for adding POSIX timers 2011-03-29 10:05:57 -04:00
Rich Felker 8250742b90 fix tempnam name generation, and a small bug in tmpnam on retry limit 2011-03-29 09:00:22 -04:00
Rich Felker a88edbec15 make tmpfile fail after exceeding max tries. 2011-03-29 08:37:57 -04:00
Rich Felker 507a9fa6ff fix tmpnam to generate better names, not depend on non-ISO-C symbols 2011-03-29 08:34:47 -04:00
Rich Felker 9646e4d024 fix messed-up errno if remove fails for a non-EISDIR reason 2011-03-29 08:25:59 -04:00
Rich Felker 0b240ccf52 learned something new - remove is supposed to support directories on POSIX 2011-03-29 08:24:28 -04:00
Rich Felker 4106cdcd2d revert some more spin optimizations that turned out to be pessimizations 2011-03-28 22:36:55 -04:00
Rich Felker 4cc78719dd fix bug from syscall overhaul: extra __syscall_ret call for 0-arg syscalls
this mainly just caused bloat, but could corrupt errno if a 0-arg
syscall ever failed.
2011-03-28 22:34:27 -04:00
Rich Felker 117581ca69 fix broken spinlock due to miscompilation
actually this trick also seems to have made the uncontended case slower.
2011-03-28 22:22:54 -04:00
Rich Felker ef839c73d7 prototype for getpass 2011-03-28 20:43:51 -04:00
Rich Felker 83b6c9e052 remove useless field in pthread struct (wasted a good bit of space) 2011-03-28 20:29:08 -04:00
Rich Felker 05b694028e fix getc - the classic error of trying to store EOF+0-255 in a char type.. 2011-03-28 17:31:01 -04:00
Rich Felker e3cd6c5c26 major stdio overhaul, using readv/writev, plus other changes
the biggest change in this commit is that stdio now uses readv to fill
the caller's buffer and the FILE buffer with a single syscall, and
likewise writev to flush the FILE buffer and write out the caller's
buffer in a single syscall.

making this change required fundamental architectural changes to
stdio, so i also made a number of other improvements in the process:

- the implementation no longer assumes that further io will fail
  following errors, and no longer blocks io when the error flag is set
  (though the latter could easily be changed back if desired)

- unbuffered mode is no longer implemented as a one-byte buffer. as a
  consequence, scanf unreading has to use ungetc, to the unget buffer
  has been enlarged to hold at least 2 wide characters.

- the FILE structure has been rearranged to maintain the locations of
  the fields that might be used in glibc getc/putc type macros, while
  shrinking the structure to save some space.

- error cases for fflush, fseek, etc. should be more correct.

- library-internal macros are used for getc_unlocked and putc_unlocked
  now, eliminating some ugly code duplication. __uflow and __overflow
  are no longer used anywhere but these macros. switch to read or
  write mode is also separated so the code can be better shared, e.g.
  with ungetc.

- lots of other small things.
2011-03-28 01:14:44 -04:00
Rich Felker ea343364a7 match glibc/lsb cancellation abi on i386
glibc made the ridiculous choice to use pass-by-register calling
convention for these functions, which is impossible to duplicate
directly on non-gcc compilers. instead, we use ugly asm to wrap and
convert the calling convention. presumably this works with every
compiler anyone could potentially want to use.
2011-03-25 22:13:57 -04:00
Rich Felker 92b52b70e8 remove -Wno-pointer-sign example from dist/config.mak 2011-03-25 16:50:49 -04:00
Rich Felker 9ae8d5fc71 fix all implicit conversion between signed/unsigned pointers
sadly the C language does not specify any such implicit conversion, so
this is not a matter of just fixing warnings (as gcc treats it) but
actual errors. i would like to revisit a number of these changes and
possibly revise the types used to reduce the number of casts required.
2011-03-25 16:34:03 -04:00
Rich Felker a37452430f simplify and optimize FILE lock handling 2011-03-24 23:16:52 -04:00
Rich Felker d8dc2faf10 prepare pthread_spin_unlock for archs that need memory barriers 2011-03-24 23:06:48 -04:00
Rich Felker b8b85a4272 optimize contended case for pthread_spin_trylock 2011-03-24 23:06:08 -04:00
Rich Felker c322fe4e1a optimize spinlock spin 2011-03-24 23:05:17 -04:00
Rich Felker 8ae2fa6563 fix non-atomicity of puts 2011-03-24 22:58:21 -04:00
Rich Felker b470030f83 overhaul cancellation to fix resource leaks and dangerous behavior with signals
this commit addresses two issues:

1. a race condition, whereby a cancellation request occurring after a
syscall returned from kernelspace but before the subsequent
CANCELPT_END would cause cancellable resource-allocating syscalls
(like open) to leak resources.

2. signal handlers invoked while the thread was blocked at a
cancellation point behaved as if asynchronous cancellation mode wer in
effect, resulting in potentially dangerous state corruption if a
cancellation request occurs.

the glibc/nptl implementation of threads shares both of these issues.

with this commit, both are fixed. however, cancellation points
encountered in a signal handler will not be acted upon if the signal
was received while the thread was already at a cancellation point.
they will of course be acted upon after the signal handler returns, so
in real-world usage where signal handlers quickly return, it should
not be a problem. it's possible to solve this problem too by having
sigaction() wrap all signal handlers with a function that uses a
pthread_cleanup handler to catch cancellation, patch up the saved
context, and return into the cancellable function that will catch and
act upon the cancellation. however that would be a lot of complexity
for minimal if any benefit...
2011-03-24 14:18:00 -04:00
Rich Felker 0958200166 very cheap double-free checks in malloc 2011-03-23 13:24:00 -04:00
Rich Felker aa398f56fa global cleanup to use the new syscall interface 2011-03-20 00:16:43 -04:00
Rich Felker be82e122bf if returning errno value directly from a syscall, we need to negate it. 2011-03-19 23:18:34 -04:00
Rich Felker bae862ab18 honor namespace for i386 syscall.h, even though it's not a standard header 2011-03-19 22:18:53 -04:00
Rich Felker 7877db6b2f fix typo in x86_64 part of syscall overhaul 2011-03-19 21:50:20 -04:00
Rich Felker 685e40bb09 syscall overhaul part two - unify public and internal syscall interface
with this patch, the syscallN() functions are no longer needed; a
variadic syscall() macro allows syscalls with anywhere from 0 to 6
arguments to be made with a single macro name. also, manually casting
each non-integer argument with (long) is no longer necessary; the
casts are hidden in the macros.

some source files which depended on being able to define the old macro
SYSCALL_RETURNS_ERRNO have been modified to directly use __syscall()
instead of syscall(). references to SYSCALL_SIGSET_SIZE and SYSCALL_LL
have also been changed.

x86_64 has not been tested, and may need a follow-up commit to fix any
minor bugs/oversights.
2011-03-19 21:36:10 -04:00
Rich Felker 462dbfc207 remove comment cruft that got left behind in x86_64 syscall.s 2011-03-19 19:05:43 -04:00
Rich Felker b0e83133e2 add some ioctl stuff to sys/mount.h 2011-03-19 18:58:32 -04:00
Rich Felker d00ff2950e overhaul syscall interface
this commit shuffles around the location of syscall definitions so
that we can make a syscall() library function with both SYS_* and
__NR_* style syscall names available to user applications, provides
the syscall() library function, and optimizes the code that performs
the actual inline syscalls in the library itself.

previously on i386 when built as PIC (shared library), syscalls were
incurring bus lock (lock prefix) overhead at entry and exit, due to
the way the ebx register was being loaded (xchg instruction with a
memory operand). now the xchg takes place between two registers.

further cleanup to arch/$(ARCH)/syscall.h is planned.
2011-03-19 18:51:42 -04:00
Rich Felker 56fd65e861 some linux headers useful from user apps.
i'm still not sure whether it's a good idea to include or use any of
these, but i'll add them for now. it may make more sense to just add
official kernel headers to the include path for compiling programs
that need them.
2011-03-18 21:53:30 -04:00
Rich Felker f5ba2bc9ca various legacy and linux-specific stuff
this commit is part of an effort to make more of busybox work
out-of-the-box.
2011-03-18 21:52:26 -04:00
Rich Felker fc27b96c7d document changes for 0.7.1 2011-03-18 09:59:20 -04:00
Rich Felker e18b563821 implement [v]swprintf 2011-03-18 09:19:09 -04:00
Rich Felker c35bb6645f implement wprintf family of functions
this implementation is extremely ugly and inefficient, but it avoids a
good deal of code duplication and bloat. it may be cleaned up later to
eliminate the remaining code duplication and some of the warts, but i
don't really care about its performance.

note that swprintf is not yet implemented.
2011-03-17 22:55:43 -04:00
Rich Felker a012aa879f fix broken wmemchr (unbounded search) 2011-03-17 22:38:45 -04:00
Rich Felker 047e434ef5 implement robust mutexes
some of this code should be cleaned up, e.g. using macros for some of
the bit flags, masks, etc. nonetheless, the code is believed to be
working and correct at this point.
2011-03-17 20:41:37 -04:00
Rich Felker 18c7ea8055 avoid function call to pthread_self in mutex unlock
if the mutex was previously locked, we can assume pthread_self was
already called at the time of locking, and thus that the thread
pointer is initialized.
2011-03-17 13:35:08 -04:00