init_posix.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (C) 2017. See AUTHORS.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build linux darwin solaris
  15. // +build !windows
  16. package openssl
  17. /*
  18. #include <errno.h>
  19. #include <openssl/crypto.h>
  20. #include <pthread.h>
  21. pthread_mutex_t* goopenssl_locks;
  22. int go_init_locks() {
  23. int rc = 0;
  24. int nlock;
  25. int i;
  26. int locks_needed = CRYPTO_num_locks();
  27. goopenssl_locks = (pthread_mutex_t*)malloc(
  28. sizeof(pthread_mutex_t) * locks_needed);
  29. if (!goopenssl_locks) {
  30. return ENOMEM;
  31. }
  32. for (nlock = 0; nlock < locks_needed; ++nlock) {
  33. rc = pthread_mutex_init(&goopenssl_locks[nlock], NULL);
  34. if (rc != 0) {
  35. break;
  36. }
  37. }
  38. if (rc != 0) {
  39. for (i = nlock - 1; i >= 0; --i) {
  40. pthread_mutex_destroy(&goopenssl_locks[i]);
  41. }
  42. free(goopenssl_locks);
  43. goopenssl_locks = NULL;
  44. }
  45. return rc;
  46. }
  47. void go_thread_locking_callback(int mode, int n, const char *file,
  48. int line) {
  49. if (mode & CRYPTO_LOCK) {
  50. pthread_mutex_lock(&goopenssl_locks[n]);
  51. } else {
  52. pthread_mutex_unlock(&goopenssl_locks[n]);
  53. }
  54. }
  55. */
  56. import "C"