Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import macOS fixes from blaztinn/macos #33

Merged
merged 7 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.1)

set(CMAKE_CXX_STANDARD 11)

set(CRUNCH_PROJECT_NAME crunch)
set(CRUNCH_LIBRARY_NAME crn)
set(CRUNCH_EXE_NAME crunch)
Expand Down
2 changes: 1 addition & 1 deletion crnlib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ COMPILE_CPU_OPTIONS = -march=core2
COMPILE_DEBUG_OPTIONS = -g
COMPILE_OPTIMIZATION_OPTIONS = -O3 -fomit-frame-pointer -ffast-math -fno-math-errno -fno-strict-aliasing
COMPILE_WARN_OPTIONS = -Wall -Wno-unused-value -Wno-unused
COMPILE_OPTIONS = $(COMPILE_CPU_OPTIONS) $(COMPILE_DEBUG_OPTIONS) $(COMPILE_OPTIMIZATION_OPTIONS) $(COMPILE_WARN_OPTIONS)
COMPILE_OPTIONS = -std=c++11 $(COMPILE_CPU_OPTIONS) $(COMPILE_DEBUG_OPTIONS) $(COMPILE_OPTIMIZATION_OPTIONS) $(COMPILE_WARN_OPTIONS)

LINKER_OPTIONS = -lpthread -g

