bthread_butex_unittest.cpp 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
gejun's avatar
gejun committed
17 18

#include <gtest/gtest.h>
19 20 21 22
#include "butil/atomicops.h"
#include "butil/time.h"
#include "butil/macros.h"
#include "butil/logging.h"
gejun's avatar
gejun committed
23 24 25 26 27 28 29
#include "bthread/butex.h"
#include "bthread/task_control.h"
#include "bthread/task_group.h"
#include "bthread/bthread.h"
#include "bthread/unstable.h"

namespace bthread {
30
extern butil::atomic<TaskControl*> g_task_control;
gejun's avatar
gejun committed
31
inline TaskControl* get_task_control() {
32
    return g_task_control.load(butil::memory_order_consume);
gejun's avatar
gejun committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
}
} // namespace bthread

namespace {
TEST(ButexTest, wait_on_already_timedout_butex) {
    uint32_t* butex = bthread::butex_create_checked<uint32_t>();
    ASSERT_TRUE(butex);
    timespec now;
    ASSERT_EQ(0, clock_gettime(CLOCK_REALTIME, &now));
    *butex = 1;
    ASSERT_EQ(-1, bthread::butex_wait(butex, 1, &now));
    ASSERT_EQ(ETIMEDOUT, errno);
}

void* sleeper(void* arg) {
    bthread_usleep((uint64_t)arg);
    return NULL;
}

void* joiner(void* arg) {
53
    const long t1 = butil::gettimeofday_us();
gejun's avatar
gejun committed
54 55 56 57
    for (bthread_t* th = (bthread_t*)arg; *th; ++th) {
        if (0 != bthread_join(*th, NULL)) {
            LOG(FATAL) << "fail to join thread_" << th - (bthread_t*)arg;
        }
58
        long elp = butil::gettimeofday_us() - t1;
59
        EXPECT_LE(labs(elp - (th - (bthread_t*)arg + 1) * 100000L), 15000L)
gejun's avatar
gejun committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            << "timeout when joining thread_" << th - (bthread_t*)arg;
        LOG(INFO) << "Joined thread " << *th << " at " << elp << "us ["
                  << bthread_self() << "]";
    }
    for (bthread_t* th = (bthread_t*)arg; *th; ++th) {
        EXPECT_EQ(0, bthread_join(*th, NULL));
    }
    return NULL;
}

struct A {
    uint64_t a;
    char dummy[0];
};

struct B {
    uint64_t a;
};


TEST(ButexTest, with_or_without_array_zero) {
    ASSERT_EQ(sizeof(B), sizeof(A));
}


TEST(ButexTest, join) {
    const size_t N = 6;
    const size_t M = 6;
    bthread_t th[N+1];
    bthread_t jth[M];
    pthread_t pth[M];
    for (size_t i = 0; i < N; ++i) {
        bthread_attr_t attr = (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        ASSERT_EQ(0, bthread_start_urgent(
                      &th[i], &attr, sleeper,
                      (void*)(100000L/*100ms*/ * (i + 1))));
    }
    th[N] = 0;  // joiner will join tids in `th' until seeing 0.
    for (size_t i = 0; i < M; ++i) {
        ASSERT_EQ(0, bthread_start_urgent(&jth[i], NULL, joiner, th));
    }
    for (size_t i = 0; i < M; ++i) {
        ASSERT_EQ(0, pthread_create(&pth[i], NULL, joiner, th));
    }
    
    for (size_t i = 0; i < M; ++i) {
        ASSERT_EQ(0, bthread_join(jth[i], NULL))
            << "i=" << i << " error=" << berror();
    }
    for (size_t i = 0; i < M; ++i) {
        ASSERT_EQ(0, pthread_join(pth[i], NULL));
    }
}


struct WaiterArg {
    int expected_result;
    int expected_value;
118
    butil::atomic<int> *butex;
gejun's avatar
gejun committed
119 120 121 122 123
    const timespec *ptimeout;
};

void* waiter(void* arg) {
    WaiterArg * wa = (WaiterArg*)arg;
124
    const long t1 = butil::gettimeofday_us();
gejun's avatar
gejun committed
125 126
    const int rc = bthread::butex_wait(
        wa->butex, wa->expected_value, wa->ptimeout);
127
    const long t2 = butil::gettimeofday_us();
gejun's avatar
gejun committed
128 129 130 131 132 133 134 135 136 137 138 139 140
    if (rc == 0) {
        EXPECT_EQ(wa->expected_result, 0) << bthread_self();
    } else {
        EXPECT_EQ(wa->expected_result, errno) << bthread_self();
    }
    LOG(INFO) << "after wait, time=" << (t2-t1) << "us";
    return NULL;
}

TEST(ButexTest, sanity) {
    const size_t N = 5;
    WaiterArg args[N * 4];
    pthread_t t1, t2;
141 142
    butil::atomic<int>* b1 =
        bthread::butex_create_checked<butil::atomic<int> >();
gejun's avatar
gejun committed
143 144 145
    ASSERT_TRUE(b1);
    bthread::butex_destroy(b1);
    
146
    b1 = bthread::butex_create_checked<butil::atomic<int> >();
gejun's avatar
gejun committed
147 148 149 150 151 152 153 154 155 156 157 158
    *b1 = 1;
    ASSERT_EQ(0, bthread::butex_wake(b1));

    WaiterArg *unmatched_arg = new WaiterArg;
    unmatched_arg->expected_value = *b1 + 1;
    unmatched_arg->expected_result = EWOULDBLOCK;
    unmatched_arg->butex = b1;
    unmatched_arg->ptimeout = NULL;
    pthread_create(&t2, NULL, waiter, unmatched_arg);
    bthread_t th;
    ASSERT_EQ(0, bthread_start_urgent(&th, NULL, waiter, unmatched_arg));

159
    const timespec abstime = butil::seconds_from_now(1);
gejun's avatar
gejun committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    for (size_t i = 0; i < 4*N; ++i) {
        args[i].expected_value = *b1;
        args[i].butex = b1;
        if ((i % 2) == 0) {
            args[i].expected_result = 0;
            args[i].ptimeout = NULL;
        } else {
            args[i].expected_result = ETIMEDOUT;
            args[i].ptimeout = &abstime;
        }
        if (i < 2*N) { 
            pthread_create(&t1, NULL, waiter, &args[i]);
        } else {
            ASSERT_EQ(0, bthread_start_urgent(&th, NULL, waiter, &args[i]));
        }
    }
    
    sleep(2);
    for (size_t i = 0; i < 2*N; ++i) {
        ASSERT_EQ(1, bthread::butex_wake(b1));
    }
    ASSERT_EQ(0, bthread::butex_wake(b1));
    sleep(1);
    bthread::butex_destroy(b1);
}


struct ButexWaitArg {
    int* butex;
    int expected_val;
    long wait_msec;
    int error_code;
};

void* wait_butex(void* void_arg) {
    ButexWaitArg* arg = static_cast<ButexWaitArg*>(void_arg);
196
    const timespec ts = butil::milliseconds_from_now(arg->wait_msec);
gejun's avatar
gejun committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210
    int rc = bthread::butex_wait(arg->butex, arg->expected_val, &ts);
    int saved_errno = errno;
    if (arg->error_code) {
        EXPECT_EQ(-1, rc);
        EXPECT_EQ(arg->error_code, saved_errno);
    } else {
        EXPECT_EQ(0, rc);
    }
    return NULL;
}

TEST(ButexTest, wait_without_stop) {
    int* butex = bthread::butex_create_checked<int>();
    *butex = 7;
211
    butil::Timer tm;
gejun's avatar
gejun committed
212 213 214 215 216 217 218 219 220 221 222 223
    const long WAIT_MSEC = 500;
    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        ButexWaitArg arg = { butex, *butex, WAIT_MSEC, ETIMEDOUT };
        bthread_t th;
        
        tm.start();
        ASSERT_EQ(0, bthread_start_urgent(&th, &attr, wait_butex, &arg));
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();
        
zhujiashun's avatar
zhujiashun committed
224
        ASSERT_LT(labs(tm.m_elapsed() - WAIT_MSEC), 250);
gejun's avatar
gejun committed
225 226 227 228 229 230 231
    }
    bthread::butex_destroy(butex);
}

TEST(ButexTest, stop_after_running) {
    int* butex = bthread::butex_create_checked<int>();
    *butex = 7;
232
    butil::Timer tm;
gejun's avatar
gejun committed
233 234 235 236 237 238
    const long WAIT_MSEC = 500;
    const long SLEEP_MSEC = 10;
    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        bthread_t th;
239
        ButexWaitArg arg = { butex, *butex, WAIT_MSEC, EINTR };
gejun's avatar
gejun committed
240 241 242 243 244 245 246 247

        tm.start();
        ASSERT_EQ(0, bthread_start_urgent(&th, &attr, wait_butex, &arg));
        ASSERT_EQ(0, bthread_usleep(SLEEP_MSEC * 1000L));
        ASSERT_EQ(0, bthread_stop(th));
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();

248
        ASSERT_LT(labs(tm.m_elapsed() - SLEEP_MSEC), 25);
gejun's avatar
gejun committed
249 250 251 252 253 254 255 256 257 258
        // ASSERT_TRUE(bthread::get_task_control()->
        //             timer_thread()._idset.empty());
        ASSERT_EQ(EINVAL, bthread_stop(th));
    }    
    bthread::butex_destroy(butex);
}

