| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include "log.h"
- #include "lock.h"
- #define MALLOC malloc
- #define FREE free
- //static global lock
- static pthread_mutex_t *mutex_pool[LOCK_ID_MAX]={0};
- int lock_s_init(void)
- {
- int i,r;
- pthread_mutex_t *m=NULL;
-
- for(i=0; i<LOCK_ID_MAX; i++) {
- m = MALLOC(sizeof(pthread_mutex_t));
- if(m) {
- r = pthread_mutex_init(m, NULL);
- if(r) {
- LOGE("lock_s_init %d failed\n", i);
- FREE(m); m = NULL;
- }
- mutex_pool[i] = m;
- }
- }
-
- return 0;
- }
- int lock_s_deinit(void)
- {
- int i,r;
-
- for(i=0; i<LOCK_ID_MAX; i++) {
- if(mutex_pool[i]) {
- r = pthread_mutex_destroy(mutex_pool[i]);
- if(r) {
- LOGE("lock_s_deinit %d failed\n", i);
- continue;
- }
-
- mutex_pool[i] = NULL;
- }
- }
-
- return 0;
- }
- int lock_s_hold(int id)
- {
- if(id<0 || id>=LOCK_ID_MAX || !mutex_pool[id]) {
- return -1;
- }
-
- return pthread_mutex_lock(mutex_pool[id]);
- }
- int lock_s_release(int id)
- {
- if(id<0 || id>=LOCK_ID_MAX || !mutex_pool[id]) {
- return -1;
- }
-
- return pthread_mutex_unlock(mutex_pool[id]);
- }
- ///////////////////////////////////////////////////////
- lock_t lock_d_init(void)
- {
- int r;
- pthread_mutex_t *m=MALLOC(sizeof(pthread_mutex_t));
- if(!m) {
- return 0;
- }
-
- r = pthread_mutex_init(m, NULL);
- if(r) {
- LOGE("pthread_mutex_create failed\n");
- FREE(m); return 0;
- }
-
- return (lock_t)m;
- }
- int lock_d_deinit(lock_t h)
- {
- int r;
-
- if(!h) {
- return -1;
- }
-
- r = pthread_mutex_destroy((pthread_mutex_t*)h);
- FREE((void*)h);
- return r;
- }
- int lock_d_hold(lock_t h)
- {
- if(!h) {
- return -1;
- }
-
- return pthread_mutex_lock((pthread_mutex_t*)h);
- }
- int lock_d_release(lock_t h)
- {
- if(!h) {
- return -1;
- }
-
- return pthread_mutex_unlock((pthread_mutex_t*)h);
- }
|