@@ -52,12 +52,20 @@ struct recursive_mutex_type_t
5252
5353struct stalled_robustness_t
5454{
55+ #if defined(__linux__)
5556 static constexpr int value = PTHREAD_MUTEX_STALLED;
57+ #else
58+ static constexpr int value = 0 ; // macOS, Windows, or other platforms fallback
59+ #endif
5660};
5761
5862struct robust_robustness_t
5963{
64+ #if defined(__linux__)
6065 static constexpr int value = PTHREAD_MUTEX_ROBUST;
66+ #else
67+ static constexpr int value = 0 ; // macOS, Windows, or other platforms fallback
68+ #endif
6169};
6270/* *
6371 * @brief A class template that provides a pthread mutex with the priority inheritance protocol
@@ -109,10 +117,14 @@ class mutex
109117 }
110118
111119 // Set the mutex attribute robustness to MutexRobustness
120+ // On platforms like macOS, pthread_mutexattr_setrobust is not available,
121+ // so skip this step
122+ #if defined(__linux__)
112123 const auto res_robust = pthread_mutexattr_setrobust (&attr, MutexRobustness::value);
113124 if (res_robust != 0 ) {
114125 throw std::system_error (res_robust, std::system_category (), " Failed to set mutex robustness" );
115126 }
127+ #endif
116128
117129 // Initialize the mutex with the attributes
118130 const auto res_init = pthread_mutex_init (&mutex_, &attr);
@@ -142,13 +154,20 @@ class mutex
142154 return ;
143155 }
144156 if (res == EOWNERDEAD) {
157+ #if defined(__linux__)
145158 const auto res_consistent = pthread_mutex_consistent (&mutex_);
146159 if (res_consistent != 0 ) {
147160 throw std::runtime_error (
148161 std::string (" Failed to make mutex consistent : " ) + std::strerror (res_consistent));
149162 }
150163 std::cerr << " Mutex owner died, but the mutex is consistent now. This shouldn't happen!"
151164 << std::endl;
165+ #else
166+ // On platforms without pthread_mutex_consistent support, just log a warning
167+ std::cerr
168+ << " Mutex owner died, but pthread_mutex_consistent is not supported on this platform."
169+ << std::endl;
170+ #endif
152171 } else if (res == EDEADLK) {
153172 throw std::system_error (res, std::system_category (), " Deadlock detected" );
154173 } else {
@@ -174,13 +193,19 @@ class mutex
174193 if (res == EBUSY) {
175194 return false ;
176195 } else if (res == EOWNERDEAD) {
196+ #if defined(__linux__)
177197 const auto res_consistent = pthread_mutex_consistent (&mutex_);
178198 if (res_consistent != 0 ) {
179199 throw std::runtime_error (
180200 std::string (" Failed to make mutex consistent : " ) + std::strerror (res_consistent));
181201 }
182202 std::cerr << " Mutex owner died, but the mutex is consistent now. This shouldn't happen!"
183203 << std::endl;
204+ #else
205+ std::cerr
206+ << " Mutex owner died, but pthread_mutex_consistent is not supported on this platform."
207+ << std::endl;
208+ #endif
184209 } else if (res == EDEADLK) {
185210 throw std::system_error (res, std::system_category (), " Deadlock detected" );
186211 } else {
0 commit comments