/** * @brief Acquire a mutex; wait if needed. * * This function waits for a mutex to become available. It * may wait forever if the mutex never becomes free. * * @return NO_ERROR on success, other values on error */ status_t mutex_acquire(mutex_t *m) { status_t ret = NO_ERROR; #if MUTEX_CHECK ASSERT(m->magic == MUTEX_MAGIC); if (current_thread == m->holder) panic("mutex_acquire: thread %p (%s) tried to acquire mutex %p it already owns.\n", current_thread, current_thread->name, m); #endif enter_critical_section(); if (unlikely(++m->count > 1)) { /* * block on the wait queue. If it returns an error, it was likely destroyed * out from underneath us, so make sure we dont scribble thread ownership * on the mutex. */ ret = wait_queue_block(&m->wait, INFINITE_TIME); if (ret < 0) goto err; } #if MUTEX_CHECK m->holder = current_thread; #endif err: exit_critical_section(); return ret; }
/** * @brief Wait for event to be signaled * * If the event has already been signaled, this function * returns immediately. Otherwise, the current thread * goes to sleep until the event object is signaled, * the timeout is reached, or the event object is destroyed * by another thread. * * @param e Event object * @param timeout Timeout value, in ms * * @return 0 on success, ERR_TIMED_OUT on timeout, * other values on other errors. */ status_t event_wait_timeout(event_t *e, time_t timeout) { status_t ret = NO_ERROR; enter_critical_section(); #if EVENT_CHECK ASSERT(e->magic == EVENT_MAGIC); #endif if (e->signalled) { /* signalled, we're going to fall through */ if (e->flags & EVENT_FLAG_AUTOUNSIGNAL) { /* autounsignal flag lets one thread fall through before unsignalling */ e->signalled = false; } } else { /* unsignalled, block here */ ret = wait_queue_block(&e->wait, timeout); if (ret < 0) goto err; } err: exit_critical_section(); return ret; }
/** * @brief Mutex wait with timeout * * This function waits up to \a timeout ms for the mutex to become available. * Timeout may be zero, in which case this function returns immediately if * the mutex is not free. * * @return NO_ERROR on success, ERR_TIMED_OUT on timeout, * other values on error */ status_t mutex_acquire_timeout(mutex_t *m, lk_time_t timeout) { status_t ret = NO_ERROR; #if MUTEX_CHECK if (timeout == INFINITE_TIME) return mutex_acquire(m); // Unecessary overhead for correct calls, this function can handle this anyway ASSERT(m->magic == MUTEX_MAGIC); if (current_thread == m->holder) panic("mutex_acquire_timeout: thread %p (%s) tried to acquire mutex %p it already owns.\n", current_thread, current_thread->name, m); #endif enter_critical_section(); if (unlikely(++m->count > 1)) { ret = wait_queue_block(&m->wait, timeout); if (ret < NO_ERROR) { /* if the acquisition timed out, back out the acquire and exit */ if (ret == ERR_TIMED_OUT) { /* * XXX race: the mutex may have been destroyed after the timeout, * but before we got scheduled again which makes messing with the * count variable dangerous. */ m->count--; goto err; } /* if there was a general error, it may have been destroyed out from * underneath us, so just exit (which is really an invalid state anyway) */ } } #if MUTEX_CHECK m->holder = current_thread; #endif err: exit_critical_section(); return ret; }