Zhiqiang Han | f9c0e27 | 2022-06-14 13:54:03 +0800 | [diff] [blame] | 1 | #include "dvr_types.h" |
| 2 | #include "dvr_mutex.h" |
| 3 | |
| 4 | #define merr(f,...) DVR_DEBUG_FL(1, "dvr_mutex", f, ##__VA_ARGS__) |
| 5 | #define mdbg(f,...) merr(f, ##__VA_ARGS__) |
| 6 | |
| 7 | void _dvr_mutex_init(void *mutex) |
| 8 | { |
| 9 | if (!mutex) { |
| 10 | merr("null mutex\n"); |
| 11 | return; |
| 12 | } |
| 13 | dvr_mutex_t *mtx = (dvr_mutex_t*)mutex; |
| 14 | memset(mtx, 0, sizeof(dvr_mutex_t)); |
| 15 | if (pthread_mutex_init(&mtx->lock, NULL) != 0) { |
| 16 | merr("init mutex fail\n"); |
| 17 | return; |
| 18 | } |
| 19 | mtx->thread = 0; |
| 20 | mtx->lock_cnt = 0; |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | void _dvr_mutex_lock(void *mutex) |
| 25 | { |
| 26 | if (!mutex) { |
| 27 | merr("null mutex\n"); |
| 28 | return; |
| 29 | } |
| 30 | dvr_mutex_t *mtx = (dvr_mutex_t*)mutex; |
| 31 | if (pthread_equal(mtx->thread, pthread_self()) != 0) { |
Zhiqiang Han | 7e77145 | 2022-06-15 22:22:39 +0800 | [diff] [blame^] | 32 | mtx->lock_cnt++; |
Zhiqiang Han | f9c0e27 | 2022-06-14 13:54:03 +0800 | [diff] [blame] | 33 | } else { |
| 34 | pthread_mutex_lock(&mtx->lock); |
| 35 | mtx->thread = pthread_self(); |
| 36 | mtx->lock_cnt = 1; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void _dvr_mutex_unlock(void *mutex) |
| 41 | { |
| 42 | if (!mutex) { |
| 43 | merr("null mutex\n"); |
| 44 | return; |
| 45 | } |
| 46 | dvr_mutex_t *mtx = (dvr_mutex_t*)mutex; |
| 47 | if (pthread_equal(mtx->thread, pthread_self()) != 0) { |
Zhiqiang Han | 7e77145 | 2022-06-15 22:22:39 +0800 | [diff] [blame^] | 48 | mtx->lock_cnt--; |
| 49 | if (mtx->lock_cnt == 0) { |
Zhiqiang Han | f9c0e27 | 2022-06-14 13:54:03 +0800 | [diff] [blame] | 50 | mtx->thread = 0; |
| 51 | pthread_mutex_unlock(&mtx->lock); |
| 52 | } |
| 53 | } else { |
| 54 | mdbg("not own mutex\n"); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void _dvr_mutex_destroy(void *mutex) |
| 59 | { |
| 60 | if (!mutex) { |
| 61 | merr("null mutex\n"); |
| 62 | return; |
| 63 | } |
| 64 | dvr_mutex_t *mtx = (dvr_mutex_t*)mutex; |
| 65 | pthread_mutex_destroy(mtx); |
| 66 | } |
| 67 | |
| 68 | int _dvr_mutex_save(void *mutex) |
| 69 | { |
| 70 | if (!mutex) { |
| 71 | merr("null mutex\n"); |
| 72 | return 0; |
| 73 | } |
| 74 | dvr_mutex_t *mtx = (dvr_mutex_t*)mutex; |
| 75 | int cnt = mtx->lock_cnt; |
| 76 | mtx->lock_cnt = 0; |
| 77 | mtx->thread = 0; |
| 78 | return cnt; |
| 79 | } |
| 80 | |
| 81 | void _dvr_mutex_restore(void *mutex, int val) |
| 82 | { |
| 83 | if (!mutex) { |
| 84 | merr("null mutex\n"); |
| 85 | return; |
| 86 | } |
| 87 | dvr_mutex_t *mtx = (dvr_mutex_t*)mutex; |
| 88 | mtx->lock_cnt = val; |
| 89 | mtx->thread = pthread_self(); |
| 90 | } |
| 91 | |
| 92 | #ifdef DVR_MUTEX_DEBUG |
| 93 | void _dvr_mutex_init_dbg(void *mutex, const char *file, int line) |
| 94 | { |
| 95 | mdbg("%s:%d\n", file, line); |
| 96 | _dvr_mutex_init(mutex); |
| 97 | } |
| 98 | void _dvr_mutex_lock_dbg(void *mutex, const char *file, int line) |
| 99 | { |
| 100 | mdbg("%s:%d\n", file, line); |
| 101 | _dvr_mutex_lock(mutex); |
| 102 | } |
| 103 | void _dvr_mutex_unlock_dbg(void *mutex, const char *file, int line) |
| 104 | { |
| 105 | mdbg("%s:%d\n", file, line); |
| 106 | _dvr_mutex_unlock(mutex); |
| 107 | } |
| 108 | void _dvr_mutex_destroy_dbg(void *mutex, const char *file, int line) |
| 109 | { |
| 110 | mdbg("%s:%d\n", file, line); |
| 111 | _dvr_mutex_destroy(mutex); |
| 112 | } |
| 113 | int _dvr_mutex_save_dbg(void *mutex, const char *file, int line) |
| 114 | { |
| 115 | mdbg("%s:%d\n", file, line); |
| 116 | return _dvr_mutex_save(mutex); |
| 117 | } |
| 118 | void _dvr_mutex_restore_dbg(void *mutex, int val, const char *file, int line) |
| 119 | { |
| 120 | mdbg("%s:%d\n", file, line); |
| 121 | _dvr_mutex_restore(mutex, val); |
| 122 | } |
| 123 | #endif |
| 124 | |