| 1 |
#include "avm_locker.h" |
| 2 |
|
| 3 |
// to get recursive mutexes |
| 4 |
// #define __USE_UNIX98 |
| 5 |
|
| 6 |
#include <sys/time.h> |
| 7 |
#include <errno.h>//EDEADLK |
| 8 |
#include <stdio.h> |
| 9 |
#include <pthread.h> |
| 10 |
|
| 11 |
// doesn't exists on BSD systems ?? #include <semaphore.h> |
| 12 |
|
| 13 |
AVM_BEGIN_NAMESPACE; |
| 14 |
|
| 15 |
PthreadMutex::PthreadMutex( /*Attr mattr */ ) |
| 16 |
{ |
| 17 |
m_pMutex = new pthread_mutex_t; |
| 18 |
#if 0 |
| 19 |
pthread_mutexattr_t pma; |
| 20 |
pthread_mutexattr_init(&pma); |
| 21 |
|
| 22 |
if (mattr == RECURSIVE) |
| 23 |
pthread_mutexattr_settype(&pma, PTHREAD_MUTEX_RECURSIVE_NP); |
| 24 |
|
| 25 |
pthread_mutex_init((pthread_mutex_t*) m_pMutex, &pma); |
| 26 |
#endif |
| 27 |
pthread_mutex_init((pthread_mutex_t*) m_pMutex, NULL); |
| 28 |
} |
| 29 |
|
| 30 |
PthreadMutex::~PthreadMutex() |
| 31 |
{ |
| 32 |
pthread_mutex_destroy((pthread_mutex_t*) m_pMutex); |
| 33 |
delete (pthread_mutex_t*) m_pMutex; |
| 34 |
} |
| 35 |
|
| 36 |
int PthreadMutex::Lock() |
| 37 |
{ |
| 38 |
return pthread_mutex_lock((pthread_mutex_t*) m_pMutex); |
| 39 |
} |
| 40 |
|
| 41 |
int PthreadMutex::TryLock() |
| 42 |
{ |
| 43 |
return (pthread_mutex_trylock((pthread_mutex_t*) m_pMutex) != EBUSY) ? 0 : -1; |
| 44 |
} |
| 45 |
|
| 46 |
int PthreadMutex::Unlock() |
| 47 |
{ |
| 48 |
return pthread_mutex_unlock((pthread_mutex_t*) m_pMutex); |
| 49 |
} |
| 50 |
|
| 51 |
PthreadCond::PthreadCond() |
| 52 |
{ |
| 53 |
m_pCond = new pthread_cond_t; |
| 54 |
pthread_cond_init((pthread_cond_t*) m_pCond, NULL); |
| 55 |
} |
| 56 |
|
| 57 |
PthreadCond::~PthreadCond() |
| 58 |
{ |
| 59 |
pthread_cond_destroy((pthread_cond_t*) m_pCond); |
| 60 |
delete (pthread_cond_t*) m_pCond; |
| 61 |
} |
| 62 |
|
| 63 |
int PthreadCond::Wait(PthreadMutex& m, float waitTime) |
| 64 |
{ |
| 65 |
if (waitTime >= 0.f) |
| 66 |
{ |
| 67 |
struct timespec timeout; |
| 68 |
struct timeval now; |
| 69 |
unsigned int round = (unsigned int) waitTime; |
| 70 |
gettimeofday(&now, 0); |
| 71 |
|
| 72 |
//timeout.tv_sec = now.tv_sec + (int)waitTime; |
| 73 |
//waitTime -= (int)waitTime; |
| 74 |
//timeout.tv_nsec = (now.tv_usec + (int)(waitTime * 1000000)) * 1000; |
| 75 |
timeout.tv_sec = now.tv_sec + round; |
| 76 |
timeout.tv_nsec = (now.tv_usec + (unsigned int)((waitTime - (float)round) * 1000000.f)) * 1000; |
| 77 |
//printf("Wait %f %d %ld\n", waitTime, round, timeout.tv_nsec ); |
| 78 |
|
| 79 |
if (timeout.tv_nsec >= 1000000000) |
| 80 |
{ |
| 81 |
timeout.tv_nsec -= 1000000000; |
| 82 |
timeout.tv_sec++; |
| 83 |
} |
| 84 |
// time limited conditional waiting here! |
| 85 |
// sometimes decoder thread could be stoped |
| 86 |
// and we would have wait here forever |
| 87 |
int r = pthread_cond_timedwait((pthread_cond_t*) m_pCond, |
| 88 |
(pthread_mutex_t*) m.m_pMutex, &timeout); |
| 89 |
if (r < 0) |
| 90 |
perror("PthreadCond::Wait()"); |
| 91 |
return r; |
| 92 |
} |
| 93 |
|
| 94 |
return pthread_cond_wait((pthread_cond_t*) m_pCond, |
| 95 |
(pthread_mutex_t*) m.m_pMutex); |
| 96 |
} |
| 97 |
|
| 98 |
int PthreadCond::Broadcast() |
| 99 |
{ |
| 100 |
return pthread_cond_broadcast((pthread_cond_t*) m_pCond); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
PthreadTask::PthreadTask(void* attr, void* (*start_routine)(void *), void* arg) |
| 105 |
{ |
| 106 |
m_pTask = new pthread_t; |
| 107 |
if (pthread_create((pthread_t*) m_pTask, |
| 108 |
(pthread_attr_t*) attr, start_routine, arg)) |
| 109 |
perror("PthreadTask()"); |
| 110 |
} |
| 111 |
|
| 112 |
PthreadTask::~PthreadTask() |
| 113 |
{ |
| 114 |
int result = pthread_join(* ((pthread_t*)m_pTask), 0); |
| 115 |
delete (pthread_t*) m_pTask; |
| 116 |
if (result == EDEADLK) |
| 117 |
{ |
| 118 |
perror("~PthreadTask()"); |
| 119 |
pthread_exit(NULL); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
AVM_END_NAMESPACE; |