| 293 | } // namespace bthread |
| 294 | |
| 295 | __BEGIN_DECLS |
| 296 | |
| 297 | int bthread_rwlock_init(bthread_rwlock_t* __restrict rwlock, |
| 298 | const bthread_rwlockattr_t* __restrict) { |
| 299 | int rc = bthread_sem_init(&rwlock->reader_sema, 0); |
| 300 | if (BAIDU_UNLIKELY(0 != rc)) { |
| 301 | return rc; |
| 302 | } |
| 303 | bthread_sem_disable_csite(&rwlock->reader_sema); |
| 304 | rc = bthread_sem_init(&rwlock->writer_sema, 0); |
| 305 | if (BAIDU_UNLIKELY(0 != rc)) { |
| 306 | bthread_sem_destroy(&rwlock->reader_sema); |
| 307 | return rc; |
| 308 | } |
| 309 | bthread_sem_disable_csite(&rwlock->writer_sema); |
| 310 | |
| 311 | rwlock->reader_count = 0; |
| 312 | rwlock->reader_wait = 0; |
| 313 | rwlock->wlock_flag = false; |
| 314 | |
| 315 | bthread_mutexattr_t attr; |
| 316 | bthread_mutexattr_init(&attr); |
| 317 | bthread_mutexattr_disable_csite(&attr); |
| 318 | rc = bthread_mutex_init(&rwlock->write_queue_mutex, &attr); |
| 319 | if (BAIDU_UNLIKELY(0 != rc)) { |
| 320 | bthread_sem_destroy(&rwlock->reader_sema); |
| 321 | bthread_sem_destroy(&rwlock->writer_sema); |
| 322 | return rc; |
| 323 | } |
| 324 | bthread_mutexattr_destroy(&attr); |
| 325 | |
| 326 | bthread::make_contention_site_invalid(&rwlock->writer_csite); |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | int bthread_rwlock_destroy(bthread_rwlock_t* rwlock) { |
| 332 | bthread_sem_destroy(&rwlock->reader_sema); |