blob: 123469fae9fc675b985ae6f232cb919ee52fe2f5 [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
Zach Johnson384f8a92014-08-25 23:22:24 -070028#include "allocator.h"
Sharvil Nanavatieba49152014-08-01 18:40:15 -070029#include "osi.h"
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070030#include "semaphore.h"
31
32#if !defined(EFD_SEMAPHORE)
33# define EFD_SEMAPHORE (1 << 0)
34#endif
35
36struct semaphore_t {
37 int fd;
38};
39
40semaphore_t *semaphore_new(unsigned int value) {
Zach Johnson384f8a92014-08-25 23:22:24 -070041 semaphore_t *ret = osi_malloc(sizeof(semaphore_t));
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070042 if (ret) {
43 ret->fd = eventfd(value, EFD_SEMAPHORE);
Sharvil Nanavati2cb29982014-08-01 17:00:12 -070044 if (ret->fd == INVALID_FD) {
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070045 ALOGE("%s unable to allocate semaphore: %s", __func__, strerror(errno));
46 free(ret);
47 ret = NULL;
48 }
49 }
50 return ret;
51}
52
53void semaphore_free(semaphore_t *semaphore) {
Sharvil Nanavati2cb29982014-08-01 17:00:12 -070054 if (semaphore->fd != INVALID_FD)
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070055 close(semaphore->fd);
Zach Johnson384f8a92014-08-25 23:22:24 -070056 osi_free(semaphore);
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070057}
58
59void semaphore_wait(semaphore_t *semaphore) {
60 assert(semaphore != NULL);
Sharvil Nanavati2cb29982014-08-01 17:00:12 -070061 assert(semaphore->fd != INVALID_FD);
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070062
63 uint64_t value;
64 if (eventfd_read(semaphore->fd, &value) == -1)
65 ALOGE("%s unable to wait on semaphore: %s", __func__, strerror(errno));
66}
67
Sharvil Nanavatia3164c92014-06-23 12:07:05 -070068bool semaphore_try_wait(semaphore_t *semaphore) {
69 assert(semaphore != NULL);
Sharvil Nanavati2cb29982014-08-01 17:00:12 -070070 assert(semaphore->fd != INVALID_FD);
Sharvil Nanavatia3164c92014-06-23 12:07:05 -070071
72 int flags = fcntl(semaphore->fd, F_GETFL);
73 if (flags == -1) {
74 ALOGE("%s unable to get flags for semaphore fd: %s", __func__, strerror(errno));
75 return false;
76 }
77 if (fcntl(semaphore->fd, F_SETFL, flags | O_NONBLOCK) == -1) {
78 ALOGE("%s unable to set O_NONBLOCK for semaphore fd: %s", __func__, strerror(errno));
79 return false;
80 }
81
82 eventfd_t value;
83 if (eventfd_read(semaphore->fd, &value) == -1)
84 return false;
85
86 if (fcntl(semaphore->fd, F_SETFL, flags) == -1)
87 ALOGE("%s unable to resetore flags for semaphore fd: %s", __func__, strerror(errno));
88 return true;
89}
90
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070091void semaphore_post(semaphore_t *semaphore) {
92 assert(semaphore != NULL);
Sharvil Nanavati2cb29982014-08-01 17:00:12 -070093 assert(semaphore->fd != INVALID_FD);
Sharvil Nanavatic11b4072014-05-02 23:55:09 -070094
95 if (eventfd_write(semaphore->fd, 1ULL) == -1)
96 ALOGE("%s unable to post to semaphore: %s", __func__, strerror(errno));
97}
Sharvil Nanavatif4013f22014-07-05 20:42:07 -070098
99int semaphore_get_fd(const semaphore_t *semaphore) {
100 assert(semaphore != NULL);
Sharvil Nanavati2cb29982014-08-01 17:00:12 -0700101 assert(semaphore->fd != INVALID_FD);
Sharvil Nanavatif4013f22014-07-05 20:42:07 -0700102 return semaphore->fd;
103}