1. 18 8月, 2014 2 次提交
    • R
      simplify and improve new cond var implementation · 2c4b510b
      Rich Felker 提交于
      previously, wake order could be unpredictable: if a waiter happened to
      leave its futex wait on the state early, e.g. due to EAGAIN while
      restarting after a signal handler, it could acquire the mutex out of
      turn. handling this required ugly O(n) list walking in the unwait
      function and accounting to remove waiters that already woke from the
      list.
      
      with the new changes, the "barrier" locks in each waiter node are only
      unlocked in turn. in addition to simplifying the code, this seems to
      improve performance slightly, probably by reducing the number of
      accesses threads make to each other's stacks.
      
      as an additional benefit, unrecoverable mutex re-locking errors
      (mainly ENOTRECOVERABLE for robust mutexes) no longer need to be
      handled with deadlock; they can be reported to the caller, since the
      unlocking sequence makes it unnecessary to rely on the mutex to
      synchronize access to the waiter list.
      2c4b510b
    • R
      redesign cond var implementation to fix multiple issues · 37195db8
      Rich Felker 提交于
      the immediate issue that was reported by Jens Gustedt and needed to be
      fixed was corruption of the cv/mutex waiter states when switching to
      using a new mutex with the cv after all waiters were unblocked but
      before they finished returning from the wait function.
      
      self-synchronized destruction was also handled poorly and may have had
      race conditions. and the use of sequence numbers for waking waiters
      admitted a theoretical missed-wakeup if the sequence number wrapped
      through the full 32-bit space.
      
      the new implementation is largely documented in the comments in the
      source. the basic principle is to use linked lists initially attached
      to the cv object, but detachable on signal/broadcast, made up of nodes
      residing in automatic storage (stack) on the threads that are waiting.
      this eliminates the need for waiters to access the cv object after
      they are signaled, and allows us to limit wakeup to one waiter at a
      time during broadcasts even when futex requeue cannot be used.
      
      performance is also greatly improved, roughly double some tests.
      
      basically nothing is changed in the process-shared cond var case,
      where this implementation does not work, since processes do not have
      access to one another's local storage.
      37195db8
  2. 16 8月, 2014 1 次提交
    • R
      make futex operations use private-futex mode when possible · bc09d58c
      Rich Felker 提交于
      private-futex uses the virtual address of the futex int directly as
      the hash key rather than requiring the kernel to resolve the address
      to an underlying backing for the mapping in which it lies. for certain
      usage patterns it improves performance significantly.
      
      in many places, the code using futex __wake and __wait operations was
      already passing a correct fixed zero or nonzero flag for the priv
      argument, so no change was needed at the site of the call, only in the
      __wake and __wait functions themselves. in other places, especially
      where the process-shared attribute for a synchronization object was
      not previously tracked, additional new code is needed. for mutexes,
      the only place to store the flag is in the type field, so additional
      bit masking logic is needed for accessing the type.
      
      for non-process-shared condition variable broadcasts, the futex
      requeue operation is unable to requeue from a private futex to a
      process-shared one in the mutex structure, so requeue is simply
      disabled in this case by waking all waiters.
      
      for robust mutexes, the kernel always performs a non-private wake when
      the owner dies. in order not to introduce a behavioral regression in
      non-process-shared robust mutexes (when the owning thread dies), they
      are simply forced to be treated as process-shared for now, giving
      correct behavior at the expense of performance. this can be fixed by
      adding explicit code to pthread_exit to do the right thing for
      non-shared robust mutexes in userspace rather than relying on the
      kernel to do it, and will be fixed in this way later.
      
      since not all supported kernels have private futex support, the new
      code detects EINVAL from the futex syscall and falls back to making
      the call without the private flag. no attempt to cache the result is
      made; caching it and using the cached value efficiently is somewhat
      difficult, and not worth the complexity when the benefits would be
      seen only on ancient kernels which have numerous other limitations and
      bugs anyway.
      bc09d58c
  3. 10 6月, 2014 1 次提交
    • R
      replace all remaining internal uses of pthread_self with __pthread_self · df15168c
      Rich Felker 提交于
      prior to version 1.1.0, the difference between pthread_self (the
      public function) and __pthread_self (the internal macro or inline
      function) was that the former would lazily initialize the thread
      pointer if it was not already initialized, whereas the latter would
      crash in this case. since lazy initialization is no longer supported,
      use of pthread_self no longer makes sense; it simply generates larger,
      slower code.
      df15168c
  4. 07 9月, 2012 1 次提交
    • R
      use restrict everywhere it's required by c99 and/or posix 2008 · 400c5e5c
      Rich Felker 提交于
      to deal with the fact that the public headers may be used with pre-c99
      compilers, __restrict is used in place of restrict, and defined
      appropriately for any supported compiler. we also avoid the form
      [restrict] since older versions of gcc rejected it due to a bug in the
      original c99 standard, and instead use the form *restrict.
      400c5e5c
  5. 03 10月, 2011 1 次提交
  6. 28 9月, 2011 3 次提交
    • R
      fix crash in pthread_cond_wait mutex-locked check · 3ac092bd
      Rich Felker 提交于
      it was assuming the result of the condition it was supposed to be
      checking for, i.e. that the thread ptr had already been initialized by
      pthread_mutex_lock. use the slower call to be safe.
      3ac092bd
    • R
      improve/debloat mutex unlock error checking in pthread_cond_wait · bc244533
      Rich Felker 提交于
      we're not required to check this except for error-checking mutexes,
      but it doesn't hurt. the new test is actually simpler/lighter, and it
      also eliminates the need to later check that pthread_mutex_unlock
      succeeds.
      bc244533
    • R
      check mutex owner in pthread_cond_wait · bfae1a8b
      Rich Felker 提交于
      when used with error-checking mutexes, pthread_cond_wait is required
      to fail with EPERM if the mutex is not locked by the caller.
      previously we relied on pthread_mutex_unlock to generate the error,
      but this is not valid, since in the case of such invalid usage the
      internal state of the cond variable has already been potentially
      corrupted (due to access outside the control of the mutex). thus, we
      have to check first.
      bfae1a8b
  7. 27 9月, 2011 2 次提交
    • R
      another cond var fix: requeue count race condition · 3bec53e0
      Rich Felker 提交于
      lock out new waiters during the broadcast. otherwise the wait count
      added to the mutex might be lower than the actual number of waiters
      moved, and wakeups may be lost.
      
      this issue could also be solved by temporarily setting the mutex
      waiter count higher than any possible real count, then relying on the
      kernel to tell us how many waiters were requeued, and updating the
      counts afterwards. however the logic is more complex, and i don't
      really trust the kernel. the solution here is also nice in that it
      replaces some atomic cas loops with simple non-atomic ops under lock.
      3bec53e0
    • R
      fix lost signals in cond vars · 1fa05210
      Rich Felker 提交于
      due to moving waiters from the cond var to the mutex in bcast, these
      waiters upon wakeup would steal slots in the count from newer waiters
      that had not yet been signaled, preventing the signal function from
      taking any action.
      
      to solve the problem, we simply use two separate waiter counts, and so
      that the original "total" waiters count is undisturbed by broadcast
      and still available for signal.
      1fa05210
  8. 26 9月, 2011 1 次提交
    • R
      redo cond vars again, use sequence numbers · 729d6368
      Rich Felker 提交于
      testing revealed that the old implementation, while correct, was
      giving way too many spurious wakeups due to races changing the value
      of the condition futex. in a test program with 5 threads receiving
      broadcast signals, the number of returns from pthread_cond_wait was
      roughly 3 times what it should have been (2 spurious wakeups for every
      legitimate wakeup). moreover, the magnitude of this effect seems to
      grow with the number of threads.
      
      the old implementation may also have had some nasty race conditions
      with reuse of the cond var with a new mutex.
      
      the new implementation is based on incrementing a sequence number with
      each signal event. this sequence number has nothing to do with the
      number of threads intended to be woken; it's only used to provide a
      value for the futex wait to avoid deadlock. in theory there is a
      danger of race conditions due to the value wrapping around after 2^32
      signals. it would be nice to eliminate that, if there's a way.
      
      testing showed no spurious wakeups (though they are of course
      possible) with the new implementation, as well as slightly improved
      performance.
      729d6368
  9. 25 9月, 2011 1 次提交
  10. 24 9月, 2011 1 次提交
    • R
      fix ABA race in cond vars, improve them overall · 97c5b5a8
      Rich Felker 提交于
      previously, a waiter could miss the 1->0 transition of block if
      another thread set block to 1 again after the signal function set
      block to 0. we now use the caller's thread id as a unique token to
      store in block, which no other thread will ever write there. this
      ensures that if block still contains the tid, no signal has occurred.
      spurious wakeups will of course occur whenever there is a spurious
      return from the futex wait and another thread has begun waiting on the
      cond var. this should be a rare occurrence except perhaps in the
      presence of interrupting signal handlers.
      
      signal/bcast operations have been improved by noting that they need
      not avoid inspecting the cond var's memory after changing the futex
      value. because the standard allows spurious wakeups, there is no way
      for an application to distinguish between a spurious wakeup just
      before another thread called signal/bcast, and the deliberate wakeup
      resulting from the signal/bcast call. thus the woken thread must
      assume that the signalling thread may still be waiting to act on the
      cond var, and therefore it cannot destroy/unmap the cond var.
      97c5b5a8
  11. 23 9月, 2011 1 次提交
  12. 03 8月, 2011 1 次提交
    • R
      unify and overhaul timed futex waits · ec381af9
      Rich Felker 提交于
      new features:
      
      - FUTEX_WAIT_BITSET op will be used for timed waits if available. this
        saves a call to clock_gettime.
      
      - error checking for the timespec struct is now inside __timedwait so
        it doesn't need to be duplicated everywhere. cond_timedwait still
        needs to duplicate it to avoid unlocking the mutex, though.
      
      - pushing and popping the cancellation handler is delegated to
        __timedwait, and cancellable/non-cancellable waits are unified.
      ec381af9
  13. 17 4月, 2011 1 次提交
    • R
      overhaul pthread cancellation · feee9890
      Rich Felker 提交于
      this patch improves the correctness, simplicity, and size of
      cancellation-related code. modulo any small errors, it should now be
      completely conformant, safe, and resource-leak free.
      
      the notion of entering and exiting cancellation-point context has been
      completely eliminated and replaced with alternative syscall assembly
      code for cancellable syscalls. the assembly is responsible for setting
      up execution context information (stack pointer and address of the
      syscall instruction) which the cancellation signal handler can use to
      determine whether the interrupted code was in a cancellable state.
      
      these changes eliminate race conditions in the previous generation of
      cancellation handling code (whereby a cancellation request received
      just prior to the syscall would not be processed, leaving the syscall
      to block, potentially indefinitely), and remedy an issue where
      non-cancellable syscalls made from signal handlers became cancellable
      if the signal handler interrupted a cancellation point.
      
      x86_64 asm is untested and may need a second try to get it right.
      feee9890
  14. 07 4月, 2011 1 次提交
  15. 25 3月, 2011 1 次提交
    • R
      overhaul cancellation to fix resource leaks and dangerous behavior with signals · b470030f
      Rich Felker 提交于
      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...
      b470030f
  16. 08 3月, 2011 1 次提交
  17. 18 2月, 2011 1 次提交
    • R
      reorganize pthread data structures and move the definitions to alltypes.h · e8827563
      Rich Felker 提交于
      this allows sys/types.h to provide the pthread types, as required by
      POSIX. this design also facilitates forcing ABI-compatible sizes in
      the arch-specific alltypes.h, while eliminating the need for
      developers changing the internals of the pthread types to poke around
      with arch-specific headers they may not be able to test.
      e8827563
  18. 12 2月, 2011 1 次提交