Skip to content

Commit 8171f85

Browse files
committed
changed the SetPid function to return the value and remove the side effect.
1 parent 620b77b commit 8171f85

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

Bson/ObjectModel/ObjectId.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static ObjectId()
5555

5656
try
5757
{
58-
SetPidToCurrentProcessId();
58+
__staticPid = GetCurrentProcessId();
5959
}
6060
catch (SecurityException)
6161
{
@@ -362,6 +362,17 @@ public static void Unpack(byte[] bytes, out int timestamp, out int machine, out
362362
}
363363

364364
// private static methods
365+
/// <summary>
366+
/// Gets the current process id. This method exists because of how CAS operates on the call stack, checking
367+
/// for permissions before executing the method. Hence, if we inlined this call, the calling method would not execute
368+
/// before throwing an exception requiring the try/catch at an even higher level that we don't necessarily control.
369+
/// </summary>
370+
[MethodImpl(MethodImplOptions.NoInlining)]
371+
private static short GetCurrentProcessId()
372+
{
373+
return (short)Process.GetCurrentProcess().Id;
374+
}
375+
365376
private static int GetMachineHash()
366377
{
367378
var hostName = Environment.MachineName; // use instead of Dns.HostName so it will work offline
@@ -375,17 +386,6 @@ private static int GetTimestampFromDateTime(DateTime timestamp)
375386
return (int)Math.Floor((BsonUtils.ToUniversalTime(timestamp) - BsonConstants.UnixEpoch).TotalSeconds);
376387
}
377388

378-
/// <summary>
379-
/// Sets the pid to current process id. This method exists because of how CAS operates on the call stack, checking
380-
/// for permissions before executing the method. Hence, if we inlined this call, the calling method would not execute
381-
/// before throwing an exception requiring the try/catch at an even higher level that we don't necessarily control.
382-
/// </summary>
383-
[MethodImpl(MethodImplOptions.NoInlining)]
384-
private static void SetPidToCurrentProcessId()
385-
{
386-
__staticPid = (short)Process.GetCurrentProcess().Id;
387-
}
388-
389389
// public methods
390390
/// <summary>
391391
/// Compares this ObjectId to another ObjectId.

0 commit comments

Comments
 (0)