1
2
3
4
5
6 using System;
7 using System.Concepts;
8
9 namespace System.Threading
10 {
11 public class Mutex
12 {
13 public nothrow Mutex() : nativeHandle(RtAllocateMutex())
14 {
15 }
16 public ~Mutex()
17 {
18 if (nativeHandle != null)
19 {
20 RtFreeMutex(nativeHandle);
21 }
22 }
23 suppress Mutex(const Mutex&);
24 suppress void operator=(Mutex&);
25 suppress Mutex(Mutex&&);
26 suppress void operator=(Mutex&&);
27 public nothrow void Lock()
28 {
29 RtLockMutex(nativeHandle);
30 }
31 public nothrow void Unlock()
32 {
33 RtUnlockMutex(nativeHandle);
34 }
35 private void* nativeHandle;
36 }
37
38 public class RecursiveMutex
39 {
40 public nothrow RecursiveMutex() : nativeHandle(RtAllocateRecursiveMutex())
41 {
42 }
43 public ~RecursiveMutex()
44 {
45 if (nativeHandle != null)
46 {
47 RtFreeRecursiveMutex(nativeHandle);
48 }
49 }
50 suppress RecursiveMutex(const RecursiveMutex&);
51 suppress void operator=(RecursiveMutex&);
52 suppress RecursiveMutex(RecursiveMutex&&);
53 suppress void operator=(RecursiveMutex&&);
54 public nothrow void Lock()
55 {
56 RtLockRecursiveMutex(nativeHandle);
57 }
58 public nothrow void Unlock()
59 {
60 RtUnlockRecursiveMutex(nativeHandle);
61 }
62 public nothrow void* NativeHandle() const
63 {
64 return nativeHandle;
65 }
66 private void* nativeHandle;
67 }
68
69 public concept Lockable<T>
70 {
71 void T.Lock();
72 void T.Unlock();
73 }
74
75 public class LockGuard<Mtx> where Mtx is Lockable
76 {
77 private typedef LockGuard<Mtx> Self;
78 public nothrow LockGuard(Mtx& mtx_) : mtx(mtx_)
79 {
80 mtx.Lock();
81 }
82 public ~LockGuard()
83 {
84 mtx.Unlock();
85 }
86 suppress LockGuard(const Self&);
87 suppress void operator=(const Self&);
88 suppress LockGuard(Self&&);
89 suppress void operator=(Self&&);
90 private Mtx& mtx;
91 }
92 }