<div dir="ltr">Pushed to master</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 14, 2021 at 9:44 AM Mahesh Pittala <<a href="mailto:mahesh@multicorewareinc.com">mahesh@multicorewareinc.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">From e0ef56408ac917ad6570f5f204accf8858326232 Mon Sep 17 00:00:00 2001<br>From: lwWang <<a href="mailto:liwei@multicorewareinc.com" target="_blank">liwei@multicorewareinc.com</a>><br>Date: Wed, 13 Oct 2021 16:54:54 +0800<br>Subject: [PATCH] fix the compiling errors on mac<br><br>---<br> source/common/ringmem.cpp |   4 +-<br> source/common/threading.h | 183 +++++++++++++++++++++++++++++++++++++-<br> 2 files changed, 184 insertions(+), 3 deletions(-)<br><br>diff --git a/source/common/ringmem.cpp b/source/common/ringmem.cpp<br>index a4f191c90..cfd44e83d 100644<br>--- a/source/common/ringmem.cpp<br>+++ b/source/common/ringmem.cpp<br>@@ -227,7 +227,7 @@ namespace X265_NS {<br>                 }<br> <br>                 ///< shared memory name<br>-                snprintf(nameBuf, sizeof(nameBuf - 1), "%s%s", X265_SEMAPHORE_RINGMEM_WRITER_NAME, name);<br>+                snprintf(nameBuf, sizeof(nameBuf) - 1, "%s%s", X265_SEMAPHORE_RINGMEM_WRITER_NAME, name);<br>                 if (!m_writeSem->create(nameBuf, m_itemCnt, m_itemCnt))<br>                 {<br>                     release();<br>@@ -242,7 +242,7 @@ namespace X265_NS {<br>                 }<br> <br>                 ///< shared memory name<br>-                snprintf(nameBuf, sizeof(nameBuf - 1), "%s%s", X265_SEMAPHORE_RINGMEM_READER_NAME, name);<br>+                snprintf(nameBuf, sizeof(nameBuf) - 1, "%s%s", X265_SEMAPHORE_RINGMEM_READER_NAME, name);<br>                 if (!m_readSem->create(nameBuf, 0, m_itemCnt))<br>                 {<br>                     release();<br>diff --git a/source/common/threading.h b/source/common/threading.h<br>index dcf6081e3..8a5c39cf0 100644<br>--- a/source/common/threading.h<br>+++ b/source/common/threading.h<br>@@ -508,7 +508,9 @@ class NamedSemaphore<br> public:<br>     NamedSemaphore() <br>         : m_sem(NULL)<br>+#ifndef __APPLE__<br>         , m_name(NULL)<br>+#endif //__APPLE__<br>     {<br>     }<br> <br>@@ -525,6 +527,59 @@ public:<br>             return false;<br>         }<br> <br>+#ifdef __APPLE__<br>+        do<br>+        {<br>+            int32_t pshared = name != NULL ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE;<br>+<br>+            m_sem = (mac_sem_t *)malloc(sizeof(mac_sem_t));<br>+            if (!m_sem)<br>+            {<br>+                break;<br>+            }<br>+<br>+            if (pthread_mutexattr_init(&m_sem->mutexAttr))<br>+            {<br>+                break;<br>+            }<br>+<br>+            if (pthread_mutexattr_setpshared(&m_sem->mutexAttr, pshared))<br>+            {<br>+                break;<br>+            }<br>+<br>+            if (pthread_condattr_init(&m_sem->condAttr))<br>+            {<br>+                break;<br>+            }<br>+<br>+            if (pthread_condattr_setpshared(&m_sem->condAttr, pshared))<br>+            {<br>+                break;<br>+            }<br>+<br>+            if (pthread_mutex_init(&m_sem->mutex, &m_sem->mutexAttr))<br>+            {<br>+                break;<br>+            }<br>+<br>+            if (pthread_cond_init(&m_sem->cond, &m_sem->condAttr))<br>+            {<br>+                break;<br>+            }<br>+<br>+            m_sem->curCnt = initcnt;<br>+            m_sem->maxCnt = maxcnt;<br>+<br>+            ret = true;<br>+        } while (0);<br>+        <br>+        if (!ret)<br>+        {<br>+            release();<br>+        }<br>+<br>+#else  //__APPLE__<br>         m_sem = sem_open(name, O_CREAT | O_EXCL, 0666, initcnt);<br>         if (m_sem != SEM_FAILED) <br>         {<br>@@ -543,12 +598,44 @@ public:<br>                 }<br>             }<br>         }<br>+#endif //__APPLE__<br> <br>         return ret;<br>     }<br> <br>     bool give(const int32_t cnt)<br>     {<br>+        if (!m_sem)<br>+        {<br>+            return false;<br>+        }<br>+<br>+#ifdef __APPLE__<br>+        if (pthread_mutex_lock(&m_sem->mutex))<br>+        {<br>+            return false;<br>+        }<br>+<br>+        int oldCnt = m_sem->curCnt;<br>+        m_sem->curCnt += cnt;<br>+        if (m_sem->curCnt > m_sem->maxCnt)<br>+        {<br>+            m_sem->curCnt = m_sem->maxCnt;<br>+        }<br>+<br>+        bool ret = true;<br>+        if (!oldCnt)<br>+        {<br>+            ret = 0 == pthread_cond_broadcast(&m_sem->cond);<br>+        }<br>+<br>+        if (pthread_mutex_unlock(&m_sem->mutex))<br>+        {<br>+            return false;<br>+        }<br>+<br>+        return ret;<br>+#else //__APPLE__<br>         int ret = 0;<br>         int32_t curCnt = cnt;<br>         while (curCnt-- && !ret) {<br>@@ -556,11 +643,82 @@ public:<br>         }<br> <br>         return 0 == ret;<br>+#endif //_APPLE__<br>     }<br> <br>     bool take(const uint32_t time_out = TIMEOUT_INFINITE)<br>     {<br>-        if (TIMEOUT_INFINITE == time_out) {<br>+        if (!m_sem)<br>+        {<br>+            return false;<br>+        }<br>+<br>+#ifdef __APPLE__<br>+<br>+        if (pthread_mutex_lock(&m_sem->mutex))<br>+        {<br>+            return false;<br>+        }<br>+<br>+        bool ret = true;<br>+        if (TIMEOUT_INFINITE == time_out) <br>+        {<br>+            if (!m_sem->curCnt)<br>+            {<br>+                if (pthread_cond_wait(&m_sem->cond, &m_sem->mutex))<br>+                {<br>+                    ret = false;<br>+                } <br>+            }<br>+<br>+            if (m_sem->curCnt && ret)<br>+            {<br>+                m_sem->curCnt--;<br>+            }<br>+        }<br>+        else<br>+        {<br>+            if (0 == time_out)<br>+            {<br>+                if (m_sem->curCnt)<br>+                {<br>+                    m_sem->curCnt--;<br>+                }<br>+                else<br>+                {<br>+                    ret = false;<br>+                }<br>+            }<br>+            else<br>+            {<br>+                if (!m_sem->curCnt)<br>+                {<br>+                    struct timespec ts;<br>+                    ts.tv_sec = time_out / 1000L;<br>+                    ts.tv_nsec = (time_out * 1000000L) - ts.tv_sec * 1000 * 1000 * 1000;<br>+<br>+                    if (pthread_cond_timedwait(&m_sem->cond, &m_sem->mutex, &ts))<br>+                    {<br>+                        ret = false;<br>+                    }<br>+                }<br>+<br>+                if (m_sem->curCnt && ret)<br>+                {<br>+                    m_sem->curCnt--;<br>+                }<br>+            }<br>+        }<br>+<br>+        if (pthread_mutex_unlock(&m_sem->mutex))<br>+        {<br>+            return false;<br>+        }<br>+<br>+        return ret;<br>+#else //__APPLE__<br>+        if (TIMEOUT_INFINITE == time_out) <br>+        {<br>             return 0 == sem_wait(m_sem);<br>         }<br>         else <br>@@ -577,23 +735,46 @@ public:<br>                 return 0 == sem_timedwait(m_sem, &ts);<br>             }<br>         }<br>+#endif //_APPLE__<br>     }<br> <br>     void release()<br>     {<br>         if (m_sem)<br>         {<br>+#ifdef __APPLE__<br>+            pthread_condattr_destroy(&m_sem->condAttr);<br>+            pthread_mutexattr_destroy(&m_sem->mutexAttr);<br>+            pthread_mutex_destroy(&m_sem->mutex);<br>+            pthread_cond_destroy(&m_sem->cond);<br>+            free(m_sem);<br>+            m_sem = NULL;<br>+#else //__APPLE__<br>             sem_close(m_sem);<br>             sem_unlink(m_name);<br>             m_sem = NULL;<br>             free(m_name);<br>             m_name = NULL;<br>+#endif //__APPLE__<br>         }<br>     }<br> <br> private:<br>+#ifdef __APPLE__<br>+    typedef struct<br>+    {<br>+        pthread_mutex_t     mutex;<br>+        pthread_cond_t      cond;<br>+        pthread_mutexattr_t mutexAttr;<br>+        pthread_condattr_t  condAttr;<br>+        uint32_t            curCnt;<br>+        uint32_t            maxCnt;<br>+    }mac_sem_t;<br>+    mac_sem_t *m_sem;<br>+#else // __APPLE__<br>     sem_t *m_sem;<br>     char  *m_name;<br>+#endif // __APPLE_<br> };<br> <br> #endif // ifdef _WIN32<br>-- <br>2.23.0.windows.1<br><br></div>
</blockquote></div>