VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Common/lzma/Threads.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/Common/lzma/Threads.c')
-rw-r--r--src/Common/lzma/Threads.c86
1 files changed, 54 insertions, 32 deletions
diff --git a/src/Common/lzma/Threads.c b/src/Common/lzma/Threads.c
index 58eb90ff..cf52bd30 100644
--- a/src/Common/lzma/Threads.c
+++ b/src/Common/lzma/Threads.c
@@ -1,5 +1,5 @@
/* Threads.c -- multithreading library
-2021-12-21 : Igor Pavlov : Public domain */
+2023-03-04 : Igor Pavlov : Public domain */
#include "Precomp.h"
@@ -11,9 +11,9 @@
#include "Threads.h"
-static WRes GetError()
+static WRes GetError(void)
{
- DWORD res = GetLastError();
+ const DWORD res = GetLastError();
return res ? (WRes)res : 1;
}
@@ -173,6 +173,9 @@ WRes CriticalSection_Init(CCriticalSection *p)
Windows XP, 2003 : can raise a STATUS_NO_MEMORY exception
Windows Vista+ : no exceptions */
#ifdef _MSC_VER
+ #ifdef __clang__
+ #pragma GCC diagnostic ignored "-Wlanguage-extension-token"
+ #endif
__try
#endif
{
@@ -193,18 +196,26 @@ WRes CriticalSection_Init(CCriticalSection *p)
// ---------- POSIX ----------
#ifndef __APPLE__
-#ifndef _7ZIP_AFFINITY_DISABLE
+#ifndef Z7_AFFINITY_DISABLE
// _GNU_SOURCE can be required for pthread_setaffinity_np() / CPU_ZERO / CPU_SET
+// clang < 3.6 : unknown warning group '-Wreserved-id-macro'
+// clang 3.6 - 12.01 : gives warning "macro name is a reserved identifier"
+// clang >= 13 : do not give warning
+#if !defined(_GNU_SOURCE)
+ #if defined(__clang__) && (__clang_major__ >= 4) && (__clang_major__ <= 12)
+ #pragma GCC diagnostic ignored "-Wreserved-id-macro"
+ #endif
#define _GNU_SOURCE
-#endif
-#endif
+#endif // !defined(_GNU_SOURCE)
+#endif // Z7_AFFINITY_DISABLE
+#endif // __APPLE__
#include "Threads.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
-#ifdef _7ZIP_AFFINITY_SUPPORTED
+#ifdef Z7_AFFINITY_SUPPORTED
// #include <sched.h>
#endif
@@ -212,15 +223,12 @@ WRes CriticalSection_Init(CCriticalSection *p)
// #include <stdio.h>
// #define PRF(p) p
#define PRF(p)
-
-#define Print(s) PRF(printf("\n%s\n", s))
-
-// #include <stdio.h>
+#define Print(s) PRF(printf("\n%s\n", s);)
WRes Thread_Create_With_CpuSet(CThread *p, THREAD_FUNC_TYPE func, LPVOID param, const CCpuSet *cpuSet)
{
// new thread in Posix probably inherits affinity from parrent thread
- Print("Thread_Create_With_CpuSet");
+ Print("Thread_Create_With_CpuSet")
pthread_attr_t attr;
int ret;
@@ -228,7 +236,7 @@ WRes Thread_Create_With_CpuSet(CThread *p, THREAD_FUNC_TYPE func, LPVOID param,
p->_created = 0;
- RINOK(pthread_attr_init(&attr));
+ RINOK(pthread_attr_init(&attr))
ret = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
@@ -236,7 +244,7 @@ WRes Thread_Create_With_CpuSet(CThread *p, THREAD_FUNC_TYPE func, LPVOID param,
{
if (cpuSet)
{
- #ifdef _7ZIP_AFFINITY_SUPPORTED
+ #ifdef Z7_AFFINITY_SUPPORTED
/*
printf("\n affinity :");
@@ -292,7 +300,7 @@ WRes Thread_Create(CThread *p, THREAD_FUNC_TYPE func, LPVOID param)
WRes Thread_Create_With_Affinity(CThread *p, THREAD_FUNC_TYPE func, LPVOID param, CAffinityMask affinity)
{
- Print("Thread_Create_WithAffinity");
+ Print("Thread_Create_WithAffinity")
CCpuSet cs;
unsigned i;
CpuSet_Zero(&cs);
@@ -312,7 +320,7 @@ WRes Thread_Create_With_Affinity(CThread *p, THREAD_FUNC_TYPE func, LPVOID param
WRes Thread_Close(CThread *p)
{
- // Print("Thread_Close");
+ // Print("Thread_Close")
int ret;
if (!p->_created)
return 0;
@@ -326,7 +334,7 @@ WRes Thread_Close(CThread *p)
WRes Thread_Wait_Close(CThread *p)
{
- // Print("Thread_Wait_Close");
+ // Print("Thread_Wait_Close")
void *thread_return;
int ret;
if (!p->_created)
@@ -343,8 +351,8 @@ WRes Thread_Wait_Close(CThread *p)
static WRes Event_Create(CEvent *p, int manualReset, int signaled)
{
- RINOK(pthread_mutex_init(&p->_mutex, NULL));
- RINOK(pthread_cond_init(&p->_cond, NULL));
+ RINOK(pthread_mutex_init(&p->_mutex, NULL))
+ RINOK(pthread_cond_init(&p->_cond, NULL))
p->_manual_reset = manualReset;
p->_state = (signaled ? True : False);
p->_created = 1;
@@ -363,7 +371,7 @@ WRes AutoResetEvent_CreateNotSignaled(CAutoResetEvent *p)
WRes Event_Set(CEvent *p)
{
- RINOK(pthread_mutex_lock(&p->_mutex));
+ RINOK(pthread_mutex_lock(&p->_mutex))
p->_state = True;
int res1 = pthread_cond_broadcast(&p->_cond);
int res2 = pthread_mutex_unlock(&p->_mutex);
@@ -372,14 +380,14 @@ WRes Event_Set(CEvent *p)
WRes Event_Reset(CEvent *p)
{
- RINOK(pthread_mutex_lock(&p->_mutex));
+ RINOK(pthread_mutex_lock(&p->_mutex))
p->_state = False;
return pthread_mutex_unlock(&p->_mutex);
}
WRes Event_Wait(CEvent *p)
{
- RINOK(pthread_mutex_lock(&p->_mutex));
+ RINOK(pthread_mutex_lock(&p->_mutex))
while (p->_state == False)
{
// ETIMEDOUT
@@ -411,8 +419,8 @@ WRes Semaphore_Create(CSemaphore *p, UInt32 initCount, UInt32 maxCount)
{
if (initCount > maxCount || maxCount < 1)
return EINVAL;
- RINOK(pthread_mutex_init(&p->_mutex, NULL));
- RINOK(pthread_cond_init(&p->_cond, NULL));
+ RINOK(pthread_mutex_init(&p->_mutex, NULL))
+ RINOK(pthread_cond_init(&p->_cond, NULL))
p->_count = initCount;
p->_maxCount = maxCount;
p->_created = 1;
@@ -448,7 +456,7 @@ WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 releaseCount)
if (releaseCount < 1)
return EINVAL;
- RINOK(pthread_mutex_lock(&p->_mutex));
+ RINOK(pthread_mutex_lock(&p->_mutex))
newCount = p->_count + releaseCount;
if (newCount > p->_maxCount)
@@ -458,13 +466,13 @@ WRes Semaphore_ReleaseN(CSemaphore *p, UInt32 releaseCount)
p->_count = newCount;
ret = pthread_cond_broadcast(&p->_cond);
}
- RINOK(pthread_mutex_unlock(&p->_mutex));
+ RINOK(pthread_mutex_unlock(&p->_mutex))
return ret;
}
WRes Semaphore_Wait(CSemaphore *p)
{
- RINOK(pthread_mutex_lock(&p->_mutex));
+ RINOK(pthread_mutex_lock(&p->_mutex))
while (p->_count < 1)
{
pthread_cond_wait(&p->_cond, &p->_mutex);
@@ -489,7 +497,7 @@ WRes Semaphore_Close(CSemaphore *p)
WRes CriticalSection_Init(CCriticalSection *p)
{
- // Print("CriticalSection_Init");
+ // Print("CriticalSection_Init")
if (!p)
return EINTR;
return pthread_mutex_init(&p->_mutex, NULL);
@@ -497,7 +505,7 @@ WRes CriticalSection_Init(CCriticalSection *p)
void CriticalSection_Enter(CCriticalSection *p)
{
- // Print("CriticalSection_Enter");
+ // Print("CriticalSection_Enter")
if (p)
{
// int ret =
@@ -507,7 +515,7 @@ void CriticalSection_Enter(CCriticalSection *p)
void CriticalSection_Leave(CCriticalSection *p)
{
- // Print("CriticalSection_Leave");
+ // Print("CriticalSection_Leave")
if (p)
{
// int ret =
@@ -517,7 +525,7 @@ void CriticalSection_Leave(CCriticalSection *p)
void CriticalSection_Delete(CCriticalSection *p)
{
- // Print("CriticalSection_Delete");
+ // Print("CriticalSection_Delete")
if (p)
{
// int ret =
@@ -527,14 +535,28 @@ void CriticalSection_Delete(CCriticalSection *p)
LONG InterlockedIncrement(LONG volatile *addend)
{
- // Print("InterlockedIncrement");
+ // Print("InterlockedIncrement")
#ifdef USE_HACK_UNSAFE_ATOMIC
LONG val = *addend + 1;
*addend = val;
return val;
#else
+
+ #if defined(__clang__) && (__clang_major__ >= 8)
+ #pragma GCC diagnostic ignored "-Watomic-implicit-seq-cst"
+ #endif
return __sync_add_and_fetch(addend, 1);
#endif
}
#endif // _WIN32
+
+WRes AutoResetEvent_OptCreate_And_Reset(CAutoResetEvent *p)
+{
+ if (Event_IsCreated(p))
+ return Event_Reset(p);
+ return AutoResetEvent_CreateNotSignaled(p);
+}
+
+#undef PRF
+#undef Print