aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorJustin Clark-Casey (justincc)2014-07-14 20:08:11 +0100
committerJustin Clark-Casey2014-08-02 00:54:42 +0100
commit539ffb776919e0285b052f7268893a37d387deac (patch)
tree82ccfb5207c380a7310469b822139094a02ff130
parentCall RemoveScriptInstance when removing from inventory (diff)
downloadopensim-SC_OLD-539ffb776919e0285b052f7268893a37d387deac.zip
opensim-SC_OLD-539ffb776919e0285b052f7268893a37d387deac.tar.gz
opensim-SC_OLD-539ffb776919e0285b052f7268893a37d387deac.tar.bz2
opensim-SC_OLD-539ffb776919e0285b052f7268893a37d387deac.tar.xz
Use thread-safe version of .NET Random as the SDK class is not thread-safe.
As per http://msdn.microsoft.com/en-us/library/system.random%28v=vs.100%29.aspx, the .NET Random class is not thread-safe. If called by multiple threads at once, methods may return 0. Except for llRand(), other OpenSimulator code did not lock before calling a shared Random instance. This commit adds a ThreadSafeRandom class that extends Random but does internal locking so that it is thread-safe. This change is invisible to existing callers and the explicit locking in the llFrand() implementation is now redundant.
-rw-r--r--OpenSim/Framework/ThreadSafeRandom.cs72
-rw-r--r--OpenSim/Framework/Util.cs2
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs6
3 files changed, 75 insertions, 5 deletions
diff --git a/OpenSim/Framework/ThreadSafeRandom.cs b/OpenSim/Framework/ThreadSafeRandom.cs
new file mode 100644
index 0000000..58853e6
--- /dev/null
+++ b/OpenSim/Framework/ThreadSafeRandom.cs
@@ -0,0 +1,72 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29
30namespace OpenSim.Framework
31{
32 /// <summary>
33 /// A thread-safe Random since the .NET version is not.
34 /// See http://msdn.microsoft.com/en-us/library/system.random%28v=vs.100%29.aspx
35 /// </summary>
36 public class ThreadSafeRandom : Random
37 {
38 public ThreadSafeRandom() : base() {}
39
40 public ThreadSafeRandom(int seed): base (seed) {}
41
42 public override int Next()
43 {
44 lock (this)
45 return base.Next();
46 }
47
48 public override int Next(int maxValue)
49 {
50 lock (this)
51 return base.Next(maxValue);
52 }
53
54 public override int Next(int minValue, int maxValue)
55 {
56 lock (this)
57 return base.Next(minValue, maxValue);
58 }
59
60 public override void NextBytes(byte[] buffer)
61 {
62 lock (this)
63 base.NextBytes(buffer);
64 }
65
66 public override double NextDouble()
67 {
68 lock (this)
69 return base.NextDouble();
70 }
71 }
72} \ No newline at end of file
diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs
index 729281c..9d7de97 100644
--- a/OpenSim/Framework/Util.cs
+++ b/OpenSim/Framework/Util.cs
@@ -138,7 +138,7 @@ namespace OpenSim.Framework
138 } 138 }
139 139
140 private static uint nextXferID = 5000; 140 private static uint nextXferID = 5000;
141 private static Random randomClass = new Random(); 141 private static Random randomClass = new ThreadSafeRandom();
142 142
143 // Get a list of invalid file characters (OS dependent) 143 // Get a list of invalid file characters (OS dependent)
144 private static string regexInvalidFileChars = "[" + new String(Path.GetInvalidFileNameChars()) + "]"; 144 private static string regexInvalidFileChars = "[" + new String(Path.GetInvalidFileNameChars()) + "]";
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs
index 7d8821c..991107e 100644
--- a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/LSL_Api.cs
@@ -544,10 +544,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.Api
544 public LSL_Float llFrand(double mag) 544 public LSL_Float llFrand(double mag)
545 { 545 {
546 m_host.AddScriptLPS(1); 546 m_host.AddScriptLPS(1);
547 lock (Util.RandomClass) 547
548 { 548 return Util.RandomClass.NextDouble() * mag;
549 return Util.RandomClass.NextDouble() * mag;
550 }
551 } 549 }
552 550
553 public LSL_Integer llFloor(double f) 551 public LSL_Integer llFloor(double f)