Reference Guide

12 Chapter 2: Multithreading Support
RSA BSAFE Crypto-C Mcro Edition 3.x to 4.1.4 Migration Guide
Lock Management
The library now manages locks internally and the application needs to provide the
synchronization method used.
Crypto-C ME 4.1.4 provides a default locking implementation for all supported
platforms using
R_SYNC_METH_default(). Alternatively, custom implementations
can be used. For more information about how to implement custom synchronization
methods, see Multithreading and Locking in the RSA BSAFE Crypto-C Mcro
Edition Developers Guide.
In Crypto-C ME 4.1.4 the locking implementation is supplied either at library
initialization time by calling
R_STATE_init_defaults_mt(), or immediately
after library initialization by calling
R_SYNC_set_method().
R_STATE_init_defaults_mt() uses the default locking implementation for the
platform you are developing on and is the recommended method for applications that
do not have special locking requirements. Applications that have special requirements
and need a custom synchronization method, use
R_SYNC_set_method()
immediately after the library state is initialized.
Note: It is important that multithreaded applications use a locking
implementation to ensure correct thread-safe access to internally shared data.
Performing locking outside Crypto-C ME does not provide thread-safe
operation in all situations.
The following code examples show locking implementations for Crypto-C ME 3.x
and Crypto-C ME 4.1.4.
Locking - Crypto-C ME 3.x
static LOCK_T *locks = NULL;
int locking_callback(int mode, int lockid,
char *file, int line)
{
if (mode & R_LOCK_LOCK)
lock(lockid);
else
unlock(lockid);
return 1;
}
int main(int argc, char **argv)
{
int ret;
int count = R_lock_num();
locks = malloc(sizeof(LOCK_T) * count);
for (i = 0; i < locks; ++i) {
/* Initialise lock i */
}
ret = R_lock_set_cb(locking_callback);
ret = R_LIB_CTX_new(…);