TEST(ButexTest, stop_before_running) {
    int* butex = bthread::butex_create_checked<int>();
    *butex = 7;
259
    butil::Timer tm;
gejun's avatar
gejun committed
260 261 262 263 264 265
    const long WAIT_MSEC = 500;

    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL) | BTHREAD_NOSIGNAL;
        bthread_t th;
266
        ButexWaitArg arg = { butex, *butex, WAIT_MSEC, EINTR };
gejun's avatar
gejun committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        
        tm.start();
        ASSERT_EQ(0, bthread_start_background(&th, &attr, wait_butex, &arg));
        ASSERT_EQ(0, bthread_stop(th));
        bthread_flush();
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();
        
        ASSERT_LT(tm.m_elapsed(), 5);
        // ASSERT_TRUE(bthread::get_task_control()->
        //             timer_thread()._idset.empty());
        ASSERT_EQ(EINVAL, bthread_stop(th));
    }
    bthread::butex_destroy(butex);
}

void* join_the_waiter(void* arg) {
284
    EXPECT_EQ(0, bthread_join((bthread_t)arg, NULL));
gejun's avatar
gejun committed
285 286 287 288 289 290 291
    return NULL;
}

TEST(ButexTest, join_cant_be_wakeup) {
    const long WAIT_MSEC = 100;
    int* butex = bthread::butex_create_checked<int>();
    *butex = 7;
292
    butil::Timer tm;
293
    ButexWaitArg arg = { butex, *butex, 1000, EINTR };
gejun's avatar
gejun committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        tm.start();
        bthread_t th, th2;
        ASSERT_EQ(0, bthread_start_urgent(&th, NULL, wait_butex, &arg));
        ASSERT_EQ(0, bthread_start_urgent(&th2, &attr, join_the_waiter, (void*)th));
        ASSERT_EQ(0, bthread_stop(th2));
        ASSERT_EQ(0, bthread_usleep(WAIT_MSEC / 2 * 1000L));
        ASSERT_TRUE(bthread::TaskGroup::exists(th));
        ASSERT_TRUE(bthread::TaskGroup::exists(th2));
        ASSERT_EQ(0, bthread_usleep(WAIT_MSEC / 2 * 1000L));
        ASSERT_EQ(0, bthread_stop(th));
        ASSERT_EQ(0, bthread_join(th2, NULL));
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();
311
        ASSERT_LT(tm.m_elapsed(), WAIT_MSEC + 15);
gejun's avatar
gejun committed
312 313 314 315 316 317 318
        ASSERT_EQ(EINVAL, bthread_stop(th));
        ASSERT_EQ(EINVAL, bthread_stop(th2));
    }
    bthread::butex_destroy(butex);
}

