/// <summary> /// Resets the tick count to zero. Timer must be stopped. /// </summary> public void Reset() { ThrowIfRunning(); Interlocked.Exchange(ref _ticks, 0); }
public void Deposit(int amount) { Interlocked.Add(ref balance, amount); }
public override bool Execute(ProgramOptions programOptions) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); StepTiming stepTimingFunction = new StepTiming(); stepTimingFunction.JobFileName = programOptions.ReportJobFilePath; stepTimingFunction.StepName = programOptions.ReportJob.Status.ToString(); stepTimingFunction.StepID = (int)programOptions.ReportJob.Status; stepTimingFunction.StartTime = DateTime.Now; stepTimingFunction.NumEntities = 0; this.DisplayJobStepStartingStatus(programOptions); this.FilePathMap = new FilePathMap(programOptions); try { FileIOHelper.CreateFolder(this.FilePathMap.Report_FolderPath()); FileIOHelper.CreateFolder(this.FilePathMap.Report_GraphViz_FolderPath()); FileIOHelper.CreateFolder(this.FilePathMap.Report_Diagram_SVG_FolderPath()); FileIOHelper.CreateFolder(this.FilePathMap.Report_Diagram_PNG_FolderPath()); FileIOHelper.CreateFolder(this.FilePathMap.Report_Diagram_PDF_FolderPath()); #region Construct object hierarchy with grants Account account = buildObjectHierarchyWithGrants(); #endregion List<Role> rolesList = FileIOHelper.ReadListFromCSVFile<Role>(FilePathMap.Report_RoleDetail_FilePath(), new RoleMap()); if (rolesList != null) { #region Make GraphViz charts loggerConsole.Info("Creating visualizations for {0} roles", rolesList.Count); Role syntheticRoleAll = new Role(); syntheticRoleAll.Name = "ALL_ROLES_TOGETHER_SYNTHETIC"; rolesList.Insert(0, syntheticRoleAll); ParallelOptions parallelOptions = new ParallelOptions(); if (programOptions.ProcessSequentially == true) { parallelOptions.MaxDegreeOfParallelism = 1; } int j = 0; Parallel.ForEach<Role, int>( rolesList, parallelOptions, () => 0, (role, loop, subtotal) => { logger.Info("Processing visualization for {0}", role); List<RoleHierarchy> thisRoleAndItsRelationsHierarchiesList = FileIOHelper.ReadListFromCSVFile<RoleHierarchy>(FilePathMap.Report_RoleHierarchy_RoleAndItsRelations_FilePath(role.Name), new RoleHierarchyMap());; List<Role> thisRoleAndItsRelationsList = FileIOHelper.ReadListFromCSVFile<Role>(FilePathMap.Report_RoleDetail_RoleAndItsRelations_FilePath(role.Name), new RoleMap()); if (role == syntheticRoleAll) { thisRoleAndItsRelationsHierarchiesList = FileIOHelper.ReadListFromCSVFile<RoleHierarchy>(FilePathMap.Report_RoleHierarchy_FilePath(), new RoleHierarchyMap());; thisRoleAndItsRelationsList = FileIOHelper.ReadListFromCSVFile<Role>(FilePathMap.Report_RoleDetail_FilePath(), new RoleMap()); } if (thisRoleAndItsRelationsList != null && thisRoleAndItsRelationsHierarchiesList != null) { Dictionary<string, Role> rolesDict = thisRoleAndItsRelationsList.ToDictionary(k => k.Name, r => r); Dictionary<string, Role> roleNamesOutput = new Dictionary<string, Role>(thisRoleAndItsRelationsList.Count); Role roleBeingOutput = null; StringBuilder sbGraphViz = new StringBuilder(64 * thisRoleAndItsRelationsHierarchiesList.Count + 128); // Start the graph and set its default settings sbGraphViz.AppendLine("digraph {"); sbGraphViz.AppendLine(" layout=\"dot\";"); sbGraphViz.AppendLine(" rankdir=\"TB\";"); sbGraphViz.AppendLine(" center=true;"); sbGraphViz.AppendLine(" splines=\"ortho\";"); sbGraphViz.AppendLine(" overlap=false;"); //sbGraphViz.AppendLine(" colorscheme=\"SVG\";"); sbGraphViz.AppendLine(" node [shape=\"rect\" style=\"filled,rounded\" fontname=\"Courier New\"];"); sbGraphViz.AppendLine(" edge [fontname=\"Courier New\"];"); sbGraphViz.AppendFormat(" // Graph for the Role {0}", role); sbGraphViz.AppendLine(); #region Role boxes // Role boxes sbGraphViz.AppendLine(); sbGraphViz.AppendLine(" // Roles"); sbGraphViz.AppendLine (" subgraph cluster_roles {"); sbGraphViz.AppendFormat(" label = \"roles related to: {0}\";", role); sbGraphViz.AppendLine(); // Add the role itself sbGraphViz.AppendFormat(" \"{0}\"{1};", role.Name.Replace("\"", "\\\""), getRoleStyleAttribute(role)); sbGraphViz.AppendLine(); roleNamesOutput.Add(role.Name, role); foreach (RoleHierarchy roleHierarchy in thisRoleAndItsRelationsHierarchiesList) { if (roleHierarchy.GrantedTo != "<NOTHING>" && roleNamesOutput.ContainsKey(roleHierarchy.GrantedTo) == false) { // Name of the role with color rolesDict.TryGetValue(roleHierarchy.GrantedTo, out roleBeingOutput); sbGraphViz.AppendFormat(" \"{0}\"{1};", roleHierarchy.GrantedTo.Replace("\"", "\\\""), getRoleStyleAttribute(roleBeingOutput)); sbGraphViz.AppendLine(); roleNamesOutput.Add(roleHierarchy.GrantedTo, roleBeingOutput); } if (roleNamesOutput.ContainsKey(roleHierarchy.Name) == false) { // Name of the role with color rolesDict.TryGetValue(roleHierarchy.Name, out roleBeingOutput); sbGraphViz.AppendFormat(" \"{0}\"{1};", roleHierarchy.Name.Replace("\"", "\\\""), getRoleStyleAttribute(roleBeingOutput)); sbGraphViz.AppendLine(); roleNamesOutput.Add(roleHierarchy.Name, roleBeingOutput); } } sbGraphViz.AppendLine(" }// /Roles"); #endregion #region Role hierachy // Role connections sbGraphViz.AppendLine(); sbGraphViz.AppendLine(" // Role hierarchy"); foreach (RoleHierarchy roleHierarchy in thisRoleAndItsRelationsHierarchiesList) { if (roleHierarchy.GrantedTo == "<NOTHING>") continue; // Role to role connector sbGraphViz.AppendFormat(" \"{0}\"->\"{1}\";", roleHierarchy.GrantedTo.Replace("\"", "\\\""), roleHierarchy.Name.Replace("\"", "\\\"")); sbGraphViz.AppendLine(); } sbGraphViz.AppendLine(" // /Role hierarchy"); #endregion if (role != syntheticRoleAll) { #region Databases, Schemas, Tables and Views sbGraphViz.AppendLine(); sbGraphViz.AppendLine(" // Databases"); sbGraphViz.AppendLine(" subgraph cluster_db_wrapper {"); sbGraphViz.AppendLine(" label = \"Databases\";"); sbGraphViz.AppendLine(); int databaseIndex = 0; foreach (Database database in account.Databases) { // Should output database bool isDatabaseRelatedToSelectedRole = false; foreach (Grant grant in database.Grants) { if (grant.Privilege == "USAGE" || grant.Privilege == "OWNERSHIP") { if (roleNamesOutput.ContainsKey(grant.GrantedTo) == true) { isDatabaseRelatedToSelectedRole = true; break; } } } if (isDatabaseRelatedToSelectedRole == false) continue; // Output database sbGraphViz.AppendFormat(" // Database {0}", database.FullName); sbGraphViz.AppendLine(); sbGraphViz.AppendFormat(" subgraph cluster_db_{0} {{", databaseIndex); sbGraphViz.AppendLine(); sbGraphViz.AppendLine (" style=\"filled\";"); sbGraphViz.AppendLine (" fillcolor=\"snow\";"); sbGraphViz.AppendFormat(" label = \"db: {0}\";", database.ShortName); sbGraphViz.AppendLine(); sbGraphViz.AppendLine (" node [shape=\"cylinder\" fillcolor=\"darkkhaki\"];"); sbGraphViz.AppendLine(); sbGraphViz.AppendFormat(" \"{0}\";", database.FullName); sbGraphViz.AppendLine(); sbGraphViz.AppendLine(); // List of schemas with number of tables and views sbGraphViz.AppendFormat(" \"{0}.schema\" [shape=\"folder\" label=<", database.FullName); sbGraphViz.AppendLine(); sbGraphViz.AppendLine (" <table border=\"0\" cellborder=\"1\" bgcolor=\"white\">"); sbGraphViz.AppendLine (" <tr><td>S</td><td>T</td><td>V</td></tr>"); int schemaLimit = 0; foreach (Schema schema in database.Schemas) { // Only output if (schemaLimit >= 10) { sbGraphViz.AppendFormat(" <tr><td align=\"left\">Up to {0}</td><td align=\"right\">...</td><td align=\"right\">...</td></tr>", database.Schemas.Count); sbGraphViz.AppendLine(); break; } // Do not output future grants which are in form of <SCHEMANAME> if (schema.ShortName.StartsWith("<") && schema.ShortName.EndsWith(">")) continue; sbGraphViz.AppendFormat(" <tr><td align=\"left\">{0}</td><td align=\"right\">{1}</td><td align=\"right\">{2}</td></tr>", System.Web.HttpUtility.HtmlEncode(schema.ShortName), schema.Tables.Count, schema.Views.Count); sbGraphViz.AppendLine(); schemaLimit++; } sbGraphViz.AppendLine (" </table>>];"); // Connect database to schemas sbGraphViz.AppendFormat(" \"{0}\"->\"{0}.schema\" [style=\"invis\"];", database.FullName); sbGraphViz.AppendLine(); sbGraphViz.AppendFormat(" }} // /Database {0}", database.FullName); sbGraphViz.AppendLine(); databaseIndex++; } sbGraphViz.AppendLine(" } // /Databases"); #endregion #region Roles using databases sbGraphViz.AppendLine(); sbGraphViz.AppendLine(" // Roles using databases"); // Output connectors from roles USAGE'ing databases foreach (Database database in account.Databases) { foreach (Grant grant in database.Grants) { if (grant.Privilege == "USAGE" || grant.Privilege == "OWNERSHIP") { if (roleNamesOutput.ContainsKey(grant.GrantedTo) == true) { sbGraphViz.AppendFormat(" \"{0}\"->\"{1}\" [color=\"darkkhaki\"];", grant.GrantedTo, grant.ObjectNameUnquoted); sbGraphViz.AppendLine(); } } } } sbGraphViz.AppendLine(" // /Roles using databases"); #endregion } #region Legend // Output Legend sbGraphViz.AppendLine(); string legend = @" // Legend ""legend"" [label=< <table border=""0"" cellborder=""0"" bgcolor=""white""> <tr><td align=""center"">Legend</td></tr> <tr><td align=""left"" bgcolor=""lightgray"">BUILT IN</td></tr> <tr><td align=""left"" bgcolor=""beige"">SCIM</td></tr> <tr><td align=""left"" bgcolor=""palegreen"">ROLE MANAGEMENT</td></tr> <tr><td align=""left"" bgcolor=""orchid"">FUNCTIONAL</td></tr> <tr><td align=""left"" bgcolor=""plum"">FUNCTIONAL, NOT UNDER SYSADMIN</td></tr> <tr><td align=""left"" bgcolor=""lightblue"">ACCESS</td></tr> <tr><td align=""left"" bgcolor=""azure"">ACCESS, NOT UNDER SYSADMIN</td></tr> <tr><td align=""left"" bgcolor=""navajowhite"">UNKNOWN</td></tr> <tr><td align=""left"" bgcolor=""orange"">UNKNOWN, NOT UNDER ACCOUNTADMIN</td></tr> </table>>];"; sbGraphViz.AppendLine(legend); #endregion // Close the graph sbGraphViz.AppendLine("}"); FileIOHelper.SaveFileToPath(sbGraphViz.ToString(), FilePathMap.Report_GraphViz_RoleAndItsRelationsGrants_FilePath(role.Name), false); } return 1; }, (finalResult) => { Interlocked.Add(ref j, finalResult); if (j % 50 == 0) { Console.Write("[{0}].", j); } } ); loggerConsole.Info("Completed {0} Roles", rolesList.Count); #endregion #region HTML file with Links to Files loggerConsole.Info("Creating HTML links for {0} roles", rolesList.Count); // Create the HTML page with links for all the images XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.OmitXmlDeclaration = true; xmlWriterSettings.Indent = true; using (XmlWriter xmlWriter = XmlWriter.Create(FilePathMap.UsersRolesAndGrantsWebReportFilePath(), xmlWriterSettings)) { xmlWriter.WriteDocType("html", null, null, null); xmlWriter.WriteStartElement("html"); xmlWriter.WriteStartElement("head"); xmlWriter.WriteStartElement("title"); xmlWriter.WriteString(String.Format("Snowflake Grants Report {0} {1} roles", programOptions.ReportJob.Connection, rolesList.Count)); xmlWriter.WriteEndElement(); // </title> xmlWriter.WriteEndElement(); // </head> xmlWriter.WriteStartElement("body"); xmlWriter.WriteStartElement("table"); xmlWriter.WriteAttributeString("border", "1"); // Header row xmlWriter.WriteStartElement("tr"); xmlWriter.WriteStartElement("th"); xmlWriter.WriteString("Role"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("th"); xmlWriter.WriteString("Type"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("th"); xmlWriter.WriteString("# Parents"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("th"); xmlWriter.WriteString("# Children"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("th"); xmlWriter.WriteString("# Ancestry Paths"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("th"); xmlWriter.WriteString("Online"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("th"); xmlWriter.WriteString("SVG"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("th"); xmlWriter.WriteString("PNG"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("th"); xmlWriter.WriteString("PDF"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); // </tr> foreach (Role role in rolesList) { xmlWriter.WriteStartElement("tr"); xmlWriter.WriteStartElement("td"); xmlWriter.WriteString(role.Name); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("td"); xmlWriter.WriteString(role.Type.ToString()); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("td"); xmlWriter.WriteString(role.NumParentRoles.ToString()); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("td"); xmlWriter.WriteString(role.NumChildRoles.ToString()); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("td"); xmlWriter.WriteString(role.NumAncestryPaths.ToString()); xmlWriter.WriteEndElement(); string graphText = FileIOHelper.ReadFileFromPath(FilePathMap.Report_GraphViz_RoleAndItsRelationsGrants_FilePath(role.Name)); xmlWriter.WriteStartElement("td"); // https://edotor.net xmlWriter.WriteStartElement("a"); xmlWriter.WriteAttributeString("href", String.Format("https://edotor.net/?#{0}", Uri.EscapeDataString(graphText))); xmlWriter.WriteString("Online"); xmlWriter.WriteEndElement(); // </a> // // http://magjac.com/graphviz-visual-editor // xmlWriter.WriteStartElement("a"); // xmlWriter.WriteAttributeString("href", String.Format("http://magjac.com/graphviz-visual-editor/?dot={0}", Uri.EscapeDataString(graphText))); // xmlWriter.WriteString("Opt2"); // xmlWriter.WriteEndElement(); // </a> // // https://stamm-wilbrandt.de/GraphvizFiddle/2.1.2/index.html // xmlWriter.WriteStartElement("a"); // xmlWriter.WriteAttributeString("href", String.Format("https://stamm-wilbrandt.de/GraphvizFiddle/2.1.2/index.html?#{0}", Uri.EscapeDataString(graphText))); // xmlWriter.WriteString("Opt3"); // xmlWriter.WriteEndElement(); // </a> // // https://dreampuf.github.io/GraphvizOnline // xmlWriter.WriteStartElement("a"); // xmlWriter.WriteAttributeString("href", String.Format("https://dreampuf.github.io/GraphvizOnline/#{0}", Uri.EscapeDataString(graphText))); // xmlWriter.WriteString("Opt4"); // xmlWriter.WriteEndElement(); // </a> xmlWriter.WriteEndElement(); // </td> xmlWriter.WriteStartElement("td"); xmlWriter.WriteStartElement("a"); xmlWriter.WriteAttributeString("href", FilePathMap.Report_Diagram_SVG_RoleAndItsRelationsGrants_FilePath(role.Name, false)); xmlWriter.WriteString("SVG"); xmlWriter.WriteEndElement(); // </a> xmlWriter.WriteEndElement(); // </td> xmlWriter.WriteStartElement("td"); xmlWriter.WriteStartElement("a"); xmlWriter.WriteAttributeString("href", FilePathMap.Report_Diagram_PNG_RoleAndItsRelationsGrants_FilePath(role.Name, false)); xmlWriter.WriteString("PNG"); xmlWriter.WriteEndElement(); // </a> xmlWriter.WriteEndElement(); // </td> xmlWriter.WriteStartElement("td"); xmlWriter.WriteStartElement("a"); xmlWriter.WriteAttributeString("href", FilePathMap.Report_Diagram_PDF_RoleAndItsRelationsGrants_FilePath(role.Name, false)); xmlWriter.WriteString("PDF"); xmlWriter.WriteEndElement(); // </a> xmlWriter.WriteEndElement(); // </td> xmlWriter.WriteEndElement(); // </tr> } xmlWriter.WriteEndElement(); // </table> xmlWriter.WriteEndElement(); // </body> xmlWriter.WriteEndElement(); // </html> } #endregion #region Make SVG, PNG and PDF Files with GraphViz binaries loggerConsole.Info("Making picture files for {0} roles", rolesList.Count); GraphVizDriver graphVizDriver = new GraphVizDriver(); graphVizDriver.ValidateToolInstalled(programOptions); if (graphVizDriver.ExecutableFilePath.Length > 0) { j = 0; Parallel.ForEach<Role, int>( rolesList, parallelOptions, () => 0, (role, loop, subtotal) => { loggerConsole.Info("Rendering graphs for {0}", role); graphVizDriver.ConvertGraphVizToFile( FilePathMap.Report_GraphViz_RoleAndItsRelationsGrants_FilePath(role.Name), FilePathMap.Report_Diagram_SVG_RoleAndItsRelationsGrants_FilePath(role.Name, true), "svg"); graphVizDriver.ConvertGraphVizToFile( FilePathMap.Report_GraphViz_RoleAndItsRelationsGrants_FilePath(role.Name), FilePathMap.Report_Diagram_PNG_RoleAndItsRelationsGrants_FilePath(role.Name, true), "png"); graphVizDriver.ConvertGraphVizToFile( FilePathMap.Report_GraphViz_RoleAndItsRelationsGrants_FilePath(role.Name), FilePathMap.Report_Diagram_PDF_RoleAndItsRelationsGrants_FilePath(role.Name, true), "pdf"); return 1; }, (finalResult) => { Interlocked.Add(ref j, finalResult); if (j % 10 == 0) { Console.Write("[{0}].", j); } } ); loggerConsole.Info("Completed {0} Roles", rolesList.Count); } #endregion } return true; } catch (Exception ex) { logger.Error(ex); loggerConsole.Error(ex); return false; } finally { stopWatch.Stop(); this.DisplayJobStepEndedStatus(programOptions, stopWatch); stepTimingFunction.EndTime = DateTime.Now; stepTimingFunction.Duration = stopWatch.Elapsed; stepTimingFunction.DurationMS = stopWatch.ElapsedMilliseconds; List<StepTiming> stepTimings = new List<StepTiming>(1); stepTimings.Add(stepTimingFunction); FileIOHelper.WriteListToCSVFile(stepTimings, new StepTimingReportMap(), FilePathMap.StepTimingReportFilePath(), true); } }
public void ResurrectedObjectsDisposedTest(Type type) { #if __WATCHOS__ if (Runtime.Arch == Arch.DEVICE) { Assert.Ignore("This test uses too much memory for the watch."); } #endif var invokerClassHandle = Class.GetHandle(typeof(ResurrectedObjectsDisposedTestClass)); // Create a number of native objects with no managed wrappers. // We create more than one to try to minimize the random effects // of the GC (a random value somewhere can keep a single object // from being collected, but the chances of X random values keeping // X objects from being collected are much lower). // Also the consequences if the GC doesn't collect an object is // that the test unexpectedly succeeds. // 10 objects seem to be a fine number that will cause pretty much // all test executions to fail. var objects = new IntPtr [10]; for (int i = 0; i < objects.Length; i++) { objects [i] = Messaging.IntPtr_objc_msgSend(Messaging.IntPtr_objc_msgSend(Class.GetHandle(type), Selector.GetHandle("alloc")), Selector.GetHandle("init")); } // Create a thread that creates managed wrappers for all of the above native objects. // We do this on a separate thread so that the GC finds no pointers to the managed // objects in any thread. var t1 = new Thread(() => { for (int i = 0; i < objects.Length; i++) { Messaging.bool_objc_msgSend_IntPtr_int(invokerClassHandle, Selector.GetHandle("invokeMe:wait:"), objects [i], 0); } }); t1.Start(); t1.Join(); // Collect all those managed wrappers, and make sure their finalizers are executed. GC.Collect(); GC.WaitForPendingFinalizers(); // Now all the managed wrappers should be on NSObject's drain queue on the main thread. // Spin up a thread for every native object, and invoke a managed method // that will fetch the managed wrapper and then wait for a while (500ms), // before verifying that the managed object hasn't been disposed while waiting. var invokerWait = 500; var threads = new Thread [objects.Length]; var brokenCount = 0; var countdown = new CountdownEvent(threads.Length); for (int t = 0; t < threads.Length; t++) { var tt = t; var thread = new Thread(() => { var ok = Messaging.bool_objc_msgSend_IntPtr_int(invokerClassHandle, Selector.GetHandle("invokeMe:wait:"), objects [tt], invokerWait); if (!ok) { //Console.WriteLine ("Broken #{0}: 0x{1}", tt, objects [tt].ToString ("x")); Interlocked.Increment(ref brokenCount); } countdown.Signal(); }); thread.Start(); } // Now all those threads should be waiting. // Run the runloop on the main thread, which will drain the managed wrappers // on NSObject's drain queue. while (!countdown.IsSet) { NSRunLoop.Current.RunUntil((NSDate)DateTime.Now.AddSeconds(0.1)); } // Release all the native objects we created for (int i = 0; i < objects.Length; i++) { Messaging.void_objc_msgSend(objects [i], Selector.GetHandle("release")); } Assert.AreEqual(0, brokenCount, "broken count"); }
/// <summary> /// Called to manually trigger a refresh of <see cref="IsCurrent"/>. /// After completion, <see cref="IsCurrentChanged"/> will always be /// raised, regardless of whether the values were changed. /// </summary> public virtual void RefreshIsCurrent() { #if DEBUG // Indicate that we've arrived here at least once. Otherwise we // will assert. _hasEverCheckedDatabase = true; #endif if (GlobalInterpreterOptions.SuppressFileSystemWatchers || string.IsNullOrEmpty(DatabasePath)) { _isCheckingDatabase = false; _isValid = true; _missingModules = null; _isCurrentException = null; OnIsCurrentChanged(); return; } if (IsGenerating) { if (PythonTypeDatabase.IsDatabaseRegenerating(DatabasePath)) { _isValid = false; _missingModules = null; _isCurrentException = null; return; } Interlocked.Exchange(ref _generating, null); } try { if (!_isCurrentSemaphore.Wait(0)) { // Another thread is working on our state, so we will wait for // them to finish and return, since the value is up to date. _isCurrentSemaphore.Wait(); _isCurrentSemaphore.Release(); return; } } catch (ObjectDisposedException) { // We've been disposed and the call has come in from // externally, probably a timer. return; } try { _isCheckingDatabase = true; OnIsCurrentChanged(); // Wait for a slot that will allow us to scan the disk. This // prevents too many threads from updating at once and causing // I/O saturation. _sharedIsCurrentSemaphore.Wait(); try { if (PythonTypeDatabase.IsDatabaseRegenerating(DatabasePath) || !PythonTypeDatabase.IsDatabaseVersionCurrent(DatabasePath)) { // Skip the rest of the checks, because we know we're // not current. _isValid = false; _missingModules = null; _isCurrentException = null; } else { _isValid = true; HashSet<string> existingDatabase = null; string[] missingModules = null; for (int retries = 3; retries > 0; --retries) { try { existingDatabase = GetExistingDatabase(DatabasePath); break; } catch (UnauthorizedAccessException) { } catch (IOException) { } Thread.Sleep(100); } if (existingDatabase == null) { // This will either succeed or throw again. If it throws // then the error is reported to the user. existingDatabase = GetExistingDatabase(DatabasePath); } for (int retries = 3; retries > 0; --retries) { try { missingModules = GetMissingModules(existingDatabase); break; } catch (UnauthorizedAccessException) { } catch (IOException) { } Thread.Sleep(100); } if (missingModules == null) { // This will either succeed or throw again. If it throws // then the error is reported to the user. missingModules = GetMissingModules(existingDatabase); } if (missingModules.Length > 0) { var oldModules = _missingModules; if (oldModules == null || oldModules.Length != missingModules.Length || !oldModules.SequenceEqual(missingModules)) { } _missingModules = missingModules; } else { _missingModules = null; } } _isCurrentException = null; } finally { _sharedIsCurrentSemaphore.Release(); } } catch (Exception ex) { // Report the exception text as the reason. _isCurrentException = ex.ToString(); _missingModules = null; } finally { _isCheckingDatabase = false; try { _isCurrentSemaphore.Release(); } catch (ObjectDisposedException) { // The semaphore is not locked for disposal as it is only // used to prevent reentrance into this function. As a // result, it may have been disposed while we were in here. } } OnIsCurrentChanged(); }
void Cleanup() { if (isInCleanup) { return; } isInCleanup = true; while (true) { lock (connectLock) { //Connect could technically fail try { Connect(); } catch (Exception ex) { Log.Info("failed! {0}", ex); var evt = this.OnException; if (evt != null) { evt(this, ex); } } } bool wasRemoved = false; lock (sentLock) { //See if anything is here to process if (sentNotifications.Count > 0) { //Don't expire any notifications while we are in a connecting state, o rat least ensure all notifications have been sent // in case we may have no connection because no notifications were causing a connection to be initiated if (connected) { //Get the oldest sent message var n = sentNotifications[0]; //If it was sent more than 3 seconds ago, // we have to assume it was sent successfully! if (n.SentAt < DateTime.UtcNow.AddMilliseconds(-1 * appleSettings.MillisecondsToWaitBeforeMessageDeclaredSuccess)) { wasRemoved = true; Interlocked.Decrement(ref trackedNotificationCount); Log.Info("Supposedly successful {0}", n.Notification.DeviceToken); if (n.Callback != null) { n.Callback(this, new SendNotificationResult(n.Notification)); } sentNotifications.RemoveAt(0); } else { wasRemoved = false; } } else { //In fact, if we weren't connected, bump up the sentat timestamp // so that we wait awhile after reconnecting to expire this message Log.Info("Bumping notification time - was not CONNECTED"); try { sentNotifications[0].SentAt = DateTime.UtcNow; } catch { } } } } //if (this.cancelToken.IsCancellationRequested) // break; //else if (!wasRemoved) { break; // Thread.Sleep(250); } } isInCleanup = false; }
public void Dispose() { var count = Interlocked.Decrement(ref _pool._count); _pool.Release(); }
public static int DumpTaskInner(string path, bool imports, bool missinghashes, bool texinfo, bool classinfo) { #region checks if (string.IsNullOrEmpty(path)) { ConsoleFunctions.logger.LogString("Please fill in an input path", Logtype.Error); return(0); } var isDirectory = false; var inputFileInfo = new FileInfo(path); var inputDirInfo = new DirectoryInfo(path); if (!inputFileInfo.Exists) { if (!inputDirInfo.Exists) { return(0); } else { isDirectory = true; } } #endregion var archives = new List <Archive>(); if (isDirectory) { archives.AddRange(inputDirInfo .GetFiles("*.archive", SearchOption.AllDirectories) .Select(_ => new Archive(_.FullName))); } else { archives.Add(new Archive(inputFileInfo.FullName)); } var mainController = ServiceLocator.Default.ResolveType <IHashService>(); var logger = ServiceLocator.Default.ResolveType <ILoggerService>(); var typedict = new ConcurrentDictionary <string, IEnumerable <string> >(); // Parallel foreach (var ar in archives) { if (classinfo) { using var mmf = MemoryMappedFile.CreateFromFile(ar.Filepath, FileMode.Open, ar.Filepath.GetHashMD5(), 0, MemoryMappedFileAccess.Read); var fileinfo = ar.Files.Values; var query = fileinfo.GroupBy( ext => Path.GetExtension(ext.FileName), file => file, (ext, finfo) => new { Key = ext, File = fileinfo.Where(_ => Path.GetExtension(_.FileName) == ext) }).ToList(); var total = query.Count; logger.LogString($"Exporting {total} bundle entries "); Thread.Sleep(1000); int progress = 0; logger.LogProgress(0); // foreach extension Parallel.ForEach(query, result => { if (!string.IsNullOrEmpty(result.Key)) { Parallel.ForEach(result.File, fi => { var(f, b) = ar.GetFileData(fi.NameHash64, mmf); using var ms = new MemoryStream(f); using var br = new BinaryReader(ms); var cr2w = new CR2WFile(); try { cr2w.ReadImportsAndBuffers(br); } catch (Exception e) { return; } foreach (var chunk in cr2w.Chunks) { var o = chunk.GetDumpObject(br); if (o != null) { Register(o); } } }); } Interlocked.Increment(ref progress); logger.LogProgress(progress / (float)total); logger.LogString($"Dumped extension {result.Key}", Logtype.Normal); }); } if (imports || texinfo) { using var mmf = MemoryMappedFile.CreateFromFile(ar.Filepath, FileMode.Open, ar.Filepath.GetHashMD5(), 0, MemoryMappedFileAccess.Read); var fileDictionary = new ConcurrentDictionary <ulong, Cr2wChunkInfo>(); var texDictionary = new ConcurrentDictionary <ulong, Cr2wTextureInfo>(); // get info var count = ar.FileCount; logger.LogString($"Exporting {count} bundle entries "); Thread.Sleep(1000); int progress = 0; logger.LogProgress(0); Parallel.For(0, count, i => { var entry = ar.Files.ToList()[i]; var hash = entry.Key; var filename = string.IsNullOrEmpty(entry.Value.FileName) ? hash.ToString() : entry.Value.FileName; if (imports) { var(f, buffers) = ar.GetFileData(hash, mmf); // check if cr2w file if (f.Length < 4) { return; } var id = f.Take(4); if (!id.SequenceEqual(MAGIC)) { return; } var cr2w = new CR2WFile(); using var ms = new MemoryStream(f); using var br = new BinaryReader(ms); cr2w.ReadImportsAndBuffers(br); var obj = new Cr2wChunkInfo { Filename = filename, Imports = cr2w.Imports }; fileDictionary.AddOrUpdate(hash, obj, (arg1, o) => obj); } if (texinfo) { if (!string.IsNullOrEmpty(entry.Value.FileName) && entry.Value.FileName.Contains(".xbm")) { var(f, buffers) = ar.GetFileData(hash, mmf); // check if cr2w file if (f.Length < 4) { return; } var id = f.Take(4); if (!id.SequenceEqual(MAGIC)) { return; } var cr2w = new CR2WFile(); using var ms = new MemoryStream(f); using var br = new BinaryReader(ms); var result = cr2w.Read(br); if (result != EFileReadErrorCodes.NoError) { return; } if (!(cr2w.Chunks.FirstOrDefault()?.data is CBitmapTexture xbm) || !(cr2w.Chunks[1]?.data is rendRenderTextureBlobPC blob)) { return; } // create dds header var texinfo = new Cr2wTextureInfo() { Filename = filename, width = blob.Header.SizeInfo.Width.val, height = blob.Header.SizeInfo.Height.val, mips = blob.Header.TextureInfo.MipCount.val, slicecount = blob.Header.TextureInfo.SliceCount.val, alignment = blob.Header.TextureInfo.DataAlignment.val, compression = xbm.Setup.Compression, Group = xbm.Setup.Group, rawFormat = xbm.Setup.RawFormat, }; texDictionary.AddOrUpdate(hash, texinfo, (arg1, o) => texinfo); } } Interlocked.Increment(ref progress); logger.LogProgress(progress / (float)count); });
internal MatchListingJoinStatus Join(ClientSession session) { if (!this._Clients.Contains(session)) { MatchListingJoinStatus canJoinStatus = this.CanJoin(session); if (canJoinStatus != MatchListingJoinStatus.Success) { return canJoinStatus; } while (true) //Concurrency complexity { int spotsLeft = this._SpotsLeft; if (spotsLeft > 0) { if (Interlocked.CompareExchange(ref this._SpotsLeft, spotsLeft - 1, spotsLeft) == spotsLeft) { break; } } else { //We are assuming if spotsLeft is int.MinValue it means the match has been started or is in progress of starting return spotsLeft == MatchListing.SPOTS_LEFT_GAME_STARTED ? MatchListingJoinStatus.Started : MatchListingJoinStatus.Full; } } if (this._Clients.Add(session)) { session.OnDisconnect -= this.OnCreatorDisconnectEarly; session.LobbySession.MatchListing = this; session.LobbySession.RemoveMatch(this); //Send the other clients foreach (ClientSession other in this._Clients.Values.OrderBy((c) => c == session)) { session.TrackUserInRoom(this.Name, other.SocketId, other.UserData.Id, other.UserData.Username, other.GetVars("userName", "rank", "hat", "head", "body", "feet", "hatColor", "headColor", "bodyColor", "feetColor", "socketID", "ping")); other.TrackUserInRoom(this.Name, session.SocketId, session.UserData.Id, session.UserData.Username, session.GetVars("userName", "rank", "hat", "head", "body", "feet", "hatColor", "headColor", "bodyColor", "feetColor", "socketID", "ping")); } foreach (ClientSession other in this.LobbyClients.Values) { other.TrackUserInRoom(this.Name, session.SocketId, session.UserData.Id, session.UserData.Username, session.GetVars("userName", "rank", "hat", "head", "body", "feet", "hatColor", "headColor", "bodyColor", "feetColor", "socketID", "ping")); } uint currentHost = this._HostSocketId; if (currentHost == session.SocketId || (currentHost == 0 && InterlockedExtansions.CompareExchange(ref this._HostSocketId, session.SocketId, currentHost) == currentHost)) { session.SendPacket(new MatchOwnerOutgoingMessage(this.Name, true, true, true)); } else if (!session.IsGuest) { bool play = session.HasPermissions(Permissions.ACCESS_FORCE_START_ANY_MATCH_LISTING); bool kick = session.HasPermissions(Permissions.ACCESS_KICK_ANY_MATCH_LISTING); bool ban = session.HasPermissions(Permissions.ACCESS_BAN_ANY_MATCH_LISTING); if (play || kick || ban) { session.SendPacket(new MatchOwnerOutgoingMessage(this.Name, play, kick, ban)); } } //Concurrency complexity if (Interlocked.Increment(ref this.UsersReady) == this.MaxMembers) //Ensures that there is no race condition while the SpotsLeft check is passed and user is being added to the clients list and ensuring safe start { if (Interlocked.CompareExchange(ref this._SpotsLeft, MatchListing.SPOTS_LEFT_GAME_STARTED, 0) == 0) //If no spots are left set the value to int.MinValue and start the match, starting should be thread-safe { this.Start(); } } return MatchListingJoinStatus.Success; } else { while (true) //Concurrency complexity { int spotsLeft = this._SpotsLeft; if (spotsLeft >= 0) { if (Interlocked.CompareExchange(ref this._SpotsLeft, spotsLeft + 1, spotsLeft) == spotsLeft) { break; } } } return MatchListingJoinStatus.Success; //We are gonna assume its failed due to dublication? } } else { return MatchListingJoinStatus.Success; } }
private void Leave0(ClientSession session) { session.LobbySession.MatchListing = null; //Concurrency complexity Interlocked.Decrement(ref this.UsersReady); //We can freely decrement this here without having issues foreach (ClientSession other in this._Clients.Sessions) { other.UntrackUserInRoom(this.Name, session.SocketId); } foreach (ClientSession other in this.LobbyClients.Sessions) { other.UntrackUserInRoom(this.Name, session.SocketId); } session.UntrackUsersInRoom(this.Name); if (this.Type != MatchListingType.LevelOfTheDay) { if (!this._Clients.MarkFullIf((int)this.MaxMembers)) { uint currentHost = this._HostSocketId; if (this.Type == MatchListingType.Normal && currentHost == session.SocketId) //Time to pick new host { //TODO: How should we handle friends only? while (this._Clients.Count > 0) { ClientSession other = this._Clients.Sessions.FirstOrDefault(); if (other != null) { if (Interlocked.CompareExchange(ref this._HostSocketId, other.SocketId, currentHost) == currentHost) { other.SendPacket(new MatchOwnerOutgoingMessage(this.Name, true, true, true)); break; } else { break; } } else { break; } } } } else { foreach (ClientSession other in this.LobbyClients.Sessions) { other.LobbySession.RemoveMatch(this); } this.matchListingManager.Die(this); } } }
/// <summary> /// Increments the assert count by one. /// </summary> public void IncrementAssertCount() { Interlocked.Increment(ref _assertCount); }
internal bool Join(ClientSession session) { session.Connection.OnDisconnected -= this.OnCreatorDisconnectEarly; MatchListingJoinStatus canJoinStatus = this.CanJoin(session); if (canJoinStatus != MatchListingJoinStatus.Success) { return(false); } if (!this._Clients.TryAdd(session)) { return(false); } session.LobbySession.MatchListing = this; session.LobbySession.RemoveMatch(this); //Send the other clients foreach (ClientSession other in this._Clients.Sessions.OrderBy((c) => c == session)) { session.TrackUserInRoom(this.Name, other.SocketId, other.UserData.Id, other.UserData.Username, other.GetVars("userName", "rank", "hat", "head", "body", "feet", "hatColor", "headColor", "bodyColor", "feetColor", "socketID", "ping")); other.TrackUserInRoom(this.Name, session.SocketId, session.UserData.Id, session.UserData.Username, session.GetVars("userName", "rank", "hat", "head", "body", "feet", "hatColor", "headColor", "bodyColor", "feetColor", "socketID", "ping")); } foreach (ClientSession other in this.LobbyClients.Sessions) { other.TrackUserInRoom(this.Name, session.SocketId, session.UserData.Id, session.UserData.Username, session.GetVars("userName", "rank", "hat", "head", "body", "feet", "hatColor", "headColor", "bodyColor", "feetColor", "socketID", "ping")); } uint currentHost = this._HostSocketId; if (this.Type == MatchListingType.Normal && (currentHost == session.SocketId || (currentHost == 0 && Interlocked.CompareExchange(ref this._HostSocketId, session.SocketId, currentHost) == currentHost))) { session.SendPacket(new MatchOwnerOutgoingMessage(this.Name, true, true, true)); } else if (!session.IsGuest) { bool play = session.HasPermissions(Permissions.ACCESS_FORCE_START_ANY_MATCH_LISTING); bool kick = session.HasPermissions(Permissions.ACCESS_KICK_ANY_MATCH_LISTING); bool ban = session.HasPermissions(Permissions.ACCESS_BAN_ANY_MATCH_LISTING); if (play || kick || ban) { session.SendPacket(new MatchOwnerOutgoingMessage(this.Name, play, kick, ban)); } } this.TryStartFull(); return(true); }
private bool CompileCombos() { DoProgress(2, 0); DoStatus("Compiling combos ..."); m_CompilationResults.Clear(); m_CompileError = false; m_CompileErrors.Clear(); CalcNumCombos(); if (m_NumCombos > 0x7fffffff) { DoError("Too many combos."); return(false); } string sourceString; string profileString = m_Profile.ToString(); WriteSourceString(out sourceString); int numCombos = (int)m_NumCombos; int queuedComobos = 0; for (int i = 0; i < numCombos; ++i) { DoProgress(2, (double)i / (double)m_NumCombos, false); DefineCombos(i); bool skip = m_Expression.Eval(); if (!skip) { // abort as we learn about past compile errors: if (m_CompileError) { break; } DoStatus("Queueing combo " + (i + 1) + "/" + m_NumCombos + " for compilation ...", false); CompileThreadClass ctc = new CompileThreadClass( this, sourceString, profileString, MakeMacros(i), i ); if (!ThreadPool.QueueUserWorkItem( CompileThreadProc, ctc )) { DoError("Failed to queue on ThreadPool for shaderCombo_" + i); return(false); } ++queuedComobos; } } int remainingCompiles = 0; while (0 != (remainingCompiles = Interlocked.CompareExchange(ref m_OutstandingCompiles, 0, 0))) { DoProgress(3, (double)(queuedComobos - remainingCompiles) / (double)queuedComobos); DoStatus("Waiting for combo compilation to finish (" + remainingCompiles + " remaining) ..."); Thread.Sleep(100); } if (0 < m_CompileErrors.Count) { DoError("Found compile errors, limiting to one error:"); DoError(m_CompileErrors.First.Value); return(false); } DoProgress(3, 1); return(true); }
/// <summary> /// All the requests should also include the obligatory POST parameter ‘nonce’ /// with incremental numerical value (>0). /// The incremental numerical value should never reiterate or decrease /// </summary> /// <param name="parameters"></param> public static void AddNonceParameter(this IDictionary <string, string> parameters) { var nonce = Interlocked.Increment(ref _nonce); parameters.Add("nonce", Convert.ToString(nonce)); }
public Editor() { fileIsOpen = false; Interlocked.Increment(ref instanceCount); InitializeComponent(); }
internal void Leave0(ClientSession session, MatchListingLeaveReason reason) { session.LobbySession.MatchListing = null; //Concurrency complexity Interlocked.Decrement(ref this.UsersReady); //We can freely decrement this here without having issues while (true) { int spotsLeft = this._SpotsLeft; if (spotsLeft != MatchListing.SPOTS_LEFT_GAME_STARTED) //Match has not been started or it is in progress of starting { if (Interlocked.CompareExchange(ref this._SpotsLeft, spotsLeft + 1, spotsLeft) == spotsLeft) { break; //Match has not been started } } } foreach (ClientSession other in this._Clients.Values) { other.UntrackUserInRoom(this.Name, session.SocketId); } foreach (ClientSession other in this.LobbyClients.Values) { other.UntrackUserInRoom(this.Name, session.SocketId); } session.UntrackUsersInRoom(this.Name); if (reason == MatchListingLeaveReason.Kicked) { session.SendPacket(new UserLeaveRoomOutgoingMessage(this.Name, session.SocketId)); } if (Interlocked.CompareExchange(ref this._SpotsLeft, (int)this.MaxMembers, MatchListing.SPOTS_LEFT_DIED) != (int)this.MaxMembers) { uint currentHost = this._HostSocketId; if (currentHost == session.SocketId) //Time to pick new host { //TODO: How should we handle friends only? while (this._Clients.Count > 0) { ClientSession other = this._Clients.Values.FirstOrDefault(); if (other != null) { if (InterlockedExtansions.CompareExchange(ref this._HostSocketId, other.SocketId, currentHost) == currentHost) { if (this._Clients.Contains(other)) { other.SendPacket(new MatchOwnerOutgoingMessage(this.Name, true, true, true)); break; } else { currentHost = other.SocketId; } } else { break; } } else { break; } } } } else { foreach (ClientSession other in this.LobbyClients.Values) { other.LobbySession.RemoveMatch(this); } PlatformRacing3Server.MatchListingManager.Die(this); } }
public void SendNotification(INotification notification, SendNotificationCallbackDelegate callback) { lock (sentLock) { Interlocked.Increment(ref trackedNotificationCount); var appleNotification = notification as AppleNotification; bool isOkToSend = true; byte[] notificationData = new byte[] {}; try { notificationData = appleNotification.ToBytes(); } catch (NotificationFailureException nfex) { //Bad notification format already isOkToSend = false; Interlocked.Decrement(ref trackedNotificationCount); Log.Info("After conversion - catchNotificationFailure {0} {0}", nfex, notification); if (callback != null) { callback(this, new SendNotificationResult(notification, false, nfex)); } } if (isOkToSend) { try { lock (connectLock) Connect(); lock (streamWriteLock) { bool stillConnected = client.Connected && client.Client.Poll(2500, SelectMode.SelectWrite) && networkStream.CanWrite; if (!stillConnected) { throw new ObjectDisposedException("Connection to APNS is not Writable"); } lock (sentLock) { Log.Info("Writing data to stream"); if (notificationData.Length > 45) { networkStream.Write(notificationData, 0, 45); networkStream.Write(notificationData, 45, notificationData.Length - 45); } else { networkStream.Write(notificationData, 0, notificationData.Length); } networkStream.Flush(); Log.Info("Stream Flushed"); sentNotifications.Add(new SentNotification(appleNotification) { Callback = callback }); } } } catch (ConnectionFailureException cex) { connected = false; //If this failed, we probably had a networking error, so let's requeue the notification Interlocked.Decrement(ref trackedNotificationCount); Log.Error("Connection failure {0} - {1}", cex, notification); if (callback != null) { callback(this, new SendNotificationResult(notification, false, cex)); } } catch (Exception ex) { connected = false; //If this failed, we probably had a networking error, so let's requeue the notification Interlocked.Decrement(ref trackedNotificationCount); Log.Error("Connection failure (Exception) - {0} - {1}", ex, notification); if (callback != null) { callback(this, new SendNotificationResult(notification, true, ex)); } } } } }
public OpcodeDispatcher(SocketUser user, NetPacket lastPacket) { Interlocked.Increment(ref ActiveDispatchersCount); User = user; LastPacket = lastPacket; }
public IndexReaderLease(IndexReaderPool pool, IndexReader reader) { _pool = pool; Interlocked.Increment(ref _pool._count); IndexReader = reader; }
public void AddRef() { Interlocked.Increment(ref m_nRefValue); }
public override IList<ClassificationSpan> GetClassificationSpans(SnapshotSpan span) { var spans = new List<ClassificationSpan>(); var htmlDoc = HtmlEditorDocumentFromTextBuffer(span.Snapshot.TextBuffer); if (htmlDoc == null) { return spans; } if (_htmlDoc == null) { _htmlDoc = htmlDoc; _htmlDoc.HtmlEditorTree.UpdateCompleted += HtmlEditorTree_UpdateCompleted; } else { Debug.Assert(htmlDoc == _htmlDoc); } // If the tree is not up to date with respect to the current snapshot, then artifact ranges and the // associated parse results are also not up to date. We cannot force a refresh here because this // can potentially change the buffer, which is not legal for GetClassificationSpans to do, and will // break the editor. Queue the refresh for later, and asynchronously notify the editor that it needs // to re-classify once it's done. if (!_htmlDoc.HtmlEditorTree.IsReady) { Interlocked.Increment(ref _deferredClassifications); return spans; } // The provided span may be in a projection snapshot, so we need to // map back to the source snapshot to find the correct // classification. If projSnapshot is null, we are already in the // correct snapshot. var projSnapshot = span.Snapshot as IProjectionSnapshot; var sourceSnapshot = span.Snapshot; var sourceStartIndex = span.Start.Position; if (projSnapshot != null) { var pt = projSnapshot.MapToSourceSnapshot(sourceStartIndex); sourceStartIndex = pt.Position; sourceSnapshot = pt.Snapshot; if (HtmlEditorDocument.TryFromTextBuffer(sourceSnapshot.TextBuffer) != _htmlDoc) { return spans; } } var index = _htmlDoc.HtmlEditorTree.ArtifactCollection.GetItemContaining(sourceStartIndex); if (index < 0) { return spans; } var artifact = _htmlDoc.HtmlEditorTree.ArtifactCollection[index] as TemplateArtifact; if (artifact == null) { return spans; } int artifactStart = artifact.InnerRange.Start; var artifactText = _htmlDoc.HtmlEditorTree.ParseTree.Text.GetText(artifact.InnerRange.Start, artifact.InnerRange.Length); artifact.Parse(artifactText); var classifications = artifact.GetClassifications(); foreach (var classification in classifications) { var cls = GetClassification(classification.Classification); int clsStart = artifactStart + classification.Span.Start; int clsLen = Math.Min(sourceSnapshot.Length - clsStart, classification.Span.Length); var clsSpan = new SnapshotSpan(sourceSnapshot, clsStart, clsLen); if (projSnapshot != null) { foreach (var sp in projSnapshot.MapFromSourceSnapshot(clsSpan)) { spans.Add(new ClassificationSpan(new SnapshotSpan(span.Snapshot, sp), cls)); } } else { spans.Add(new ClassificationSpan(clsSpan, cls)); } } return spans; }
public Task PutSameContentManyTimesTest(bool useRedundantPutFileShortcut) { var context = new Context(Logger); ContentStoreSettings = new ContentStoreSettings() { UseRedundantPutFileShortcut = useRedundantPutFileShortcut }; return TestStore(context, Clock, async store => { byte[] bytes = ThreadSafeRandom.GetBytes(ValueSize); ContentHash contentHash = bytes.CalculateHash(ContentHashType); // Verify content doesn't exist yet in store Assert.False(await store.ContainsAsync(context, contentHash, null)); using (var tempDirectory = new DisposableDirectory(FileSystem)) { ContentHash hashFromPut; using (var pinContext = store.CreatePinContext()) { var concurrency = 24; var iterations = 100; var items = Enumerable.Range(0, concurrency).Select(i => { AbsolutePath pathToContent = tempDirectory.Path / $"tempContent{i}.txt"; FileSystem.WriteAllBytes(pathToContent, bytes); return (pathToContent, iterations); }).ToArray(); int nonDuplicatedPuts = 0; await ParallelAlgorithms.WhenDoneAsync(24, CancellationToken.None, async (scheduleItem, item) => { // Put the content into the store w/ hard link var r = await store.PutFileAsync( context, item.pathToContent, FileRealizationMode.Any, ContentHashType, new PinRequest(pinContext)); hashFromPut = r.ContentHash; if (!r.ContentAlreadyExistsInCache) { Interlocked.Increment(ref nonDuplicatedPuts); } Clock.Increment(); Assert.True(pinContext.Contains(hashFromPut)); if (item.iterations != 0) { scheduleItem((item.pathToContent, item.iterations - 1)); } }, items); Assert.Equal(1, nonDuplicatedPuts); } } }); }
public IntPtr StealHandle() { var retval = Interlocked.Exchange(ref handle, IntPtr.Zero); return(retval); }
/// <summary>Initializes <see cref="customAttributes"/></summary> protected virtual void InitializeCustomAttributes() { Interlocked.CompareExchange(ref customAttributes, new CustomAttributeCollection(), null); }
public void IncrementReferenceCount() { int newRefCount = Interlocked.Increment(ref _referenceCount); Debug.Assert(newRefCount >= 2); }
/// <summary> /// Get a grain reference for the specified cache-key. /// The grain reference will either be taken from cahce, or a new one will be created by calling the <c>FetchValueDelegate</c> /// </summary> /// <param name="key"></param> /// <returns></returns> public TValue Get(TKey key) { TimestampedValue result; bool readerLockUpgraded = false; try { rwLock.EnterReadLock(); if (cache.TryGetValue(key, out result)) { result.Generation = Interlocked.Increment(ref nextGeneration); TimeSpan age = result.WhenLoaded.Subtract(DateTime.UtcNow); if (age > requiredFreshness) { try { rwLock.ExitReadLock(); readerLockUpgraded = true; rwLock.EnterWriteLock(); cache.Remove(key); } finally { rwLock.ExitWriteLock(); } result = null; } } if (result != null) return result.Value; } finally { if (!readerLockUpgraded) rwLock.ExitReadLock(); } try { rwLock.EnterWriteLock(); if (cache.TryGetValue(key, out result)) { result.Generation = Interlocked.Increment(ref nextGeneration); return result.Value; } while (cache.Count >= maximumCount) { long generationToDelete = Interlocked.Increment(ref generationToFree); KeyValuePair<TKey, TimestampedValue> entryToFree = cache.FirstOrDefault( kvp => kvp.Value.Generation == generationToDelete); if (entryToFree.Key != null) { cache.Remove(entryToFree.Key); } } result = new TimestampedValue {Generation = Interlocked.Increment(ref nextGeneration)}; try { var r = fetcher(key); result.Value = r; result.WhenLoaded = DateTime.UtcNow; cache.Add(key, result); } catch (Exception) { if (cache.ContainsKey(key)) cache.Remove(key); throw; } } finally { rwLock.ExitWriteLock(); } return result.Value; }
public void Withdraw(int amount) { Interlocked.Add(ref balance, -amount); //balance -= amount; }
public void Dispose() { DisposableHelper.Dispose(ref upstream); Interlocked.Exchange(ref current, DISPOSED)?.Dispose(); }
protected async Task <(RavenServer Leader, List <ProcessNode> Peers, List <RavenServer> LocalPeers)> CreateMixedCluster( string[] peers, int localPeers = 0, IDictionary <string, string> customSettings = null, X509Certificate2 certificate = null) { var leaderServer = GetNewServer(new ServerCreationOptions { CustomSettings = customSettings }); await leaderServer.ServerStore.EnsureNotPassiveAsync(leaderServer.WebUrl); var nodeAdded = new ManualResetEvent(false); var expectedMembers = 2; leaderServer.ServerStore.Engine.TopologyChanged += (sender, clusterTopology) => { var count = expectedMembers; if (clusterTopology.Promotables.Count == 0 && clusterTopology.Members.Count == count) { var result = Interlocked.CompareExchange(ref expectedMembers, count + 1, count); if (result == count) { nodeAdded.Set(); } } }; using (leaderServer.ServerStore.ContextPool.AllocateOperationContext(out JsonOperationContext context)) using (var requestExecutor = ClusterRequestExecutor.CreateForSingleNode(leaderServer.WebUrl, certificate)) { var local = new List <RavenServer>(); for (int i = 0; i < localPeers; i++) { var peer = GetNewServer(new ServerCreationOptions { CustomSettings = customSettings }); var addCommand = new AddClusterNodeCommand(peer.WebUrl); await requestExecutor.ExecuteAsync(addCommand, context); Assert.True(nodeAdded.WaitOne(TimeSpan.FromSeconds(30))); nodeAdded.Reset(); local.Add(peer); } var processes = new List <ProcessNode>(); foreach (var peer in peers) { processes.Add(await GetServerAsync(peer)); } foreach (var processNode in processes) { var addCommand = new AddClusterNodeCommand(processNode.Url); await requestExecutor.ExecuteAsync(addCommand, context); Assert.True(nodeAdded.WaitOne(TimeSpan.FromSeconds(30))); nodeAdded.Reset(); } Assert.Equal(peers.Length + localPeers + 1, leaderServer.ServerStore.GetClusterTopology().Members.Count); return(leaderServer, processes, local); } }
public bool Raise() { return(Interlocked.CompareExchange(ref _state, 1, 0) == 0); }