Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
ScopeTryLock.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Misc/AssertionMacros.h"
7#include "HAL/CriticalSection.h"
8
9/**
10 * Implements a scope lock using TryLock.
11 *
12 * This is a utility class that handles scope level locking using TryLock.
13 * Scope locking helps to avoid programming errors by which a lock is acquired
14 * and never released.
15 *
16 * <code>
17 * {
18 * // Try to acquire a lock on CriticalSection for the current scope.
19 * FScopeTryLock ScopeTryLock(CriticalSection);
20 * // Check that the lock was acquired.
21 * if (ScopeTryLock.IsLocked())
22 * {
23 * // If the lock was acquired, we can safely access resources protected
24 * // by the lock.
25 * }
26 * // When ScopeTryLock goes out of scope, the critical section will be
27 * // released if it was ever acquired.
28 * }
29 * </code>
30 */
32{
33public:
34 // Constructor that tries to lock the critical section. Note that you must
35 // call IsLocked to test that the lock was acquired.
36 FScopeTryLock(FCriticalSection *InCriticalSection)
37 : CriticalSection(InCriticalSection)
38 {
39 check(CriticalSection);
41 {
42 CriticalSection = nullptr;
43 }
44 }
45
46 // Destructor that will release the lock if it was ever acquired.
48 {
50 {
52 }
53 }
54
55 FORCEINLINE bool IsLocked() const
56 {
57 return nullptr != CriticalSection;
58 }
59
60private:
61 FScopeTryLock(FScopeTryLock const &InScopeLock) = delete;
63
65};
#define check(expr)
#define FORCEINLINE
Definition Platform.h:644
FWindowsCriticalSection FCriticalSection
FCriticalSection * CriticalSection
FORCEINLINE bool IsLocked() const
FScopeTryLock & operator=(FScopeTryLock &Rhs)=delete
FScopeTryLock(FScopeTryLock const &InScopeLock)=delete
FScopeTryLock(FCriticalSection *InCriticalSection)