Sharvil Nanavati | a3164c9 | 2014-06-23 12:07:05 -0700 | [diff] [blame] | 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 | |
Chris Manton | f802700 | 2015-03-12 09:22:48 -0700 | [diff] [blame] | 19 | #define LOG_TAG "bt_osi_semaphore" |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 20 | |
| 21 | #include <assert.h> |
| 22 | #include <errno.h> |
Sharvil Nanavati | a3164c9 | 2014-06-23 12:07:05 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
Elliott Hughes | b9164f4 | 2015-01-28 15:31:26 -0800 | [diff] [blame] | 24 | #include <malloc.h> |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <sys/eventfd.h> |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 27 | |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 28 | #include "osi/include/allocator.h" |
| 29 | #include "osi/include/osi.h" |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 30 | #include "osi/include/log.h" |
Sharvil Nanavati | 0f9b91e | 2015-03-12 15:42:50 -0700 | [diff] [blame] | 31 | #include "osi/include/semaphore.h" |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 32 | |
| 33 | #if !defined(EFD_SEMAPHORE) |
| 34 | # define EFD_SEMAPHORE (1 << 0) |
| 35 | #endif |
| 36 | |
| 37 | struct semaphore_t { |
| 38 | int fd; |
| 39 | }; |
| 40 | |
| 41 | semaphore_t *semaphore_new(unsigned int value) { |
Zach Johnson | 384f8a9 | 2014-08-25 23:22:24 -0700 | [diff] [blame] | 42 | semaphore_t *ret = osi_malloc(sizeof(semaphore_t)); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 43 | if (ret) { |
| 44 | ret->fd = eventfd(value, EFD_SEMAPHORE); |
Sharvil Nanavati | 2cb2998 | 2014-08-01 17:00:12 -0700 | [diff] [blame] | 45 | if (ret->fd == INVALID_FD) { |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 46 | LOG_ERROR("%s unable to allocate semaphore: %s", __func__, strerror(errno)); |
Sharvil Nanavati | c0745da | 2014-11-13 01:04:19 -0800 | [diff] [blame] | 47 | osi_free(ret); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 48 | ret = NULL; |
| 49 | } |
| 50 | } |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | void semaphore_free(semaphore_t *semaphore) { |
Zach Johnson | 207fa23 | 2014-09-18 17:29:54 -0700 | [diff] [blame] | 55 | if (!semaphore) |
| 56 | return; |
| 57 | |
Sharvil Nanavati | 2cb2998 | 2014-08-01 17:00:12 -0700 | [diff] [blame] | 58 | if (semaphore->fd != INVALID_FD) |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 59 | close(semaphore->fd); |
Zach Johnson | 384f8a9 | 2014-08-25 23:22:24 -0700 | [diff] [blame] | 60 | osi_free(semaphore); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void semaphore_wait(semaphore_t *semaphore) { |
| 64 | assert(semaphore != NULL); |
Sharvil Nanavati | 2cb2998 | 2014-08-01 17:00:12 -0700 | [diff] [blame] | 65 | assert(semaphore->fd != INVALID_FD); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 66 | |
| 67 | uint64_t value; |
| 68 | if (eventfd_read(semaphore->fd, &value) == -1) |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 69 | LOG_ERROR("%s unable to wait on semaphore: %s", __func__, strerror(errno)); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Sharvil Nanavati | a3164c9 | 2014-06-23 12:07:05 -0700 | [diff] [blame] | 72 | bool semaphore_try_wait(semaphore_t *semaphore) { |
| 73 | assert(semaphore != NULL); |
Sharvil Nanavati | 2cb2998 | 2014-08-01 17:00:12 -0700 | [diff] [blame] | 74 | assert(semaphore->fd != INVALID_FD); |
Sharvil Nanavati | a3164c9 | 2014-06-23 12:07:05 -0700 | [diff] [blame] | 75 | |
Sharvil Nanavati | ae973d5 | 2016-06-20 19:16:12 -0700 | [diff] [blame] | 76 | int flags = TEMP_FAILURE_RETRY(fcntl(semaphore->fd, F_GETFL)); |
Sharvil Nanavati | a3164c9 | 2014-06-23 12:07:05 -0700 | [diff] [blame] | 77 | if (flags == -1) { |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 78 | LOG_ERROR("%s unable to get flags for semaphore fd: %s", __func__, strerror(errno)); |
Sharvil Nanavati | a3164c9 | 2014-06-23 12:07:05 -0700 | [diff] [blame] | 79 | return false; |
| 80 | } |
Sharvil Nanavati | ae973d5 | 2016-06-20 19:16:12 -0700 | [diff] [blame] | 81 | if (TEMP_FAILURE_RETRY(fcntl(semaphore->fd, F_SETFL, flags | O_NONBLOCK)) == -1) { |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 82 | LOG_ERROR("%s unable to set O_NONBLOCK for semaphore fd: %s", __func__, strerror(errno)); |
Sharvil Nanavati | a3164c9 | 2014-06-23 12:07:05 -0700 | [diff] [blame] | 83 | return false; |
| 84 | } |
| 85 | |
| 86 | eventfd_t value; |
| 87 | if (eventfd_read(semaphore->fd, &value) == -1) |
| 88 | return false; |
| 89 | |
Sharvil Nanavati | ae973d5 | 2016-06-20 19:16:12 -0700 | [diff] [blame] | 90 | if (TEMP_FAILURE_RETRY(fcntl(semaphore->fd, F_SETFL, flags)) == -1) |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 91 | LOG_ERROR("%s unable to resetore flags for semaphore fd: %s", __func__, strerror(errno)); |
Sharvil Nanavati | a3164c9 | 2014-06-23 12:07:05 -0700 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 95 | void semaphore_post(semaphore_t *semaphore) { |
| 96 | assert(semaphore != NULL); |
Sharvil Nanavati | 2cb2998 | 2014-08-01 17:00:12 -0700 | [diff] [blame] | 97 | assert(semaphore->fd != INVALID_FD); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 98 | |
| 99 | if (eventfd_write(semaphore->fd, 1ULL) == -1) |
Sharvil Nanavati | 4480276 | 2014-12-23 23:08:58 -0800 | [diff] [blame] | 100 | LOG_ERROR("%s unable to post to semaphore: %s", __func__, strerror(errno)); |
Sharvil Nanavati | c11b407 | 2014-05-02 23:55:09 -0700 | [diff] [blame] | 101 | } |
Sharvil Nanavati | f4013f2 | 2014-07-05 20:42:07 -0700 | [diff] [blame] | 102 | |
| 103 | int semaphore_get_fd(const semaphore_t *semaphore) { |
| 104 | assert(semaphore != NULL); |
Sharvil Nanavati | 2cb2998 | 2014-08-01 17:00:12 -0700 | [diff] [blame] | 105 | assert(semaphore->fd != INVALID_FD); |
Sharvil Nanavati | f4013f2 | 2014-07-05 20:42:07 -0700 | [diff] [blame] | 106 | return semaphore->fd; |
| 107 | } |