isapego commented on code in PR #7285: URL: https://github.com/apache/ignite-3/pull/7285#discussion_r2642518921
########## modules/platforms/dotnet/Apache.Ignite/Internal/HybridTimestampTracker.cs: ########## @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +namespace Apache.Ignite.Internal; + +using System.Threading; + +/// <summary> +/// Hybrid timestamp tracker. +/// </summary> +internal sealed class HybridTimestampTracker +{ + private long _val; + + /// <summary> + /// Gets the current value. + /// </summary> + public long Value => Interlocked.Read(ref _val); + + /// <summary> + /// Updates the timestamp to max(newVal, currentVal). + /// </summary> + /// <param name="newVal">New value.</param> + /// <returns>Previous value.</returns> + public long Update(long newVal) + { + // Atomically update the observable timestamp to max(newTs, curTs). + while (true) + { + var current = Interlocked.Read(ref _val); + if (current >= newVal) + { + return current; + } + + if (Interlocked.CompareExchange(ref _val, newVal, current) == current) + { + return current; + } Review Comment: You can reduce number of readings in the loop to 1 if you move first read out of the loop and save value returned by the `CompareExchange`. I'm not sure it needs to be optimized though :) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
