Skip to content
Open
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
3 changes: 3 additions & 0 deletions fmus/gain/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ project(${FMU_NAME})
if(WIN32)
set(FMU_PLATFORM win)
set(FMU_SO_SUFFIX ".dll")
elseif(APPLE)
set(FMU_PLATFORM darwin)
set(FMU_SO_SUFFIX ".dylib")
elseif(UNIX)
set(FMU_PLATFORM linux)
set(FMU_SO_SUFFIX ".so")
Expand Down
3 changes: 3 additions & 0 deletions fmus/sinusGenerator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ project(${FMU_NAME})
if(WIN32)
set(FMU_PLATFORM win)
set(FMU_SO_SUFFIX ".dll")
elseif(APPLE)
set(FMU_PLATFORM darwin)
set(FMU_SO_SUFFIX ".dylib")
elseif(UNIX)
set(FMU_PLATFORM linux)
set(FMU_SO_SUFFIX ".so")
Expand Down
3 changes: 3 additions & 0 deletions fmus/vectorSum/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ project(${FMU_NAME})
if(WIN32)
set(FMU_PLATFORM win)
set(FMU_SO_SUFFIX ".dll")
elseif(APPLE)
set(FMU_PLATFORM darwin)
set(FMU_SO_SUFFIX ".dylib")
elseif(UNIX)
set(FMU_PLATFORM linux)
set(FMU_SO_SUFFIX ".so")
Expand Down
8 changes: 4 additions & 4 deletions libs/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ if(WIN32)
)
else()
set(UTIL_PLATFORM_SOURCES
"${CMAKE_CURRENT_SOURCE_DIR}/src/linux/libs.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/linux/os.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/linux/paths.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/linux/string.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/unix/libs.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/unix/os.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/unix/paths.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/unix/string.c"
)
endif()

Expand Down
11 changes: 9 additions & 2 deletions libs/util/src/linux/libs.c → libs/util/src/unix/libs.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ static McxStatus __mcx_dll_load(DllHandle * handle, const char * dllPath, int fl
return RETURN_OK;
}


McxStatus mcx_dll_load(DllHandle * handle, const char * dllPath) {
#if(__APPLE__)
return __mcx_dll_load(handle, dllPath, RTLD_LAZY|RTLD_LOCAL);
#else
return __mcx_dll_load(handle, dllPath, RTLD_LAZY|RTLD_LOCAL|RTLD_DEEPBIND);
#endif
}

McxStatus mcx_dll_load_global(DllHandle * handle, const char * dllPath) {
#if(__APPLE__)
return __mcx_dll_load(handle, dllPath, RTLD_LAZY|RTLD_GLOBAL);
#else
return __mcx_dll_load(handle, dllPath, RTLD_LAZY|RTLD_GLOBAL|RTLD_DEEPBIND);
#endif
}

void * mcx_dll_get_function(DllHandle dllHandle, const char* functionName) {
Expand All @@ -55,4 +62,4 @@ void mcx_dll_free(DllHandle dllHandle)

#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif /* __cplusplus */
#endif /* __cplusplus */
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion mcx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ target_link_libraries(${EXECUTABLE_NAME} PRIVATE mcx_common)

source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}/" PREFIX "Source Files" FILES ${MCX_EXE_SOURCES})

if(UNIX)
if(UNIX AND NOT APPLE)
set_target_properties(${EXECUTABLE_NAME} PROPERTIES LINK_OPTIONS -Wl,--exclude-libs,ALL)
endif()

Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ file(GLOB UTIL_HEADERS "util/*.h")
if(WIN32)
file(GLOB UTIL_SRC "util/win/*.c" "util/*.c" "util/common/*.c")
else()
file(GLOB UTIL_SRC "util/linux/*.c" "util/*.c" "util/common/*.c")
file(GLOB UTIL_SRC "util/unix/*.c" "util/*.c" "util/common/*.c")
endif()


Expand Down Expand Up @@ -94,7 +94,7 @@ target_compile_definitions(
)


if(UNIX)
if(UNIX AND NOT APPLE)
set_target_properties(mcx_common PROPERTIES LINK_OPTIONS -Wl,--exclude-libs,ALL)
endif()

10 changes: 8 additions & 2 deletions src/objects/ObjectContainer.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ typedef struct {
void * arg;
} StrCmpCtx;

