xref: /llvm-project/openmp/runtime/test/tasking/kmp_detach_tasks_t1.c (revision 405037c4e62a9fcd5485c4e1f05b61683e4c2737)
1 // RUN: %libomp-compile && env OMP_NUM_THREADS='3' %libomp-run
2 // RUN: %libomp-compile && env OMP_NUM_THREADS='1' %libomp-run
3 
4 #include <stdio.h>
5 #include <omp.h>
6 #include "omp_my_sleep.h"
7 
8 // detached untied
9 #define PTASK_FLAG_DETACHABLE 0x40
10 
11 // OpenMP RTL interfaces
12 typedef unsigned long long kmp_uint64;
13 typedef long long kmp_int64;
14 
15 typedef struct ID {
16   int reserved_1;
17   int flags;
18   int reserved_2;
19   int reserved_3;
20   char *psource;
21 } id;
22 
23 // Compiler-generated code (emulation)
24 typedef struct ident {
25   void* dummy; // not used in the library
26 } ident_t;
27 
28 typedef enum kmp_event_type_t {
29   KMP_EVENT_UNINITIALIZED = 0,
30   KMP_EVENT_ALLOW_COMPLETION = 1
31 } kmp_event_type_t;
32 
33 typedef struct {
34   kmp_event_type_t type;
35   union {
36     void *task;
37   } ed;
38 } kmp_event_t;
39 
40 typedef struct shar { // shareds used in the task
41 } *pshareds;
42 
43 typedef struct task {
44   pshareds shareds;
45   int(*routine)(int,struct task*);
46   int part_id;
47 // void *destructor_thunk; // optional, needs flag setting if provided
48 // int priority; // optional, needs flag setting if provided
49 // ------------------------------
50 // privates used in the task:
51   omp_event_handle_t evt;
52 } *ptask, kmp_task_t;
53 
54 typedef int(*task_entry_t)(int, ptask);
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58 extern int __kmpc_global_thread_num(void *id_ref);
59 extern ptask __kmpc_omp_task_alloc(id *loc, int gtid, int flags,
60                                    size_t sz, size_t shar, task_entry_t rtn);
61 extern int __kmpc_omp_task(id *loc, int gtid, ptask task);
62 extern omp_event_handle_t __kmpc_task_allow_completion_event(
63                               ident_t *loc_ref, int gtid, ptask task);
64 #if __cplusplus
65 }
66 #endif
67 
68 int volatile checker;
69 
70 // User's code, outlined into task entry
task_entry(int gtid,ptask task)71 int task_entry(int gtid, ptask task) {
72   checker = 1;
73   return 0;
74 }
75 
main()76 int main() {
77   int i, j, gtid = __kmpc_global_thread_num(NULL);
78   int nt = omp_get_max_threads();
79   ptask task;
80   pshareds psh;
81   checker = 0;
82   omp_set_dynamic(0);
83   #pragma omp parallel //num_threads(N)
84   {
85     #pragma omp master
86     {
87       int gtid = __kmpc_global_thread_num(NULL);
88       omp_event_handle_t evt;
89 /*
90       #pragma omp task detach(evt)
91       {}
92 */
93       task = (ptask)__kmpc_omp_task_alloc(NULL,gtid,PTASK_FLAG_DETACHABLE,sizeof(struct task),sizeof(struct shar),&task_entry);
94       psh = task->shareds;
95       evt = (omp_event_handle_t)__kmpc_task_allow_completion_event(NULL,gtid,task);
96       task->evt = evt;
97 
98       __kmpc_omp_task(NULL, gtid, task);
99       my_sleep(2.0);
100       omp_fulfill_event(evt);
101 
102     } // end master
103   } // end parallel
104 
105   // check results
106   if (checker == 1) {
107     printf("passed\n");
108     return 0;
109   } else {
110     printf("failed\n");
111     return 1;
112   }
113 }
114