Skip to content

Commit d64d794

Browse files
committed
Pthread mutex implementation, fix clang compile
1 parent babce49 commit d64d794

File tree

12 files changed

+309
-97
lines changed

12 files changed

+309
-97
lines changed

projects/core/include/tempest/array.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -318,13 +318,13 @@ namespace tempest
318318

319319
template <typename T>
320320
requires is_default_constructible_v<T>
321-
inline void array<T, 0>::fill(const T& value)
321+
inline void array<T, 0>::fill([[maybe_unused]] const T& value)
322322
{
323323
}
324324

325325
template <typename T>
326326
requires is_default_constructible_v<T>
327-
inline void array<T, 0>::swap(array& other) noexcept
327+
inline void array<T, 0>::swap([[maybe_unused]] array& other) noexcept
328328
{
329329
}
330330

projects/core/include/tempest/char_traits.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ namespace tempest
260260
{
261261
auto it = i - 1;
262262
auto index = Traits::to_int_type(str[it]) - Traits::to_int_type(min_val);
263-
if (index >= 0 && index < table.size())
263+
if (index >= 0 && static_cast<size_t>(index) < table.size())
264264
{
265265
table[index] = static_cast<Traits::int_type>(it);
266266
}
@@ -290,7 +290,7 @@ namespace tempest
290290
}
291291

292292
auto index = Traits::to_int_type(str[s + p]) - min_value;
293-
if (index < 0 || index >= bad_char_table.size())
293+
if (index < 0 || static_cast<size_t>(index) >= bad_char_table.size())
294294
{
295295
s += 1;
296296
continue;
@@ -325,7 +325,7 @@ namespace tempest
325325
}
326326

327327
auto index = Traits::to_int_type(str[s + p]) - min_value;
328-
if (index < 0 || index >= bad_char_table.size())
328+
if (index < 0 || static_cast<size_t>(index) >= bad_char_table.size())
329329
{
330330
s -= 1;
331331
continue;

projects/core/include/tempest/limits.hpp

+64
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define tempest_core_limits_hpp
33

44
#include <tempest/bit.hpp>
5+
#include <tempest/concepts.hpp>
56
#include <tempest/int.hpp>
67

78
namespace tempest
@@ -13,6 +14,69 @@ namespace tempest
1314
inline static constexpr bool is_specialized = false;
1415
};
1516

17+
namespace detail
18+
{
19+
template <typename T>
20+
struct integral_numeric_limits_specialization
21+
{
22+
inline static constexpr bool is_specialized = true;
23+
inline static constexpr bool is_signed = is_signed_v<T>;
24+
inline static constexpr bool is_integer = true;
25+
inline static constexpr bool is_exact = true;
26+
inline static constexpr bool has_infinity = false;
27+
inline static constexpr bool has_quiet_NaN = false;
28+
inline static constexpr bool has_signaling_NaN = false;
29+
inline static constexpr bool is_iec559 = false;
30+
inline static constexpr bool is_bounded = true;
31+
inline static constexpr bool is_modulo = true;
32+
inline static constexpr int digits = is_same_v<T, bool> ? 1 : sizeof(T) * 8 - (is_signed_v<T> ? 1 : 0);
33+
inline static constexpr int digits10 = is_same_v<T, bool> ? 0 : digits * 30103 / 100000;
34+
inline static constexpr int max_digits10 = 0;
35+
inline static constexpr int radix = 2;
36+
inline static constexpr int min_exponent = 0;
37+
inline static constexpr int min_exponent10 = 0;
38+
inline static constexpr int max_exponent = 0;
39+
inline static constexpr int max_exponent10 = 0;
40+
41+
inline static constexpr T min() noexcept
42+
{
43+
if constexpr (is_same_v<T, bool>)
44+
{
45+
return false;
46+
}
47+
else if constexpr (is_signed_v<T>)
48+
{
49+
return T(1) << (digits - 1);
50+
}
51+
else
52+
{
53+
return 0;
54+
}
55+
}
56+
57+
inline static constexpr T max() noexcept
58+
{
59+
if constexpr (is_same_v<T, bool>)
60+
{
61+
return true;
62+
}
63+
else if constexpr (is_signed_v<T>)
64+
{
65+
return ~(T(1) << (digits - 1));
66+
}
67+
else
68+
{
69+
return ~T(0);
70+
}
71+
}
72+
};
73+
}
74+
75+
template <integral T>
76+
class numeric_limits<T> : public detail::integral_numeric_limits_specialization<T>
77+
{
78+
};
79+
1680
template <>
1781
class numeric_limits<float>
1882
{

projects/core/include/tempest/mutex.hpp

+39-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#if defined(TEMPEST_WIN_THREADS)
99
#include <windows.h>
1010
#elif defined(TEMPEST_POSIX_THREADS)
11-
11+
#include <pthread.h>
1212
#else
1313
#error "Unsupported platform"
1414
#endif
@@ -21,6 +21,7 @@ namespace tempest
2121
#if defined(TEMPEST_WIN_THREADS)
2222
using native_handle_type = HANDLE;
2323
#elif defined(TEMPEST_POSIX_THREADS)
24+
using native_handle_type = pthread_mutex_t;
2425
#else
2526
#error "Unsupported platform"
2627
#endif
@@ -42,6 +43,7 @@ namespace tempest
4243
};
4344

