Skip to content

Commit

Permalink
Made rngs, distributions, and maps thread safe.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerilk committed Aug 22, 2023
1 parent 14a625b commit c7e1442
Show file tree
Hide file tree
Showing 17 changed files with 247 additions and 151 deletions.
17 changes: 16 additions & 1 deletion include/cconfigspace/binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ extern "C" {
/**
* @file binding.h
* A Binding is set of value in a Context see context.h. Those values can be
* accessed by using the parameter index in the context.
* accessed by using the parameter index in the context. Bindings are immutable
* except from a reference counting and callback management point of view.
*/

/**
Expand All @@ -20,6 +21,8 @@ extern "C" {
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p binding is not a valid CCS
* object
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p context_ret is NULL
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_binding_get_context(ccs_binding_t binding, ccs_context_t *context_ret);
Expand All @@ -35,6 +38,8 @@ ccs_binding_get_context(ccs_binding_t binding, ccs_context_t *context_ret);
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p value_ret is NULL
* @return #CCS_RESULT_ERROR_OUT_OF_BOUNDS if \p index is greater than the count
* of parameters in the context
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_binding_get_value(
Expand All @@ -59,6 +64,8 @@ ccs_binding_get_value(
* num_values is greater than 0; or if \p values is NULL and \p num_values_ret
* is NULL; or if \p num_values is less than the number of values that would be
* returned
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_binding_get_values(
Expand All @@ -78,6 +85,8 @@ ccs_binding_get_values(
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p value_ret is NULL
* @return #CCS_RESULT_ERROR_INVALID_NAME if no parameter with such \p name
* exist in the \p binding context
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_binding_get_value_by_name(
Expand All @@ -95,6 +104,8 @@ ccs_binding_get_value_by_name(
* object
* @return #CCS_RESULT_ERROR_INVALID_PARAMETER if \p parameter does not exist in
* the \p binding context
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_binding_get_value_by_parameter(
Expand All @@ -112,6 +123,8 @@ ccs_binding_get_value_by_parameter(
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p binding is not a valid CCS
* object
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p hash_ret is NULL
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_binding_hash(ccs_binding_t binding, ccs_hash_t *hash_ret);
Expand All @@ -129,6 +142,8 @@ ccs_binding_hash(ccs_binding_t binding, ccs_hash_t *hash_ret);
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p binding or \p other_binding
* are not valid CCS objects
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p cmp_ret is NULL
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_binding_cmp(
Expand Down
19 changes: 18 additions & 1 deletion include/cconfigspace/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ extern "C" {
/**
* @file configuration.h
* A configuration is a binding (see binding.h) on a configuration space (see
* configuration_space.h).
* configuration_space.h). Configurations are immutable except from a reference
* counting and callback management point of view.
*/

/**
Expand All @@ -29,6 +30,8 @@ extern "C" {
* space
* @return #CCS_RESULT_ERROR_OUT_OF_MEMORY if there was a lack of memory to
* allocate the new configuration
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_create_configuration(
Expand All @@ -46,6 +49,8 @@ ccs_create_configuration(
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p configuration is not a valid
* CCS configuration
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p configuration_space_ret is NULL
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_configuration_get_configuration_space(
Expand All @@ -64,6 +69,8 @@ ccs_configuration_get_configuration_space(
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p value_ret is NULL
* @return #CCS_RESULT_ERROR_OUT_OF_BOUNDS if \p index is greater than the count
* of parameters in the configuration space
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_configuration_get_value(
Expand All @@ -88,6 +95,8 @@ ccs_configuration_get_value(
* num_values is greater than 0; or if \p values is NULL and \p num_values_ret
* is NULL; or if \p num_values is less than the number of values that would be
* returned
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_configuration_get_values(
Expand All @@ -107,6 +116,8 @@ ccs_configuration_get_values(
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p value_ret is NULL
* @return #CCS_RESULT_ERROR_INVALID_NAME if no parameter with such \p name
* exist in the \p configuration space
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_configuration_get_value_by_name(
Expand All @@ -130,6 +141,8 @@ ccs_configuration_get_value_by_name(
* CCS configuration
* @return #CCS_RESULT_ERROR_INVALID_CONFIGURATION if \p configuration has
* become invalid for the configuration space
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_configuration_check(
Expand All @@ -147,6 +160,8 @@ ccs_configuration_check(
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p configuration is not a valid
* CCS configuration
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p hash_ret is NULL
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_configuration_hash(ccs_configuration_t configuration, ccs_hash_t *hash_ret);
Expand All @@ -164,6 +179,8 @@ ccs_configuration_hash(ccs_configuration_t configuration, ccs_hash_t *hash_ret);
* @return #CCS_RESULT_SUCCESS on success
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p configuration or \p
* other_configuration are not a valid CCS object
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_configuration_cmp(
Expand Down
2 changes: 1 addition & 1 deletion include/cconfigspace/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extern "C" {
* @file context.h
* A Context is a collection of parameters defining a value space. Each
* parameter has a specific index that can be used to reference it.
* The methods defined in this file can be used on objects who re contexts.
* The methods defined in this file can be used on objects who are contexts.
* In practice those are useful for binding to avoid binding the children
* methods, whereas a C application would rather use the object class specific
* versions in order to benefit from the added type safety.
Expand Down
12 changes: 6 additions & 6 deletions include/cconfigspace/distribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ ccs_multivariate_distribution_get_distributions(
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p distribution is not a valid
* CCS distribution; or if \p rng is not a valid CCS rng
* @remarks
* This function is thread-safe if \p rng is only used by one thread at a time
* This function is thread-safe
*/
extern ccs_result_t
ccs_distribution_sample(
Expand All @@ -702,7 +702,7 @@ ccs_distribution_sample(
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p distribution is not a valid
* CCS distribution; or if \p rng is not a valid CCS rng
* @remarks
* This function is thread-safe if \p rng is only used by one thread at a time
* This function is thread-safe
*/
extern ccs_result_t
ccs_distribution_samples(
Expand Down Expand Up @@ -733,7 +733,7 @@ ccs_distribution_samples(
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p distribution is not a valid
* CCS distribution; or if \p rng is not a valid CCS rng
* @remarks
* This function is thread-safe if \p rng is only used by one thread at a time
* This function is thread-safe
*/
extern ccs_result_t
ccs_distribution_strided_samples(
Expand All @@ -758,7 +758,7 @@ ccs_distribution_strided_samples(
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p distribution is not a valid
* CCS distribution; or if \p rng is not a valid CCS rng
* @remarks
* This function is thread-safe if \p rng is only used by one thread at a time
* This function is thread-safe
*/
extern ccs_result_t
ccs_distribution_soa_samples(
Expand Down Expand Up @@ -787,7 +787,7 @@ ccs_distribution_soa_samples(
* CCS distribution; or if \p rng is not a valid CCS rng; or if at least one of
* the parameters provided is NULL
* @remarks
* This function is thread-safe if \p rng is only used by one thread at a time
* This function is thread-safe
*/
extern ccs_result_t
ccs_distribution_parameters_samples(
Expand All @@ -812,7 +812,7 @@ ccs_distribution_parameters_samples(
* CCS distribution; or if \p rng is not a valid CCS rng; or if at least one of
* the parameters provided is NULL
* @remarks
* This function is thread-safe if \p rng is only used by one thread at a time
* This function is thread-safe
*/
extern ccs_result_t
ccs_distribution_parameters_sample(
Expand Down
6 changes: 3 additions & 3 deletions include/cconfigspace/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ccs_create_map(ccs_map_t *map_ret);
* @return #CCS_RESULT_ERROR_OUT_OF_MEMORY if there was not enough memory to
* allocate data structures
* @remarks
* This function is NOT thread-safe
* This function is thread-safe
*/
extern ccs_result_t
ccs_map_set(ccs_map_t map, ccs_datum_t key, ccs_datum_t value);
Expand Down Expand Up @@ -79,7 +79,7 @@ ccs_map_get(ccs_map_t map, ccs_datum_t key, ccs_datum_t *value_ret);
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p map is not a valid CCS map
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p key does not exist in \p map
* @remarks
* This function is NOT thread-safe
* This function is thread-safe
*/
extern ccs_result_t
ccs_map_del(ccs_map_t map, ccs_datum_t key);
Expand Down Expand Up @@ -179,7 +179,7 @@ ccs_map_get_pairs(
* @return #CCS_RESULT_SUCCESS on success
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p map is not a valid CCS map
* @remarks
* This function is NOT thread-safe
* This function is thread-safe
*/
extern ccs_result_t
ccs_map_clear(ccs_map_t map);
Expand Down
21 changes: 3 additions & 18 deletions include/cconfigspace/rng.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ ccs_rng_get_type(ccs_rng_t rng, const gsl_rng_type **rng_type_ret);
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p rng is not a valid CCS random
* number generator
* @remarks
* This function is NOT thread-safe
* This function is thread-safe
*/
extern ccs_result_t
ccs_rng_set_seed(ccs_rng_t rng, unsigned long int seed);
Expand All @@ -84,7 +84,7 @@ ccs_rng_set_seed(ccs_rng_t rng, unsigned long int seed);
* number generator
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p value_ret is NULL
* @remarks
* This function is NOT thread-safe
* This function is thread-safe
*/
extern ccs_result_t
ccs_rng_get(ccs_rng_t rng, unsigned long int *value_ret);
Expand All @@ -100,25 +100,10 @@ ccs_rng_get(ccs_rng_t rng, unsigned long int *value_ret);
* number generator
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p value_ret is NULL
* @remarks
* This function is NOT thread-safe
*/
extern ccs_result_t
ccs_rng_uniform(ccs_rng_t rng, ccs_float_t *value_ret);

/**
* Get the underlying gsl random number generator.
* @param [in] rng
* @param [out] gsl_rng_ret a pointer to the variable that will contain a
* pointer to the underlying random number generator
* @return #CCS_RESULT_SUCCESS on success
* @return #CCS_RESULT_ERROR_INVALID_OBJECT if \p rng is not a valid CCS random
* number generator
* @return #CCS_RESULT_ERROR_INVALID_VALUE if \p gsl_rng_ret is NULL
* @remarks
* This function is thread-safe
*/
extern ccs_result_t
ccs_rng_get_gsl_rng(ccs_rng_t rng, gsl_rng **gsl_rng_ret);
ccs_rng_uniform(ccs_rng_t rng, ccs_float_t *value_ret);

/**
* Get the minimum value that can be returned by #ccs_rng_get.
Expand Down
2 changes: 1 addition & 1 deletion src/configuration_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ ccs_configuration_space_samples(
size_t count = 0;
ccs_bool_t found;
ccs_configuration_t config = NULL;
CCS_OBJ_WRLOCK(configuration_space);
CCS_OBJ_RDLOCK(configuration_space);
CCS_REFUTE_ERR_GOTO(
err, !configuration_space->data->graph_ok,
CCS_RESULT_ERROR_INVALID_GRAPH, errgraph);
Expand Down
Loading

0 comments on commit c7e1442

Please sign in to comment.