PAPI  5.3.0.0
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
threads.h
Go to the documentation of this file.
1 
6 #ifndef PAPI_THREADS_H
7 #define PAPI_THREADS_H
8 
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <unistd.h>
12 
13 #ifdef HAVE_THREAD_LOCAL_STORAGE
14 #define THREAD_LOCAL_STORAGE_KEYWORD HAVE_THREAD_LOCAL_STORAGE
15 #else
16 #define THREAD_LOCAL_STORAGE_KEYWORD
17 #endif
18 
19 #if defined(ANY_THREAD_GETS_SIGNAL) && !defined(_AIX)
20 #error "lookup_and_set_thread_symbols and _papi_hwi_broadcast_signal have only been tested on AIX"
21 #endif
22 
23 typedef struct _ThreadInfo
24 {
25  unsigned long int tid;
26  unsigned long int allocator_tid;
27  struct _ThreadInfo *next;
29  void *thread_storage[PAPI_MAX_TLS];
31  EventSetInfo_t *from_esi; /* ESI used for last update this control state */
33 } ThreadInfo_t;
34 
38 extern volatile ThreadInfo_t *_papi_hwi_thread_head;
39 
40 /* If we have TLS, this variable ALWAYS points to our thread descriptor. It's like magic! */
41 
42 #if defined(HAVE_THREAD_LOCAL_STORAGE)
44 #endif
45 
49 extern unsigned long int ( *_papi_hwi_thread_id_fn ) ( void );
50 
54 extern int ( *_papi_hwi_thread_kill_fn ) ( int, int );
55 
56 extern int _papi_hwi_initialize_thread( ThreadInfo_t ** dest, int tid );
57 extern int _papi_hwi_init_global_threads( void );
58 extern int _papi_hwi_shutdown_thread( ThreadInfo_t * thread, int force );
59 extern int _papi_hwi_shutdown_global_threads( void );
60 extern int _papi_hwi_broadcast_signal( unsigned int mytid );
61 extern int _papi_hwi_set_thread_id_fn( unsigned long int ( *id_fn ) ( void ) );
62 
63 inline_static int
64 _papi_hwi_lock( int lck )
65 {
66  if ( _papi_hwi_thread_id_fn ) {
67  _papi_hwd_lock( lck );
68  THRDBG( "Lock %d\n", lck );
69  } else {
70  ( void ) lck; /* unused if !defined(DEBUG) */
71  THRDBG( "Skipped lock %d\n", lck );
72  }
73 
74  return ( PAPI_OK );
75 }
76 
77 inline_static int
78 _papi_hwi_unlock( int lck )
79 {
80  if ( _papi_hwi_thread_id_fn ) {
81  _papi_hwd_unlock( lck );
82  THRDBG( "Unlock %d\n", lck );
83  } else {
84  ( void ) lck; /* unused if !defined(DEBUG) */
85  THRDBG( "Skipped unlock %d\n", lck );
86  }
87 
88  return ( PAPI_OK );
89 }
90 
92 _papi_hwi_lookup_thread( int custom_tid )
93 {
94 
95  unsigned long int tid;
97 
98 
99  if (custom_tid==0) {
100 #ifdef HAVE_THREAD_LOCAL_STORAGE
101  THRDBG( "TLS returning %p\n", _papi_hwi_my_thread );
102  return ( _papi_hwi_my_thread );
103 #else
104  if ( _papi_hwi_thread_id_fn == NULL ) {
105  THRDBG( "Threads not initialized, returning master thread at %p\n",
106  _papi_hwi_thread_head );
107  return ( ( ThreadInfo_t * ) _papi_hwi_thread_head );
108  }
109 
110  tid = ( *_papi_hwi_thread_id_fn ) ( );
111 #endif
112  }
113  else {
114  tid=custom_tid;
115  }
116  THRDBG( "Threads initialized, looking for thread 0x%lx\n", tid );
117 
119 
120  tmp = ( ThreadInfo_t * ) _papi_hwi_thread_head;
121  while ( tmp != NULL ) {
122  THRDBG( "Examining thread tid 0x%lx at %p\n", tmp->tid, tmp );
123  if ( tmp->tid == tid )
124  break;
125  tmp = tmp->next;
126  if ( tmp == _papi_hwi_thread_head ) {
127  tmp = NULL;
128  break;
129  }
130  }
131 
132  if ( tmp ) {
133  _papi_hwi_thread_head = tmp;
134  THRDBG( "Found thread %ld at %p\n", tid, tmp );
135  } else {
136  THRDBG( "Did not find tid %ld\n", tid );
137  }
138 
140  return ( tmp );
141 
142 }
143 
144 inline_static int
146 {
148  int retval = PAPI_OK;
149 
150  if ( tmp == NULL )
151  retval = _papi_hwi_initialize_thread( &tmp, tid );
152 
153  if ( retval == PAPI_OK )
154  *here = tmp;
155 
156  return ( retval );
157 }
158 
159 /* Prototypes */
161 void _papi_hwi_cleanup_thread_list( void );
165 int _papi_hwi_get_thr_context( void ** );
167 
168 #endif
int _papi_hwi_set_thread_id_fn(unsigned long(*id_fn)(void))
Definition: threads.c:352
int _papi_hwi_insert_in_thread_list(ThreadInfo_t *ptr)
struct _ThreadInfo * next
Definition: threads.h:27
int wants_signal
Definition: threads.h:32
return PAPI_OK
Definition: linux-nvml.c:458
#define THRDBG(format, args...)
Definition: papi_debug.h:66
void _papi_hwi_shutdown_the_thread_list(void)
void
Definition: iozone.c:18627
EventSetInfo_t * from_esi
Definition: threads.h:31
char * where
Definition: iozone.c:19608
volatile ThreadInfo_t * _papi_hwi_thread_head
Definition: threads.c:32
void _papi_hwi_cleanup_thread_list(void)
int _papi_hwi_shutdown_global_threads(void)
Definition: threads.c:457
inline_static int _papi_hwi_lock(int lck)
Definition: threads.h:64
unsigned long int tid
Definition: threads.h:25
hwd_context_t ** context
Definition: threads.h:28
inline_static int _papi_hwi_unlock(int lck)
Definition: threads.h:78
void * thread(void *arg)
Definition: kufrin.c:31
void *long long tid
Definition: iozone.c:18586
#define _papi_hwd_unlock(lck)
Definition: aix-lock.h:11
#define inline_static
#define PAPI_MAX_TLS
Definition: papi_internal.h:81
#define THREADS_LOCK
Definition: papi_internal.h:87
unsigned long int(* _papi_hwi_thread_id_fn)(void)
Definition: threads.c:42
unsigned long int allocator_tid
Definition: threads.h:26
int _papi_hwi_broadcast_signal(unsigned int mytid)
int(* _papi_hwi_thread_kill_fn)(int, int)
EventSetInfo_t ** running_eventset
Definition: threads.h:30
inline_static int _papi_hwi_lookup_or_create_thread(ThreadInfo_t **here, int tid)
Definition: threads.h:145
int _papi_hwi_gather_all_thrspec_data(int tag, PAPI_all_thr_spec_t *where)
Definition: threads.c:554
#define THREAD_LOCAL_STORAGE_KEYWORD
Definition: threads.h:16
int
Definition: iozone.c:18528
inline_static ThreadInfo_t * _papi_hwi_lookup_thread(int custom_tid)
Definition: threads.h:92
THREAD_LOCAL_STORAGE_KEYWORD ThreadInfo_t * _papi_hwi_my_thread
Definition: threads.c:37
int _papi_hwi_shutdown_thread(ThreadInfo_t *thread, int force_shutdown)
Definition: threads.c:418
ssize_t retval
Definition: libasync.c:338
long long tmp
Definition: iozone.c:12031
int _papi_hwi_initialize_thread(ThreadInfo_t **dest, int tid)
Definition: threads.c:278
#define _papi_hwd_lock(lck)
Definition: aix-lock.h:6
int _papi_hwi_init_global_threads(void)
Definition: threads.c:527
ThreadInfo_t * _papi_hwi_lookup_in_thread_list()
int _papi_hwi_get_thr_context(void **)
char * ptr
Definition: iozone.c:23586