File: pthread_join.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (69 lines) | stat: -rw-r--r-- 1,879 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// RUN: %clangxx -pthread %s -o %t
// RUN: %env_tool_opts=detect_invalid_join=false %run %t 0

// FIXME: Crashes on some bots in pthread_exit.
// RUN: %env_tool_opts=detect_invalid_join=false %run %t %if tsan %{ 0 %} %else %{ 1 %}

// REQUIRES: glibc

#include <assert.h>
#include <ctime>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>

bool use_exit;
static void *fn(void *args) {
  sleep(1);
  auto ret = (void *)(~(uintptr_t)args);
  if (use_exit)
    pthread_exit(ret);
  return ret;
}

int main(int argc, char **argv) {
  use_exit = atoi(argv[1]);
  bool check_invalid_join = !use_exit;
  pthread_t thread[5];
  assert(!pthread_create(&thread[0], nullptr, fn, (void *)1000));
  assert(!pthread_create(&thread[1], nullptr, fn, (void *)1001));
  assert(!pthread_create(&thread[2], nullptr, fn, (void *)1002));
  assert(!pthread_create(&thread[3], nullptr, fn, (void *)1003));
  pthread_attr_t attr;
  assert(!pthread_attr_init(&attr));
  assert(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED));
  assert(!pthread_create(&thread[4], &attr, fn, (void *)1004));
  assert(!pthread_attr_destroy(&attr));

  assert(!pthread_detach(thread[0]));

  {
    void *res;
    while (pthread_tryjoin_np(thread[1], &res))
      sleep(1);
    assert(~(uintptr_t)res == 1001);
    assert(check_invalid_join ||
           (pthread_tryjoin_np(thread[1], &res) == EBUSY));
  }

  {
    void *res;
    timespec tm = {0, 1};
    while (pthread_timedjoin_np(thread[2], &res, &tm))
      sleep(1);
    assert(~(uintptr_t)res == 1002);
    assert(check_invalid_join ||
           (pthread_timedjoin_np(thread[2], &res, &tm) == ESRCH));
  }

  {
    void *res;
    assert(!pthread_join(thread[3], &res));
    assert(~(uintptr_t)res == 1003);
    assert(check_invalid_join || (pthread_join(thread[3], &res) == ESRCH));
  }

  return 0;
}