Commit graph

99 commits

Author SHA1 Message Date
Chih-Hung Hsieh
fa658eb094 Fix bugprone-macro-parentheses warnings
Bug: 150783705
Test: WITH_TIDY=1 make
Change-Id: If47b7adaa5f3ba3c518fb0be48f0ffdc81d204b2
2020-03-04 13:22:05 -08:00
Elliott Hughes
95c6cd753f Stop using the __ANDROID_API_x__ constants.
Historically we've made a few mistakes where they haven't matched the
right number. And most non-Googlers are much more familiar with the
numbers, so it seems to make sense to rely more on them. Especially in
header files, which we actually expect real people to have to read from
time to time.

Test: treehugger
Change-Id: I0d4a97454ee108de1d32f21df285315c5488d886
2019-12-20 13:26:14 -08:00
Elliott Hughes
f106a391d3 Remove __INTRODUCED_IN for obsolete API levels.
The NDK only supports >= 16, so remove anything older than that to avoid
giving the misleading impression that such old targets are still
supported.

(This change doesn't touch <unistd.h>. I'll follow up with that once the
outstanding FORTIFY changes to that file are in.)

Test: builds
Change-Id: I6cc6ecdb99fe228a4afa71f78e5fd45309ba9786
2019-10-03 16:09:04 -07:00
Tom Cherry
69010802d0 Implement new clock wait functions
pthread_cond_clockwait
pthread_mutex_clocklock
pthread_rwlock_clockrdlock
pthread_rwlock_clockwrlock
sem_clockwait

Bug: 35756266
Test: new unit tests
Change-Id: I71bd25eeec6476134b368d5bdf2f729d0bba595e
2019-07-15 09:06:19 -07:00
Logan Chien
3f68efc9b1 Suppress warnings on pthread_create
This commit suppress warnings on pthread_create because clang-r353983
would check the function declaration against the built-in function
defined in llvm/tools/clang/include/clang/Basic/Builtins.def and find a
mismatch.

Note: This is only found by versioner because these files are not system
headers from the perspective of versioner.  This warning is ignored in
normal builds because bionic headers are system headers in normal
builds.

Bug: 126457671
Test: lunch walleye-userdebug && make
Change-Id: I3f05ba19861f1b9db55c7c55c4496a845802e831
2019-03-20 22:49:03 +08:00
Elliott Hughes
7c5bbf627c Add missing __INTRODUCED_IN to pthread_attr_setinheritsched/pthread_attr_getinheritsched.
Bug: https://github.com/android-ndk/ndk/issues/716
Test: N/A
Change-Id: Ie49f96976f93b0f0d59796169e98918a9f69c0df
2018-06-07 09:11:47 -07:00
Tom Cherry
f0f3d2f002 Create PTHREAD_COND_INITIALIZER_MONOTONIC_NP
Needed for std::condition_variable to wait on CLOCK_MONOTONIC.

Test: boot bullhead
Change-Id: I32a30817f0ec2190393fcc6faec45b40822cba32
2018-03-26 09:00:51 -07:00
Tom Cherry
c6b5bcd182 Add _monotonic_np versions of timed wait functions
As a follow up to Ibba98f5d88be1c306d14e9b9366302ecbef6d534, where we
added a work around to convert the CLOCK_REALTIME timeouts to
CLOCK_MONOTONIC for pthread and semaphore timed wait functions, we're
introducing a set of _monotonic_np versions of each of these functions
that wait on CLOCK_MONOTONIC directly.

The primary motivation here is that while the above work around helps
for 3rd party code, it creates a dilemma when implementing new code
that would use these functions: either one implements code with these
functions knowing there is a race condition possible or one avoids
these functions and reinvent their own waiting/signaling mechanisms.
Neither are satisfactory, so we create a third option to use these
Android specific _monotonic_np functions that completely remove the
race condition while keeping the rest of the interface.

Specifically this adds the below functions:
pthread_mutex_timedlock_monotonic_np()
pthread_cond_timedwait_monotonic_np()
pthread_rwlock_timedrdlock_monotonic_np()
pthread_rwlock_timedwrlock_monotonic_np()
sem_timedwait_monotonic_np()

