| 1 |
// AprSharp, a wrapper library around the Apache Portable Runtime Library |
|---|
| 2 |
#region Copyright (C) 2004 SOFTEC sa. |
|---|
| 3 |
// |
|---|
| 4 |
// This library is free software; you can redistribute it and/or |
|---|
| 5 |
// modify it under the terms of the GNU Lesser General Public |
|---|
| 6 |
// License as published by the Free Software Foundation; either |
|---|
| 7 |
// version 2.1 of the License, or (at your option) any later version. |
|---|
| 8 |
// |
|---|
| 9 |
// This library is distributed in the hope that it will be useful, |
|---|
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 12 |
// Lesser General Public License for more details. |
|---|
| 13 |
// |
|---|
| 14 |
// You should have received a copy of the GNU Lesser General Public |
|---|
| 15 |
// License along with this library; if not, write to the Free Software |
|---|
| 16 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 17 |
// |
|---|
| 18 |
// Sources, support options and lastest version of the complete library |
|---|
| 19 |
// is available from: |
|---|
| 20 |
// http://www.softec.st/AprSharp |
|---|
| 21 |
// |
|---|
| 22 |
// |
|---|
| 23 |
// Initial authors : |
|---|
| 24 |
// Denis Gervalle |
|---|
| 25 |
// Olivier Desaive |
|---|
| 26 |
#endregion |
|---|
| 27 |
// |
|---|
| 28 |
using System; |
|---|
| 29 |
using System.Diagnostics; |
|---|
| 30 |
|
|---|
| 31 |
namespace Softec.AprSharp |
|---|
| 32 |
{ |
|---|
| 33 |
public struct AprThreadMutex : IAprUnmanaged |
|---|
| 34 |
{ |
|---|
| 35 |
IntPtr mThreadMutex; |
|---|
| 36 |
|
|---|
| 37 |
///<remark>Should be synchronized with #define from APR</remarks> |
|---|
| 38 |
public enum AprThreadMutexFlags |
|---|
| 39 |
{ |
|---|
| 40 |
Default, |
|---|
| 41 |
Nested, |
|---|
| 42 |
Unnested |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
#region Generic embedding functions of an IntPtr |
|---|
| 46 |
public AprThreadMutex(IntPtr ptr) |
|---|
| 47 |
{ |
|---|
| 48 |
mThreadMutex = ptr; |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
public bool IsNull |
|---|
| 52 |
{ |
|---|
| 53 |
get |
|---|
| 54 |
{ |
|---|
| 55 |
return( mThreadMutex == IntPtr.Zero ); |
|---|
| 56 |
} |
|---|
| 57 |
} |
|---|
| 58 |
|
|---|
| 59 |
private void CheckPtr() |
|---|
| 60 |
{ |
|---|
| 61 |
if( IsNull ) |
|---|
| 62 |
throw new AprNullReferenceException(); |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
public void ClearPtr() |
|---|
| 66 |
{ |
|---|
| 67 |
mThreadMutex = IntPtr.Zero; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
public IntPtr ToIntPtr() |
|---|
| 71 |
{ |
|---|
| 72 |
return mThreadMutex; |
|---|
| 73 |
} |
|---|
| 74 |
|
|---|
| 75 |
public bool ReferenceEquals(IAprUnmanaged obj) |
|---|
| 76 |
{ |
|---|
| 77 |
return(obj.ToIntPtr() == ToIntPtr()); |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
public static implicit operator IntPtr(AprThreadMutex threadMutex) |
|---|
| 81 |
{ |
|---|
| 82 |
return threadMutex.mThreadMutex; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
public static implicit operator AprThreadMutex(IntPtr ptr) |
|---|
| 86 |
{ |
|---|
| 87 |
return new AprThreadMutex(ptr); |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
public override string ToString() |
|---|
| 91 |
{ |
|---|
| 92 |
return("[apr_thread_mutex_t:"+mThreadMutex.ToInt32().ToString("X")+"]"); |
|---|
| 93 |
} |
|---|
| 94 |
#endregion |
|---|
| 95 |
|
|---|
| 96 |
#region Wrapper methods |
|---|
| 97 |
public static AprThreadMutex Create(AprPool pool) |
|---|
| 98 |
{ |
|---|
| 99 |
return(Create(AprThreadMutexFlags.Default, pool)); |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
public static AprThreadMutex Create( AprThreadMutexFlags flags, |
|---|
| 103 |
AprPool pool) |
|---|
| 104 |
{ |
|---|
| 105 |
IntPtr ptr; |
|---|
| 106 |
|
|---|
| 107 |
Debug.Write(String.Format("apr_thread_mutex_create({0},{1})...",flags,pool)); |
|---|
| 108 |
int res = Apr.apr_thread_mutex_create(out ptr, (uint)flags, pool); |
|---|
| 109 |
if(res != 0 ) |
|---|
| 110 |
throw new AprException(res); |
|---|
| 111 |
Debug.WriteLine(String.Format("Done({0:X})",((Int32)ptr))); |
|---|
| 112 |
|
|---|
| 113 |
return((AprThreadMutex) ptr); |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
public void Destroy() |
|---|
| 117 |
{ |
|---|
| 118 |
CheckPtr(); |
|---|
| 119 |
Debug.Write(String.Format("apr_thread_mutex_destroy({0:X})...",((Int32)mThreadMutex))); |
|---|
| 120 |
int res = Apr.apr_thread_mutex_destroy(mThreadMutex); |
|---|
| 121 |
if(res != 0 ) |
|---|
| 122 |
throw new AprException(res); |
|---|
| 123 |
Debug.WriteLine("Done"); |
|---|
| 124 |
ClearPtr(); |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
public void Lock() |
|---|
| 128 |
{ |
|---|
| 129 |
CheckPtr(); |
|---|
| 130 |
Debug.Write(String.Format("apr_thread_mutex_lock({0:X})...",((Int32)mThreadMutex))); |
|---|
| 131 |
int res = Apr.apr_thread_mutex_lock(mThreadMutex); |
|---|
| 132 |
if(res != 0 ) |
|---|
| 133 |
throw new AprException(res); |
|---|
| 134 |
Debug.WriteLine("Done"); |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
public bool TryLock() |
|---|
| 138 |
{ |
|---|
| 139 |
CheckPtr(); |
|---|
| 140 |
Debug.Write(String.Format("apr_thread_mutex_trylock({0:X})...",((Int32)mThreadMutex))); |
|---|
| 141 |
int res = Apr.apr_thread_mutex_trylock(mThreadMutex); |
|---|
| 142 |
if(res != 0 ) { |
|---|
| 143 |
if(res == 70025 ) { |
|---|
| 144 |
Debug.WriteLine(String.Format("Fail({0})",res)); |
|---|
| 145 |
return(false); |
|---|
| 146 |
} |
|---|
| 147 |
throw new AprException(res); |
|---|
| 148 |
} |
|---|
| 149 |
Debug.WriteLine(String.Format("Done({0})",res)); |
|---|
| 150 |
return(true); |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
public void Unlock() |
|---|
| 154 |
{ |
|---|
| 155 |
CheckPtr(); |
|---|
| 156 |
Debug.Write(String.Format("apr_thread_mutex_unlock({0:X})...",((Int32)mThreadMutex))); |
|---|
| 157 |
int res = Apr.apr_thread_mutex_unlock(mThreadMutex); |
|---|
| 158 |
if(res != 0 ) |
|---|
| 159 |
throw new AprException(res); |
|---|
| 160 |
Debug.WriteLine("Done"); |
|---|
| 161 |
} |
|---|
| 162 |
#endregion |
|---|
| 163 |
|
|---|
| 164 |
#region Wrapper properties |
|---|
| 165 |
public AprPool Pool |
|---|
| 166 |
{ |
|---|
| 167 |
get { |
|---|
| 168 |
Debug.WriteLine(String.Format("apr_thread_mutex_pool_get({0:X})",((Int32)mThreadMutex))); |
|---|
| 169 |
return(Apr.apr_thread_mutex_pool_get(mThreadMutex)); |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
#endregion |
|---|
| 173 |
} |
|---|
| 174 |
} |
|---|