ARGOBOTS  66b1c39742507d8df30e8d28c54839b961a14814
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
abti_local.h
Go to the documentation of this file.
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  * See COPYRIGHT in top-level directory.
4  */
5 
6 #ifndef ABTI_LOCAL_H_INCLUDED
7 #define ABTI_LOCAL_H_INCLUDED
8 
9 /*
10  * An inlined getter function for ES Local Data. This function is more
11  * efficient than ABTI_local_get_xstream_uninlined, but it can be used once only
12  * at the beginning of the function to avoid TLS caching across context switch.
13  *
14  * Consider the following case:
15  *
16  * int ABT_any_func()
17  * {
18  * ABTI_xstream *p_xstream = ABTI_local_get_xstream()->p_xstream;
19  * [context switch (e.g., ABTI_ythread_yield())];
20  * ABTI_xstream *p_xstream2 = ABTI_local_get_xstream()->p_xstream;
21  * }
22  *
23  * p_xstream and p_xstream2 can be always the same although context switch
24  * changes the running execution stream because a compiler assumes that the
25  * running Pthreads is the same across the function call
26  * (i.e., ABTI_ythread_yield()) and caches a thread local value as a compiler
27  * optimization. To avoid this, we need to assure that the second
28  * ABTI_local_get_xstream() really reads the thread local value again.
29  *
30  * See https://github.com/pmodels/argobots/issues/55 for details.
31  *
32  * ABTI_local_get_xstream_uninlined() guarantees that it truly reads the thread
33  * local value of the current Pthreads, but it is slow.
34  * ABTI_local_get_xstream_uninlined() should be used only after context switch
35  * happens, and in other cases, ABTI_local_get_xstream() should be called for
36  * performance.
37  *
38  * If you don't understand this problem well and it is not in the critical path,
39  * use the uninlined version for correctness.
40  */
41 static inline ABTI_local *ABTI_local_get_local(void)
42 {
43  return lp_ABTI_local;
44 }
45 
46 /*
47  * A safe getter function for ES Local Data, which guarantees that it reads
48  * the thread local value without referring to the cached TLS. This is slower
49  * than ABTI_local_get_xstream(), so use ABTI_local_get_xstream() if possible.
50  */
51 static inline ABTI_local *ABTI_local_get_local_uninlined(void)
52 {
53  return gp_ABTI_local_func.get_local_f();
54 }
55 
56 /*
57  * A setter function for ES Local Data. This function is rarely called, so it
58  * uses a slow version for correctness.
59  */
60 static inline void ABTI_local_set_xstream(ABTI_xstream *p_local_xstream)
61 {
62  gp_ABTI_local_func.set_local_xstream_f(p_local_xstream);
63 }
64 
65 /*
66  * A safe getter function for a pointer to an ES Local Data, which is useful to
67  * identify a native thread (i.e., execution streams and external threads).
68  */
69 static inline void *ABTI_local_get_local_ptr(void)
70 {
71  return gp_ABTI_local_func.get_local_ptr_f();
72 }
73 
74 /*
75  * A developer must be aware that p_local can be NULL.
76  */
77 static inline ABTI_xstream *ABTI_local_get_xstream_or_null(ABTI_local *p_local)
78 {
79  return (ABTI_xstream *)p_local;
80 }
81 
82 /*
83  * This function assumes that the given p_local is not NULL (=running on an
84  * execution stream).
85  */
86 static inline ABTI_xstream *ABTI_local_get_xstream(ABTI_local *p_local)
87 {
88  return (ABTI_xstream *)p_local;
89 }
90 
91 #endif /* ABTI_LOCAL_H_INCLUDED */
ABTI_local_func gp_ABTI_local_func
Definition: local.c:23
ABTD_XSTREAM_LOCAL ABTI_local * lp_ABTI_local
Definition: local.c:29