Skip to content

Commit

Permalink
Fix build on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
gammasoft71 committed Aug 4, 2023
1 parent 9084ae8 commit 041f13a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#define __XTD_CORE_NATIVE_LIBRARY__
#include <xtd/native/named_event_wait_handle.h>
#include <xtd/native/unnamed_event_wait_handle.h>
#include "../../../../include/xtd/native/unix/semaphore.h"
#undef __XTD_CORE_NATIVE_LIBRARY__

using namespace xtd::native;

intmax_t unnamed_event_wait_handle::create(bool initial_state, bool manual_reset) {
semaphore_t semaphore;
if (semaphore_create(current_task(), &semaphore, SYNC_POLICY_FIFO, initial_state ? 1 : 0) != err_none)
return reinterpret_cast<intmax_t>(SEM_FAILED);
return static_cast<intmax_t>(semaphore);
sem_t* semaphore = new sem_t;
if (sem_init(semaphore, 0, initial_state ? 1 : 0) != 0) return reinterpret_cast<intmax_t>(SEM_FAILED);
return reinterpret_cast<intmax_t>(semaphore);
}

void unnamed_event_wait_handle::destroy(intmax_t handle) {
if (reinterpret_cast<sem_t*>(handle) == SEM_FAILED) return;
semaphore_destroy(current_task(), static_cast<semaphore_t>(handle));
sem_destroy(reinterpret_cast<sem_t*>(handle));
delete reinterpret_cast<sem_t*>(handle);
}

bool unnamed_event_wait_handle::set(intmax_t handle, bool& io_error) {
Expand All @@ -26,7 +26,7 @@ bool unnamed_event_wait_handle::set(intmax_t handle, bool& io_error) {
auto previous_count = -1;
sem_getvalue(reinterpret_cast<sem_t*>(handle), &previous_count);
if (previous_count == 1) return true;
if (semaphore_signal(static_cast<semaphore_t>(handle)) != err_none) io_error = true;
if (sem_post(reinterpret_cast<sem_t*>(handle)) == -1 && errno == EINVAL) io_error = true;
return !io_error;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
using namespace xtd::native;

intmax_t unnamed_semaphore::create(int_least32_t initial_count, int_least32_t max_count) {
sem_t* semaphore = SEM_FAILED;
if (sem_int(semaphore, 0, std::min(initial_count, max_count)) == -1) return SEM_FAILED;
sem_t* semaphore = new sem_t;
if (sem_init(semaphore, 0, std::min(initial_count, max_count)) == -1) return reinterpret_cast<intmax_t>(SEM_FAILED);
return reinterpret_cast<intmax_t>(semaphore);
}

void unnamed_semaphore::destroy(intmax_t handle) {
if (reinterpret_cast<sem_t*>(handle) == SEM_FAILED) return;
sem_destroy(reinterpret_cast<sem_t*>(handle));
delete reinterpret_cast<sem_t*>(handle);
}

bool unnamed_semaphore::signal(intmax_t handle, int_least32_t release_count, int_least32_t& previous_count, bool& io_error) {
Expand Down
1 change: 1 addition & 0 deletions src/xtd.core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ add_sources(
include/xtd/threading/auto_reset_event.h
include/xtd/threading/auto_reset_event
include/xtd/threading/event_reset_mode.h
include/xtd/threading/event_reset_mode
include/xtd/threading/event_wait_handle.h
include/xtd/threading/event_wait_handle
include/xtd/threading/interlocked.h
Expand Down
3 changes: 2 additions & 1 deletion src/xtd.core/src/xtd/threading/unnamed_semaphore.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "semaphore_base.h"
#include <condition_variable>
#include "../../../include/xtd/int32_object.h"
#include "../../../include/xtd/semaphore.h"
#include "../../../include/xtd/invalid_operation_exception.h"

Expand Down Expand Up @@ -60,7 +61,7 @@ class xtd::threading::semaphore::unnamed_semaphore : public semaphore_base {
struct data {
int count = 0;
int maximum_count = std::numeric_limits<int>::max();
std::counting_semaphore<std::numeric_limits<std::ptrdiff_t>::max()> semaphore {0};
std::counting_semaphore<int32_object::max_value> semaphore {0};
};
std::shared_ptr<data> handle_;
};

0 comments on commit 041f13a

Please sign in to comment.