TEST(ButexTest, stop_after_slept) {
319
    butil::Timer tm;
gejun's avatar
gejun committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    const long SLEEP_MSEC = 100;
    const long WAIT_MSEC = 10;
    
    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        tm.start();
        bthread_t th;
        ASSERT_EQ(0, bthread_start_urgent(
                      &th, &attr, sleeper, (void*)(SLEEP_MSEC*1000L)));
        ASSERT_EQ(0, bthread_usleep(WAIT_MSEC * 1000L));
        ASSERT_EQ(0, bthread_stop(th));
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();
        if (attr.stack_type == BTHREAD_STACKTYPE_PTHREAD) {
335
            ASSERT_LT(labs(tm.m_elapsed() - SLEEP_MSEC), 15);
gejun's avatar
gejun committed
336
        } else {
337
            ASSERT_LT(labs(tm.m_elapsed() - WAIT_MSEC), 15);
gejun's avatar
gejun committed
338 339 340 341 342 343 344 345
        }
        // ASSERT_TRUE(bthread::get_task_control()->
        //             timer_thread()._idset.empty());
        ASSERT_EQ(EINVAL, bthread_stop(th));
    }
}

TEST(ButexTest, stop_just_when_sleeping) {
346
    butil::Timer tm;
gejun's avatar
gejun committed
347 348 349 350 351 352 353 354 355 356 357 358 359
    const long SLEEP_MSEC = 100;
    
    for (int i = 0; i < 2; ++i) {
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL);
        tm.start();
        bthread_t th;
        ASSERT_EQ(0, bthread_start_urgent(
                      &th, &attr, sleeper, (void*)(SLEEP_MSEC*1000L)));
        ASSERT_EQ(0, bthread_stop(th));
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();
        if (attr.stack_type == BTHREAD_STACKTYPE_PTHREAD) {
360
            ASSERT_LT(labs(tm.m_elapsed() - SLEEP_MSEC), 15);
gejun's avatar
gejun committed
361 362 363 364 365 366 367 368 369 370
        } else {
            ASSERT_LT(tm.m_elapsed(), 15);
        }
        // ASSERT_TRUE(bthread::get_task_control()->
        //             timer_thread()._idset.empty());
        ASSERT_EQ(EINVAL, bthread_stop(th));
    }
}

