1 /* 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 #pragma once
18 
19 #include <kernel/mutex.h>
20 #include <kernel/thread.h>
21 #include <lk/compiler.h>
22 
23 __BEGIN_CDECLS
24 
25 #define SEMAPHORE_MAGIC (0x73656D61) // 'sema'
26 
27 typedef struct semaphore {
28     int magic;
29     int count;
30     wait_queue_t wait;
31 } semaphore_t;
32 
33 #define SEMAPHORE_INITIAL_VALUE(s, _count) \
34 { \
35     .magic = SEMAPHORE_MAGIC, \
36     .count = _count, \
37     .wait = WAIT_QUEUE_INITIAL_VALUE((s).wait), \
38 }
39 
40 void sem_init(semaphore_t *, unsigned int);
41 void sem_destroy(semaphore_t *);
42 int sem_post(semaphore_t *, bool resched);
43 status_t sem_wait(semaphore_t *);
44 status_t sem_trywait(semaphore_t *);
45 status_t sem_timedwait(semaphore_t *, lk_time_t);
46 
47 __END_CDECLS
48