HttpClientHelpers.h 955 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef UTIL_HTTPCLIENTHELPERS_H
  2. #define UTIL_HTTPCLIENTHELPERS_H
  3. #include <boost/shared_ptr.hpp>
  4. #include <boost/thread.hpp>
  5. #include <openssl/crypto.h>
  6. namespace PresenceDetection {
  7. namespace Util {
  8. static std::vector<boost::shared_ptr<boost::mutex> > g_mutexes;
  9. static void LockCallback(int mode, int type, const char* /*file*/, int /*line*/)
  10. {
  11. if (mode & CRYPTO_LOCK)
  12. g_mutexes[type]->lock();
  13. else
  14. g_mutexes[type]->unlock();
  15. }
  16. static unsigned long ThreadId(void)
  17. {
  18. return (unsigned long)pthread_self();
  19. }
  20. static void InitializeLocks(void)
  21. {
  22. g_mutexes.resize(CRYPTO_num_locks());
  23. for (int i = 0; i < CRYPTO_num_locks(); ++i)
  24. g_mutexes[i] = boost::shared_ptr<boost::mutex>(new boost::mutex());
  25. CRYPTO_set_id_callback(ThreadId);
  26. CRYPTO_set_locking_callback(LockCallback);
  27. }
  28. static void FreeLocks(void)
  29. {
  30. CRYPTO_set_locking_callback(NULL);
  31. }
  32. } // namespace Util
  33. } // namespace PresenceDetection
  34. #endif // UTIL_HTTPCLIENTHELPERS_H