TEST(ButexTest, stop_before_sleeping) {
371
    butil::Timer tm;
gejun's avatar
gejun committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
    const long SLEEP_MSEC = 100;

    for (int i = 0; i < 2; ++i) {
        bthread_t th;
        const bthread_attr_t attr =
            (i == 0 ? BTHREAD_ATTR_PTHREAD : BTHREAD_ATTR_NORMAL) | BTHREAD_NOSIGNAL;
        
        tm.start();
        ASSERT_EQ(0, bthread_start_background(&th, &attr, sleeper,
                                              (void*)(SLEEP_MSEC*1000L)));
        ASSERT_EQ(0, bthread_stop(th));
        bthread_flush();
        ASSERT_EQ(0, bthread_join(th, NULL));
        tm.stop();

        if (attr.stack_type == BTHREAD_STACKTYPE_PTHREAD) {
388
            ASSERT_LT(labs(tm.m_elapsed() - SLEEP_MSEC), 10);
gejun's avatar
gejun committed
389 390 391 392 393 394 395 396 397
        } else {
            ASSERT_LT(tm.m_elapsed(), 10);
        }
        // ASSERT_TRUE(bthread::get_task_control()->
        //             timer_thread()._idset.empty());
        ASSERT_EQ(EINVAL, bthread_stop(th));
    }
}
} // namespace