Skip to content

Commit c9b35e2

Browse files
committed
Protect source data access with thread locking.
Signed-off-by: Chris Chambreau <[email protected]>
1 parent d402907 commit c9b35e2

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

opal/mca/base/mca_base_source.c

+36-2
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,24 @@ int mca_base_source_init (void)
6363
int ret = OPAL_SUCCESS;
6464

6565
OPAL_THREAD_LOCK(&mca_base_source_lock);
66+
6667
if (false == mca_base_source_initialized) {
6768
mca_base_source_initialized = true;
6869

6970
OBJ_CONSTRUCT(&registered_sources, opal_pointer_array_t);
7071
opal_pointer_array_init(&registered_sources, 16, 512, 16);
7172

73+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
74+
75+
/* mca_base_source_register will use lock */
7276
mca_base_source_default_source = mca_base_source_register ("opal", "mca", "base", "default_source",
7377
"Default source for MCA events", true,
7478
mca_base_source_default_time_source,
7579
mca_base_source_default_time_source_ticks ());
7680

7781
}
78-
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
82+
else
83+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
7984

8085
return ret;
8186
}
@@ -107,17 +112,28 @@ int mca_base_source_finalize (void)
107112

108113
mca_base_source_t *mca_base_source_get (int source_index)
109114
{
110-
return opal_pointer_array_get_item (&registered_sources, source_index);
115+
mca_base_source_t *ret;
116+
117+
OPAL_THREAD_LOCK(&mca_base_source_lock);
118+
119+
ret = opal_pointer_array_get_item (&registered_sources, source_index);
120+
121+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
122+
123+
return ret;
111124
}
112125

113126
int mca_base_source_set_time_source (int source_index, mca_base_source_time_fn_t time_source, uint64_t time_ticks)
114127
{
128+
/* Uses source lock/unlock */
115129
mca_base_source_t *source = mca_base_source_get (source_index);
116130

117131
if (NULL == source) {
118132
return OPAL_ERR_NOT_FOUND;
119133
}
120134

135+
OPAL_THREAD_LOCK(&mca_base_source_lock);
136+
121137
if (!time_source) {
122138
time_source = mca_base_source_default_time_source;
123139
time_ticks = mca_base_source_default_time_source_ticks ();
@@ -126,19 +142,28 @@ int mca_base_source_set_time_source (int source_index, mca_base_source_time_fn_t
126142
source->source_time = time_source;
127143
source->source_ticks = time_ticks;
128144

145+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
146+
129147
return OPAL_SUCCESS;
130148
}
131149

132150
/***************************************************************************************************/
133151

134152
int mca_base_source_get_count (int *count)
135153
{
154+
OPAL_THREAD_LOCK(&mca_base_source_lock);
155+
136156
*count = source_count;
157+
158+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
159+
137160
return OPAL_SUCCESS;
138161
}
139162

140163
static inline int mca_base_source_get_by_name (const char *name, mca_base_source_t **source_out)
141164
{
165+
OPAL_THREAD_LOCK(&mca_base_source_lock);
166+
142167
/* there are expected to be a relatively small number of sources so a linear search should be fine */
143168
for (int i = 0 ; i < source_count ; ++i) {
144169
mca_base_source_t *source = opal_pointer_array_get_item (&registered_sources, i);
@@ -147,10 +172,12 @@ static inline int mca_base_source_get_by_name (const char *name, mca_base_source
147172
*source_out = source;
148173
}
149174

175+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
150176
return OPAL_SUCCESS;
151177
}
152178
}
153179

180+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
154181
return OPAL_ERR_NOT_FOUND;
155182
}
156183

@@ -161,9 +188,12 @@ int mca_base_source_register (const char *project, const char *framework, const
161188
char *source_name;
162189
int ret;
163190

191+
OPAL_THREAD_LOCK(&mca_base_source_lock);
192+
164193
/* generate the variable's full name */
165194
ret = mca_base_var_generate_full_name4 (NULL, framework, component, name, &source_name);
166195
if (OPAL_SUCCESS != ret) {
196+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
167197
return ret;
168198
}
169199

@@ -174,6 +204,7 @@ int mca_base_source_register (const char *project, const char *framework, const
174204
/* create a new parameter entry */
175205
source = OBJ_NEW(mca_base_source_t);
176206
if (NULL == source) {
207+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
177208
return OPAL_ERR_OUT_OF_RESOURCE;
178209
}
179210

@@ -200,6 +231,7 @@ int mca_base_source_register (const char *project, const char *framework, const
200231

201232
if (OPAL_SUCCESS != ret) {
202233
OBJ_RELEASE(source);
234+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
203235
return ret;
204236
}
205237
} else {
@@ -215,6 +247,8 @@ int mca_base_source_register (const char *project, const char *framework, const
215247
source->source_time = source_time;
216248
source->source_ticks = source_ticks;
217249

250+
OPAL_THREAD_UNLOCK(&mca_base_source_lock);
251+
218252
return OPAL_SUCCESS;
219253
}
220254
int mca_base_component_source_register (const mca_base_component_t *component, const char *name, const char *description, bool ordered,

0 commit comments

Comments
 (0)