lock.c 869 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include "log.h"
  5. #include "lock.h"
  6. #define MALLOC malloc
  7. #define FREE free
  8. lock_t lock_init(void)
  9. {
  10. int r;
  11. pthread_mutex_t *m=MALLOC(sizeof(pthread_mutex_t));
  12. if(!m) {
  13. return 0;
  14. }
  15. r = pthread_mutex_init(m, NULL);
  16. if(r) {
  17. LOGE("pthread_mutex_create failed\n");
  18. FREE(m); return 0;
  19. }
  20. return (lock_t)m;
  21. }
  22. int lock_deinit(lock_t h)
  23. {
  24. int r;
  25. if(!h) {
  26. return -1;
  27. }
  28. r = pthread_mutex_destroy((pthread_mutex_t*)h);
  29. FREE((void*)h);
  30. return r;
  31. }
  32. int lock_on(lock_t h)
  33. {
  34. if(!h) {
  35. return -1;
  36. }
  37. return pthread_mutex_lock((pthread_mutex_t*)h);
  38. }
  39. int lock_off(lock_t h)
  40. {
  41. if(!h) {
  42. return -1;
  43. }
  44. return pthread_mutex_unlock((pthread_mutex_t*)h);
  45. }