00001 /******************************************************************************* 00002 00003 @file Resource.d 00004 00005 Copyright (C) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, March 2004 00031 @author Kris 00032 00033 00034 *******************************************************************************/ 00035 00036 module mango.io.Resource; 00037 00038 private import mango.io.model.IConduit, 00039 mango.io.model.IResource; 00040 00041 /******************************************************************************* 00042 00043 Protocol for acquiring and releasing a resource such as a file, or 00044 socket. This isn't strictly necessary, but will aid migration to a 00045 multi-threaded runtime environment. 00046 00047 *******************************************************************************/ 00048 00049 class Resource : IResource 00050 { 00051 protected Object _lock; 00052 private int usage; 00053 00054 /*********************************************************************** 00055 00056 Subclasses must implement closure mechanics. This is 00057 invoked when the number of close() invocations match 00058 that of aquire() -- the latter is typically called 00059 immediately following the successful construction of 00060 an IResource implementation; one should expect close 00061 to be required at least once. 00062 00063 ***********************************************************************/ 00064 00065 abstract protected void closure (); 00066 00067 00068 /*********************************************************************** 00069 00070 Set the default lock to be this object 00071 00072 ***********************************************************************/ 00073 00074 this () 00075 { 00076 _lock = this; 00077 } 00078 00079 /*********************************************************************** 00080 00081 Make a reasonable attempt to clean up 00082 00083 ***********************************************************************/ 00084 00085 ~this () 00086 { 00087 version (TraceLinux) 00088 { 00089 printf ("closing resource via destructor\n"); 00090 } 00091 00092 close(); 00093 } 00094 00095 /*********************************************************************** 00096 00097 Return the lock used by this resource 00098 00099 ***********************************************************************/ 00100 00101 Object getLock () 00102 { 00103 synchronized (_lock) 00104 return _lock; 00105 } 00106 00107 /*********************************************************************** 00108 00109 Set an alternate lock. Typically one would use this to 00110 synchronize multiple different Resource via a common 00111 object. 00112 00113 ***********************************************************************/ 00114 00115 void setLock (Object lock) 00116 in { 00117 assert (lock); 00118 } 00119 body 00120 { 00121 synchronized (_lock) 00122 _lock = lock; 00123 } 00124 00125 /*********************************************************************** 00126 00127 hold a reference to this resource. The caller is now 00128 responsible for invoking close() upon this resource; 00129 00130 ***********************************************************************/ 00131 00132 void acquire () 00133 { 00134 synchronized (_lock) 00135 { 00136 ++usage; 00137 } 00138 } 00139 00140 /*********************************************************************** 00141 00142 If this resource is still open, acquire it and return true. 00143 This is a threadsafe test-and-set mechanism that fails only 00144 if the resource is already closed, or was never opened. 00145 00146 Note that you are responsible for invoking close() whenever 00147 this method returns true. 00148 00149 ***********************************************************************/ 00150 00151 bool acquireIfOpen () 00152 { 00153 synchronized (_lock) 00154 { 00155 if (usage > 0) 00156 { 00157 ++usage; 00158 return true; 00159 } 00160 return false; 00161 } 00162 } 00163 00164 /*********************************************************************** 00165 00166 release a reference to this resource 00167 00168 ***********************************************************************/ 00169 00170 void close () 00171 { 00172 synchronized (_lock) 00173 { 00174 if (--usage == 0) 00175 closure(); 00176 } 00177 } 00178 }