SRMutex.m 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //
  2. // Copyright (c) 2016-present, Facebook, Inc.
  3. // All rights reserved.
  4. //
  5. // This source code is licensed under the BSD-style license found in the
  6. // LICENSE file in the root directory of this source tree. An additional grant
  7. // of patent rights can be found in the PATENTS file in the same directory.
  8. //
  9. #import "SRMutex.h"
  10. #import <pthread/pthread.h>
  11. NS_ASSUME_NONNULL_BEGIN
  12. SRMutex SRMutexInitRecursive(void)
  13. {
  14. pthread_mutex_t *mutex = malloc(sizeof(pthread_mutex_t));
  15. pthread_mutexattr_t attributes;
  16. pthread_mutexattr_init(&attributes);
  17. pthread_mutexattr_settype(&attributes, PTHREAD_MUTEX_RECURSIVE);
  18. pthread_mutex_init(mutex, &attributes);
  19. pthread_mutexattr_destroy(&attributes);
  20. return mutex;
  21. }
  22. void SRMutexDestroy(SRMutex mutex)
  23. {
  24. pthread_mutex_destroy(mutex);
  25. free(mutex);
  26. }
  27. __attribute__((no_thread_safety_analysis))
  28. void SRMutexLock(SRMutex mutex)
  29. {
  30. pthread_mutex_lock(mutex);
  31. }
  32. __attribute__((no_thread_safety_analysis))
  33. void SRMutexUnlock(SRMutex mutex)
  34. {
  35. pthread_mutex_unlock(mutex);
  36. }
  37. NS_ASSUME_NONNULL_END