Note that pthread_cond_timedwait_monotonic_np() previously existed and
was removed since it's possible to initialize a condition variable to
use CLOCK_MONOTONIC.  It is added back for a mix of reasons,
1) Symmetry with the rest of the functions we're adding
2) libc++ cannot easily take advantage of the new initializer, but
   will be able to use this function in order to wait on
   std::steady_clock
3) Frankly, it's a better API to specify the clock in the waiter function
   than to specify the clock when the condition variable is
   initialized.

Bug: 73951740
Test: new unit tests
Change-Id: I23aa5c204e36a194237d41e064c5c8ccaa4204e3
2018-03-20 18:41:22 -07:00
Elliott Hughes
cc0fe6e8c0 The future is now (2018 edition).
Bug: N/A
Test: builds
Change-Id: Ic638e5b1f4e5901885fe7bc60b2a265817636b21
2018-01-30 08:54:12 -08:00
Yabin Cui
6b9c85b36d Support priority inheritance mutex in 64bit programs.
Bug: http://b/29177606
Test: run bionic-unit-tests on walleye.
Test: run bionic-unit-tests-glibc on host.
Change-Id: Iac349284aa73515f384e7509445f87434757f59e
2018-01-24 16:11:07 -08:00
Elliott Hughes
8aecba7aa6 Implement pthread_attr_getinheritsched/pthread_attr_setinheritsched.
Historically, Android defaulted to EXPLICIT but with a special case
because SCHED_NORMAL/priority 0 was awkward. Because the code couldn't
actually tell whether SCHED_NORMAL/priority 0 was a genuine attempt to
explicitly set those attributes (because the parent thread is SCHED_FIFO,
say) or just because the pthread_attr_t was left at its defaults.

Now we support INHERIT, we could call sched_getscheduler to see whether
we actually need to call sched_setscheduler, but since the major cost
is the fixed syscall overhead, we may as well just conservatively
call sched_setscheduler and let the kernel decide whether it's a
no-op. (Especially because we'd then have to add both sched_getscheduler
and sched_setscheduler to any seccomp filter.)

Platform code (or app code that only needs to support >= P) can actually
add a call to pthread_attr_setinheritsched to say that they just want
to inherit (if they know that none of their threads actually mess with
scheduler attributes at all), which will save them a sched_setscheduler
call except in the doubly-special case of SCHED_RESET_ON_FORK (which we
do handle).

An alternative would be "make pthread_attr_setschedparams and
pthread_attr_setschedprio set EXPLICIT and change the platform default
to INHERIT", but even though I can only think of weird pathological
examples where anyone would notice that change, that behavior -- of
pthread_attr_setschedparams/pthread_attr_setschedprio overriding an
earlier call to pthread_attr_setinheritsched -- isn't allowed by POSIX
(whereas defaulting to EXPLICIT is).

If we have a lot of trouble with this change in the app compatibility
testing phase, though, we'll want to reconsider this decision!

 -*-

This change also removes a comment about setting the scheduler attributes
in main_thread because we'd have to actually keep them up to date,
and it's not clear that doing so would be worth the trouble.

Also make async_safe_format_log preserve errno so we don't have to be
so careful around it.

Bug: http://b/67471710
Test: ran tests
Change-Id: Idd026c4ce78a536656adcb57aa2e7b2c616eeddf
2017-10-25 14:28:42 -07:00
Elliott Hughes
dff08ced56 Add pthread_setschedprio.
Bug: http://b/26204555
Test: ran tests
Change-Id: Ic34062b9b6036a1ce2642a069514bab48a893338
2017-10-17 09:14:05 -07:00
Elliott Hughes
ff26a16c1d Re-submit "Name function arguments in libc headers for Studio."
This reverts commit 9af9120091 (a revert
of 079bff4fa5), now the versioner bug is
fixed.

Bug: http://b/64613623 # header bug
Bug: http://b/64802958 # versioner bug
Change-Id: I1cb9d7832d4b3aecdc57a9285e2291443e59d02d
2017-08-18 00:00:38 +00:00
Colin Cross
9af9120091 Revert "Name function arguments in libc headers for Studio."
This reverts commit 079bff4fa5.

Broke builds with SANITIZE_HOST=address with an asan failure in versioner.

Change-Id: I22b113fd5405589d1a25e5e137c450aaba1ade5f
2017-08-17 18:29:54 +00:00
Elliott Hughes
079bff4fa5 Name function arguments in libc headers for Studio.
Second batch of headers...

