pthread_test.cpp revision 0e714a5b41451e84c5ded93a42c9a4b0a9440691
1/*
2 * Copyright (C) 2012 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 <errno.h>
20#include <inttypes.h>
21#include <limits.h>
22#include <pthread.h>
23#include <signal.h>
24#include <sys/mman.h>
25#include <time.h>
26#include <unistd.h>
27
28TEST(pthread, pthread_key_create) {
29  pthread_key_t key;
30  ASSERT_EQ(0, pthread_key_create(&key, NULL));
31  ASSERT_EQ(0, pthread_key_delete(key));
32  // Can't delete a key that's already been deleted.
33  ASSERT_EQ(EINVAL, pthread_key_delete(key));
34}
35
36TEST(pthread, pthread_key_create_lots) {
37#if defined(__BIONIC__) // glibc uses keys internally that its sysconf value doesn't account for.
38  // POSIX says PTHREAD_KEYS_MAX should be at least 128.
39  ASSERT_GE(PTHREAD_KEYS_MAX, 128);
40
41  int sysconf_max = sysconf(_SC_THREAD_KEYS_MAX);
42
43  // sysconf shouldn't return a smaller value.
44  ASSERT_GE(sysconf_max, PTHREAD_KEYS_MAX);
45
46  // We can allocate _SC_THREAD_KEYS_MAX keys.
47  sysconf_max -= 2; // (Except that gtest takes two for itself.)
48  std::vector<pthread_key_t> keys;
49  for (int i = 0; i < sysconf_max; ++i) {
50    pthread_key_t key;
51    // If this fails, it's likely that GLOBAL_INIT_THREAD_LOCAL_BUFFER_COUNT is wrong.
52    ASSERT_EQ(0, pthread_key_create(&key, NULL)) << i << " of " << sysconf_max;
53    keys.push_back(key);
54  }
55
56  // ...and that really is the maximum.
57  pthread_key_t key;
58  ASSERT_EQ(EAGAIN, pthread_key_create(&key, NULL));
59
60  // (Don't leak all those keys!)
61  for (size_t i = 0; i < keys.size(); ++i) {
62    ASSERT_EQ(0, pthread_key_delete(keys[i]));
63  }
64#else // __BIONIC__
65  GTEST_LOG_(INFO) << "This test does nothing.\n";
66#endif // __BIONIC__
67}
68
69static void* IdFn(void* arg) {
70  return arg;
71}
72
73static void* SleepFn(void* arg) {
74  sleep(reinterpret_cast<uintptr_t>(arg));
75  return NULL;
76}
77
78static void* SpinFn(void* arg) {
79  volatile bool* b = reinterpret_cast<volatile bool*>(arg);
80  while (!*b) {
81  }
82  return NULL;
83}
84
85static void* JoinFn(void* arg) {
86  return reinterpret_cast<void*>(pthread_join(reinterpret_cast<pthread_t>(arg), NULL));
87}
88
89static void AssertDetached(pthread_t t, bool is_detached) {
90  pthread_attr_t attr;
91  ASSERT_EQ(0, pthread_getattr_np(t, &attr));
92  int detach_state;
93  ASSERT_EQ(0, pthread_attr_getdetachstate(&attr, &detach_state));
94  pthread_attr_destroy(&attr);
95  ASSERT_EQ(is_detached, (detach_state == PTHREAD_CREATE_DETACHED));
96}
97
98static void MakeDeadThread(pthread_t& t) {
99  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, NULL));
100  void* result;
101  ASSERT_EQ(0, pthread_join(t, &result));
102}
103
104TEST(pthread, pthread_create) {
105  void* expected_result = reinterpret_cast<void*>(123);
106  // Can we create a thread?
107  pthread_t t;
108  ASSERT_EQ(0, pthread_create(&t, NULL, IdFn, expected_result));
109  // If we join, do we get the expected value back?
110  void* result;
111  ASSERT_EQ(0, pthread_join(t, &result));
112  ASSERT_EQ(expected_result, result);
113}
114
115TEST(pthread, pthread_create_EAGAIN) {
116  pthread_attr_t attributes;
117  ASSERT_EQ(0, pthread_attr_init(&attributes));
118  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, static_cast<size_t>(-1) & ~(getpagesize() - 1)));
119
120  pthread_t t;
121  ASSERT_EQ(EAGAIN, pthread_create(&t, &attributes, IdFn, NULL));
122}
123
124TEST(pthread, pthread_no_join_after_detach) {
125  pthread_t t1;
126  ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
127
128  // After a pthread_detach...
129  ASSERT_EQ(0, pthread_detach(t1));
130  AssertDetached(t1, true);
131
132  // ...pthread_join should fail.
133  void* result;
134  ASSERT_EQ(EINVAL, pthread_join(t1, &result));
135}
136
137TEST(pthread, pthread_no_op_detach_after_join) {
138  bool done = false;
139
140  pthread_t t1;
141  ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
142
143  // If thread 2 is already waiting to join thread 1...
144  pthread_t t2;
145  ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
146
147  sleep(1); // (Give t2 a chance to call pthread_join.)
148
149  // ...a call to pthread_detach on thread 1 will "succeed" (silently fail)...
150  ASSERT_EQ(0, pthread_detach(t1));
151  AssertDetached(t1, false);
152
153  done = true;
154
155  // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
156  void* join_result;
157  ASSERT_EQ(0, pthread_join(t2, &join_result));
158  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
159}
160
161TEST(pthread, pthread_join_self) {
162  void* result;
163  ASSERT_EQ(EDEADLK, pthread_join(pthread_self(), &result));
164}
165
166struct TestBug37410 {
167  pthread_t main_thread;
168  pthread_mutex_t mutex;
169
170  static void main() {
171    TestBug37410 data;
172    data.main_thread = pthread_self();
173    ASSERT_EQ(0, pthread_mutex_init(&data.mutex, NULL));
174    ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
175
176    pthread_t t;
177    ASSERT_EQ(0, pthread_create(&t, NULL, TestBug37410::thread_fn, reinterpret_cast<void*>(&data)));
178
179    // Wait for the thread to be running...
180    ASSERT_EQ(0, pthread_mutex_lock(&data.mutex));
181    ASSERT_EQ(0, pthread_mutex_unlock(&data.mutex));
182
183    // ...and exit.
184    pthread_exit(NULL);
185  }
186
187 private:
188  static void* thread_fn(void* arg) {
189    TestBug37410* data = reinterpret_cast<TestBug37410*>(arg);
190
191    // Let the main thread know we're running.
192    pthread_mutex_unlock(&data->mutex);
193
194    // And wait for the main thread to exit.
195    pthread_join(data->main_thread, NULL);
196
197    return NULL;
198  }
199};
200
201// Even though this isn't really a death test, we have to say "DeathTest" here so gtest knows to
202// run this test (which exits normally) in its own process.
203TEST(pthread_DeathTest, pthread_bug_37410) {
204  // http://code.google.com/p/android/issues/detail?id=37410
205  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
206  ASSERT_EXIT(TestBug37410::main(), ::testing::ExitedWithCode(0), "");
207}
208
209static void* SignalHandlerFn(void* arg) {
210  sigset_t wait_set;
211  sigfillset(&wait_set);
212  return reinterpret_cast<void*>(sigwait(&wait_set, reinterpret_cast<int*>(arg)));
213}
214
215TEST(pthread, pthread_sigmask) {
216  // Check that SIGUSR1 isn't blocked.
217  sigset_t original_set;
218  sigemptyset(&original_set);
219  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &original_set));
220  ASSERT_FALSE(sigismember(&original_set, SIGUSR1));
221
222  // Block SIGUSR1.
223  sigset_t set;
224  sigemptyset(&set);
225  sigaddset(&set, SIGUSR1);
226  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, NULL));
227
228  // Check that SIGUSR1 is blocked.
229  sigset_t final_set;
230  sigemptyset(&final_set);
231  ASSERT_EQ(0, pthread_sigmask(SIG_BLOCK, NULL, &final_set));
232  ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
233  // ...and that sigprocmask agrees with pthread_sigmask.
234  sigemptyset(&final_set);
235  ASSERT_EQ(0, sigprocmask(SIG_BLOCK, NULL, &final_set));
236  ASSERT_TRUE(sigismember(&final_set, SIGUSR1));
237
238  // Spawn a thread that calls sigwait and tells us what it received.
239  pthread_t signal_thread;
240  int received_signal = -1;
241  ASSERT_EQ(0, pthread_create(&signal_thread, NULL, SignalHandlerFn, &received_signal));
242
243  // Send that thread SIGUSR1.
244  pthread_kill(signal_thread, SIGUSR1);
245
246  // See what it got.
247  void* join_result;
248  ASSERT_EQ(0, pthread_join(signal_thread, &join_result));
249  ASSERT_EQ(SIGUSR1, received_signal);
250  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
251
252  // Restore the original signal mask.
253  ASSERT_EQ(0, pthread_sigmask(SIG_SETMASK, &original_set, NULL));
254}
255
256#if defined(__BIONIC__)
257extern "C" pid_t __bionic_clone(int flags, void* child_stack, pid_t* parent_tid, void* tls, pid_t* child_tid, int (*fn)(void*), void* arg);
258#endif // __BIONIC__
259
260TEST(pthread, __bionic_clone) {
261#if defined(__BIONIC__)
262  // Check that our hand-written clone assembler sets errno correctly on failure.
263  uintptr_t fake_child_stack[16];
264  errno = 0;
265  ASSERT_EQ(-1, __bionic_clone(CLONE_THREAD, &fake_child_stack[16], NULL, NULL, NULL, NULL, NULL));
266  ASSERT_EQ(EINVAL, errno);
267#else // __BIONIC__
268  GTEST_LOG_(INFO) << "This test does nothing.\n";
269#endif // __BIONIC__
270}
271
272TEST(pthread, pthread_setname_np__too_long) {
273#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
274  ASSERT_EQ(ERANGE, pthread_setname_np(pthread_self(), "this name is far too long for linux"));
275#else // __BIONIC__
276  GTEST_LOG_(INFO) << "This test does nothing.\n";
277#endif // __BIONIC__
278}
279
280TEST(pthread, pthread_setname_np__self) {
281#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
282  ASSERT_EQ(0, pthread_setname_np(pthread_self(), "short 1"));
283#else // __BIONIC__
284  GTEST_LOG_(INFO) << "This test does nothing.\n";
285#endif // __BIONIC__
286}
287
288TEST(pthread, pthread_setname_np__other) {
289#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
290  // Emulator kernels don't currently support setting the name of other threads.
291  char* filename = NULL;
292  asprintf(&filename, "/proc/self/task/%d/comm", gettid());
293  struct stat sb;
294  bool has_comm = (stat(filename, &sb) != -1);
295  free(filename);
296
297  if (has_comm) {
298    pthread_t t1;
299    ASSERT_EQ(0, pthread_create(&t1, NULL, SleepFn, reinterpret_cast<void*>(5)));
300    ASSERT_EQ(0, pthread_setname_np(t1, "short 2"));
301  } else {
302    fprintf(stderr, "skipping test: this kernel doesn't have /proc/self/task/tid/comm files!\n");
303  }
304#else // __BIONIC__
305  GTEST_LOG_(INFO) << "This test does nothing.\n";
306#endif // __BIONIC__
307}
308
309TEST(pthread, pthread_setname_np__no_such_thread) {
310#if defined(__BIONIC__) // Not all build servers have a new enough glibc? TODO: remove when they're on gprecise.
311  pthread_t dead_thread;
312  MakeDeadThread(dead_thread);
313
314  // Call pthread_setname_np after thread has already exited.
315  ASSERT_EQ(ESRCH, pthread_setname_np(dead_thread, "short 3"));
316#else // __BIONIC__
317  GTEST_LOG_(INFO) << "This test does nothing.\n";
318#endif // __BIONIC__
319}
320
321TEST(pthread, pthread_kill__0) {
322  // Signal 0 just tests that the thread exists, so it's safe to call on ourselves.
323  ASSERT_EQ(0, pthread_kill(pthread_self(), 0));
324}
325
326TEST(pthread, pthread_kill__invalid_signal) {
327  ASSERT_EQ(EINVAL, pthread_kill(pthread_self(), -1));
328}
329
330static void pthread_kill__in_signal_handler_helper(int signal_number) {
331  static int count = 0;
332  ASSERT_EQ(SIGALRM, signal_number);
333  if (++count == 1) {
334    // Can we call pthread_kill from a signal handler?
335    ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
336  }
337}
338
339TEST(pthread, pthread_kill__in_signal_handler) {
340  struct sigaction action;
341  struct sigaction original_action;
342  sigemptyset(&action.sa_mask);
343  action.sa_flags = 0;
344  action.sa_handler = pthread_kill__in_signal_handler_helper;
345  ASSERT_EQ(0, sigaction(SIGALRM, &action, &original_action));
346  ASSERT_EQ(0, pthread_kill(pthread_self(), SIGALRM));
347  ASSERT_EQ(0, sigaction(SIGALRM, &original_action, NULL));
348}
349
350TEST(pthread, pthread_detach__no_such_thread) {
351  pthread_t dead_thread;
352  MakeDeadThread(dead_thread);
353
354  ASSERT_EQ(ESRCH, pthread_detach(dead_thread));
355}
356
357TEST(pthread, pthread_getcpuclockid__clock_gettime) {
358  pthread_t t;
359  ASSERT_EQ(0, pthread_create(&t, NULL, SleepFn, reinterpret_cast<void*>(5)));
360
361  clockid_t c;
362  ASSERT_EQ(0, pthread_getcpuclockid(t, &c));
363  timespec ts;
364  ASSERT_EQ(0, clock_gettime(c, &ts));
365}
366
367TEST(pthread, pthread_getcpuclockid__no_such_thread) {
368  pthread_t dead_thread;
369  MakeDeadThread(dead_thread);
370
371  clockid_t c;
372  ASSERT_EQ(ESRCH, pthread_getcpuclockid(dead_thread, &c));
373}
374
375TEST(pthread, pthread_getschedparam__no_such_thread) {
376  pthread_t dead_thread;
377  MakeDeadThread(dead_thread);
378
379  int policy;
380  sched_param param;
381  ASSERT_EQ(ESRCH, pthread_getschedparam(dead_thread, &policy, &param));
382}
383
384TEST(pthread, pthread_setschedparam__no_such_thread) {
385  pthread_t dead_thread;
386  MakeDeadThread(dead_thread);
387
388  int policy = 0;
389  sched_param param;
390  ASSERT_EQ(ESRCH, pthread_setschedparam(dead_thread, policy, &param));
391}
392
393TEST(pthread, pthread_join__no_such_thread) {
394  pthread_t dead_thread;
395  MakeDeadThread(dead_thread);
396
397  void* result;
398  ASSERT_EQ(ESRCH, pthread_join(dead_thread, &result));
399}
400
401TEST(pthread, pthread_kill__no_such_thread) {
402  pthread_t dead_thread;
403  MakeDeadThread(dead_thread);
404
405  ASSERT_EQ(ESRCH, pthread_kill(dead_thread, 0));
406}
407
408TEST(pthread, pthread_join__multijoin) {
409  bool done = false;
410
411  pthread_t t1;
412  ASSERT_EQ(0, pthread_create(&t1, NULL, SpinFn, &done));
413
414  pthread_t t2;
415  ASSERT_EQ(0, pthread_create(&t2, NULL, JoinFn, reinterpret_cast<void*>(t1)));
416
417  sleep(1); // (Give t2 a chance to call pthread_join.)
418
419  // Multiple joins to the same thread should fail.
420  ASSERT_EQ(EINVAL, pthread_join(t1, NULL));
421
422  done = true;
423
424  // ...but t2's join on t1 still goes ahead (which we can tell because our join on t2 finishes).
425  void* join_result;
426  ASSERT_EQ(0, pthread_join(t2, &join_result));
427  ASSERT_EQ(0U, reinterpret_cast<uintptr_t>(join_result));
428}
429
430TEST(pthread, pthread_join__race) {
431  // http://b/11693195 --- pthread_join could return before the thread had actually exited.
432  // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread.
433  for (size_t i = 0; i < 1024; ++i) {
434    size_t stack_size = 64*1024;
435    void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
436
437    pthread_attr_t a;
438    pthread_attr_init(&a);
439    pthread_attr_setstack(&a, stack, stack_size);
440
441    pthread_t t;
442    ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL));
443    ASSERT_EQ(0, pthread_join(t, NULL));
444    ASSERT_EQ(0, munmap(stack, stack_size));
445  }
446}
447
448static void* GetActualGuardSizeFn(void* arg) {
449  pthread_attr_t attributes;
450  pthread_getattr_np(pthread_self(), &attributes);
451  pthread_attr_getguardsize(&attributes, reinterpret_cast<size_t*>(arg));
452  return NULL;
453}
454
455static size_t GetActualGuardSize(const pthread_attr_t& attributes) {
456  size_t result;
457  pthread_t t;
458  pthread_create(&t, &attributes, GetActualGuardSizeFn, &result);
459  void* join_result;
460  pthread_join(t, &join_result);
461  return result;
462}
463
464static void* GetActualStackSizeFn(void* arg) {
465  pthread_attr_t attributes;
466  pthread_getattr_np(pthread_self(), &attributes);
467  pthread_attr_getstacksize(&attributes, reinterpret_cast<size_t*>(arg));
468  return NULL;
469}
470
471static size_t GetActualStackSize(const pthread_attr_t& attributes) {
472  size_t result;
473  pthread_t t;
474  pthread_create(&t, &attributes, GetActualStackSizeFn, &result);
475  void* join_result;
476  pthread_join(t, &join_result);
477  return result;
478}
479
480TEST(pthread, pthread_attr_setguardsize) {
481  pthread_attr_t attributes;
482  ASSERT_EQ(0, pthread_attr_init(&attributes));
483
484  // Get the default guard size.
485  size_t default_guard_size;
486  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &default_guard_size));
487
488  // No such thing as too small: will be rounded up to one page by pthread_create.
489  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 128));
490  size_t guard_size;
491  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
492  ASSERT_EQ(128U, guard_size);
493  ASSERT_EQ(4096U, GetActualGuardSize(attributes));
494
495  // Large enough and a multiple of the page size.
496  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024));
497  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
498  ASSERT_EQ(32*1024U, guard_size);
499
500  // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
501  ASSERT_EQ(0, pthread_attr_setguardsize(&attributes, 32*1024 + 1));
502  ASSERT_EQ(0, pthread_attr_getguardsize(&attributes, &guard_size));
503  ASSERT_EQ(32*1024U + 1, guard_size);
504}
505
506TEST(pthread, pthread_attr_setstacksize) {
507  pthread_attr_t attributes;
508  ASSERT_EQ(0, pthread_attr_init(&attributes));
509
510  // Get the default stack size.
511  size_t default_stack_size;
512  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &default_stack_size));
513
514  // Too small.
515  ASSERT_EQ(EINVAL, pthread_attr_setstacksize(&attributes, 128));
516  size_t stack_size;
517  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
518  ASSERT_EQ(default_stack_size, stack_size);
519  ASSERT_GE(GetActualStackSize(attributes), default_stack_size);
520
521  // Large enough and a multiple of the page size.
522  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024));
523  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
524  ASSERT_EQ(32*1024U, stack_size);
525  ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
526
527  // Large enough but not a multiple of the page size; will be rounded up by pthread_create.
528  ASSERT_EQ(0, pthread_attr_setstacksize(&attributes, 32*1024 + 1));
529  ASSERT_EQ(0, pthread_attr_getstacksize(&attributes, &stack_size));
530  ASSERT_EQ(32*1024U + 1, stack_size);
531#if defined(__BIONIC__)
532  // Bionic rounds up, which is what POSIX allows.
533  ASSERT_EQ(GetActualStackSize(attributes), (32 + 4)*1024U);
534#else // __BIONIC__
535  // glibc rounds down, in violation of POSIX. They document this in their BUGS section.
536  ASSERT_EQ(GetActualStackSize(attributes), 32*1024U);
537#endif // __BIONIC__
538}
539
540TEST(pthread, pthread_rwlock_smoke) {
541  pthread_rwlock_t l;
542  ASSERT_EQ(0, pthread_rwlock_init(&l, NULL));
543
544  ASSERT_EQ(0, pthread_rwlock_rdlock(&l));
545  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
546
547  ASSERT_EQ(0, pthread_rwlock_wrlock(&l));
548  ASSERT_EQ(0, pthread_rwlock_unlock(&l));
549
550  ASSERT_EQ(0, pthread_rwlock_destroy(&l));
551}
552
553static int gOnceFnCallCount = 0;
554static void OnceFn() {
555  ++gOnceFnCallCount;
556}
557
558TEST(pthread, pthread_once_smoke) {
559  pthread_once_t once_control = PTHREAD_ONCE_INIT;
560  ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
561  ASSERT_EQ(0, pthread_once(&once_control, OnceFn));
562  ASSERT_EQ(1, gOnceFnCallCount);
563}
564
565static int gAtForkPrepareCalls = 0;
566static void AtForkPrepare1() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 1; }
567static void AtForkPrepare2() { gAtForkPrepareCalls = (gAtForkPrepareCalls << 4) | 2; }
568static int gAtForkParentCalls = 0;
569static void AtForkParent1() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 1; }
570static void AtForkParent2() { gAtForkParentCalls = (gAtForkParentCalls << 4) | 2; }
571static int gAtForkChildCalls = 0;
572static void AtForkChild1() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 1; }
573static void AtForkChild2() { gAtForkChildCalls = (gAtForkChildCalls << 4) | 2; }
574
575TEST(pthread, pthread_atfork) {
576  ASSERT_EQ(0, pthread_atfork(AtForkPrepare1, AtForkParent1, AtForkChild1));
577  ASSERT_EQ(0, pthread_atfork(AtForkPrepare2, AtForkParent2, AtForkChild2));
578
579  int pid = fork();
580  ASSERT_NE(-1, pid) << strerror(errno);
581
582  // Child and parent calls are made in the order they were registered.
583  if (pid == 0) {
584    ASSERT_EQ(0x12, gAtForkChildCalls);
585    _exit(0);
586  }
587  ASSERT_EQ(0x12, gAtForkParentCalls);
588
589  // Prepare calls are made in the reverse order.
590  ASSERT_EQ(0x21, gAtForkPrepareCalls);
591}
592
593TEST(pthread, pthread_attr_getscope) {
594  pthread_attr_t attr;
595  ASSERT_EQ(0, pthread_attr_init(&attr));
596
597  int scope;
598  ASSERT_EQ(0, pthread_attr_getscope(&attr, &scope));
599  ASSERT_EQ(PTHREAD_SCOPE_SYSTEM, scope);
600}
601
602TEST(pthread, pthread_condattr_init) {
603  pthread_condattr_t attr;
604  pthread_condattr_init(&attr);
605
606  clockid_t clock;
607  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
608  ASSERT_EQ(CLOCK_REALTIME, clock);
609
610  int pshared;
611  ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
612  ASSERT_EQ(PTHREAD_PROCESS_PRIVATE, pshared);
613}
614
615TEST(pthread, pthread_condattr_setclock) {
616  pthread_condattr_t attr;
617  pthread_condattr_init(&attr);
618
619  ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_REALTIME));
620  clockid_t clock;
621  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
622  ASSERT_EQ(CLOCK_REALTIME, clock);
623
624  ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
625  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
626  ASSERT_EQ(CLOCK_MONOTONIC, clock);
627
628  ASSERT_EQ(EINVAL, pthread_condattr_setclock(&attr, CLOCK_PROCESS_CPUTIME_ID));
629}
630
631TEST(pthread, pthread_cond_broadcast__preserves_condattr_flags) {
632#if defined(__BIONIC__) // This tests a bionic implementation detail.
633  pthread_condattr_t attr;
634  pthread_condattr_init(&attr);
635
636  ASSERT_EQ(0, pthread_condattr_setclock(&attr, CLOCK_MONOTONIC));
637  ASSERT_EQ(0, pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED));
638
639  pthread_cond_t cond_var;
640  ASSERT_EQ(0, pthread_cond_init(&cond_var, &attr));
641
642  ASSERT_EQ(0, pthread_cond_signal(&cond_var));
643  ASSERT_EQ(0, pthread_cond_broadcast(&cond_var));
644
645  attr = static_cast<pthread_condattr_t>(cond_var.value);
646  clockid_t clock;
647  ASSERT_EQ(0, pthread_condattr_getclock(&attr, &clock));
648  ASSERT_EQ(CLOCK_MONOTONIC, clock);
649  int pshared;
650  ASSERT_EQ(0, pthread_condattr_getpshared(&attr, &pshared));
651  ASSERT_EQ(PTHREAD_PROCESS_SHARED, pshared);
652#else // __BIONIC__
653  GTEST_LOG_(INFO) << "This test does nothing.\n";
654#endif // __BIONIC__
655}
656
657TEST(pthread, pthread_mutex_timedlock) {
658  pthread_mutex_t m;
659  ASSERT_EQ(0, pthread_mutex_init(&m, NULL));
660
661  // If the mutex is already locked, pthread_mutex_timedlock should time out.
662  ASSERT_EQ(0, pthread_mutex_lock(&m));
663
664  timespec ts;
665  ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
666  ts.tv_nsec += 1;
667  ASSERT_EQ(ETIMEDOUT, pthread_mutex_timedlock(&m, &ts));
668
669  // If the mutex is unlocked, pthread_mutex_timedlock should succeed.
670  ASSERT_EQ(0, pthread_mutex_unlock(&m));
671
672  ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
673  ts.tv_nsec += 1;
674  ASSERT_EQ(0, pthread_mutex_timedlock(&m, &ts));
675
676  ASSERT_EQ(0, pthread_mutex_unlock(&m));
677  ASSERT_EQ(0, pthread_mutex_destroy(&m));
678}
679