blob: 7c1723be73fce24d018c1132e52bd0602f8188a1 [file] [log] [blame]
Reid Spencerb2164e52005-07-12 15:37:43 +00001//===- llvm/System/Win32/Mutex.inc - Win32 Mutex Implementation -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencerb2164e52005-07-12 15:37:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Win32 specific (non-pthread) Mutex class.
11//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15//=== WARNING: Implementation here must contain only generic Win32 code that
16//=== is guaranteed to work on *all* Win32 variants.
17//===----------------------------------------------------------------------===//
18
Jeff Cohen6d235222005-07-13 02:15:18 +000019#include "Win32.h"
20#include "llvm/System/Mutex.h"
21
22namespace llvm {
Reid Spencerb2164e52005-07-12 15:37:43 +000023using namespace sys;
24
Owen Anderson95efbcb2009-06-16 20:57:51 +000025Mutex::Mutex(bool /*recursive*/)
Reid Spencerb2164e52005-07-12 15:37:43 +000026{
Jeff Cohen6d235222005-07-13 02:15:18 +000027 data_ = new CRITICAL_SECTION;
28 InitializeCriticalSection((LPCRITICAL_SECTION)data_);
Reid Spencerb2164e52005-07-12 15:37:43 +000029}
30
31Mutex::~Mutex()
32{
Jeff Cohen6d235222005-07-13 02:15:18 +000033 DeleteCriticalSection((LPCRITICAL_SECTION)data_);
Jeff Cohen24972512005-07-13 02:58:04 +000034 delete (LPCRITICAL_SECTION)data_;
Jeff Cohen6d235222005-07-13 02:15:18 +000035 data_ = 0;
Reid Spencerb2164e52005-07-12 15:37:43 +000036}
37
38bool
39Mutex::acquire()
40{
Jeff Cohen6d235222005-07-13 02:15:18 +000041 EnterCriticalSection((LPCRITICAL_SECTION)data_);
42 return true;
Reid Spencerb2164e52005-07-12 15:37:43 +000043}
44
45bool
46Mutex::release()
47{
Jeff Cohen6d235222005-07-13 02:15:18 +000048 LeaveCriticalSection((LPCRITICAL_SECTION)data_);
49 return true;
Reid Spencerb2164e52005-07-12 15:37:43 +000050}
51
52bool
Jeff Cohen6d235222005-07-13 02:15:18 +000053Mutex::tryacquire()
Reid Spencerb2164e52005-07-12 15:37:43 +000054{
Jeff Cohen6d235222005-07-13 02:15:18 +000055 return TryEnterCriticalSection((LPCRITICAL_SECTION)data_);
Reid Spencerb2164e52005-07-12 15:37:43 +000056}
57
58}