Bug: http://b/64613623
Test: builds
Change-Id: I8eef043dbf32afee8ff814e9d005f46aee8fa21f
2017-08-16 16:30:54 -07:00
Elliott Hughes
0138c46794 Remove reference to non-existent __LP32__.
Since there is no 64-bit before 21, there's no need for us to talk about
bitness at all.

Bug: https://github.com/android-ndk/ndk/issues/481
Test: builds
Change-Id: I307466997df35d7f1d0eef7dc7cb35ac3033d25d
2017-08-07 09:04:08 -07:00
Elliott Hughes
3f66e74b90 Remove nullability specifications.
Bug: http://b/64251432
Test: builds
Change-Id: I5b1613484783f7478d30b5e694007f77fa626659
2017-08-01 13:24:40 -07:00
Dan Albert
c2ce12046c Put back more legacy pthread APIs.
Test: make checkbuild
Bug: https://github.com/android-ndk/ndk/issues/423
Bug: https://stackoverflow.com/q/44580542/632035
Change-Id: Ibf52a969afffbfcdf6793a0bf8b0e10bbdd1f32c
2017-06-22 12:52:17 -07:00
Dan Albert
8c67f1e617 Expose pthread_mutex_lock_timeout_np to the NDK.
The proper API for this isn't available until L, so expose this for
API levels earlier than that.

Test: make checkbuild
Bug: https://github.com/android-ndk/ndk/issues/420
Change-Id: I382b8f557be9530f3e13aaae353b4a6e7f9301ab
2017-06-12 16:46:01 -07:00
Josh Gao
2e8e5e60b6 DO NOT MERGE: The future is now.
__INTRODUCED_IN_FUTURE -> __INTRODUCED_IN(26)

Bug: http://b/37437368
Test: treehugger
Change-Id: I601a23da83b65a0cd582cc840ed3856a9031b673
(cherry picked from commit cb302f932a)
2017-04-20 16:57:25 -07:00
Elliott Hughes
468a5d2d83 pthread barrier and spinlock types should only appear at API level 24.
Bug: https://github.com/android-ndk/ndk/issues/336
Test: builds
Change-Id: I938d9d7ea879d1dbc355f14e100f1ea31a51a1f0
2017-03-21 09:55:32 -07:00
Elliott Hughes
95fa061678 Rename __pure, __pure2, and __purefunc.
These names were pretty misleading (aka "backwards"), so switch to the
same obvious names glibc uses.

Test: build.
Change-Id: Ia98c9dbbccd0820386116562347654e84669034a
2016-09-28 12:36:38 -07:00
Elliott Hughes
280236537e POSIX says <sys/types.h> should make all the pthread_* types available.
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_types.h.html

Change-Id: I606194e15ceaa3129f266a9a460c358eaa8bf686
2016-08-12 13:18:32 -07:00
Elliott Hughes
02ac028bf7 Fix pthread_barrierattr_getpshared prototype.
As http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_barrierattr_getpshared.html
is my witness, there's a missing `const` here.

Change-Id: Ifd68188fb952460b64d144bdc21a53bc40ed82a7
2016-08-11 23:18:13 -07:00
Elliott Hughes
5470c18361 Make it possible to build bionic with -I instead of -isystem.
Various things:

* work around -Wnullability-completeness.
* use C++ casts in C++ and C casts in C.
* stop using attributes clang doesn't support (such as `warning`).
* remove duplicate definitions of XATTR_CREATE and XATTR_REPLACE.

Change-Id: I07649e46275b28a23ca477deea119fe843999533
2016-07-22 13:12:53 -07:00
Josh Gao
46b44160e9 Update header versions for NDK platform fixes.
Bug: http://b/28178111
Change-Id: Icd638673b409aa43a91490f77c6b4d79c9ea20d9
2016-06-02 13:40:35 -07:00
Elliott Hughes
e2f25831eb Remove declaration of pthread_cond_timedwait_relative_np.
It's been deprecated long enough, and nothing left in the tree (except
code that isn't even built) is still using it.

Bug: http://b/27918161
Change-Id: Ibf824c1063d49484037de5c03b98bec5bdd4dcf6
2016-05-09 13:29:26 -07:00
Elliott Hughes
3164ab733c Remove pthread_mutex_lock_timeout_np from <pthread.h>.
It's been deprecated long enough, and nothing left in the tree (except
code that isn't even built) is still using it.