4445
#if defined(TEMPEST_WIN_THREADS)
46+
4547
inline constexpr mutex::mutex() noexcept
4648
{
4749
if (is_constant_evaluated())
@@ -53,7 +55,21 @@ namespace tempest
5355
_handle = CreateMutexW(nullptr, FALSE, nullptr);
5456
}
5557
}
58+
5659
#elif defined(TEMPEST_POSIX_THREADS)
60+
61+
inline constexpr mutex::mutex() noexcept
62+
{
63+
if (is_constant_evaluated())
64+
{
65+
_handle = {};
66+
}
67+
else
68+
{
69+
pthread_mutex_init(&_handle, nullptr);
70+
}
71+
}
72+
5773
#else
5874
#error "Unsupported platform"
5975
#endif
@@ -69,6 +85,7 @@ namespace tempest
6985
#if defined(TEMPEST_WIN_THREADS)
7086
using native_handle_type = SRWLOCK;
7187
#elif defined(TEMPEST_POSIX_THREADS)
88+
using native_handle_type = pthread_rwlock_t;
7289
#else
7390
#error "Unsupported platform"
7491
#endif
@@ -94,6 +111,7 @@ namespace tempest
94111
};
95112

96113
#if defined(TEMPEST_WIN_THREADS)
114+
97115
inline constexpr shared_mutex::shared_mutex() noexcept
98116
{
99117
if (is_constant_evaluated())
@@ -105,7 +123,21 @@ namespace tempest
105123
InitializeSRWLock(&_handle);
106124
}
107125
}
126+
108127
#elif defined(TEMPEST_POSIX_THREADS)
128+
129+
inline constexpr shared_mutex::shared_mutex() noexcept
130+
{
131+
if (is_constant_evaluated())
132+
{
133+
_handle = {};
134+
}
135+
else
136+
{
137+
pthread_rwlock_init(&_handle, nullptr);
138+
}
139+
}
140+
109141
#else
110142
#error "Unsupported platform"
111143
#endif
@@ -228,7 +260,7 @@ namespace tempest
228260
template <lockable Mutex>
229261
inline unique_lock<Mutex>::unique_lock(mutex_type& m) : _mutex{&m}
230262
{
231-
_mutex.lock();
263+
_mutex->lock();
232264
_owns_lock = true;
233265
}
234266

@@ -247,7 +279,7 @@ namespace tempest
247279
{
248280
if (_owns_lock)
249281
{
250-
_mutex.unlock();
282+
_mutex->unlock();
251283
}
252284

253285
_mutex = nullptr;
@@ -264,7 +296,7 @@ namespace tempest
264296

265297
if (_owns_lock)
266298
{
267-
_mutex.unlock();
299+
_mutex->unlock();
268300
}
269301

270302
_mutex = exchange(rhs._mutex, nullptr);
@@ -281,7 +313,7 @@ namespace tempest
281313
std::terminate();
282314
}
283315

