2012-10-17 23:09:43 -07:00
|
|
|
/* semaphore.c
|
|
|
|
|
*
|
2012-10-31 21:37:45 -07:00
|
|
|
* Copyright 2012 Christopher Anderson <chris@nullcode.org>
|
2012-10-17 23:09:43 -07:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2012-10-16 16:05:09 -07:00
|
|
|
#include <kernel/semaphore.h>
|
2019-07-13 17:21:00 -07:00
|
|
|
|
2012-10-16 16:05:09 -07:00
|
|
|
#include <kernel/thread.h>
|
2019-07-13 17:21:00 -07:00
|
|
|
#include <lk/debug.h>
|
|
|
|
|
#include <lk/err.h>
|
2012-10-16 16:05:09 -07:00
|
|
|
|
2025-07-09 20:51:26 -07:00
|
|
|
void sem_init(semaphore_t *sem, int initial_count) {
|
|
|
|
|
*sem = (semaphore_t)SEMAPHORE_INITIAL_VALUE(*sem, initial_count);
|
2012-10-16 16:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
void sem_destroy(semaphore_t *sem) {
|
2016-02-14 12:24:01 -08:00
|
|
|
THREAD_LOCK(state);
|
|
|
|
|
sem->count = 0;
|
|
|
|
|
wait_queue_destroy(&sem->wait, true);
|
|
|
|
|
THREAD_UNLOCK(state);
|
2012-10-16 16:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
int sem_post(semaphore_t *sem, bool resched) {
|
2016-02-14 12:24:01 -08:00
|
|
|
int ret = 0;
|
2015-04-29 15:48:21 -07:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
THREAD_LOCK(state);
|
2012-10-16 16:05:09 -07:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
/*
|
|
|
|
|
* If the count is or was negative then a thread is waiting for a resource, otherwise
|
|
|
|
|
* it's safe to just increase the count available with no downsides
|
|
|
|
|
*/
|
|
|
|
|
if (unlikely(++sem->count <= 0))
|
|
|
|
|
ret = wait_queue_wake_one(&sem->wait, resched, NO_ERROR);
|
2012-10-16 16:05:09 -07:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
THREAD_UNLOCK(state);
|
2015-04-29 15:48:21 -07:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
return ret;
|
2012-10-16 16:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
status_t sem_wait(semaphore_t *sem) {
|
2016-02-14 12:24:01 -08:00
|
|
|
status_t ret = NO_ERROR;
|
|
|
|
|
THREAD_LOCK(state);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* If there are no resources available then we need to
|
|
|
|
|
* sit in the wait queue until sem_post adds some.
|
|
|
|
|
*/
|
|
|
|
|
if (unlikely(--sem->count < 0))
|
|
|
|
|
ret = wait_queue_block(&sem->wait, INFINITE_TIME);
|
|
|
|
|
|
|
|
|
|
THREAD_UNLOCK(state);
|
|
|
|
|
return ret;
|
2012-10-16 16:05:09 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
status_t sem_trywait(semaphore_t *sem) {
|
2016-02-14 12:24:01 -08:00
|
|
|
status_t ret = NO_ERROR;
|
|
|
|
|
THREAD_LOCK(state);
|
2012-10-16 16:05:09 -07:00
|
|
|
|
2025-07-09 20:51:26 -07:00
|
|
|
if (unlikely(sem->count <= 0)) {
|
2016-02-14 12:24:01 -08:00
|
|
|
ret = ERR_NOT_READY;
|
2025-07-09 20:51:26 -07:00
|
|
|
} else {
|
2016-02-14 12:24:01 -08:00
|
|
|
sem->count--;
|
2025-07-09 20:51:26 -07:00
|
|
|
}
|
2012-10-31 21:37:45 -07:00
|
|
|
|
2016-02-14 12:24:01 -08:00
|
|
|
THREAD_UNLOCK(state);
|
|
|
|
|
return ret;
|
2012-10-16 16:05:09 -07:00
|
|
|
}
|
2012-10-17 17:32:29 -07:00
|
|
|
|
2019-06-19 20:54:28 -07:00
|
|
|
status_t sem_timedwait(semaphore_t *sem, lk_time_t timeout) {
|
2016-02-14 12:24:01 -08:00
|
|
|
status_t ret = NO_ERROR;
|
|
|
|
|
THREAD_LOCK(state);
|
|
|
|
|
|
|
|
|
|
if (unlikely(--sem->count < 0)) {
|
|
|
|
|
ret = wait_queue_block(&sem->wait, timeout);
|
|
|
|
|
if (ret < NO_ERROR) {
|
|
|
|
|
if (ret == ERR_TIMED_OUT) {
|
|
|
|
|
sem->count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
THREAD_UNLOCK(state);
|
|
|
|
|
return ret;
|
2012-10-17 17:32:29 -07:00
|
|
|
}
|