This is one of a series of CLs to isolate the dependencies.
Test: builds OK on Android, host Linux, and host macOS
Change-Id: I8ffcab7b7c4acab141b09f75f593ebde1ebec31a
#include <stdlib.h>
#include <string.h>
-// FIXME futex portion is not supported on macOS, should use the macOS alternative
-#ifdef __linux__
-#include <linux/futex.h>
-#include <sys/syscall.h>
-#else
-#define FUTEX_WAIT 0
-#define FUTEX_WAIT_PRIVATE 0
-#define FUTEX_WAKE 0
-#define FUTEX_WAKE_PRIVATE 0
-#endif
-
+#include <audio_utils/clock_nanosleep.h>
#include <audio_utils/fifo.h>
+#include <audio_utils/futex.h>
#include <audio_utils/roundup.h>
#include <cutils/log.h>
#include <utils/Errors.h>
-#ifdef __linux__
-#ifdef __ANDROID__
-// bionic for Android provides clock_nanosleep
-#else
-// bionic for desktop Linux omits clock_nanosleep
-int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request,
- struct timespec *remain)
-{
- return syscall(SYS_clock_nanosleep, clock_id, flags, request, remain);
-}
-#endif // __ANDROID__
-#else // __linux__
-// macOS <10.12 doesn't have clockid_t / CLOCK_MONOTONIC
-#ifndef CLOCK_MONOTONIC
-typedef int clockid_t;
-#define CLOCK_MONOTONIC 0
-#endif
-// macOS doesn't have clock_nanosleep
-int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request,
- struct timespec *remain)
-{
- (void) clock_id;
- (void) flags;
- (void) request;
- (void) remain;
- errno = ENOSYS;
- return -1;
-}
-#endif // __linux__
-
-static int sys_futex(void *addr1, int op, int val1, const struct timespec *timeout, void *addr2,
- int val3)
-{
-#ifdef __linux__
- return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
-#else // __linux__
- // macOS doesn't have futex
- (void) addr1;
- (void) op;
- (void) val1;
- (void) timeout;
- (void) addr2;
- (void) val3;
- errno = ENOSYS;
- return -1;
-#endif // __linux__
-}
-
audio_utils_fifo_base::audio_utils_fifo_base(uint32_t frameCount,
audio_utils_fifo_index& writerRear, audio_utils_fifo_index *throttleFront)
__attribute__((no_sanitize("integer"))) :
int op = FUTEX_WAIT;
switch (mFifo.mThrottleFrontSync) {
case AUDIO_UTILS_FIFO_SYNC_SLEEP:
- err = clock_nanosleep(CLOCK_MONOTONIC, 0 /*flags*/, timeout, NULL /*remain*/);
+ err = audio_utils_clock_nanosleep(CLOCK_MONOTONIC, 0 /*flags*/, timeout,
+ NULL /*remain*/);
if (err < 0) {
LOG_ALWAYS_FATAL_IF(errno != EINTR, "unexpected err=%d errno=%d", err, errno);
err = -errno;
int op = FUTEX_WAIT;
switch (mFifo.mWriterRearSync) {
case AUDIO_UTILS_FIFO_SYNC_SLEEP:
- err = clock_nanosleep(CLOCK_MONOTONIC, 0 /*flags*/, timeout, NULL /*remain*/);
+ err = audio_utils_clock_nanosleep(CLOCK_MONOTONIC, 0 /*flags*/, timeout,
+ NULL /*remain*/);
if (err < 0) {
LOG_ALWAYS_FATAL_IF(errno != EINTR, "unexpected err=%d errno=%d", err, errno);
err = -errno;
--- /dev/null
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_CLOCK_NANOSLEEP_H
+#define ANDROID_AUDIO_CLOCK_NANOSLEEP_H
+
+#include <time.h>
+#include <unistd.h>
+
+#ifdef __linux__
+
+#include <sys/syscall.h>
+#ifdef __ANDROID__
+// bionic for Android provides clock_nanosleep
+#define audio_utils_clock_nanosleep clock_nanosleep
+#else
+// bionic for desktop Linux omits clock_nanosleep
+static inline
+int audio_utils_clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request,
+ struct timespec *remain)
+{
+ return syscall(SYS_clock_nanosleep, clock_id, flags, request, remain);
+}
+#endif // __ANDROID__
+
+#else // __linux__
+
+// macOS <10.12 doesn't have clockid_t / CLOCK_MONOTONIC
+#ifndef CLOCK_MONOTONIC
+typedef int clockid_t;
+#define CLOCK_MONOTONIC 0
+#endif
+// macOS doesn't have clock_nanosleep
+static inline
+int audio_utils_clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *request,
+ struct timespec *remain)
+{
+ (void) clock_id;
+ (void) flags;
+ (void) request;
+ (void) remain;
+ errno = ENOSYS;
+ return -1;
+}
+
+#endif // __linux__
+
+#endif // !ANDROID_AUDIO_CLOCK_NANOSLEEP_H
private:
// Linux futex is 32 bits regardless of platform.
// It would make more sense to declare this as atomic_uint32_t, but there is no such type name.
+ // TODO Support 64-bit index with 32-bit futex in low-order bits.
std::atomic_uint_least32_t mIndex; // accessed by both sides using atomic operations
static_assert(sizeof(mIndex) == sizeof(uint32_t), "mIndex must be 32 bits");
--- /dev/null
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_AUDIO_FUTEX_H
+#define ANDROID_AUDIO_FUTEX_H
+
+// FIXME futex portion is not supported on macOS, should use the macOS alternative
+
+#ifdef __linux__
+#include <unistd.h>
+#include <linux/futex.h>
+#include <sys/syscall.h>
+#else
+#define FUTEX_WAIT 0
+#define FUTEX_WAIT_PRIVATE 0
+#define FUTEX_WAKE 0
+#define FUTEX_WAKE_PRIVATE 0
+#endif
+
+static inline
+int sys_futex(void *addr1, int op, int val1, const struct timespec *timeout, void *addr2,
+ int val3)
+{
+#ifdef __linux__
+ return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
+#else // __linux__
+ // macOS doesn't have futex
+ (void) addr1;
+ (void) op;
+ (void) val1;
+ (void) timeout;
+ (void) addr2;
+ (void) val3;
+ errno = ENOSYS;
+ return -1;
+#endif // __linux__
+}
+
+#endif // !ANDROID_AUDIO_FUTEX_H