284-
_mutex.lock();
316+
_mutex->lock();
285317
_owns_lock = true;
286318
}
287319

@@ -292,7 +324,7 @@ namespace tempest
292324
{
293325
std::terminate();
294326
}
295-
_owns_lock = _mutex.try_lock();
327+
_owns_lock = _mutex->try_lock();
296328
return _owns_lock;
297329
}
298330

@@ -303,7 +335,7 @@ namespace tempest
303335
{
304336
std::terminate();
305337
}
306-
_mutex.unlock();
338+
_mutex->unlock();
307339
_owns_lock = false;
308340
}
309341

projects/core/include/tempest/source_location.hpp

+12-48
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33

44
#include <tempest/int.hpp>
55

6+
#include <source_location>
7+
68
namespace tempest
79
{
810
struct source_location
911
{
10-
private:
11-
#if defined(__GNUC__)
12-
using builtin_ret_type = decltype(__builtin_source_location());
13-
#endif
1412
public:
1513
#if defined(_MSC_VER)
1614
static consteval source_location current(const uint32_t line = __builtin_LINE(),
1715
const uint32_t column = __builtin_COLUMN(),
1816
const char* file = __builtin_FILE(),
1917
const char* func = __builtin_FUNCSIG()) noexcept;
2018
#elif defined(__GNUC__)
21-
static consteval source_location current(builtin_ret_type loc = __builtin_source_location()) noexcept;
19+
static consteval source_location current(const uint32_t line = __builtin_LINE(),
20+
const uint32_t column = __builtin_COLUMN(),
21+
const char* file = __builtin_FILE(),
22+
const char* func = __builtin_source_location()->_M_function_name) noexcept;
2223
#else
2324
#error "Unsupported compiler."
2425
#endif
@@ -40,16 +41,11 @@ namespace tempest
4041
uint32_t _column = 0;
4142
};
4243

43-
#if defined(_MSC_VER)
4444
impl _impl;
45-
#elif defined(__GNUC__)
46-
const impl* _impl;
47-
#endif
4845
};
4946

50-
#if defined(_MSC_VER)
51-
source_location source_location::current(const uint32_t line, const uint32_t column, const char* file,
52-
const char* func) noexcept
47+
inline consteval source_location source_location::current(const uint32_t line, const uint32_t column,
48+
const char* file, const char* func) noexcept
5349
{
5450
source_location loc;
5551
loc._impl._file = file;
@@ -59,57 +55,25 @@ namespace tempest
5955
return loc;
6056
}
6157

62-
constexpr const char* source_location::file_name() const noexcept
58+
inline constexpr const char* source_location::file_name() const noexcept
6359
{
6460
return _impl._file;
6561
}
6662

67-
constexpr const char* source_location::function_name() const noexcept
63+
inline constexpr const char* source_location::function_name() const noexcept
6864
{
6965
return _impl._function;
7066
}
7167

72-
constexpr size_t source_location::line() const noexcept
68+
inline constexpr size_t source_location::line() const noexcept
7369
{
7470
return _impl._line;
7571
}
7672

77-
constexpr size_t source_location::column() const noexcept
73+
inline constexpr size_t source_location::column() const noexcept
7874
{
7975
return _impl._column;
8076
}
81-
82-
#elif defined(__GNUC__)
83-
source_location source_location::current(builtin_ret_type loc) noexcept
84-
{
85-
source_location sl;
86-
sl._impl = static_cast<const impl*>(loc);
87-
return sl;
88-
}
89-
90-
constexpr const char* source_location::file_name() const noexcept
91-
{
92-
return _impl->_file;
93-
}
94-
95-
constexpr const char* source_location::function_name() const noexcept
96-
{
97-
return _impl->_function;
98-
}
99-
100-
constexpr size_t source_location::line() const noexcept
101-
{
102-
return _impl->_line;
103-
}
104-
105-
constexpr size_t source_location::column() const noexcept
106-
{
107-
return _impl->_column;
108-
}
109-
110-
#else
111-
#error "Unsupported compiler."
112-
#endif
11377
} // namespace tempest
11478

11579
#endif // tempest_core_source_location_hpp

0 commit comments

Comments
 (0)