1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "gtest/gtest.h"
18
19 #include <pthread.h>
20 #include <semaphore.h>
21
22 #include <cerrno>
23 #include <ctime>
24
25 // Provide prototypes of these two functions: they are not defined anywhere
26 // but since they are actually provided by Bionic we could actually call them.
27 extern "C" int __futex_wake(volatile void* ftx, int count) __attribute__((__weak__));
28 extern "C" int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout)
29 __attribute__((__weak__));
30
TEST(Futex,SingleThread)31 TEST(Futex, SingleThread) {
32 volatile int dummy_var = 0;
33 volatile void* futex = reinterpret_cast<volatile void*>(&dummy_var);
34 ASSERT_EQ(__futex_wait(futex, 1, nullptr), -EWOULDBLOCK);
35 ASSERT_EQ(__futex_wake(futex, 1), 0);
36 struct timespec timeout;
37 timeout.tv_sec = 0;
38 timeout.tv_nsec = 1000000; // 1ms
39 ASSERT_EQ(__futex_wait(futex, 0, &timeout), -ETIMEDOUT);
40 }
41
FutexThread(void * arg)42 static void* FutexThread(void* arg) {
43 volatile void* futex = *reinterpret_cast<volatile void**>(arg);
44 // Sleep a little to improve chances that main thread started waiting.
45 struct timespec time;
46 time.tv_sec = 0;
47 time.tv_nsec = 10 * 1000000; // 10 ms
48 nanosleep(&time, nullptr);
49
50 *reinterpret_cast<volatile int*>(futex) = 1;
51 int res = __futex_wake(futex, 1);
52
53 return new int(res);
54 }
55
TEST(Futex,Wake)56 TEST(Futex, Wake) {
57 volatile int dummy_var = 0;
58 volatile void* futex = reinterpret_cast<volatile void*>(&dummy_var);
59 pthread_t thread;
60 ASSERT_EQ(pthread_create(&thread, nullptr, &FutexThread, reinterpret_cast<void*>(&futex)), 0);
61
62 int waked = 0;
63 while (*reinterpret_cast<volatile int*>(futex) == 0) {
64 if (__futex_wait(futex, 0, nullptr) == 0) {
65 waked = 1;
66 }
67 }
68
69 int* futex_wake_result = nullptr;
70 ASSERT_EQ(pthread_join(thread, reinterpret_cast<void**>(&futex_wake_result)), 0);
71 ASSERT_NE(futex_wake_result, nullptr);
72 EXPECT_EQ(*futex_wake_result, waked);
73 delete futex_wake_result;
74 }
75
76 struct WakeMultiple {
77 volatile void* futex;
78 sem_t sem;
79 };
80
FutexWaitThread(void * arg)81 void* FutexWaitThread(void* arg) {
82 WakeMultiple* data = reinterpret_cast<WakeMultiple*>(arg);
83 sem_post(&data->sem);
84
85 int* waked = new int(0);
86 while (*reinterpret_cast<volatile int*>(data->futex) == 0) {
87 if (__futex_wait(data->futex, 0, nullptr) == 0) {
88 *waked = 1;
89 }
90 }
91
92 return waked;
93 }
94
TEST(Futex,WakeMultiple)95 TEST(Futex, WakeMultiple) {
96 volatile int dummy_var = 0;
97 WakeMultiple data;
98 data.futex = reinterpret_cast<volatile void*>(&dummy_var);
99 ASSERT_EQ(sem_init(&data.sem, 0, 0), 0);
100 const int kThreads = 3;
101 pthread_t thread[kThreads];
102 for (int i = 0; i < kThreads; i++) {
103 ASSERT_EQ(pthread_create(&thread[i], nullptr, &FutexWaitThread, reinterpret_cast<void*>(&data)),
104 0);
105 }
106 // Use semaphore to improve chances that threads started waiting on the futex.
107 for (int i = 0; i < kThreads; i++) {
108 ASSERT_EQ(sem_wait(&data.sem), 0);
109 }
110
111 *reinterpret_cast<volatile int*>(data.futex) = 1;
112 int wake = __futex_wake(data.futex, kThreads);
113
114 int waked = 0;
115 for (int i = 0; i < kThreads; i++) {
116 int* wake_result = nullptr;
117 ASSERT_EQ(pthread_join(thread[i], reinterpret_cast<void**>(&wake_result)), 0);
118 ASSERT_NE(wake_result, nullptr);
119 waked += *wake_result;
120 delete wake_result;
121 }
122 ASSERT_EQ(waked, wake);
123 }
124