ARGOBOTS  1.1
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  */
52 {
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 {
72 }
73 
74 /*
75  * A developer must be aware that p_local can be NULL.
76  */
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  */
87 {
88  return (ABTI_xstream *)p_local;
89 }
90 
91 #endif /* ABTI_LOCAL_H_INCLUDED */
gp_ABTI_local_func
ABTI_local_func gp_ABTI_local_func
Definition: local.c:23
ABTI_local_func::get_local_f
ABTI_local *(* get_local_f)(void)
Definition: abti.h:258
ABTI_xstream
Definition: abti.h:264
ABTI_local_func::get_local_ptr_f
void *(* get_local_ptr_f)(void)
Definition: abti.h:260
ABTI_local_get_local
static ABTI_local * ABTI_local_get_local(void)
Definition: abti_local.h:41
ABTI_local_set_xstream
static void ABTI_local_set_xstream(ABTI_xstream *p_local_xstream)
Definition: abti_local.h:60
ABTI_local_get_local_uninlined
static ABTI_local * ABTI_local_get_local_uninlined(void)
Definition: abti_local.h:51
ABTI_local_get_xstream_or_null
static ABTI_xstream * ABTI_local_get_xstream_or_null(ABTI_local *p_local)
Definition: abti_local.h:77
ABTI_local_func::set_local_xstream_f
void(* set_local_xstream_f)(ABTI_xstream *)
Definition: abti.h:259
ABTI_local_get_local_ptr
static void * ABTI_local_get_local_ptr(void)
Definition: abti_local.h:69
lp_ABTI_local
ABTD_XSTREAM_LOCAL ABTI_local * lp_ABTI_local
Definition: local.c:29
ABTI_local
struct ABTI_local ABTI_local
Definition: abti.h:110
ABTI_local_get_xstream
static ABTI_xstream * ABTI_local_get_xstream(ABTI_local *p_local)
Definition: abti_local.h:86