File: bug_taskwait_detach.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (117 lines) | stat: -rw-r--r-- 2,712 bytes parent folder | download | duplicates (19)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// RUN: %libomp-cxx-compile-and-run

#include <omp.h>

#include <chrono>
#include <cstdint>
#include <iostream>
#include <thread>

// detached
#define PTASK_FLAG_DETACHABLE 0x40

// OpenMP RTL interfaces
using kmp_int32 = int32_t;

typedef struct ID {
  int reserved_1;
  int flags;
  int reserved_2;
  int reserved_3;
  char *psource;
} id;

// Compiler-generated code (emulation)
typedef struct ident {
  void *dummy; // not used in the library
} ident_t;

typedef enum kmp_event_type_t {
  KMP_EVENT_UNINITIALIZED = 0,
  KMP_EVENT_ALLOW_COMPLETION = 1
} kmp_event_type_t;

typedef struct {
  kmp_event_type_t type;
  union {
    void *task;
  } ed;
} kmp_event_t;

typedef struct shar { // shareds used in the task
} * pshareds;

typedef struct task {
  pshareds shareds;
  int (*routine)(int, struct task *);
  int part_id;
  // void *destructor_thunk; // optional, needs flag setting if provided
  // int priority; // optional, needs flag setting if provided
  // ------------------------------
  // privates used in the task:
  omp_event_handle_t evt;
} * ptask, kmp_task_t;

typedef int (*task_entry_t)(int, ptask);

#ifdef __cplusplus
extern "C" {
#endif
extern int __kmpc_global_thread_num(void *id_ref);
extern int **__kmpc_omp_task_alloc(id *loc, int gtid, int flags, size_t sz,
                                   size_t shar, task_entry_t rtn);
extern kmp_int32 __kmpc_omp_task(ident_t *loc_ref, kmp_int32 gtid,
                                 kmp_task_t *new_task);
extern omp_event_handle_t __kmpc_task_allow_completion_event(ident_t *loc_ref,
                                                             int gtid,
                                                             kmp_task_t *task);
#ifdef __cplusplus
}
#endif

int volatile checker;

void target(ptask task) {
  std::this_thread::sleep_for(std::chrono::seconds(3));
  checker = 1;
  omp_fulfill_event(task->evt);
}

// User's code
int task_entry(int gtid, ptask task) {
  std::thread t(target, task);
  t.detach();
  return 0;
}

int main(int argc, char *argv[]) {
  int gtid = __kmpc_global_thread_num(nullptr);
  checker = 0;

  /*
    #pragma omp task detach(evt)
    {}
  */
  std::cout << "detaching...\n";
  ptask task = (ptask)__kmpc_omp_task_alloc(
      nullptr, gtid, PTASK_FLAG_DETACHABLE, sizeof(struct task),
      sizeof(struct shar), &task_entry);
  omp_event_handle_t evt =
      (omp_event_handle_t)__kmpc_task_allow_completion_event(nullptr, gtid,
                                                             task);
  task->evt = evt;

  __kmpc_omp_task(nullptr, gtid, task);

#pragma omp taskwait

  // check results
  if (checker == 1) {
    std::cout << "PASS\n";
    return 0;
  }

  return 1;
}

// CHECK: PASS