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