Sourceware Bugzilla – Attachment 11382 Details for
Bug 23861
rdlock stalls indefinitely on an unlocked pthread rwlock
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
test program that exposes rdlock stall
wrloop.cc (text/x-c++src), 2.23 KB, created by
Rik Prohaska
on 2018-11-05 15:25:30 UTC
(
hide
)
Description:
test program that exposes rdlock stall
Filename:
MIME Type:
Creator:
Rik Prohaska
Created:
2018-11-05 15:25:30 UTC
Size:
2.23 KB
patch
obsolete
>// this program sometimes causes a thread to stall indefinately inside rdlock even >// when the reader writer lock is not locked. in addition, a thread may spin rather >// wait on the futex. both of these occur because the RWAITING bit is not set in the >// expected value in the futex_wait system call. these failures only occur when >// using the PREFER_WRITER_NONRECURSIVE attribute. >// this program works fine when using a PREFER_READER or PREFER_WRITER attribute. > >#include <stdlib.h> >#include <unistd.h> >#include <assert.h> >#include <cstring> >#include <atomic> >#include <thread> >#include <pthread.h> > >std::atomic_int do_exit = 0; >pthread_rwlockattr_t mylock_attr; >pthread_rwlock_t mylock; > >void wrloop(void) { > int r; > while (!do_exit) { > if (random() & 1) { > r = pthread_rwlock_wrlock(&mylock); > assert(r == 0); > r = pthread_rwlock_unlock(&mylock); > assert(r == 0); > } else { > r = pthread_rwlock_rdlock(&mylock); > assert(r == 0); > r = pthread_rwlock_unlock(&mylock); > assert(r == 0); > } > } >} > >int main(int argc, char *argv[]) { > int r; > > r = pthread_rwlockattr_init(&mylock_attr); > assert(r == 0); > if (argc > 1) { > if (strcmp(argv[1], "--prefer-reader") == 0) { > r = pthread_rwlockattr_setkind_np(&mylock_attr, PTHREAD_RWLOCK_PREFER_READER_NP); > assert(r == 0); > } else if (strcmp(argv[1], "--prefer-writer") == 0) { > r = pthread_rwlockattr_setkind_np(&mylock_attr, PTHREAD_RWLOCK_PREFER_WRITER_NP); > assert(r == 0); > } else if (strcmp(argv[1], "--prefer-writer-nonrecursive") == 0) { > r = pthread_rwlockattr_setkind_np(&mylock_attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP); > assert(r == 0); > } else > abort(); > } > r = pthread_rwlock_init(&mylock, &mylock_attr); > assert(r == 0); > > const int N = 3; > std::thread tids[N]; > for (int i=0; i<N; i++) > tids[i] = std::thread(wrloop); > sleep(60); > do_exit = 1; > for (int i=0; i<N; i++) > tids[i].join(); > r = pthread_rwlock_destroy(&mylock); > assert(r == 0); > r = pthread_rwlockattr_destroy(&mylock_attr); > assert(r == 0); > return 0; >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 23861
: 11382