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