1. 28 9月, 2011 3 次提交
  2. 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
  3. 26 9月, 2011 3 次提交
    • 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
    • R
      revert previous change in cond var waiter move · c11d1e69
      Rich Felker 提交于
      using swap has a race condition: the waiters must be added to the
      mutex waiter count *before* they are taken off the cond var waiter
      count, or wake events can be lost.
      c11d1e69
    • R
      cf940165
  4. 25 9月, 2011 2 次提交
  5. 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
  6. 23 9月, 2011 1 次提交
  7. 19 9月, 2011 1 次提交
    • R
      initial commit of the arm port · d960d4f2
      Rich Felker 提交于
      this port assumes eabi calling conventions, eabi linux syscall
      convention, and presence of the kernel helpers at 0xffff0f?0 needed
      for threads support. otherwise it makes very few assumptions, and the
      code should work even on armv4 without thumb support, as well as on
      systems with thumb interworking. the bits headers declare this a
      little endian system, but as far as i can tell the code should work
      equally well on big endian.
      
      some small details are probably broken; so far, testing has been
      limited to qemu/aboriginal linux.
      d960d4f2
  8. 18 9月, 2011 1 次提交
    • R
      overhaul clone syscall wrapping · 3f72cdac
      Rich Felker 提交于
      several things are changed. first, i have removed the old __uniclone
      function signature and replaced it with the "standard" linux
      __clone/clone signature. this was necessary to expose clone to
      applications anyway, and it makes it easier to port __clone to new
      archs, since it's now testable independently of pthread_create.
      
      secondly, i have removed all references to the ugly ldt descriptor
      structure (i386 only) from the c code and pthread structure. in places
      where it is needed, it is now created on the stack just when it's
      needed, in assembly code. thus, the i386 __clone function takes the
      desired thread pointer as its argument, rather than an ldt descriptor
      pointer, just like on all other sane archs. this should not affect
      applications since there is really no way an application can use clone
      with threads/tls in a way that doesn't horribly conflict with and
      clobber the underlying implementation's use. applications are expected
      to use clone only for creating actual processes, possibly with new
      namespace features and whatnot.
      3f72cdac
  9. 17 9月, 2011 3 次提交
  10. 13 9月, 2011 1 次提交
  11. 12 9月, 2011 1 次提交
    • R
      fix serious bug in pthread_join · 8fe5fa56
      Rich Felker 提交于
      on spurious wakeups/returns from __timedwait, pthread_join would
      "succeed" and unmap the thread's stack while it was still running. at
      best this would lead to SIGSEGV when the thread resumed execution, but
      in the worst case, the thread would later resume executing on top of
      another new thread's stack mapped at the same address.
      
      spent about 4 hours tracking this bug down, chasing rare
      difficult-to-reproduce stack corruption in a stress test program.
      still no idea *what* caused the spurious wakeups; i suspect it's a
      kernel bug.
      8fe5fa56
  12. 10 9月, 2011 1 次提交
    • R
      fix pthread_join wait call: thread termination tid futex is not private · eb351a9e
      Rich Felker 提交于
      this seeme to be the bug that prevented enabling of private futex
      support. i'm going to hold off on switching to private futexes until
      after the next release, and until i get a chance to audit all
      wait/wake calls to make sure they're using the correct private
      argument, but with this change it should be safe to enable private
      futex support.
      eb351a9e
  13. 05 9月, 2011 1 次提交
    • R
      handle pending cancellation when enabling async cancellation · a7778dae
      Rich Felker 提交于
      this is not strictly required by the standard, but without it, there
      is a race condition where cancellation arriving just before async
      cancellation is enabled might not be acted upon. it is impossible for
      a conforming application to work around this race condition since
      calling pthread_testcancel after setting async cancellation mode is
      not allowed (pthread_testcancel is not specified to be
      async-cancel-safe). thus the implementation should be responsible for
      eliminating the race, from a quality-of-implementation standpoint.
      a7778dae
  14. 15 8月, 2011 1 次提交
  15. 12 8月, 2011 2 次提交
    • R
      implement forkall · 4054a135
      Rich Felker 提交于
      this is a "nonstandard" function that was "rejected" by POSIX, but
      nonetheless had its behavior documented in the POSIX rationale for
      fork. it's present on solaris and possibly some other systems, and
      duplicates the whole calling process, not just a single thread. glibc
      does not have this function. it should not be used in programs
      intending to be portable, but may be useful for testing,
      checkpointing, etc. and it's an interesting (and quite small) example
      of the usefulness of the __synccall framework originally written to
      work around deficiencies in linux's setuid syscall.
      4054a135
    • R
      pthread and synccall cleanup, new __synccall_wait op · 407d9330
      Rich Felker 提交于
      fix up clone signature to match the actual behavior. the new
      __syncall_wait function allows a __synccall callback to wait for other
      threads to continue without returning, so that it can resume action
      after the caller finishes. this interface could be made significantly
      more general/powerful with minimal effort, but i'll wait to do that
      until it's actually useful for something.
      407d9330
  16. 08 8月, 2011 1 次提交
  17. 07 8月, 2011 5 次提交
    • R
      simplify unified timed wait code, drop support for newer method · b3c08a16
      Rich Felker 提交于
      the new absolute-time-based wait kernelside was hard to get right and
      basically just code duplication. it could only improve "performance"
      when waiting, and even then, the improvement was just slight drop in
      cpu usage during a wait.
      
      actually, with vdso clock_gettime, the "old" way will be even faster
      than the "new" way if the time has already expired, since it will not
      invoke any syscalls. it can determine entirely in userspace that it
      needs to return ETIMEDOUT.
      b3c08a16
    • R
      b2b95a58
    • R
      close should not be cancellable after "failing" with EINTR · 188ebf51
      Rich Felker 提交于
      normally we allow cancellation to be acted upon when a syscall fails
      with EINTR, since there is no useful status to report to the caller in
      this case, and the signal that caused the interruption was almost
      surely the cancellation request, anyway.
      
      however, unlike all other syscalls, close has actually performed its
      resource-deallocation function whenever it returns, even when it
      returned an error. if we allow cancellation at this point, the caller
      has no way of informing the program that the file descriptor was
      closed, and the program may later try to close the file descriptor
      again, possibly closing a different, newly-opened file.
      
      the workaround looks ugly (special-casing one syscall), but it's
      actually the case that close is the one and only syscall (at least
      among cancellation points) with this ugly property.
      188ebf51
    • R
      35787605
    • R
  18. 04 8月, 2011 3 次提交
    • R
      further debloat cancellation handlers · 5f37fc13
      Rich Felker 提交于
      cleanup push and pop are also no-ops if pthread_exit is not reachable.
      this can make a big difference for library code which needs to protect
      itself against cancellation, but which is unlikely to actually be used
      in programs with threads/cancellation.
      5f37fc13
    • R
      missed detail in cancellation bloat fix · 56385dd5
      Rich Felker 提交于
      56385dd5
    • R
      fix static linking dependency bloat with cancellation · 730bee72
      Rich Felker 提交于
      previously, pthread_cleanup_push/pop were pulling in all of
      pthread_create due to dependency on the __pthread_unwind_next
      function. this was not needed, as cancellation cleanup handlers can
      never be called unless pthread_exit or pthread_cancel is reachable.
      730bee72
  19. 03 8月, 2011 7 次提交
    • R
      overhaul rwlocks to address several issues · 50304f2e
      Rich Felker 提交于
      like mutexes and semaphores, rwlocks suffered from a race condition
      where the unlock operation could access the lock memory after another
      thread successfully obtained the lock (and possibly destroyed or
      unmapped the object). this has been fixed in the same way it was fixed
      for other lock types.
      
      in addition, the previous implementation favored writers over readers.
      in the absence of other considerations, that is the best behavior for
      rwlocks, and posix explicitly allows it. however posix also requires
      read locks to be recursive. if writers are favored, any attempt to
      obtain a read lock while a writer is waiting for the lock will fail,
      causing "recursive" read locks to deadlock. this can be avoided by
      keeping track of which threads already hold read locks, but doing so
      requires unbounded memory usage, and there must be a fallback case
      that favors readers in case memory allocation failed. and all of this
      must be synchronized. the cost, complexity, and risk of errors in
      getting it right is too great, so we simply favor readers.
      
      tracking of the owner of write locks has been removed, as it was not
      useful for anything. it could allow deadlock detection, but it's not
      clear to me that returning EDEADLK (which a buggy program is likely to
      ignore) is better than deadlocking; at least the latter behavior
      prevents further data corruption. a correct program cannot invoke this
      situation anyway.
      
      the reader count and write lock state, as well as the "last minute"
      waiter flag have all been combined into a single atomic lock. this
      means all state transitions for the lock are atomic compare-and-swap
      operations. this makes establishing correctness much easier and may
      improve performance.
      
      finally, some code duplication has been cleaned up. more is called
      for, especially the standard __timedwait idiom repeated in all locks.
      50304f2e
    • R
      timedwait: play it safe for now · 8aeee8db
      Rich Felker 提交于
      it's unclear whether EINVAL or ENOSYS is used when the operation is
      not supported, so check for both...
      8aeee8db
    • R
      correctly handle old kernels without FUTEX_WAIT_BITSET · 4717bfec
      Rich Felker 提交于
      futex returns EINVAL, not ENOSYS, when op is not supported.
      unfortunately this looks just like EINVAL from other causes, and we
      end up running the fallback code and getting EINVAL again. fortunately
      this case should be rare since correct code should not generate EINVAL
      anyway.
      4717bfec
    • R
      fix sem_timedwait bug introduced in timedwait unification · bdd89337
      Rich Felker 提交于
      this dec used to be performed by the cancellation handler, which was
      called when popped.
      bdd89337
    • 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
    • R
      avoid accessing mutex memory after atomic unlock · c68de0be
      Rich Felker 提交于
      this change is needed to fix a race condition and ensure that it's
      possible to unlock and destroy or unmap the mutex as soon as
      pthread_mutex_lock succeeds. POSIX explicitly gives such an example in
      the rationale and requires an implementation to allow such usage.
      c68de0be
    • R
      fix breakage in cancellation due to signal functions overhaul · 344ea148
      Rich Felker 提交于
      sigaddset was not accepting SIGCANCEL as a valid signal number.
      344ea148