public StubReference(string dllPath) { // according to MSDN, LoadLibrary requires "\" dllPath = dllPath.Replace("/", @"\"); this.library = Unmanaged.LoadLibrary(dllPath); if (this.library == IntPtr.Zero) { throw new Exception( String.Format("Could not load library '{0}' . Error code:{1}", dllPath, Unmanaged.GetLastError())); } }
Acquire() { if (Unmanaged.WaitForSingleObject(this.hMutex, INFINITE) == ABANDONED) { Console.WriteLine("warning: mutex abandoned\n{0}", System.Environment.StackTrace); } this.owner = Thread.CurrentThread.ManagedThreadId; this.count += 1; return(this.count); }
Dispose(bool disposing) { if (this.alive) { this.alive = false; foreach (IntPtr l in this.handles) { Unmanaged.FreeLibrary(l); } this.handles.Clear(); } }
IC_file_dealloc(IntPtr ptr) { if (this.FILEs.ContainsKey(ptr)) { Unmanaged.fclose(this.FILEs[ptr]); this.FILEs.Remove(ptr); } IntPtr _type = CPyMarshal.ReadPtrField(ptr, typeof(PyObject), "ob_type"); dgt_void_ptr freeDgt = (dgt_void_ptr) CPyMarshal.ReadFunctionPtrField( _type, typeof(PyTypeObject), "tp_free", typeof(dgt_void_ptr)); freeDgt(ptr); }
Dispose(bool disposing) { if (!this.alive) { return; } this.alive = false; while (this.exitfuncs.Count > 0) { this.exitfuncs.Pop()(); } PythonDictionary modules = (PythonDictionary)this.python.SystemState.Get__dict__()["modules"]; if (!modules.Contains("numpy")) { // TODO: FIXME? // I don't know what it is about numpy, but using it heavily // makes this step extremely flaky: that's, like, BSOD flaky. // OTOH, even attempting this operation is very optimistic // indeed, and it's only really here so that I can repeatedly // construct/destroy mappers -- without leaking *too* much -- // during test runs. In the wild, all this will be reclaimed // by the operating system at process shutdown time anyway. this.map.MapOverBridgePtrs(new PtrFunc(this.DumpPtr)); } this.allocator.FreeAll(); foreach (IntPtr FILE in this.FILEs.Values) { Unmanaged.fclose(FILE); } if (this.stub != null) { PythonCalls.Call(this.removeSysHacks); modules.Remove("mmap"); modules.Remove("posix"); modules.Remove("_csv"); if (modules.Contains("csv")) { modules.Remove("csv"); } this.importer.Dispose(); this.stub.Dispose(); } }
Init(dgt_getfuncptr addressGetter, dgt_registerdata dataSetter) { IntPtr initFP = Unmanaged.GetProcAddress(this.library, "init"); InitDelegate initDgt = (InitDelegate)Marshal.GetDelegateForFunctionPointer(initFP, typeof(InitDelegate)); IntPtr addressGetterFP = Marshal.GetFunctionPointerForDelegate(addressGetter); IntPtr dataSetterFP = Marshal.GetFunctionPointerForDelegate(dataSetter); initDgt(addressGetterFP, dataSetterFP); // yes, these do appear to be necessary: rare NullReferenceExceptions will be thrown // from the initDgt call otherwise. run functionalitytest in a loop to observe. GC.KeepAlive(addressGetter); GC.KeepAlive(dataSetter); }
Dispose(bool disposing) { if (this.hMutex == IntPtr.Zero) { return; } while (this.count > 0) { this.Release(); } Unmanaged.CloseHandle(this.hMutex); this.hMutex = IntPtr.Zero; }
Release() { if (!this.IsAcquired) { throw new LockException("you can't release a lock you don't own"); } this.count -= 1; if (this.count == 0) { this.owner = -1; } if (Unmanaged.ReleaseMutex(this.hMutex) == 0) { throw new LockException("ReleaseMutex failed, even though we're pretty certain we do own this lock"); } }
TryAcquire() { int result = Unmanaged.WaitForSingleObject(this.hMutex, 0); if (result == TIMEOUT) { return(false); } if (result == ABANDONED) { Console.WriteLine("warning: mutex abandoned\n{0}", System.Environment.StackTrace); } this.owner = Thread.CurrentThread.ManagedThreadId; this.count += 1; return(true); }
public StubReference(string dllPath) { this.library = Unmanaged.LoadLibrary(dllPath); }
Load(string path) { // the ActCtx stuff allows us to import .pyds which link to msvcr90 but don't have manifests // implementation explained in stub/ic_msvcr90.c IntPtr cookie = Unmanaged._Py_ActivateActCtx(); IntPtr l = Unmanaged.LoadLibrary(path); Unmanaged._Py_DeactivateActCtx(cookie); if (l == IntPtr.Zero) { throw new Exception( String.Format("Could not load library '{0}' . Error code:{1}", path, Unmanaged.GetLastError())); } this.handles.Add(l); string funcName = "init" + Path.GetFileNameWithoutExtension(path); IntPtr funcPtr = Unmanaged.GetProcAddress(l, funcName); if (funcPtr == IntPtr.Zero) { throw new Exception( String.Format("Could not find module init function {0} in dll {1}", funcName, path)); } PydInit_Delegate initmodule = (PydInit_Delegate)Marshal.GetDelegateForFunctionPointer( funcPtr, typeof(PydInit_Delegate)); initmodule(); }
public Lock() { this.hMutex = Unmanaged.CreateMutex(IntPtr.Zero, 0, IntPtr.Zero); this.count = 0; this.owner = -1; }