1
0
mirror of synced 2025-11-06 03:20:55 +08:00

修复多平台编译问题

This commit is contained in:
Albert
2023-09-12 11:47:30 +08:00
parent 04a13eee38
commit 10409330f3
2 changed files with 18 additions and 12 deletions

View File

@@ -10,7 +10,7 @@
extern int ncpu;
extern int spin;
void spin_lock(atomic_t* lock, uint32_t pid)
void spin_lock(atomic_t *lock, uint32_t pid)
{
int i, n;
@@ -22,8 +22,8 @@ void spin_lock(atomic_t* lock, uint32_t pid)
InterlockedCompareExchange(lock, pid, 0) == 0
#else
__sync_bool_compare_and_swap(lock, 0, pid)
#endif
)
#endif
)
{
return;
}
@@ -36,11 +36,7 @@ void spin_lock(atomic_t* lock, uint32_t pid)
for (i = 0; i < n; i++)
{
#ifdef WIN32
MemoryBarrier();
#else
__asm("pause");
#endif
atomic_cpu_pause();
}
if (*lock == 0 &&
@@ -48,8 +44,8 @@ void spin_lock(atomic_t* lock, uint32_t pid)
InterlockedCompareExchange(lock, pid, 0) == 0
#else
__sync_bool_compare_and_swap(lock, 0, pid)
#endif
)
#endif
)
{
return;
}
@@ -63,11 +59,11 @@ void spin_lock(atomic_t* lock, uint32_t pid)
}
}
void spin_unlock(atomic_t* lock, uint32_t pid)
void spin_unlock(atomic_t *lock, uint32_t pid)
{
#ifdef WIN32
InterlockedCompareExchange(lock, 0, pid);
#else
__sync_bool_compare_and_swap(lock, pid, 0);
#endif
#endif
}

View File

@@ -8,4 +8,14 @@ extern void spin_lock(atomic_t *lock, uint32_t pid);
extern void spin_unlock(atomic_t *lock, uint32_t pid);
#if defined(WIN32)
#define atomic_cpu_pause() MemoryBarrier();
#elif defined(__x86_64__)
#define atomic_cpu_pause() __asm__ __volatile__("pause")
#elif defined(__aarch64__)
#define atomic_cpu_pause() __asm__ __volatile__("yield")
#else
#define atomic_cpu_pause()
#endif
#endif