2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file crypto-init.cpp
18 * @author Maciej Karpiuk (m.karpiuk2@samsung.com)
22 #include "crypto-init.h"
24 #include <openssl/evp.h>
30 std::mutex cryptoInitMutex;
34 typedef void(*initFnPtr)();
36 // has to be atomic as storing function pointer is not an atomic operation on armv7l
37 std::atomic<initFnPtr> initFn (&initOpenSSL);
43 std::lock_guard<std::mutex> lock(cryptoInitMutex);
45 * We don't care about memory ordering here. Current thread will order it correctly and for
46 * other threads only store matters. Also only one thread can be here at once because of lock.
48 if(initFn.load(std::memory_order_relaxed) != &initEmpty)
50 OpenSSL_add_all_ciphers();
51 OpenSSL_add_all_algorithms();
52 OpenSSL_add_all_digests();
55 * Synchronizes with load. Everything that happened before this store in this thread is
56 * visible to everything that happens after load in another thread. We switch to an empty
59 initFn.store(&initEmpty, std::memory_order_release);
63 } // namespace anonymous
65 void initCryptoLib() {
67 * Synchronizes with store. Everything that happened before store in another thread will be
68 * visible in this thread after load.
70 initFn.load(std::memory_order_acquire)();