blob: 99d9a2013152024595bda6c0d8d5ef12427e4aff [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/* semaphore.h
2 *
3 * Copyright 2012 Christopher Anderson <chris@nullcode.org>
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __KERNEL_SEMAPHORE_H
18#define __KERNEL_SEMAPHORE_H
19
20#include <compiler.h>
21#include <kernel/thread.h>
22#include <kernel/mutex.h>
23
24__BEGIN_CDECLS;
25
26#define SEMAPHORE_MAGIC 'sema'
27
28typedef struct semaphore {
29 int magic;
30 int count;
31 wait_queue_t wait;
32} semaphore_t;
33
34#define SEMAPHORE_INITIAL_VALUE(s, _count) \
35{ \
36 .magic = SEMAPHORE_MAGIC, \
37 .count = _count, \
38 .wait = WAIT_QUEUE_INITIAL_VALUE((s).wait), \
39}
40
41void sem_init(semaphore_t *, unsigned int);
42void sem_destroy(semaphore_t *);
43int sem_post(semaphore_t *, bool resched);
44status_t sem_wait(semaphore_t *);
45status_t sem_trywait(semaphore_t *);
46status_t sem_timedwait(semaphore_t *, lk_time_t);
47
48__END_CDECLS;
49#endif