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