blob: be4373cb1ac5234bb4ca0f3cbb79c56beea28808 [file] [log] [blame]
Sharvil Nanavatia3164c92014-06-23 12:07:05 -07001/******************************************************************************
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 Nanavatic11b4072014-05-02 23:55:09 -070019#define LOG_TAG "osi_semaphore"
20
21#include <assert.h>
22#include <errno.h>
Sharvil Nanavatia3164c92014-06-23 12:07:05 -070023#include <fcntl.h>
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070024#include <string.h>
25#include <sys/eventfd.h>
26#include <utils/Log.h>
27
Sharvil Nanavatieba49152014-08-01 18:40:15 -070028#include "osi.h"
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070029#include "semaphore.h"
30
31#if !defined(EFD_SEMAPHORE)
32# define EFD_SEMAPHORE (1 << 0)
33#endif
34
35struct semaphore_t {
36 int fd;
37};
38
39semaphore_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 Nanavati2cb29982014-08-01 17:00:12 -070043 if (ret->fd == INVALID_FD) {
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070044 ALOGE("%s unable to allocate semaphore: %s", __func__, strerror(errno));
45 free(ret);
46 ret = NULL;
47 }
48 }
49 return ret;
50}
51
52void semaphore_free(semaphore_t *semaphore) {
Sharvil Nanavati2cb29982014-08-01 17:00:12 -070053 if (semaphore->fd != INVALID_FD)
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070054 close(semaphore->fd);
55 free(semaphore);
56}
57
58void semaphore_wait(semaphore_t *semaphore) {
59 assert(semaphore != NULL);
Sharvil Nanavati2cb29982014-08-01 17:00:12 -070060 assert(semaphore->fd != INVALID_FD);
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070061
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 Nanavatia3164c92014-06-23 12:07:05 -070067bool semaphore_try_wait(semaphore_t *semaphore) {
68 assert(semaphore != NULL);
Sharvil Nanavati2cb29982014-08-01 17:00:12 -070069 assert(semaphore->fd != INVALID_FD);
Sharvil Nanavatia3164c92014-06-23 12:07:05 -070070
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 Nanavatic11b4072014-05-02 23:55:09 -070090void semaphore_post(semaphore_t *semaphore) {
91 assert(semaphore != NULL);
Sharvil Nanavati2cb29982014-08-01 17:00:12 -070092 assert(semaphore->fd != INVALID_FD);
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070093
94 if (eventfd_write(semaphore->fd, 1ULL) == -1)
95 ALOGE("%s unable to post to semaphore: %s", __func__, strerror(errno));
96}
Sharvil Nanavatif4013f22014-07-05 20:42:07 -070097
98int semaphore_get_fd(const semaphore_t *semaphore) {
99 assert(semaphore != NULL);
Sharvil Nanavati2cb29982014-08-01 17:00:12 -0700100 assert(semaphore->fd != INVALID_FD);
Sharvil Nanavatif4013f22014-07-05 20:42:07 -0700101 return semaphore->fd;
102}