@@ -119,27 +119,54 @@ bool Thread::start(void* arg) {
119119 }
120120
121121 result = pthread_create (&m_thread, nullptr , threadEntryPointStub, this );
122- pthread_setname_np (m_thread, m_name.c_str ());
123- m_started = (result == 0 );
122+ if (result != 0 ) {
123+ return false ;
124+ }
124125
125- return result == 0 ;
126+ // Set thread name only if creation succeeded
127+ int name_result = pthread_setname_np (m_thread, m_name.c_str ());
128+ (void )name_result; // Suppress unused variable warning if no logging
129+
130+ m_started = true ;
131+ return true ;
126132}
127133
128134bool Thread::stop () {
129135 if (!m_started) {
130136 return false ;
131137 }
132- pthread_cancel (m_thread);
133- pthread_join (m_thread, nullptr );
138+
139+ // Send cancellation request
140+ int cancel_result = pthread_cancel (m_thread);
141+ if (cancel_result != 0 ) {
142+ // Handle cancellation errors
143+ switch (cancel_result) {
144+ case ESRCH:
145+ // Thread not found - may have already exited
146+ m_started = false ;
147+ return true ;
148+ default :
149+ // Other errors - still try to join
150+ break ;
151+ }
152+ }
153+
154+ // Always try to join, even if cancel failed
155+ int join_result = pthread_join (m_thread, nullptr );
134156 m_started = false ;
135- return true ;
157+
158+ return (cancel_result == 0 || cancel_result == ESRCH) && join_result == 0 ;
136159}
137160
138161bool Thread::join () {
139162 if (!m_started) {
140163 return false ;
141164 }
142- pthread_join (m_thread, nullptr );
165+ int result = pthread_join (m_thread, nullptr );
166+ if (result != 0 ) {
167+ m_started = false ;
168+ return false ;
169+ }
143170 m_started = false ;
144171 return true ;
145172}
@@ -181,7 +208,21 @@ Mutex::operator bool() const {
181208}
182209
183210bool Mutex::tryLock (ma_tick_t timeout) {
184- return pthread_mutex_trylock (&m_mutex) == 0 ;
211+ if (timeout == 0 ) {
212+ return pthread_mutex_trylock (&m_mutex) == 0 ;
213+ }
214+
215+ struct timespec ts;
216+ clock_gettime (CLOCK_MONOTONIC, &ts);
217+ ts.tv_sec += timeout / 1000000000 ;
218+ ts.tv_nsec += (timeout % 1000000000 );
219+ if (ts.tv_nsec >= 1000000000 ) {
220+ ts.tv_sec ++;
221+ ts.tv_nsec -= 1000000000 ;
222+ }
223+
224+ int result = pthread_mutex_timedlock (&m_mutex, &ts);
225+ return result == 0 ;
185226}
186227
187228bool Mutex::lock () const {
@@ -230,6 +271,10 @@ bool Semaphore::wait(ma_tick_t timeout) {
230271 clock_gettime (CLOCK_MONOTONIC, &ts);
231272 ts.tv_sec += timeout / 1000000000 ;
232273 ts.tv_nsec += (timeout % 1000000000 );
274+ if (ts.tv_nsec >= 1000000000 ) {
275+ ts.tv_sec ++;
276+ ts.tv_nsec -= 1000000000 ;
277+ }
233278 }
234279
235280 Guard guard (m_mutex);
@@ -256,10 +301,8 @@ uint32_t Semaphore::getCount() const {
256301
257302void Semaphore::signal () {
258303 Guard guard (m_mutex);
259- if (m_sem.count == 0 ) {
260- pthread_cond_signal (&m_sem.cond );
261- }
262304 m_sem.count ++;
305+ pthread_cond_signal (&m_sem.cond );
263306}
264307
265308Event::Event () noexcept {
@@ -287,12 +330,16 @@ bool Event::wait(uint32_t mask, uint32_t* value, ma_tick_t timeout, bool clear,
287330 clock_gettime (CLOCK_MONOTONIC, &ts);
288331 ts.tv_sec += timeout / 1000000000 ;
289332 ts.tv_nsec += (timeout % 1000000000 );
333+ if (ts.tv_nsec >= 1000000000 ) {
334+ ts.tv_sec ++;
335+ ts.tv_nsec -= 1000000000 ;
336+ }
290337 }
291338 Guard guard (m_mutex);
292339
293340 do {
294341 if (waitAll) {
295- if (m_event.value & mask == mask) {
342+ if (( m_event.value & mask) == mask) {
296343 break ;
297344 }
298345 } else {
@@ -331,7 +378,7 @@ void Event::clear(uint32_t value) {
331378void Event::set (uint32_t value) {
332379 Guard guard (m_mutex);
333380 m_event.value |= value;
334- pthread_cond_signal (&m_event.cond );
381+ pthread_cond_broadcast (&m_event.cond );
335382}
336383
337384uint32_t Event::get () const {
@@ -384,9 +431,15 @@ bool MessageBox::fetch(void** msg, ma_tick_t timeout) {
384431 error = pthread_cond_wait (&m_mbox.cond , static_cast <ma_mutex_t *>(m_mutex));
385432 }
386433 }
434+
435+ bool was_full = (m_mbox.count == m_mbox.size );
387436 m_mbox.count --;
388437 *msg = m_mbox.msg [m_mbox.r ];
389438 m_mbox.r = (m_mbox.r + 1 ) % m_mbox.size ;
439+
440+ if (was_full) {
441+ pthread_cond_signal (&m_mbox.cond );
442+ }
390443 return true ;
391444}
392445
0 commit comments