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