1 /******************************************************************************
2  *
3  *  Copyright 2014 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #pragma once
20 
21 #ifndef LIB_OSI_INTERNAL
22 #error "Please do not include this outside of osi."
23 #endif
24 
25 #include <stdbool.h>
26 
27 struct semaphore_t;
28 typedef struct semaphore_t semaphore_t;
29 
30 // Creates a new semaphore with an initial value of |value|.
31 // Returns NULL on failure. The returned object must be released
32 // with |semaphore_free|.
33 semaphore_t* semaphore_new(unsigned int value);
34 
35 // Frees a semaphore allocated with |semaphore_new|. |semaphore| may
36 // be NULL.
37 void semaphore_free(semaphore_t* semaphore);
38 
39 // Decrements the value of |semaphore|. If it is 0, this call blocks until
40 // it becomes non-zero. |semaphore| may not be NULL.
41 void semaphore_wait(semaphore_t* semaphore);
42 
43 // Tries to decrement the value of |semaphore|. Returns true if the value was
44 // decremented, false if the value was 0. This function never blocks.
45 // |semaphore| may not be NULL.
46 bool semaphore_try_wait(semaphore_t* semaphore);
47 
48 // Increments the value of |semaphore|. |semaphore| may not be NULL.
49 void semaphore_post(semaphore_t* semaphore);
50 
51 // Returns a file descriptor representing this semaphore. The caller may
52 // only perform one operation on the file descriptor: select(2). If |select|
53 // indicates the fd is readable, the caller may call |semaphore_wait|
54 // without blocking. If select indicates the fd is writable, the caller may
55 // call |semaphore_post| without blocking. Note that there may be a race
56 // condition between calling |select| and |semaphore_wait| or |semaphore_post|
57 // which results in blocking behaviour.
58 //
59 // The caller must not close the returned file descriptor. |semaphore| may not
60 // be NULL.
61 int semaphore_get_fd(const semaphore_t* semaphore);
62