blob: b55b14febf2cd23a4aa4448255c28069ae5ce530 [file] [log] [blame]
Charles Davis54c9eb62010-11-29 19:44:50 +00001//===- llvm/Support/Win32/Mutex.inc - Win32 Mutex Implementation -*- C++ -*-===//
Michael J. Spencer447762d2010-11-29 18:16:10 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael J. Spencer447762d2010-11-29 18:16:10 +00006//
Reid Spencerf4049812005-07-12 15:37:43 +00007//===----------------------------------------------------------------------===//
8//
9// This file implements the Win32 specific (non-pthread) Mutex class.
10//
11//===----------------------------------------------------------------------===//
12
13//===----------------------------------------------------------------------===//
14//=== WARNING: Implementation here must contain only generic Win32 code that
15//=== is guaranteed to work on *all* Win32 variants.
16//===----------------------------------------------------------------------===//
17
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000018#include "WindowsSupport.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000019#include "llvm/Support/Mutex.h"
Jeff Cohen3800a282005-07-13 02:15:18 +000020
21namespace llvm {
Reid Spencerf4049812005-07-12 15:37:43 +000022
Kristof Beylsa11dbf22017-03-31 14:26:44 +000023sys::MutexImpl::MutexImpl(bool /*recursive*/)
Reid Spencerf4049812005-07-12 15:37:43 +000024{
Jeff Cohen3800a282005-07-13 02:15:18 +000025 data_ = new CRITICAL_SECTION;
26 InitializeCriticalSection((LPCRITICAL_SECTION)data_);
Reid Spencerf4049812005-07-12 15:37:43 +000027}
28
Kristof Beylsa11dbf22017-03-31 14:26:44 +000029sys::MutexImpl::~MutexImpl()
Reid Spencerf4049812005-07-12 15:37:43 +000030{
Jeff Cohen3800a282005-07-13 02:15:18 +000031 DeleteCriticalSection((LPCRITICAL_SECTION)data_);
Jeff Cohen65806e62005-07-13 02:58:04 +000032 delete (LPCRITICAL_SECTION)data_;
Jeff Cohen3800a282005-07-13 02:15:18 +000033 data_ = 0;
Reid Spencerf4049812005-07-12 15:37:43 +000034}
35
Michael J. Spencer447762d2010-11-29 18:16:10 +000036bool
Kristof Beylsa11dbf22017-03-31 14:26:44 +000037sys::MutexImpl::acquire()
Reid Spencerf4049812005-07-12 15:37:43 +000038{
Jeff Cohen3800a282005-07-13 02:15:18 +000039 EnterCriticalSection((LPCRITICAL_SECTION)data_);
40 return true;
Reid Spencerf4049812005-07-12 15:37:43 +000041}
42
Michael J. Spencer447762d2010-11-29 18:16:10 +000043bool
Kristof Beylsa11dbf22017-03-31 14:26:44 +000044sys::MutexImpl::release()
Reid Spencerf4049812005-07-12 15:37:43 +000045{
Jeff Cohen3800a282005-07-13 02:15:18 +000046 LeaveCriticalSection((LPCRITICAL_SECTION)data_);
47 return true;
Reid Spencerf4049812005-07-12 15:37:43 +000048}
49
Michael J. Spencer447762d2010-11-29 18:16:10 +000050bool
Kristof Beylsa11dbf22017-03-31 14:26:44 +000051sys::MutexImpl::tryacquire()
Reid Spencerf4049812005-07-12 15:37:43 +000052{
Jeff Cohen3800a282005-07-13 02:15:18 +000053 return TryEnterCriticalSection((LPCRITICAL_SECTION)data_);
Reid Spencerf4049812005-07-12 15:37:43 +000054}
55
56}