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