1/******************************************************************************
2 *
3 *  Copyright (C) 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#define LOG_TAG "bt_osi_semaphore"
20
21#include <assert.h>
22#include <errno.h>
23#include <fcntl.h>
24#include <malloc.h>
25#include <string.h>
26#include <sys/eventfd.h>
27
28#include "osi/include/allocator.h"
29#include "osi/include/osi.h"
30#include "osi/include/log.h"
31#include "osi/include/semaphore.h"
32
33#if !defined(EFD_SEMAPHORE)
34#  define EFD_SEMAPHORE (1 << 0)
35#endif
36
37struct semaphore_t {
38  int fd;
39};
40
41semaphore_t *semaphore_new(unsigned int value) {
42  semaphore_t *ret = osi_malloc(sizeof(semaphore_t));
43  if (ret) {
44    ret->fd = eventfd(value, EFD_SEMAPHORE);
45    if (ret->fd == INVALID_FD) {
46      LOG_ERROR("%s unable to allocate semaphore: %s", __func__, strerror(errno));
47      osi_free(ret);
48      ret = NULL;
49    }
50  }
51  return ret;
52}
53
54void semaphore_free(semaphore_t *semaphore) {
55  if (!semaphore)
56    return;
57
58  if (semaphore->fd != INVALID_FD)
59    close(semaphore->fd);
60  osi_free(semaphore);
61}
62
63void semaphore_wait(semaphore_t *semaphore) {
64  assert(semaphore != NULL);
65  assert(semaphore->fd != INVALID_FD);
66
67  uint64_t value;
68  if (eventfd_read(semaphore->fd, &value) == -1)
69    LOG_ERROR("%s unable to wait on semaphore: %s", __func__, strerror(errno));
70}
71
72bool semaphore_try_wait(semaphore_t *semaphore) {
73  assert(semaphore != NULL);
74  assert(semaphore->fd != INVALID_FD);
75
76  int flags = fcntl(semaphore->fd, F_GETFL);
77  if (flags == -1) {
78    LOG_ERROR("%s unable to get flags for semaphore fd: %s", __func__, strerror(errno));
79    return false;
80  }
81  if (fcntl(semaphore->fd, F_SETFL, flags | O_NONBLOCK) == -1) {
82    LOG_ERROR("%s unable to set O_NONBLOCK for semaphore fd: %s", __func__, strerror(errno));
83    return false;
84  }
85
86  eventfd_t value;
87  if (eventfd_read(semaphore->fd, &value) == -1)
88    return false;
89
90  if (fcntl(semaphore->fd, F_SETFL, flags) == -1)
91    LOG_ERROR("%s unable to resetore flags for semaphore fd: %s", __func__, strerror(errno));
92  return true;
93}
94
95void semaphore_post(semaphore_t *semaphore) {
96  assert(semaphore != NULL);
97  assert(semaphore->fd != INVALID_FD);
98
99  if (eventfd_write(semaphore->fd, 1ULL) == -1)
100    LOG_ERROR("%s unable to post to semaphore: %s", __func__, strerror(errno));
101}
102
103int semaphore_get_fd(const semaphore_t *semaphore) {
104  assert(semaphore != NULL);
105  assert(semaphore->fd != INVALID_FD);
106  return semaphore->fd;
107}
108