1 /*
2 * Copyright (c) 2007-2013 Los Alamos National Security, LLC. All rights
3 * reserved.
4 * Copyright (c) 2008 Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2015-2017 Research Organization for Information Science
6 * and Technology (RIST). All rights reserved.
7 * Copyright (c) 2017-2018 Intel, Inc. All rights reserved.
8 * $COPYRIGHT$
9 *
10 * Additional copyrights may follow
11 *
12 * $HEADER$
13 */
14
15
16 #ifndef PMIX_THREADS_TSD_H
17 #define PMIX_THREADS_TSD_H
18
19 #include "pmix_config.h"
20
21 #include <pthread.h>
22
23 #include "pmix_common.h"
24
25 BEGIN_C_DECLS
26
27 /**
28 * @file
29 *
30 * Thread Specific Datastore Interface
31 *
32 * Functions for providing thread-specific datastore capabilities.
33 */
34
35
36 /**
37 * Prototype for callback when tsd data is being destroyed
38 */
39 typedef void (*pmix_tsd_destructor_t)(void *value);
40
41
42 typedef pthread_key_t pmix_tsd_key_t;
43
44 static inline int
45 pmix_tsd_key_delete(pmix_tsd_key_t key)
46 {
47 return pthread_key_delete(key);
48 }
49
50 static inline int
51 pmix_tsd_setspecific(pmix_tsd_key_t key, void *value)
52 {
53 return pthread_setspecific(key, value);
54 }
55
56 static inline int
57 pmix_tsd_getspecific(pmix_tsd_key_t key, void **valuep)
58 {
59 *valuep = pthread_getspecific(key);
60 return PMIX_SUCCESS;
61 }
62
63 /**
64 * Create thread-specific data key
65 *
66 * Create a thread-specific data key visible to all threads in the
67 * current process. The returned key is valid in all threads,
68 * although the values bound to the key by pmix_tsd_setspecific() are
69 * allocated on a per-thread basis and persist for the life of the
70 * calling thread.
71 *
72 * Upon key creation, the value NULL is associated with the new key in
73 * all active threads. When a new thread is created, the value NULL
74 * is associated with all defined keys in the new thread.
75 *
76 * The destructor parameter may be NULL. At thread exit, if
77 * destructor is non-NULL AND the thread has a non-NULL value
78 * associated with the key, the function is called with the current
79 * value as its argument.
80 *
81 * @param key[out] The key for accessing thread-specific data
82 * @param destructor[in] Cleanup function to call when a thread exits
83 *
84 * @retval PMIX_SUCCESS Success
85 * @retval EAGAIN The system lacked the necessary resource to
86 * create another thread specific data key
87 * @retval ENOMEM Insufficient memory exists to create the key
88 */
89 PMIX_EXPORT int pmix_tsd_key_create(pmix_tsd_key_t *key,
90 pmix_tsd_destructor_t destructor);
91
92
93 /**
94 * Destruct all thread-specific data keys
95 *
96 * Destruct all thread-specific data keys and invoke the destructor
97 *
98 * This should only be invoked in the main thread.
99 * This is made necessary since destructors are not invoked on the
100 * keys of the main thread, since there is no such thing as
101 * pthread_join(main_thread)
102 *
103 * @retval PMIX_SUCCESS Success
104 */
105 PMIX_EXPORT int pmix_tsd_keys_destruct(void);
106
107 END_C_DECLS
108
109 #endif /* PMIX_MTHREADS_TSD_H */