RefCounted part of Attachment object, placed into permanent pool
| 204 | // RefCounted part of Attachment object, placed into permanent pool |
| 205 | // |
| 206 | class StableAttachmentPart : public Firebird::RefCounted, public Firebird::GlobalStorage |
| 207 | { |
| 208 | public: |
| 209 | class Sync |
| 210 | { |
| 211 | public: |
| 212 | Sync() |
| 213 | : waiters(0), threadId(0), totalLocksCounter(0), currentLocksCounter(0) |
| 214 | { } |
| 215 | |
| 216 | void enter(const char* aReason) |
| 217 | { |
| 218 | ThreadId curTid = getThreadId(); |
| 219 | |
| 220 | if (threadId == curTid) |
| 221 | { |
| 222 | currentLocksCounter++; |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | if (threadId || !syncMutex.tryEnter(aReason)) |
| 227 | { |
| 228 | // we have contention with another thread |
| 229 | waiters.fetch_add(1, std::memory_order_relaxed); |
| 230 | syncMutex.enter(aReason); |
| 231 | waiters.fetch_sub(1, std::memory_order_relaxed); |
| 232 | } |
| 233 | |
| 234 | threadId = curTid; |
| 235 | totalLocksCounter++; |
| 236 | fb_assert(currentLocksCounter == 0); |
| 237 | currentLocksCounter++; |
| 238 | } |
| 239 | |
| 240 | bool tryEnter(const char* aReason) |
| 241 | { |
| 242 | ThreadId curTid = getThreadId(); |
| 243 | |
| 244 | if (threadId == curTid) |
| 245 | { |
| 246 | currentLocksCounter++; |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | if (threadId || !syncMutex.tryEnter(aReason)) |
| 251 | return false; |
| 252 | |
| 253 | threadId = curTid; |
| 254 | totalLocksCounter++; |
| 255 | fb_assert(currentLocksCounter == 0); |
| 256 | currentLocksCounter++; |
| 257 | return true; |
| 258 | } |
| 259 | |
| 260 | void leave() |
| 261 | { |
| 262 | fb_assert(currentLocksCounter > 0); |
| 263 |