[x265] [PATCH] fix the compiling errors on mac
Mahesh Pittala
mahesh at multicorewareinc.com
Thu Oct 14 04:14:21 UTC 2021
>From e0ef56408ac917ad6570f5f204accf8858326232 Mon Sep 17 00:00:00 2001
From: lwWang <liwei at multicorewareinc.com>
Date: Wed, 13 Oct 2021 16:54:54 +0800
Subject: [PATCH] fix the compiling errors on mac
---
source/common/ringmem.cpp | 4 +-
source/common/threading.h | 183 +++++++++++++++++++++++++++++++++++++-
2 files changed, 184 insertions(+), 3 deletions(-)
diff --git a/source/common/ringmem.cpp b/source/common/ringmem.cpp
index a4f191c90..cfd44e83d 100644
--- a/source/common/ringmem.cpp
+++ b/source/common/ringmem.cpp
@@ -227,7 +227,7 @@ namespace X265_NS {
}
///< shared memory name
- snprintf(nameBuf, sizeof(nameBuf - 1), "%s%s",
X265_SEMAPHORE_RINGMEM_WRITER_NAME, name);
+ snprintf(nameBuf, sizeof(nameBuf) - 1, "%s%s",
X265_SEMAPHORE_RINGMEM_WRITER_NAME, name);
if (!m_writeSem->create(nameBuf, m_itemCnt, m_itemCnt))
{
release();
@@ -242,7 +242,7 @@ namespace X265_NS {
}
///< shared memory name
- snprintf(nameBuf, sizeof(nameBuf - 1), "%s%s",
X265_SEMAPHORE_RINGMEM_READER_NAME, name);
+ snprintf(nameBuf, sizeof(nameBuf) - 1, "%s%s",
X265_SEMAPHORE_RINGMEM_READER_NAME, name);
if (!m_readSem->create(nameBuf, 0, m_itemCnt))
{
release();
diff --git a/source/common/threading.h b/source/common/threading.h
index dcf6081e3..8a5c39cf0 100644
--- a/source/common/threading.h
+++ b/source/common/threading.h
@@ -508,7 +508,9 @@ class NamedSemaphore
public:
NamedSemaphore()
: m_sem(NULL)
+#ifndef __APPLE__
, m_name(NULL)
+#endif //__APPLE__
{
}
@@ -525,6 +527,59 @@ public:
return false;
}
+#ifdef __APPLE__
+ do
+ {
+ int32_t pshared = name != NULL ? PTHREAD_PROCESS_SHARED :
PTHREAD_PROCESS_PRIVATE;
+
+ m_sem = (mac_sem_t *)malloc(sizeof(mac_sem_t));
+ if (!m_sem)
+ {
+ break;
+ }
+
+ if (pthread_mutexattr_init(&m_sem->mutexAttr))
+ {
+ break;
+ }
+
+ if (pthread_mutexattr_setpshared(&m_sem->mutexAttr, pshared))
+ {
+ break;
+ }
+
+ if (pthread_condattr_init(&m_sem->condAttr))
+ {
+ break;
+ }
+
+ if (pthread_condattr_setpshared(&m_sem->condAttr, pshared))
+ {
+ break;
+ }
+
+ if (pthread_mutex_init(&m_sem->mutex, &m_sem->mutexAttr))
+ {
+ break;
+ }
+
+ if (pthread_cond_init(&m_sem->cond, &m_sem->condAttr))
+ {
+ break;
+ }
+
+ m_sem->curCnt = initcnt;
+ m_sem->maxCnt = maxcnt;
+
+ ret = true;
+ } while (0);
+
+ if (!ret)
+ {
+ release();
+ }
+
+#else //__APPLE__
m_sem = sem_open(name, O_CREAT | O_EXCL, 0666, initcnt);
if (m_sem != SEM_FAILED)
{
@@ -543,12 +598,44 @@ public:
}
}
}
+#endif //__APPLE__
return ret;
}
bool give(const int32_t cnt)
{
+ if (!m_sem)
+ {
+ return false;
+ }
+
+#ifdef __APPLE__
+ if (pthread_mutex_lock(&m_sem->mutex))
+ {
+ return false;
+ }
+
+ int oldCnt = m_sem->curCnt;
+ m_sem->curCnt += cnt;
+ if (m_sem->curCnt > m_sem->maxCnt)
+ {
+ m_sem->curCnt = m_sem->maxCnt;
+ }
+
+ bool ret = true;
+ if (!oldCnt)
+ {
+ ret = 0 == pthread_cond_broadcast(&m_sem->cond);
+ }
+
+ if (pthread_mutex_unlock(&m_sem->mutex))
+ {
+ return false;
+ }
+
+ return ret;
+#else //__APPLE__
int ret = 0;
int32_t curCnt = cnt;
while (curCnt-- && !ret) {
@@ -556,11 +643,82 @@ public:
}
return 0 == ret;
+#endif //_APPLE__
}
bool take(const uint32_t time_out = TIMEOUT_INFINITE)
{
- if (TIMEOUT_INFINITE == time_out) {
+ if (!m_sem)
+ {
+ return false;
+ }
+
+#ifdef __APPLE__
+
+ if (pthread_mutex_lock(&m_sem->mutex))
+ {
+ return false;
+ }
+
+ bool ret = true;
+ if (TIMEOUT_INFINITE == time_out)
+ {
+ if (!m_sem->curCnt)
+ {
+ if (pthread_cond_wait(&m_sem->cond, &m_sem->mutex))
+ {
+ ret = false;
+ }
+ }
+
+ if (m_sem->curCnt && ret)
+ {
+ m_sem->curCnt--;
+ }
+ }
+ else
+ {
+ if (0 == time_out)
+ {
+ if (m_sem->curCnt)
+ {
+ m_sem->curCnt--;
+ }
+ else
+ {
+ ret = false;
+ }
+ }
+ else
+ {
+ if (!m_sem->curCnt)
+ {
+ struct timespec ts;
+ ts.tv_sec = time_out / 1000L;
+ ts.tv_nsec = (time_out * 1000000L) - ts.tv_sec * 1000
* 1000 * 1000;
+
+ if (pthread_cond_timedwait(&m_sem->cond,
&m_sem->mutex, &ts))
+ {
+ ret = false;
+ }
+ }
+
+ if (m_sem->curCnt && ret)
+ {
+ m_sem->curCnt--;
+ }
+ }
+ }
+
+ if (pthread_mutex_unlock(&m_sem->mutex))
+ {
+ return false;
+ }
+
+ return ret;
+#else //__APPLE__
+ if (TIMEOUT_INFINITE == time_out)
+ {
return 0 == sem_wait(m_sem);
}
else
@@ -577,23 +735,46 @@ public:
return 0 == sem_timedwait(m_sem, &ts);
}
}
+#endif //_APPLE__
}
void release()
{
if (m_sem)
{
+#ifdef __APPLE__
+ pthread_condattr_destroy(&m_sem->condAttr);
+ pthread_mutexattr_destroy(&m_sem->mutexAttr);
+ pthread_mutex_destroy(&m_sem->mutex);
+ pthread_cond_destroy(&m_sem->cond);
+ free(m_sem);
+ m_sem = NULL;
+#else //__APPLE__
sem_close(m_sem);
sem_unlink(m_name);
m_sem = NULL;
free(m_name);
m_name = NULL;
+#endif //__APPLE__
}
}
private:
+#ifdef __APPLE__
+ typedef struct
+ {
+ pthread_mutex_t mutex;
+ pthread_cond_t cond;
+ pthread_mutexattr_t mutexAttr;
+ pthread_condattr_t condAttr;
+ uint32_t curCnt;
+ uint32_t maxCnt;
+ }mac_sem_t;
+ mac_sem_t *m_sem;
+#else // __APPLE__
sem_t *m_sem;
char *m_name;
+#endif // __APPLE_
};
#endif // ifdef _WIN32
--
2.23.0.windows.1
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20211014/beaf04d3/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mac_build_fix (2).diff
Type: application/octet-stream
Size: 7173 bytes
Desc: not available
URL: <http://mailman.videolan.org/pipermail/x265-devel/attachments/20211014/beaf04d3/attachment-0001.obj>
More information about the x265-devel
mailing list