Skip to content

Commit 9e52bca

Browse files
committed
os/posix: port of the posix implementation to macOS
1 parent 64ad0f5 commit 9e52bca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2518
-33
lines changed

CMakeLists.txt

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,30 @@
11
cmake_minimum_required(VERSION 2.6.4)
22
project(OSAL C)
33

4+
# TODO-MAC:
5+
# This enforces the cwd() to be in the ${CMAKE_BINARY_DIR}. This needed for
6+
# deterministic calculation of full path to MODULE*.dylib libraries when running
7+
# from CLion.
8+
# https://stackoverflow.com/a/38166227/598057
9+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
10+
11+
# TODO/MAC: Quick fix to resolve the error:
12+
# In file included from /sandbox/cFS/osal/src/bsp/pc-linux/ut-src/bsp_ut_voltab.c:21:
13+
#/sandbox/cFS/osal/src/os/inc/osapi.h:89:10: fatal error: 'osconfig.h' file not found
14+
##include "osconfig.h"
15+
# ^~~~~~~~~~~~
16+
include_directories(
17+
src/bsp/pc-linux/config
18+
)
19+
20+
# TODO-MAC: Defining this globally but can be made more focused.
21+
# In file included from /sandbox/cFS/osal/src/os/posix/osloader.c:32:
22+
#/sandbox/cFS/osal/src/os/posix/../portable/os-impl-posix-dl.c:130:30: error: use of undeclared identifier 'RTLD_DEFAULT'
23+
# Function = dlsym((void *)RTLD_DEFAULT, SymbolName);
24+
# ^
25+
# 1 error generated.
26+
add_definitions("-D_DARWIN_C_SOURCE")
27+
428
enable_testing()
529

630
# Generic function for consistent definition of a unit testing target
@@ -16,7 +40,10 @@ function(add_osal_ut_exe TGTNAME)
1640
# It is not an issue for UT builds that use OSAL stubs or no OSAL at all.
1741
set_target_properties(${TGTNAME} PROPERTIES COMPILE_DEFINITIONS "_UNIT_TEST_")
1842
set_target_properties(${TGTNAME} PROPERTIES COMPILE_FLAGS "${UT_C_FLAGS}")
19-
set_target_properties(${TGTNAME} PROPERTIES LINK_FLAGS "${UT_C_FLAGS} -u OS_VolumeTable -u OS_Application_Startup")
43+
44+
# TODO-MAC
45+
# set_target_properties(${TGTNAME} PROPERTIES LINK_FLAGS "${UT_C_FLAGS} -u OS_VolumeTable -u OS_Application_Startup")
46+
2047
target_link_libraries(${TGTNAME} ut_assert osal)
2148
add_test(${TGTNAME} ${TGTNAME})
2249
foreach(TGT ${INSTALL_TARGET_LIST})
@@ -152,8 +179,10 @@ if (OSAL_SELECTED_BSPTYPE)
152179
aux_source_directory(src/bsp/${OSAL_SELECTED_BSPTYPE}/src BSPFILES)
153180
endif (OSAL_SELECTED_BSPTYPE)
154181

182+
# TODO-MAC
183+
add_subdirectory(posix-mac-addons)
155184
add_library(osal STATIC ${OSALFILES} ${BSPFILES})
156-
target_link_libraries(osal ${OSAL_LINK_LIBS})
185+
target_link_libraries(osal ${OSAL_LINK_LIBS} posix-mac-addons)
157186

158187
# Determine if this build is standalone or part of a larger build
159188
# If this is part of a larger build, certain key values will be exported to the parent

Makefile

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
3+
test: test.unit test.integration
4+
5+
test.unit: build.cmake
6+
cd build.commandline.dir && ./osal_core_UT
7+
cd build.commandline.dir && ./osal_file_UT
8+
cd build.commandline.dir && ./osal_filesys_UT
9+
cd build.commandline.dir && ./osal_loader_UT
10+
cd build.commandline.dir && ./osal_timer_UT
11+
cd build.commandline.dir && ./osal_network_UT
12+
13+
test.integration: build.cmake
14+
cd build.commandline.dir && ./bin-sem-flush-test
15+
cd build.commandline.dir && ./bin-sem-test
16+
cd build.commandline.dir && ./bin-sem-timeout-test
17+
cd build.commandline.dir && ./count-sem-test
18+
cd build.commandline.dir && ./file-api-test
19+
cd build.commandline.dir && ./mutex-test
20+
cd build.commandline.dir && ./osal-core-test
21+
cd build.commandline.dir && ./queue-timeout-test
22+
cd build.commandline.dir && ./sem-speed-test
23+
cd build.commandline.dir && ./symbol-api-test
24+
cd build.commandline.dir && ./timer-test
25+
26+
build.cmake:
27+
mkdir -p build.commandline.dir
28+
cd build.commandline.dir && cmake -G Ninja \
29+
-DENABLE_UNIT_TESTS=1 \
30+
-DOSAL_SYSTEM_OSTYPE=posix \
31+
-DOSAL_SYSTEM_BSPTYPE=pc-linux \
32+
..
33+
cd build.commandline.dir && ninja
34+