static int ObjectContainerElementCmp(const void * first, const void * second, void * ctx) {
StrCmpCtx * data = (StrCmpCtx *)ctx;
#if(__APPLE__)
static int ObjectContainerElementCmp(void *ctx, const void *first,
const void *second) {
#else
static int ObjectContainerElementCmp(const void * first, const void * second,
void * ctx) {
#endif
StrCmpCtx *data = (StrCmpCtx *)ctx;

ObjectContainerElement * firstElement = (ObjectContainerElement *) first;
ObjectContainerElement * secondElement = (ObjectContainerElement *) second;
Expand Down
29 changes: 0 additions & 29 deletions src/util/linux/stdlib.c

This file was deleted.

5 changes: 4 additions & 1 deletion src/util/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
#ifndef MCX_UTIL_MUTEX_H
#define MCX_UTIL_MUTEX_H

#include "CentralParts.h"
#if(__APPLE__)
#include "sys/types.h"
#endif

#include "CentralParts.h"
#if defined (ENABLE_MT)

#ifdef __cplusplus
Expand Down
8 changes: 7 additions & 1 deletion src/util/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ extern "C" {
* is the context passed to mcx_sort.
* @param arg context for the compar function
*/
void mcx_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg);

#if(__APPLE__)
void mcx_sort(void *base, size_t nmemb, size_t size,
int (*compar)(void *, const void *, const void *), void *arg);
#else
Copy link
Contributor

@klausschuch klausschuch Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange that the arg argument comes first in the apple implementation. We should add a comment to the source code to point this out.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment 👍

void mcx_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *, void *), void *arg);
#endif
int mcx_natural_sort_cmp(const char * left, const char * right);

/**
Expand Down
4 changes: 4 additions & 0 deletions src/util/threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ extern "C" {

#include <stdint.h>

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

#if defined(OS_WINDOWS)
#define _WINSOCKAPI_ // stops windows.h including winsock.h
#include <windows.h>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions src/util/unix/stdlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/********************************************************************************
* Copyright (c) 2020 AVL List GmbH and others
*
* This program and the accompanying materials are made available under the
* terms of the Apache Software License 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

#include "CentralParts.h"

#include "util/stdlib.h"

#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */


#if (__APPLE__)
void mcx_sort(void *base, size_t nmemb, size_t size,
int (*compar)(void *, const void *, const void *), void *arg) {
// FreeBSD added qsort_r in Sept 2002, but with a poor interface. In
// 2008 GNU fixed the interface and decided the compatibility breakage
// was worth it following ISO. Linux is GNU. OS X is a mish-mash of many
// things, but for C it follows BSD. As a result, we have difference in
// the non-standard qsort_r function.
// See https://stackoverflow.com/a/39561369 for details.
qsort_r(base, nmemb, size, arg, compar);
}
#else
void mcx_sort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *, void *), void *arg) {
qsort_r(base, nmemb, size, compar, arg);
}
#endif


#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif /* __cplusplus */
64 changes: 62 additions & 2 deletions src/util/linux/threads.c → src/util/unix/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,66 @@ int mcx_thread_join(McxThread handle, long * ret) {
return pthread_join(handle, (void * *) ret);
}

struct args {
int joined;
pthread_t td;
pthread_mutex_t mtx;
pthread_cond_t cond;
void **res;
};

static void *waiter(void *ap)
{
struct args *args = ap;
pthread_join(args->td, args->res);
pthread_mutex_lock(&args->mtx);
args->joined = 1;
pthread_mutex_unlock(&args->mtx);
pthread_cond_signal(&args->cond);
return 0;
}

// Portable implementation of `pthread_timedjoin_np()`. Inspired
// and copied from https://stackoverflow.com/a/11552244. As this
// implementation is more costly than `pthread_timedjoin_np()`,
// we only use this portable version for Apple platforms.
int pthread_timedjoin_p(pthread_t td, void **res, struct timespec *ts)
{
pthread_t tmp;
int ret;
struct args args = { .td = td, .res = res };

pthread_mutex_init(&args.mtx, 0);
pthread_cond_init(&args.cond, 0);
pthread_mutex_lock(&args.mtx);

ret = pthread_create(&tmp, 0, waiter, &args);
if (!ret) {
do {
ret = pthread_cond_timedwait(&args.cond, &args.mtx, ts);
} while (!args.joined && ret != ETIMEDOUT);
}

pthread_mutex_unlock(&args.mtx);

pthread_cancel(tmp);
pthread_join(tmp, 0);

pthread_cond_destroy(&args.cond);
pthread_mutex_destroy(&args.mtx);

return args.joined ? 0 : ret;
}

int pthread_timedjoin(pthread_t handle, void **ret, struct timespec *time)
{
#if(__APPLE__)
return pthread_timedjoin_p(handle, ret, time);
#else
return pthread_timedjoin_np(handle, ret, time);
#endif
}

int mcx_thread_join_with_timeout(McxThread handle, long * ret, int secs) {
struct timespec time;
int status = 0;
Expand All @@ -53,7 +113,7 @@ int mcx_thread_join_with_timeout(McxThread handle, long * ret, int secs) {

time.tv_sec += secs;

status = pthread_timedjoin_np(handle, (void * *) ret, &time);
status = pthread_timedjoin(handle, (void * *) ret, &time);
if (status) {
const char * cause = NULL;

Expand All @@ -79,4 +139,4 @@ int mcx_thread_join_with_timeout(McxThread handle, long * ret, int secs) {
} /* closing brace for extern "C" */
#endif /* __cplusplus */

#endif // ENABLE_MT
#endif // ENABLE_MT
File renamed without changes.