small adjustments, many enhancements to rktplayer
This commit is contained in:
32
shm.cpp
32
shm.cpp
@@ -256,6 +256,8 @@ public:
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <semaphore.h>
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
class ShmApi : public ShmApiBase
|
||||
@@ -338,22 +340,41 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
#define TIME_NS_IN_MSEC 1000000ULL
|
||||
|
||||
class ShmSemApi {
|
||||
private:
|
||||
sem_t *_sem;
|
||||
char *_name;
|
||||
bool _owner;
|
||||
|
||||
private:
|
||||
void makeSemTimeoutTime(struct timespec *ts, int ms) {
|
||||
clock_gettime(CLOCK_REALTIME, ts);
|
||||
ts->tv_sec += ms / 1000;
|
||||
ts->tv_nsec += (ms % 1000) * TIME_NS_IN_MSEC;
|
||||
if (ts->tv_nsec >= 1000000000L) {
|
||||
ts->tv_sec++;
|
||||
ts->tv_nsec = ts->tv_nsec - 1000000000L;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void post() {
|
||||
sem_post(_sem);
|
||||
}
|
||||
|
||||
void wait() {
|
||||
int r = sem_wait(_sem);
|
||||
bool wait(int ms) {
|
||||
struct timespec ts;
|
||||
makeSemTimeoutTime(&ts, ms);
|
||||
int r = sem_timedwait(_sem, &ts);
|
||||
if (r != 0) {
|
||||
ERROR2("sem_wait error: %d, %s\n", errno, strerror(errno));
|
||||
if (errno != ETIMEDOUT) {
|
||||
ERROR2("sem_wait error: %d, %s\n", errno, strerror(errno));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool trywait() {
|
||||
@@ -365,6 +386,11 @@ class ShmSemApi {
|
||||
return (r == 0);
|
||||
}
|
||||
|
||||
public:
|
||||
void takeOwnership() {
|
||||
_owner = true;
|
||||
}
|
||||
|
||||
public:
|
||||
ShmSemApi(const char *n, bool owner) {
|
||||
_name = strdup(n);
|
||||
|
||||
Reference in New Issue
Block a user