10
10
* Copyright (c) 2004-2005 The Regents of the University of California.
11
11
* All rights reserved.
12
12
* Copyright (c) 2014-2016 Intel, Inc. All rights reserved.
13
+ * Copyright (c) 2016 Research Organization for Information Science
14
+ * and Technology (RIST). All rights reserved.
13
15
* $COPYRIGHT$
14
16
*
15
17
* Additional copyrights may follow
28
30
29
31
30
32
#include <stdio.h>
33
+ #include <pthread.h>
31
34
32
35
#include "src/class/pmix_object.h"
33
36
@@ -48,9 +51,12 @@ pmix_class_t pmix_object_t_class = {
48
51
sizeof (pmix_object_t ) /* size of the pmix object */
49
52
};
50
53
54
+ int pmix_class_init_epoch = 1 ;
55
+
51
56
/*
52
57
* Local variables
53
58
*/
59
+ static pthread_mutex_t class_mutex = PTHREAD_MUTEX_INITIALIZER ;
54
60
static void * * classes = NULL ;
55
61
static int num_classes = 0 ;
56
62
static int max_classes = 0 ;
@@ -81,7 +87,17 @@ void pmix_class_initialize(pmix_class_t *cls)
81
87
/* Check to see if anyone initialized
82
88
this class before we got a chance to */
83
89
84
- if (1 == cls -> cls_initialized ) {
90
+ if (pmix_class_init_epoch == cls -> cls_initialized ) {
91
+ return ;
92
+ }
93
+ pthread_mutex_lock (& class_mutex );
94
+
95
+ /* If another thread initializing this same class came in at
96
+ roughly the same time, it may have gotten the lock and
97
+ initialized. So check again. */
98
+
99
+ if (pmix_class_init_epoch == cls -> cls_initialized ) {
100
+ pthread_mutex_unlock (& class_mutex );
85
101
return ;
86
102
}
87
103
@@ -141,10 +157,12 @@ void pmix_class_initialize(pmix_class_t *cls)
141
157
}
142
158
* cls_destruct_array = NULL ; /* end marker for the destructors */
143
159
144
- cls -> cls_initialized = 1 ;
160
+ cls -> cls_initialized = pmix_class_init_epoch ;
145
161
save_class (cls );
146
162
147
163
/* All done */
164
+
165
+ pthread_mutex_unlock (& class_mutex );
148
166
}
149
167
150
168
@@ -155,6 +173,12 @@ int pmix_class_finalize(void)
155
173
{
156
174
int i ;
157
175
176
+ if (INT_MAX == pmix_class_init_epoch ) {
177
+ pmix_class_init_epoch = 1 ;
178
+ } else {
179
+ pmix_class_init_epoch ++ ;
180
+ }
181
+
158
182
if (NULL != classes ) {
159
183
for (i = 0 ; i < num_classes ; ++ i ) {
160
184
if (NULL != classes [i ]) {
0 commit comments