OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2014 The Native Client Authors. All rights reserved. | |
3 * Use of this source code is governed by a BSD-style license that can be | |
4 * found in the LICENSE file. | |
5 */ | |
6 | |
7 #include <errno.h> | |
8 #include <pthread.h> | |
9 #include <stdio.h> | |
10 #include <time.h> | |
11 | |
12 #include "native_client/src/include/nacl_assert.h" | |
13 | |
14 static pthread_rwlock_t rwlock; | |
15 volatile int thread_has_lock = 0; | |
16 volatile int thread_should_acquire_lock = 0; | |
17 volatile int thread_should_release_lock = 0; | |
18 | |
19 void *locking_thread(void *unused) { | |
20 for (;;) { | |
21 while (!thread_should_acquire_lock) { /* Spin. */ } | |
22 | |
23 ASSERT_EQ(thread_has_lock, 0); | |
24 int rc = pthread_rwlock_rdlock(&rwlock); | |
25 ASSERT_EQ(rc, 0); | |
26 __sync_fetch_and_add(&thread_has_lock, 1); | |
27 | |
28 while (!thread_should_release_lock) { /* Spin. */ } | |
29 | |
30 ASSERT_EQ(thread_has_lock, 1); | |
31 rc = pthread_rwlock_unlock(&rwlock); | |
32 ASSERT_EQ(rc, 0); | |
33 __sync_fetch_and_sub(&thread_has_lock, 1); | |
34 } | |
35 | |
36 return NULL; | |
37 } | |
38 | |
39 void test_unlocked_with_zero_timestamp(void) { | |
40 int rc; | |
41 struct timespec abstime = { 0, 0 }; | |
42 ASSERT_EQ(thread_has_lock, 0); | |
43 fprintf(stderr, "Trying to lock the unlocked rwlock with a valid " | |
44 "zero absolute timestamp. " | |
45 "Expected to succeed instantly since the lock is free.\n"); | |
46 rc = pthread_rwlock_timedrdlock(&rwlock, &abstime); | |
47 ASSERT_EQ(rc, 0); | |
48 rc = pthread_rwlock_unlock(&rwlock); | |
49 ASSERT_EQ(rc, 0); | |
50 } | |
51 | |
52 int main(int argc, char **argv) { | |
53 int rc; | |
54 fprintf(stderr, "Running...\n"); | |
55 | |
56 pthread_rwlockattr_t attrs; | |
57 rc = pthread_rwlockattr_init(&attrs); | |
58 ASSERT_EQ(rc, 0); | |
59 rc = pthread_rwlock_init(&rwlock, &attrs); | |
60 ASSERT_EQ(rc, 0); | |
61 rc = pthread_rwlockattr_destroy(&attrs); | |
62 ASSERT_EQ(rc, 0); | |
63 | |
64 pthread_t thread; | |
65 rc = pthread_create(&thread, NULL, locking_thread, NULL); | |
Mark Seaborn
2014/10/02 19:58:51
AFAICT you don't synchronise with this thread, so
| |
66 ASSERT_EQ(rc, 0); | |
67 fprintf(stderr, "Thread started.\n"); | |
68 | |
69 test_unlocked_with_zero_timestamp(); | |
70 | |
71 fprintf(stderr, "Done.\n"); | |
72 return 0; | |
73 } | |
OLD | NEW |