1/*
2 Copyright (c) 2005-2019 Intel Corporation
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 "spin_rw_mutex_v2.h"
18#include "tbb/tbb_machine.h"
19#include "../tbb/itt_notify.h"
20#include "tbb/atomic.h"
21
22namespace tbb {
23
24using namespace internal;
25
26static inline bool CAS(volatile uintptr_t &addr, uintptr_t newv, uintptr_t oldv) {
27 return as_atomic(addr).compare_and_swap(newv, oldv) == oldv;
28}
29
30//! Signal that write lock is released
31void spin_rw_mutex::internal_itt_releasing(spin_rw_mutex *mutex) {
32 __TBB_ASSERT_EX(mutex, NULL); // To prevent compiler warnings
33 ITT_NOTIFY(sync_releasing, mutex);
34}
35
36//! Acquire write (exclusive) lock on the given mutex.
37bool spin_rw_mutex::internal_acquire_writer(spin_rw_mutex *mutex)
38{
39 ITT_NOTIFY(sync_prepare, mutex);
40 for( atomic_backoff backoff;;backoff.pause() ) {
41 state_t s = mutex->state;
42 if( !(s & BUSY) ) { // no readers, no writers
43 if( CAS(mutex->state, WRITER, s) )
44 break; // successfully stored writer flag
45 backoff.reset(); // we could be very close to complete op.
46 } else if( !(s & WRITER_PENDING) ) { // no pending writers
47 __TBB_AtomicOR(&mutex->state, WRITER_PENDING);
48 }
49 }
50 ITT_NOTIFY(sync_acquired, mutex);
51 __TBB_ASSERT( (mutex->state & BUSY)==WRITER, "invalid state of a write lock" );
52 return false;
53}
54
55//! Release write lock on the given mutex
56void spin_rw_mutex::internal_release_writer(spin_rw_mutex *mutex) {
57 __TBB_ASSERT( (mutex->state & BUSY)==WRITER, "invalid state of a write lock" );
58 ITT_NOTIFY(sync_releasing, mutex);
59 mutex->state = 0;
60}
61
62//! Acquire read (shared) lock on the given mutex.
63void spin_rw_mutex::internal_acquire_reader(spin_rw_mutex *mutex) {
64 ITT_NOTIFY(sync_prepare, mutex);
65 for( atomic_backoff backoff;;backoff.pause() ) {
66 state_t s = mutex->state;
67 if( !(s & (WRITER|WRITER_PENDING)) ) { // no writer or write requests
68 if( CAS(mutex->state, s+ONE_READER, s) )
69 break; // successfully stored increased number of readers
70 backoff.reset(); // we could be very close to complete op.
71 }
72 }
73 ITT_NOTIFY(sync_acquired, mutex);
74 __TBB_ASSERT( mutex->state & READERS, "invalid state of a read lock: no readers" );
75 __TBB_ASSERT( !(mutex->state & WRITER), "invalid state of a read lock: active writer" );
76}
77
78//! Upgrade reader to become a writer.
79/** Returns whether the upgrade happened without releasing and re-acquiring the lock */
80bool spin_rw_mutex::internal_upgrade(spin_rw_mutex *mutex) {
81 state_t s = mutex->state;
82 __TBB_ASSERT( s & READERS, "invalid state before upgrade: no readers " );
83 __TBB_ASSERT( !(s & WRITER), "invalid state before upgrade: active writer " );
84 // check and set writer-pending flag
85 // required conditions: either no pending writers, or we are the only reader
86 // (with multiple readers and pending writer, another upgrade could have been requested)
87 while( (s & READERS)==ONE_READER || !(s & WRITER_PENDING) ) {
88 if( CAS(mutex->state, s | WRITER_PENDING, s) )
89 {
90 ITT_NOTIFY(sync_prepare, mutex);
91 atomic_backoff backoff;
92 while( (mutex->state & READERS) != ONE_READER ) backoff.pause();
93 __TBB_ASSERT(mutex->state == (ONE_READER | WRITER_PENDING),"invalid state when upgrading to writer");
94 // both new readers and writers are blocked at this time
95 mutex->state = WRITER;
96 ITT_NOTIFY(sync_acquired, mutex);
97 __TBB_ASSERT( (mutex->state & BUSY) == WRITER, "invalid state after upgrade" );
98 return true; // successfully upgraded
99 } else {
100 s = mutex->state; // re-read
101 }
102 }
103 // slow reacquire
104 internal_release_reader(mutex);
105 return internal_acquire_writer(mutex); // always returns false
106}
107
108//! Downgrade writer to a reader
109void spin_rw_mutex::internal_downgrade(spin_rw_mutex *mutex) {
110 __TBB_ASSERT( (mutex->state & BUSY) == WRITER, "invalid state before downgrade" );
111 ITT_NOTIFY(sync_releasing, mutex);
112 mutex->state = ONE_READER;
113 __TBB_ASSERT( mutex->state & READERS, "invalid state after downgrade: no readers" );
114 __TBB_ASSERT( !(mutex->state & WRITER), "invalid state after downgrade: active writer" );
115}
116
117//! Release read lock on the given mutex
118void spin_rw_mutex::internal_release_reader(spin_rw_mutex *mutex)
119{
120 __TBB_ASSERT( mutex->state & READERS, "invalid state of a read lock: no readers" );
121 __TBB_ASSERT( !(mutex->state & WRITER), "invalid state of a read lock: active writer" );
122 ITT_NOTIFY(sync_releasing, mutex); // release reader
123 __TBB_FetchAndAddWrelease((volatile void *)&(mutex->state),-(intptr_t)ONE_READER);
124}
125
126//! Try to acquire write lock on the given mutex
127bool spin_rw_mutex::internal_try_acquire_writer( spin_rw_mutex * mutex )
128{
129 // for a writer: only possible to acquire if no active readers or writers
130 state_t s = mutex->state; // on IA-64 architecture, this volatile load has acquire semantic
131 if( !(s & BUSY) ) // no readers, no writers; mask is 1..1101
132 if( CAS(mutex->state, WRITER, s) ) {
133 ITT_NOTIFY(sync_acquired, mutex);
134 return true; // successfully stored writer flag
135 }
136 return false;
137}
138
139//! Try to acquire read lock on the given mutex
140bool spin_rw_mutex::internal_try_acquire_reader( spin_rw_mutex * mutex )
141{
142 // for a reader: acquire if no active or waiting writers
143 state_t s = mutex->state; // on IA-64 architecture, a load of volatile variable has acquire semantic
144 while( !(s & (WRITER|WRITER_PENDING)) ) // no writers
145 if( CAS(mutex->state, s+ONE_READER, s) ) {
146 ITT_NOTIFY(sync_acquired, mutex);
147 return true; // successfully stored increased number of readers
148 }
149 return false;
150}
151
152} // namespace tbb
153