Skip to content

Commit

Permalink
fix windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
artpaul committed Apr 14, 2021
1 parent 213f340 commit 99adab2
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 396 deletions.
29 changes: 17 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,7 @@ cmake_minimum_required (VERSION 3.1)
project (acto LANGUAGES CXX)
find_package (Threads REQUIRED)

if (UNIX)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -Wall -Werror")

link_libraries (pthread)
elseif (WIN32)
#
endif ()

# Library sources.
add_subdirectory (lib)
option (ACTO_BUILD_SAMPLES "Set to ON to build samples" OFF)

# Determine whether this is a standalone project or
# included by other projects.
Expand All @@ -21,8 +12,22 @@ project (acto LANGUAGES CXX)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
set (CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wno-sign-conversion")
if (MSVC)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W3")
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wshadow -Wsign-compare -Wshadow -Wwrite-strings -Wpointer-arith -Winit-self -Wconversion -Wno-sign-conversion")
endif ()

set (ACTO_BUILD_SAMPLES ON)
elseif (NOT CMAKE_CXX_STANDARD)
set (CMAKE_CXX_STANDARD 14)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
endif ()

# Library sources.
add_subdirectory (lib)

# Build samples.
# Build samples.
if (ACTO_BUILD_SAMPLES)
add_subdirectory (samples)
endif ()
6 changes: 6 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ target_sources (acto-lib PRIVATE
"runtime.cpp"
"worker.cpp"
)

if (UNIX)
target_link_libraries (acto-lib
pthread
)
endif ()
185 changes: 70 additions & 115 deletions lib/platform.h
Original file line number Diff line number Diff line change
@@ -1,149 +1,104 @@
#pragma once

#if !defined (_WIN64) && !defined (_WIN32)
# define ACTO_UNIX

# if defined (__linux__)
# define ACTO_LINUX
# else
# error "Undefined target platform"
# endif
#if defined (__linux__)
# define ACTO_LINUX
# define ACTO_UNIX
#elif defined (__APPLE__)
# define ACTO_DARWIN
# define ACTO_UNIX
#elif defined (_WIN64)
# define ACTO_WIN
# define ACTO_WIN64
#else
# define ACTO_WIN

# if defined (_WIN64)
# define ACTO_WIN64
# elif defined (_WIN32)
# define ACTO_WIN32
# else
# error "Undefined windows platform"
# endif
# error "Unsupported target platform"
#endif


///////////////////////////////////////////////////////////////////////////////
// НАСТРОЙКА БИБЛИОТЕКИ ПОД ТЕКУЩЙИ КОМПИЛЯТОР //
///////////////////////////////////////////////////////////////////////////////


// Используется компилятор Microsoft, либо совместимый
#if defined (_MSC_VER)
# if !defined (_MT)
# error "Multithreaded mode not defined. Use /MD or /MT compiler options."
# endif

# if !defined _MT
# error "Multithreaded mode not defined. Use /MD or /MT compiler options."
# endif

// Если текущий режим - отладочный
# if defined (_DEBUG)
# if !defined (DEBUG)
# define ACTO_DEBUG 1
# endif
# endif

// Директивы экспорта для сборки DLL
# ifdef ACTO_EXPORT
# define ACTO_API __declspec( dllexport )
#elif ACTO_IMPORT
# define ACTO_API __declspec( dllimport )
# else
# define ACTO_API
# endif

#else
# ifdef ACTO_EXPORT
# define ACTO_API __declspec( dllexport )
# elif ACTO_IMPORT
# define ACTO_API __declspec( dllimport )
# else
# define ACTO_API
# endif
#else
# define ACTO_API
#endif


///////////////////////////////////////////////////////////////////////////////
// НАСТРОЙКА БИБЛИОТЕКИ ПОД ЦЕЛЕВУЮ ПЛАТФОРМУ //
///////////////////////////////////////////////////////////////////////////////

#if defined ( ACTO_WIN )
#if defined (ACTO_WIN)
# include <windows.h>

# if !defined (_MSC_VER)
# error "Unknown windows compiler"
# endif
/* Basic signed types. */
typedef __int8 i8;
typedef __int16 i16;
typedef __int32 i32;
typedef __int64 i64;

# if !defined ( _WIN32_WINNT )
// Целевая платформа Windows XP или выше
# define _WIN32_WINNT 0x0501
# endif
// -
# include <windows.h>
/* Basic unsigned types. */
typedef unsigned __int8 ui8;
typedef unsigned __int16 ui16;
typedef unsigned __int32 ui32;
typedef unsigned __int64 ui64;

/* Целые со знаком */
typedef __int8 i8;
typedef __int16 i16;
typedef __int32 i32;
typedef __int64 i64;

/* Беззнаковые целые */
typedef unsigned __int8 ui8;
typedef unsigned __int16 ui16;
typedef unsigned __int32 ui32;
typedef unsigned __int64 ui64;

namespace acto {

namespace core {
/// Количетсов физически процессоров (ядер) в системе
namespace acto {
namespace core {
inline unsigned long NumberOfProcessors() {
SYSTEM_INFO si;
// -
::GetSystemInfo( &si );
// -
return si.dwNumberOfProcessors;
SYSTEM_INFO si;
::GetSystemInfo(&si);
return si.dwNumberOfProcessors;
}

inline void sleep(unsigned int milliseconds) {
::Sleep( milliseconds );
::Sleep( milliseconds );
}

inline void yield() {
::SwitchToThread();
::SwitchToThread();
}
}

}

#elif defined (ACTO_LINUX)

# include <pthread.h>
# include <stdint.h>
# include <unistd.h>
# include <sched.h>

/* Целые со знаком */
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;

/* Беззнаковые целые */
typedef uint8_t ui8;
typedef uint16_t ui16;
typedef uint32_t ui32;
typedef uint64_t ui64;

namespace acto {

namespace core {
/// Количетсов физически процессоров (ядер) в системе
} // namespace core
} // namespace acto

#elif defined (ACTO_UNIX)

# include <pthread.h>
# include <stdint.h>
# include <unistd.h>
# include <sched.h>

/* Basic signed types. */
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;

/* Basic unsigned types. */
typedef uint8_t ui8;
typedef uint16_t ui16;
typedef uint32_t ui32;
typedef uint64_t ui64;

namespace acto {
namespace core {
inline unsigned long NumberOfProcessors() {
static long n_cpu = ::sysconf(_SC_NPROCESSORS_CONF);

return n_cpu;
static const long n_cpu = ::sysconf(_SC_NPROCESSORS_CONF);
return n_cpu;
}

inline void sleep(unsigned int milliseconds) {
::sleep(milliseconds / 1000);
::sleep(milliseconds / 1000);
}

inline void yield() {
sched_yield();
sched_yield();
}
}

}
} // namespace core
} // namespace acto

#endif
21 changes: 9 additions & 12 deletions lib/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ unsigned long runtime_t::release(object_t* const obj) {
void runtime_t::send(object_t* const target, std::unique_ptr<msg_t> msg) {
assert(msg);
assert(target);
assert(target->impl);

msg->sender = active_actor;
msg->target = target;
Expand Down Expand Up @@ -262,10 +261,10 @@ void runtime_t::shutdown() {
{
std::lock_guard<std::mutex> g(m_cs);

actors temporary(m_actors);
actors_set temporary(m_actors);

for (actors::iterator i = temporary.begin(); i != temporary.end(); ++i) {
deconstruct_object(*i);
for (auto ti = temporary.cbegin(); ti != temporary.cend(); ++ti) {
deconstruct_object(*ti);
}
}

Expand All @@ -278,21 +277,19 @@ void runtime_t::startup() {
create_thread_binding();
}

void runtime_t::process_binded_actors(std::set<object_t*>& actors, const bool need_delete) {
for (std::set<object_t*>::iterator i = actors.begin(); i != actors.end(); ++i) {
object_t* const actor = *i;

while (auto msg = actor->select_message()) {
void runtime_t::process_binded_actors(actors_set& actors, const bool need_delete) {
for (auto ai = actors.cbegin(); ai != actors.cend(); ++ai) {
while (auto msg = (*ai)->select_message()) {
handle_message(std::move(msg));
}
if (need_delete) {
deconstruct_object(actor);
deconstruct_object(*ai);
}
}

if (need_delete) {
for (std::set<object_t*>::iterator i = actors.begin(); i != actors.end(); ++i) {
release(*i);
for (auto ai = actors.cbegin(); ai != actors.cend(); ++ai) {
release(*ai);
}

actors.clear();
Expand Down
10 changes: 5 additions & 5 deletions lib/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ namespace core {
* Данные среды выполнения
*/
class runtime_t : public worker_t::callbacks {
using actors_set = std::set<object_t*>;

// Максимальное кол-во рабочих потоков в системе
static constexpr ssize_t MAX_WORKERS = 512;
static constexpr unsigned int MAX_WORKERS = 512;

public:
runtime_t();
Expand Down Expand Up @@ -69,11 +71,9 @@ class runtime_t : public worker_t::callbacks {

worker_t* create_worker();

void process_binded_actors(std::set<object_t*>& actors, const bool need_delete);
void process_binded_actors(actors_set& actors, const bool need_delete);

private:
using actors = std::set<object_t*>;

struct workers_t {
/// Number of allocated threads.
std::atomic<unsigned long> count{0};
Expand All @@ -91,7 +91,7 @@ class runtime_t : public worker_t::callbacks {
///
event_t m_clean;
/// Текущее множество актеров
actors m_actors;
actors_set m_actors;
///
event_t m_event{true};
event_t m_evworker{true};
Expand Down
Loading

0 comments on commit 99adab2

Please sign in to comment.