1 /* semaphore.h
2  *
3  * Copyright 2012 Christopher Anderson <[email protected]>
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 (0x73656D61) // 'sema'
27 
28 typedef 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 
41 void sem_init(semaphore_t *, unsigned int);
42 void sem_destroy(semaphore_t *);
43 int sem_post(semaphore_t *, bool resched);
44 status_t sem_wait(semaphore_t *);
45 status_t sem_trywait(semaphore_t *);
46 status_t sem_timedwait(semaphore_t *, lk_time_t);
47 
48 __END_CDECLS;
49 #endif
50