Expand Down
5 changes: 3 additions & 2 deletions crnlib/crn_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#ifdef WIN32
#include <tchar.h>
#include <conio.h>
#elif defined(__GNUC__)
#include <termios.h>
#include <unistd.h>
#endif
namespace crnlib {
class dynamic_string;
Expand Down Expand Up @@ -99,8 +102,6 @@ inline int crn_getch() {
return _getch();
}
#elif defined(__GNUC__)
#include <termios.h>
#include <unistd.h>
inline int crn_getch() {
struct termios oldt, newt;
int ch;
Expand Down
4 changes: 4 additions & 0 deletions crnlib/crn_jpge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

#include <stdlib.h>
#include <string.h>
#if !defined(__APPLE__)
#include <malloc.h>
#else
#include <malloc/malloc.h>
#endif

#include "crn_core.h"

Expand Down
8 changes: 7 additions & 1 deletion crnlib/crn_mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
#include "crn_core.h"
#include "crn_console.h"
#include "../inc/crnlib.h"
#if !defined(__APPLE__)
#include <malloc.h>
#else
#include <malloc/malloc.h>
#endif
#if CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#endif

#define CRNLIB_MEM_STATS 0

#if !CRNLIB_USE_WIN32_API
#if defined(__APPLE__)
#define _msize malloc_size
#elif !CRNLIB_USE_WIN32_API
#define _msize malloc_usable_size
#endif

Expand Down
2 changes: 1 addition & 1 deletion crnlib/crn_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const bool c_crnlib_little_endian_platform = false;

const bool c_crnlib_big_endian_platform = !c_crnlib_little_endian_platform;

#ifdef __GNUC__
#if defined(__GNUC__) && !defined(__APPLE__)
#define crn_fopen(pDstFile, f, m) *(pDstFile) = fopen64(f, m)
#define crn_fseek fseeko64
#define crn_ftell ftello64
Expand Down
74 changes: 61 additions & 13 deletions crnlib/crn_threading_pthreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
#include "crn_winhdr.h"
#endif

#ifdef __GNUC__
#if defined(__GNUC__) && !defined(__APPLE__)
#include <sys/sysinfo.h>
#endif

#ifdef WIN32
#include <process.h>
#endif

#if defined(__APPLE__)
#include <sys/sysctl.h>
#endif

namespace crnlib {
uint g_number_of_processors = 1;

Expand All @@ -27,16 +31,19 @@ void crn_threading_init() {
SYSTEM_INFO g_system_info;
GetSystemInfo(&g_system_info);
g_number_of_processors = math::maximum<uint>(1U, g_system_info.dwNumberOfProcessors);
#elif defined(__APPLE__)
g_number_of_processors = math::maximum<int>(1, sysconf(_SC_NPROCESSORS_ONLN));
#elif defined(__GNUC__)
g_number_of_processors = math::maximum<int>(1, get_nprocs());
#else
g_number_of_processors = 1;
#endif
g_number_of_processors = math::minimum<int>(g_number_of_processors, task_pool::cMaxThreads);
}

crn_thread_id_t crn_get_current_thread_id() {
// FIXME: Not portable
return static_cast<crn_thread_id_t>(pthread_self());
return (crn_thread_id_t)(pthread_self());
}

void crn_sleep(unsigned int milliseconds) {
Expand Down Expand Up @@ -95,15 +102,35 @@ void mutex::set_spin_count(unsigned int count) {
}

semaphore::semaphore(long initialCount, long maximumCount, const char* pName) {
maximumCount, pName;
CRNLIB_ASSERT(maximumCount >= initialCount);
if (sem_init(&m_sem, 0, initialCount)) {
#if !defined(__APPLE__)
maximumCount, pName;
m_sem = new sem_t();
if (sem_init(m_sem, 0, initialCount)) {
CRNLIB_FAIL("semaphore: sem_init() failed");
}
#else
m_name = pName ? pName : "semaphore";
for(int i = 0; i < 256; i++) {
m_sem = sem_open(m_name, O_CREAT | O_EXCL, 0644, initialCount);
if (m_sem != SEM_FAILED)
{
break;
}
sem_unlink(m_name);
}
if (m_sem == SEM_FAILED)
{
CRNLIB_FAIL("semaphore: sem_open() failed");
}
#endif
}

semaphore::~semaphore() {
sem_destroy(&m_sem);
sem_destroy(m_sem);
#if defined(__APPLE__)
sem_unlink(m_name);
#endif
}

void semaphore::release(long releaseCount) {
Expand All @@ -112,12 +139,12 @@ void semaphore::release(long releaseCount) {
int status = 0;
#ifdef WIN32
if (1 == releaseCount)
status = sem_post(&m_sem);
status = sem_post(m_sem);
else
status = sem_post_multiple(&m_sem, releaseCount);
status = sem_post_multiple(m_sem, releaseCount);
#else
while (releaseCount > 0) {
status = sem_post(&m_sem);
status = sem_post(m_sem);
if (status)
break;
releaseCount--;
Expand All @@ -134,12 +161,12 @@ void semaphore::try_release(long releaseCount) {

#ifdef WIN32
if (1 == releaseCount)
sem_post(&m_sem);
sem_post(m_sem);
else
sem_post_multiple(&m_sem, releaseCount);
sem_post_multiple(m_sem, releaseCount);
#else
while (releaseCount > 0) {
sem_post(&m_sem);
sem_post(m_sem);
releaseCount--;
}
#endif
Expand All @@ -148,12 +175,16 @@ void semaphore::try_release(long releaseCount) {
bool semaphore::wait(uint32 milliseconds) {
int status;
if (milliseconds == cUINT32_MAX) {
status = sem_wait(&m_sem);
status = sem_wait(m_sem);
} else {
#if !defined(__APPLE__)
struct timespec interval;
interval.tv_sec = milliseconds / 1000;
interval.tv_nsec = (milliseconds % 1000) * 1000000L;
status = sem_timedwait(&m_sem, &interval);
status = sem_timedwait(m_sem, &interval);
#else
status = sem_wait(m_sem);
#endif
}

if (status) {
Expand All @@ -167,25 +198,42 @@ bool semaphore::wait(uint32 milliseconds) {
}

spinlock::spinlock() {
#if !defined(__APPLE__)
if (pthread_spin_init(&m_spinlock, 0)) {
CRNLIB_FAIL("spinlock: pthread_spin_init() failed");
}
#else
m_lock = new os_unfair_lock();
*m_lock = OS_UNFAIR_LOCK_INIT;
#endif
}

spinlock::~spinlock() {
#if !defined(__APPLE__)
pthread_spin_destroy(&m_spinlock);
#else
delete m_lock;
#endif
}

void spinlock::lock() {
#if !defined(__APPLE__)
if (pthread_spin_lock(&m_spinlock)) {
CRNLIB_FAIL("spinlock: pthread_spin_lock() failed");
}
#else
os_unfair_lock_lock(m_lock);
#endif
}

void spinlock::unlock() {
#if !defined(__APPLE__)
if (pthread_spin_unlock(&m_spinlock)) {
CRNLIB_FAIL("spinlock: pthread_spin_unlock() failed");
}
#else
os_unfair_lock_unlock(m_lock);
#endif
}

task_pool::task_pool()
Expand Down
13 changes: 12 additions & 1 deletion crnlib/crn_threading_pthreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include <semaphore.h>
#include <unistd.h>

#if defined(__APPLE__)
#include <os/lock.h>
#endif

namespace crnlib {
// g_number_of_processors defaults to 1. Will be higher on multicore machines.
extern uint g_number_of_processors;
Expand Down Expand Up @@ -71,7 +75,10 @@ class semaphore {
bool wait(uint32 milliseconds = cUINT32_MAX);

private:
sem_t m_sem;
sem_t* m_sem;
#if defined(__APPLE__)
const char* m_name;
#endif
};

class spinlock {
Expand All @@ -83,7 +90,11 @@ class spinlock {
void unlock();

private:
#if !defined(__APPLE__)
pthread_spinlock_t m_spinlock;
#else
os_unfair_lock_t m_lock;
#endif
};

class scoped_spinlock {
Expand Down
1 change: 1 addition & 0 deletions crnlib/crn_threading_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void crn_threading_init() {
GetSystemInfo(&g_system_info);

g_number_of_processors = math::maximum<uint>(1U, g_system_info.dwNumberOfProcessors);
g_number_of_processors = math::minimum<int>(g_number_of_processors, task_pool::cMaxThreads);
}

crn_thread_id_t crn_get_current_thread_id() {
Expand Down
Loading