root/trunk/AprSharp/dev/src/AprThreadMutex.cs

Revision 1, 4.0 kB (checked in by DenisG, 3 years ago)

Initial import

  • Property svn:eol-style set to native
Line 
1 //
2 // Softec
3 //
4 // Contact: Support@softec.st
5 //
6 // Designed by Denis Gervalle and Olivier Desaive
7 // Written by Denis Gervalle
8 //
9 // Copyright 2004 by SOFTEC. All rights reserved.
10 //
11 using System;
12 using System.Diagnostics;
13
14 namespace Softec.AprSharp
15 {
16     public struct AprThreadMutex
17     {
18         IntPtr mThreadMutex;
19        
20         ///<remark>Should be synchronized with #define from APR</remarks>
21         public enum AprThreadMutexFlags
22         {
23                Default,
24                Nested,
25                Unnested
26         }
27
28         #region Generic embedding functions of an IntPtr
29         private AprThreadMutex(IntPtr ptr)
30         {
31             mThreadMutex = ptr;
32         }
33        
34         public bool IsNull()
35         {
36             return( mThreadMutex == IntPtr.Zero );
37         }
38
39         private void CheckPtr()
40         {
41             if( IsNull() )
42                 throw new AprNullReferenceException();
43         }
44
45         public void ClearPtr()
46         {
47             mThreadMutex = IntPtr.Zero;
48         }
49
50         public static implicit operator IntPtr(AprThreadMutex threadMutex)
51         {
52             return threadMutex.mThreadMutex;
53         }
54        
55         public static implicit operator AprThreadMutex(IntPtr ptr)
56         {
57             return new AprThreadMutex(ptr);
58         }
59
60         public override string ToString()
61         {
62             return("[apr_thread_mutex_t:"+mThreadMutex.ToInt32().ToString("X")+"]");
63         }
64         #endregion
65
66         #region Wrapper methods
67         public static AprThreadMutex Create(AprPool pool)
68         {
69             return(Create(AprThreadMutexFlags.Default, pool));
70         }
71
72         public static AprThreadMutex Create( AprThreadMutexFlags flags,
73                                              AprPool pool)
74         {
75             IntPtr ptr;
76            
77             Debug.Write(String.Format("apr_thread_mutex_create({0},{1})...",flags,pool));
78             int res = Apr.apr_thread_mutex_create(out ptr, (uint)flags, pool);
79             if(res != 0 )
80                 throw new AprException(res);
81             Debug.WriteLine(String.Format("Done({0:X})",((Int32)ptr)));
82
83             return((AprThreadMutex) ptr);
84         }
85        
86         public void Destroy()
87         {
88             CheckPtr();
89             Debug.Write(String.Format("apr_thread_mutex_destroy({0:X})...",((Int32)mThreadMutex)));
90             int res = Apr.apr_thread_mutex_destroy(mThreadMutex);
91             if(res != 0 )
92                 throw new AprException(res);
93             Debug.WriteLine("Done");
94             ClearPtr();
95         }
96
97         public void Lock()
98         {
99             CheckPtr();
100             Debug.Write(String.Format("apr_thread_mutex_lock({0:X})...",((Int32)mThreadMutex)));
101             int res = Apr.apr_thread_mutex_lock(mThreadMutex);
102             if(res != 0 )
103                 throw new AprException(res);
104             Debug.WriteLine("Done");
105         }
106
107         public bool TryLock()
108         {
109             CheckPtr();
110             Debug.Write(String.Format("apr_thread_mutex_trylock({0:X})...",((Int32)mThreadMutex)));
111             int res = Apr.apr_thread_mutex_trylock(mThreadMutex);
112             if(res != 0 ) {
113                 if(res == 70025 ) {
114                     Debug.WriteLine("Fail");
115                     return(false);
116                 }
117                 throw new AprException(res);
118             }
119             Debug.WriteLine("Done");
120             return(true);
121         }
122        
123         public void Unlock()
124         {
125             CheckPtr();
126             Debug.Write(String.Format("apr_thread_mutex_unlock({0:X})...",((Int32)mThreadMutex)));
127             int res = Apr.apr_thread_mutex_unlock(mThreadMutex);
128             if(res != 0 )
129                 throw new AprException(res);
130             Debug.WriteLine("Done");
131         }
132         #endregion
133
134         #region Wrapper properties
135         public AprPool Pool
136         {
137             get {
138                 Debug.WriteLine(String.Format("apr_thread_mutex_pool_get({0:X})",((Int32)mThreadMutex)));
139                 return(Apr.apr_thread_mutex_pool_get(mThreadMutex));
140             }
141         }
142         #endregion
143     }
144 }
Note: See TracBrowser for help on using the browser.