Bug: http://b/27918161
Change-Id: I1786f5e2528a23c17b3f7298f4ba5fc7761a26c5
2016-05-06 17:46:48 -07:00
Dan Albert
baa2a973bd Use clang's nullability instead of nonnull.
http://clang.llvm.org/docs/AttributeReference.html#nonnull

_Nonnull is similar to the nonnull attribute in that it will instruct
compilers to warn the user if it can prove that a null argument is
being passed. Unlike the nonnull attribute, this annotation indicated
that a value *should not* be null, not that it *cannot* be null, or
even that the behavior is undefined. The important distinction is that
the optimizer will perform surprising optimizations like the
following:

    void foo(void*) __attribute__(nonnull, 1);

    int bar(int* p) {
      foo(p);

      // The following null check will be elided because nonnull
      // attribute means that, since we call foo with p, p can be
      // assumed to not be null. Thus this will crash if we are called
      // with a null pointer.
      if (src != NULL) {
        return *p;
      }
      return 0;
    }

    int main() {
      return bar(NULL);
    }

Note that by doing this we are no longer attaching any sort of
attribute for GCC (GCC doesn't support attaching nonnull directly to a
parameter, only to the function and naming the arguments
positionally). This means we won't be getting a warning for this case
from GCC any more. People that listen to warnings tend to use clang
anyway, and we're quickly moving toward that as the default, so this
seems to be an acceptable tradeoff.

Change-Id: Ie05fe7cec2f19a082c1defb303f82bcf9241b88d
2016-05-05 17:11:54 -07:00
Josh Gao
34c599a8b0 Add __INTRODUCED_IN_FUTURE for unreleased APIs.
Future API levels aren't known (e.g. 25 could be a maintenance release
of N that doesn't contain any bionic updates), so use a placeholder
macro that we can find and replace with the actual API level before each
release.

Bug: http://b/28178111
Change-Id: I667fe53ea1ac49b64135170fc30d5dbe9df94e29
2016-04-29 16:39:50 -07:00
Josh Gao
14adff1cfa Add versioning information to symbols.
Bug: http://b/28178111
Change-Id: I46bf95accd819f4521afb1173d8badcc5e9df31c
2016-04-29 16:39:50 -07:00
Elliott Hughes
3615a52126 Remove declaration of pthread_cond_timeout_np.
Bug: http://b/27918161
Change-Id: I61b5710dc394c155bdb4577a750b96195033251e
2016-04-28 10:07:21 -07:00
Elliott Hughes
96583dcf95 Stop defining HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE.
Bug: http://b/27918161
Change-Id: I21d4dfa4636aa06a93b3c8a215113f66f37f0bc7
2016-03-30 10:33:54 -07:00
Elliott Hughes
cc78b252d1 Remove the __USE_GNU guard from pthread_setname_np again.
Caused build breakage, but not spotted by TreeHugger:

  external/libnfc-nxp/Linux_x86/phDal4Nfc.c:737:5: error: implicit declaration of function 'pthread_setname_np' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
    pthread_setname_np(pthread_self(), "reader");
    ^

Nice file name.

Change-Id: I102e55718babf4e4f2f2c64066c6a83ed3ac86d3
2016-03-23 20:26:35 -07:00
Elliott Hughes
725b2a96a7 Add pthread_getname_np.
Also guard both these GNU extensions with _GNU_SOURCE.

Also improve the tests to test each case on both the current thread and
another thread, since the code paths are totally different.

Bug: http://b/27810459
Change-Id: I72b05bca5c5b6ca8ba4585b8edfb716a1c252f92
2016-03-23 17:40:25 -07:00
Elliott Hughes
5704c423c8 Implement POSIX lockf.
This has been requested a few times over the years. This is basically
a very late rebase of https://android-review.googlesource.com/45470
which was abandoned years ago. One addition is that this version has
_FILE_OFFSET_BITS=64 support.

POSIX puts this in <unistd.h>. glibc also has it in <fcntl.h>.

Bug: http://b/13077650
Change-Id: I5862b1dc326e326c01ad92438ecc1578d19ba739
2016-01-26 11:09:44 -08:00
Yabin Cui
fe3a83a934 Implement pthread spin.
In order to run tsan unit tests, we need to support pthread spin APIs.