posix-mac-addons/CMakeLists.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
3+
project(POSIX-Mac-Addons C)
4+
5+
set (CMAKE_CXX_STANDARD 11)
6+
7+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
8+
option(NO_FLAGS "Disable usual compilation flags" OFF)
9+
if(NOT NO_FLAGS)
10+
set(CMAKE_C_FLAGS "-Werror -Wall -Wunused -Wpedantic -fsanitize=address")
11+
set(CMAKE_CXX_FLAGS "-Werror -Wall -Wunused -Wpedantic -fsanitize=address")
12+
set(CMAKE_LINKER_FLAGS_DEBUG "-fsanitize=address")
13+
else()
14+
message("NO_FLAGS is provided: skipping strict compilation flags.")
15+
endif()
16+
endif()
17+
18+
add_subdirectory(src)
19+
20+
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
21+
add_subdirectory(tests)
22+
endif()

posix-mac-addons/src/CMakeLists.txt

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
add_subdirectory(mqueue)
2+
add_subdirectory(pthread)
3+
add_subdirectory(semaphore)
4+
add_subdirectory(time)
5+
add_subdirectory(timer)
6+
add_subdirectory(stubs)
7+
8+
add_library(posix-mac-addons INTERFACE)
9+
10+
target_link_libraries(posix-mac-addons
11+
INTERFACE
12+
mqueue
13+
posix-mac-pthread
14+
posix-mac-semaphore
15+
posix-mac-stubs
16+
posix-mac-time
17+
posix-mac-timer
18+
rt
19+
)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
set(SOURCES
2+
src/mq_close.c
3+
src/mq_getattr.c
4+
src/mq_notify.c
5+
src/mq_open.c
6+
src/mq_receive.c
7+
src/mq_send.c
8+
src/mq_setattr.c
9+
src/mq_timedreceive.c
10+
src/mq_timedsend.c
11+
src/mq_unlink.c
12+
src/error.c
13+
)
14+
15+
add_library(mqueue ${SOURCES})
16+
target_include_directories(mqueue PUBLIC include include/mqueue)
17+
target_link_libraries(mqueue rt)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <pthread.h>
2+
#include <signal.h>
3+
#include <unistd.h>
4+
5+
#ifdef __cplusplus
6+
extern "C"{
7+
#endif
8+
9+
typedef struct mq_info *mqd_t; /* opaque datatype */
10+
11+
struct mq_attr {
12+
long mq_flags; /* message queue flag: O_NONBLOCK */
13+
long mq_maxmsg; /* max number of messages allowed on queue */
14+
long mq_msgsize; /* max size of a message (in bytes) */
15+
long mq_curmsgs; /* number of messages currently on queue */
16+
};
17+
18+
/* 4one mq_hdr{} per queue, at beginning of mapped file */
19+
struct mq_hdr {
20+
struct mq_attr mqh_attr; /* the queue's attributes */
21+
long mqh_head; /* index of first message */
22+
long mqh_free; /* index of first free message */
23+
long mqh_nwait; /* #threads blocked in mq_receive() */
24+
pid_t mqh_pid; /* nonzero PID if mqh_event set */
25+
struct sigevent mqh_event; /* for mq_notify() */
26+
pthread_mutex_t mqh_lock; /* mutex lock */
27+
pthread_cond_t mqh_wait; /* and condition variable */
28+
};
29+
30+
/* 4one mymsg_hdr{} at the front of each message in the mapped file */
31+
struct mymsg_hdr {
32+
long msg_next; /* index of next on linked list */
33+
/* 4msg_next must be first member in struct */
34+
ssize_t msg_len; /* actual length */
35+
unsigned int msg_prio; /* priority */
36+
};
37+
38+
/* 4one mq_info{} malloc'ed per process per mq_open() */
39+
struct mq_info {
40+
struct mq_hdr *mqi_hdr; /* start of mmap'ed region */
41+
long mqi_magic; /* magic number if open */
42+
int mqi_flags; /* flags for this process */
43+
};
44+
#define MQI_MAGIC 0x98765432
45+
46+
/* 4size of message in file is rounded up for alignment */
47+
#define MSGSIZE(i) ((((i) + sizeof(long)-1) / sizeof(long)) * sizeof(long))
48+
/* end mqueueh */
49+
50+
/* 4our functions */
51+
int mq_close(mqd_t);
52+
int mq_getattr(mqd_t, struct mq_attr *);
53+
int mq_notify(mqd_t, const struct sigevent *);
54+
mqd_t mq_open(const char *, int, ...);
55+
ssize_t mq_receive(mqd_t, char *, size_t, unsigned int *);
56+
int mq_send(mqd_t, const char *, size_t, unsigned int);
57+
int mq_setattr(mqd_t, const struct mq_attr *, struct mq_attr *);
58+
int mq_unlink(const char *name);
59+
int mq_timedsend(mqd_t mqdes, const char *msg_ptr,
60+
size_t msg_len, unsigned msg_prio,
61+
const struct timespec *abs_timeout);
62+
ssize_t mq_timedreceive(mqd_t mqdes, char *msg_ptr,
63+
size_t msg_len, unsigned *msg_prio,
64+
const struct timespec *abs_timeout);
65+
66+
/* 4and the corresponding wrapper functions */
67+
void Mymq_close(mqd_t);
68+
void Mymq_getattr(mqd_t, struct mq_attr *);
69+
void Mymq_notify(mqd_t, const struct sigevent *);
70+
ssize_t Mymq_receive(mqd_t, char *, size_t, unsigned int *);
71+
void Mymq_send(mqd_t, const char *, size_t, unsigned int);
72+
void Mymq_setattr(mqd_t, const struct mq_attr *, struct mq_attr *);
73+
void Mymq_unlink(const char *name);
74+
75+
#ifdef __cplusplus
76+
}
77+
#endif

0 commit comments

Comments
 (0)