blob: e517f818f31d762440095274e288c295545ccc5c [file] [log] [blame]
Elliott Hughes04303f52014-09-18 16:11:59 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <semaphore.h>
18
19#include <errno.h>
20#include <gtest/gtest.h>
21#include <limits.h>
22#include <pthread.h>
23#include <time.h>
24#include <unistd.h>
25
26#include "private/bionic_constants.h"
27
28TEST(semaphore, sem_init) {
29 sem_t s;
30
31 // Perfectly fine initial values.
32 ASSERT_EQ(0, sem_init(&s, 0, 0));
33 ASSERT_EQ(0, sem_init(&s, 0, 1));
34 ASSERT_EQ(0, sem_init(&s, 0, 123));
35
36 // Too small an initial value.
37 errno = 0;
38 ASSERT_EQ(-1, sem_init(&s, 0, -1));
39 ASSERT_EQ(EINVAL, errno);
40
41 ASSERT_EQ(SEM_VALUE_MAX, sysconf(_SC_SEM_VALUE_MAX));
42
43 // The largest initial value.
44 ASSERT_EQ(0, sem_init(&s, 0, SEM_VALUE_MAX));
45
46 // Too large an initial value.
47 errno = 0;
48 ASSERT_EQ(-1, sem_init(&s, 0, SEM_VALUE_MAX + 1));
49 ASSERT_EQ(EINVAL, errno);
50
51 ASSERT_EQ(0, sem_destroy(&s));
52}
53
54TEST(semaphore, sem_trywait) {
55 sem_t s;
56 ASSERT_EQ(0, sem_init(&s, 0, 3));
57 ASSERT_EQ(0, sem_trywait(&s));
58 ASSERT_EQ(0, sem_trywait(&s));
59 ASSERT_EQ(0, sem_trywait(&s));
60 errno = 0;
61 ASSERT_EQ(-1, sem_trywait(&s));
62 ASSERT_EQ(EAGAIN, errno);
63 ASSERT_EQ(0, sem_destroy(&s));
64}
65
66static void SemWaitThreadTestFn(sem_t& sem) {
67 ASSERT_EQ(0, sem_wait(&sem));
68}
69
70static void* SemWaitThreadFn(void* arg) {
71 SemWaitThreadTestFn(*reinterpret_cast<sem_t*>(arg));
72 return nullptr;
73}
74
75TEST(semaphore, sem_wait__sem_post) {
76 sem_t s;
77 ASSERT_EQ(0, sem_init(&s, 0, 0));
78
79 pthread_t t1, t2, t3;
80 ASSERT_EQ(0, pthread_create(&t1, NULL, SemWaitThreadFn, &s));
81 ASSERT_EQ(0, pthread_create(&t2, NULL, SemWaitThreadFn, &s));
82 ASSERT_EQ(0, pthread_create(&t3, NULL, SemWaitThreadFn, &s));
83
84 ASSERT_EQ(0, sem_post(&s));
85 ASSERT_EQ(0, sem_post(&s));
86 ASSERT_EQ(0, sem_post(&s));
87
88 void* result;
89 ASSERT_EQ(0, pthread_join(t1, &result));
90 ASSERT_EQ(0, pthread_join(t2, &result));
91 ASSERT_EQ(0, pthread_join(t3, &result));
92}
93
94static inline void timespec_add_ms(timespec& ts, size_t ms) {
95 ts.tv_sec += ms / 1000;
96 ts.tv_nsec += (ms % 1000) * 1000000;
97 if (ts.tv_nsec >= NS_PER_S) {
98 ts.tv_sec++;
99 ts.tv_nsec -= NS_PER_S;
100 }
101}
102
103TEST(semaphore, sem_timedwait) {
104 sem_t s;
105 ASSERT_EQ(0, sem_init(&s, 0, 0));
106
107 timespec ts;
108 ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &ts));
109 timespec_add_ms(ts, 100);
110
111 errno = 0;
112 ASSERT_EQ(-1, sem_timedwait(&s, &ts));
113 ASSERT_EQ(ETIMEDOUT, errno);
114
115 // A negative timeout is an error.
116 errno = 0;
117 ts.tv_nsec = -1;
118 ASSERT_EQ(-1, sem_timedwait(&s, &ts));
119 ASSERT_EQ(EINVAL, errno);
120
121 ASSERT_EQ(0, sem_destroy(&s));
122}
123
124TEST(semaphore, sem_getvalue) {
125 sem_t s;
126 ASSERT_EQ(0, sem_init(&s, 0, 0));
127
128 int i;
129 ASSERT_EQ(0, sem_getvalue(&s, &i));
130 ASSERT_EQ(0, i);
131
132 ASSERT_EQ(0, sem_post(&s));
133 ASSERT_EQ(0, sem_getvalue(&s, &i));
134 ASSERT_EQ(1, i);
135
136 ASSERT_EQ(0, sem_post(&s));
137 ASSERT_EQ(0, sem_getvalue(&s, &i));
138 ASSERT_EQ(2, i);
139
140 ASSERT_EQ(0, sem_wait(&s));
141 ASSERT_EQ(0, sem_getvalue(&s, &i));
142 ASSERT_EQ(1, i);
143}