Bug: 18623621
Bug: 25392375
Change-Id: Icbb4a74e72e467824b3715982a01600031868e29
2015-11-18 17:51:21 -08:00
Yabin Cui
e7c2fffa16 Implement pthread barrier.
Bug: 24341262
Change-Id: I5472549e5d7545c1c3f0bef78235f545557b9630
2015-11-16 14:02:26 -08:00
Elliott Hughes
2b028f5ce4 Fix bad merge of 64e9e71cdb.
Bug: http://b/22029751
Change-Id: I6245fe5f274c9af8a632da5eebe50cf9fe29dc4a
2015-06-23 09:14:16 -07:00
Sharvil Nanavati
fdcaeac1a9 am 00cef820: am 64e9e71c: Define nonportable pthread mutex macros with the _NP suffix.
* commit '00cef82013c0826bb43c11651d25de8b99335618':
  Define nonportable pthread mutex macros with the _NP suffix.
2015-06-15 15:48:00 +00:00
Sharvil Nanavati
00cef82013 am 64e9e71c: Define nonportable pthread mutex macros with the _NP suffix.
* commit '64e9e71cdb7d88397e510e1986d494b797ed3b45':
  Define nonportable pthread mutex macros with the _NP suffix.
2015-06-12 20:13:46 +00:00
Sharvil Nanavati
64e9e71cdb Define nonportable pthread mutex macros with the _NP suffix.
Change-Id: I1066352e5b2607f64daf6aeb0030d0bbe88ead8d
2015-06-12 10:56:17 -07:00
Christopher Ferris
511cfd9dc8 Allow NULL in pthread_mutex_lock/unlock.
The pthread_mutex_lock and pthread_mutex_unlock were allowed to
fail silently on L 32 bit devices when passed a NULL. We changed
this to a crash on 32 bit devices, but there are still games that make
these calls and are not likely to be updated. Therefore, once again
allow NULL to be passed in on 32 bit devices.

Bug: 19995172
Change-Id: If7e8860075ecd63c0064d80f64e226fad7bd3c26
2015-06-09 18:46:15 -07:00
Yabin Cui
76615dae93 Provide writer preference option in rwlock.
Previous implementation of rwlock contains four atomic variables, which
is hard to maintain and change. So I make following changes in this CL:

1. Add pending flags in rwlock.state, so we don't need to synchronize
between different atomic variables. Using compare_and_swap operations
on rwlock.state is enough for all state change.

2. Add pending_lock to protect readers/writers waiting and wake up
operations. As waiting/wakeup is not performance critical, using a
lock is easier to maintain.

3. Add writer preference option.

4. Add unit tests for rwlock.

Bug: 19109156

Change-Id: Idcaa58d695ea401d64445610b465ac5cff23ec7c
2015-04-08 13:11:13 -07:00
Yabin Cui
377243b78c Remove PTHREAD_RECURSIVE_MUTEX_INITIALIZER and PTHREAD_ERRORCHECK_MUTEX_INITIALIZER.
These macros are also not used in glibc. And we should use
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
and PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP instead.

Change-Id: I35195e2f499712dcde9305bbb93622d0f7ca874b
2015-03-25 21:02:36 -07:00
Yabin Cui
17393b06ba Hide internal of pthread_mutex_t.
Bug: 19249079
Change-Id: Iffb79c8d861b698d474f212dc80c638fc2cf1620
2015-03-25 17:54:17 -07:00
Yabin Cui
b584572213 Add test for pthread types alignment check.
Bug: 19249079
Change-Id: I83c4f0d11ec5d82a346ae0057d02a92bb1d519e8
2015-03-20 17:42:09 -07:00
Dmitriy Ivanov
b5cd3c7a75 Fix build: align pthread_cond_t to sizeof(long)
Change-Id: Ieaee6d09089b161ec516ba22cafaee1ecb4342da
2015-03-16 17:06:59 -07:00
Christopher Ferris
eb8b122d67 Add alignment to opaque types.
If there is no alignment forced, then the compiler might put these
structures at any alignment.

Change-Id: I6416db72433504e0ec1178bfae6f5b18b6e363fb
2015-03-16 16:28:54 -07:00
Yabin Cui
32651b8e8e Hide content of pthread_cond_t in pthread_cond_internal_t.
Bug: 19249079
Change-Id: I6f55af30bcd6211ce71630c6cacbef0e1663dcee
2015-03-16 11:09:52 -07:00