lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [15/45] lucenenet git commit: Moved all threading-related types from Support into Lucene.Net.Support.Threading namespace
Date Sun, 30 Apr 2017 21:55:54 GMT
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/00b95661/src/Lucene.Net/Support/Threading/ThreadClass.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Support/Threading/ThreadClass.cs b/src/Lucene.Net/Support/Threading/ThreadClass.cs
new file mode 100644
index 0000000..c725e70
--- /dev/null
+++ b/src/Lucene.Net/Support/Threading/ThreadClass.cs
@@ -0,0 +1,331 @@
+/*
+ *
+ * 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.
+ *
+*/
+
+using System;
+using System.Threading;
+
+namespace Lucene.Net.Support.Threading
+{
+    /// <summary>
+    /// Support class used to handle threads
+    /// </summary>
+    public class ThreadClass : IThreadRunnable
+    {
+        /// <summary>
+        /// The instance of System.Threading.Thread
+        /// </summary>
+        private Thread _threadField;
+
+        /// <summary>
+        /// Initializes a new instance of the ThreadClass class
+        /// </summary>
+        public ThreadClass()
+        {
+            _threadField = new Thread(Run);
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the Thread class.
+        /// </summary>
+        /// <param name="name">The name of the thread</param>
+        public ThreadClass(string name)
+        {
+            _threadField = new Thread(Run);
+            this.Name = name;
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the Thread class.
+        /// </summary>
+        /// <param name="start">A ThreadStart delegate that references the methods
to be invoked when this thread begins executing</param>
+        public ThreadClass(ThreadStart start)
+        {
+            _threadField = new Thread(start);
+        }
+
+        /// <summary>
+        /// Initializes a new instance of the Thread class.
+        /// </summary>
+        /// <param name="start">A ThreadStart delegate that references the methods
to be invoked when this thread begins executing</param>
+        /// <param name="name">The name of the thread</param>
+        public ThreadClass(ThreadStart start, string name)
+        {
+            _threadField = new Thread(start);
+            this.Name = name;
+        }
+
+        /// <summary>
+        /// This method has no functionality unless the method is overridden
+        /// </summary>
+        public virtual void Run()
+        {
+        }
+
+        /// <summary>
+        /// Causes the operating system to change the state of the current thread instance
to ThreadState.Running
+        /// </summary>
+        public virtual void Start()
+        {
+            _threadField.Start();
+        }
+
+        /// <summary>
+        /// Interrupts a thread that is in the WaitSleepJoin thread state
+        /// </summary>
+        public virtual void Interrupt()
+        {
+#if !NETSTANDARD
+            _threadField.Interrupt();
+#endif
+        }
+
+        /// <summary>
+        /// Gets the current thread instance
+        /// </summary>
+        public System.Threading.Thread Instance
+        {
+            get
+            {
+                return _threadField;
+            }
+            set
+            {
+                _threadField = value;
+            }
+        }
+
+        /// <summary>
+        /// Gets or sets the name of the thread
+        /// </summary>
+        public String Name
+        {
+            get
+            {
+                return _threadField.Name;
+            }
+            set
+            {
+                if (_threadField.Name == null)
+                    _threadField.Name = value;
+            }
+        }
+
+        public void SetDaemon(bool isDaemon)
+        {
+            _threadField.IsBackground = isDaemon;
+        }
+
+#if !NETSTANDARD
+        /// <summary>
+        /// Gets or sets a value indicating the scheduling priority of a thread
+        /// </summary>
+        public ThreadPriority Priority
+        {
+           get
+           {
+               try
+               {
+                   return _threadField.Priority;
+               }
+               catch
+               {
+                   return ThreadPriority.Normal;
+               }
+           }
+           set
+           {
+               try
+               {
+                   _threadField.Priority = value;
+               }
+               catch { }
+           }
+        }
+#endif
+
+        /// <summary>
+        /// Gets a value indicating the execution status of the current thread
+        /// </summary>
+        public bool IsAlive
+        {
+            get
+            {
+                return _threadField.IsAlive;
+            }
+        }
+
+        /// <summary>
+        /// Gets or sets a value indicating whether or not a thread is a background thread.
+        /// </summary>
+        public bool IsBackground
+        {
+            get
+            {
+                return _threadField.IsBackground;
+            }
+            set
+            {
+                _threadField.IsBackground = value;
+            }
+        }
+
+        /// <summary>
+        /// Blocks the calling thread until a thread terminates
+        /// </summary>
+        public void Join()
+        {
+            _threadField.Join();
+        }
+
+        /// <summary>
+        /// Blocks the calling thread until a thread terminates or the specified time elapses
+        /// </summary>
+        /// <param name="milliSeconds">Time of wait in milliseconds</param>
+        public void Join(long milliSeconds)
+        {
+            _threadField.Join(Convert.ToInt32(milliSeconds));
+        }
+
+        /// <summary>
+        /// Blocks the calling thread until a thread terminates or the specified time elapses
+        /// </summary>
+        /// <param name="milliSeconds">Time of wait in milliseconds</param>
+        /// <param name="nanoSeconds">Time of wait in nanoseconds</param>
+        public void Join(long milliSeconds, int nanoSeconds)
+        {
+            int totalTime = Convert.ToInt32(milliSeconds + (nanoSeconds*0.000001));
+
+            _threadField.Join(totalTime);
+        }
+
+        /// <summary>
+        /// Resumes a thread that has been suspended
+        /// </summary>
+        public void Resume()
+        {
+            Monitor.PulseAll(_threadField);
+        }
+
+#if !NETSTANDARD
+
+        /// <summary>
+        /// Raises a ThreadAbortException in the thread on which it is invoked,
+        /// to begin the process of terminating the thread. Calling this method
+        /// usually terminates the thread
+        /// </summary>
+        public void Abort()
+        {
+            _threadField.Abort();
+        }
+
+        /// <summary>
+        /// Raises a ThreadAbortException in the thread on which it is invoked,
+        /// to begin the process of terminating the thread while also providing
+        /// exception information about the thread termination.
+        /// Calling this method usually terminates the thread.
+        /// </summary>
+        /// <param name="stateInfo">An object that contains application-specific information,
such as state, which can be used by the thread being aborted</param>
+        public void Abort(object stateInfo)
+        {
+            _threadField.Abort(stateInfo);
+        }
+#endif
+
+        /// <summary>
+        /// Suspends the thread, if the thread is already suspended it has no effect
+        /// </summary>
+        public void Suspend()
+        {
+            Monitor.Wait(_threadField);
+        }
+
+        /// <summary>
+        /// Obtain a String that represents the current object
+        /// </summary>
+        /// <returns>A String that represents the current object</returns>
+        public override System.String ToString()
+        {
+#if !NETSTANDARD
+            return "Thread[" + Name + "," + Priority.ToString() + "]";
+#else
+            return "Thread[" + Name + "]";
+#endif
+        }
+
+        [ThreadStatic]
+        private static ThreadClass This = null;
+
+        // named as the Java version
+        public static ThreadClass CurrentThread()
+        {
+            return Current();
+        }
+
+        public static void Sleep(long ms)
+        {
+            // casting long ms to int ms could lose resolution, however unlikely
+            // that someone would want to sleep for that long...
+            Thread.Sleep((int)ms);
+        }
+
+        /// <summary>
+        /// Gets the currently running thread
+        /// </summary>
+        /// <returns>The currently running thread</returns>
+        public static ThreadClass Current()
+        {
+            if (This == null)
+            {
+                This = new ThreadClass();
+                This.Instance = Thread.CurrentThread;
+            }
+            return This;
+        }
+
+        public static bool operator ==(ThreadClass t1, object t2)
+        {
+            if (((object)t1) == null) return t2 == null;
+            return t1.Equals(t2);
+        }
+
+        public static bool operator !=(ThreadClass t1, object t2)
+        {
+            return !(t1 == t2);
+        }
+
+        public override bool Equals(object obj)
+        {
+            if (obj == null) return false;
+            if (obj is ThreadClass) return this._threadField.Equals(((ThreadClass)obj)._threadField);
+            return false;
+        }
+
+        public override int GetHashCode()
+        {
+            return this._threadField.GetHashCode();
+        }
+
+        public ThreadState State
+        {
+            get { return _threadField.ThreadState; }
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/00b95661/src/Lucene.Net/Support/Threading/ThreadFactory.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Support/Threading/ThreadFactory.cs b/src/Lucene.Net/Support/Threading/ThreadFactory.cs
new file mode 100644
index 0000000..e9978b7
--- /dev/null
+++ b/src/Lucene.Net/Support/Threading/ThreadFactory.cs
@@ -0,0 +1,32 @@
+/*
+ *
+ * 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.
+ *
+*/
+
+// LUCENENET NOTE: The only class that depends on this is NamedThreadFactory, which is Java-specific,
so this is being excluded.
+
+//using System.Threading;
+
+//namespace Lucene.Net.Support.Threading
+//{
+//    public abstract class ThreadFactory
+//    {
+//        public abstract Thread NewThread(IThreadRunnable r);
+//    }
+//}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/00b95661/src/Lucene.Net/Support/Threading/ThreadLocal.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Support/Threading/ThreadLocal.cs b/src/Lucene.Net/Support/Threading/ThreadLocal.cs
new file mode 100644
index 0000000..aadb741
--- /dev/null
+++ b/src/Lucene.Net/Support/Threading/ThreadLocal.cs
@@ -0,0 +1,55 @@
+/*
+ * 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.
+ */
+
+#if NET35
+
+using System;
+
+namespace Lucene.Net.Support.Threading
+{
+    public class ThreadLocal<T> : IDisposable
+    {
+        [ThreadStatic]
+        static WeakDictionary<ThreadLocal<T>, T> slots;
+
+        static void Init()
+        {
+            if (slots == null) slots = new WeakDictionary<ThreadLocal<T>, T>();
+        }
+
+        public T Value
+        {
+            set
+            {
+                Init();
+                slots.Add(this, value);
+            }
+            get
+            {
+                Init();
+                return (T)slots[this];
+            }
+        }
+
+        public void Dispose()
+        {
+            if (slots != null) slots.Remove(this);
+        }
+    }
+}
+
+#endif
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/00b95661/src/Lucene.Net/Support/Threading/ThreadLock.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net/Support/Threading/ThreadLock.cs b/src/Lucene.Net/Support/Threading/ThreadLock.cs
new file mode 100644
index 0000000..d3b725c
--- /dev/null
+++ b/src/Lucene.Net/Support/Threading/ThreadLock.cs
@@ -0,0 +1,83 @@
+/*
+ *
+ * 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.
+ *
+*/
+
+using System.Threading;
+
+namespace Lucene.Net.Support.Threading
+{
+    /// <summary>
+    /// Abstract base class that provides a synchronization interface
+    /// for derived lock types
+    /// </summary>
+    public abstract class ThreadLock
+    {
+        public abstract void Enter(object obj);
+
+        public abstract void Exit(object obj);
+
+        private static readonly ThreadLock _nullLock = new NullThreadLock();
+        private static readonly ThreadLock _monitorLock = new MonitorThreadLock();
+
+        /// <summary>
+        /// A ThreadLock class that actually does no locking
+        /// Used in ParallelMultiSearcher/MultiSearcher
+        /// </summary>
+        public static ThreadLock NullLock
+        {
+            get { return _nullLock; }
+        }
+
+        /// <summary>
+        /// Wrapper class for the Monitor Enter/Exit methods
+        /// using the <see cref="ThreadLock"/> interface
+        /// </summary>
+        public static ThreadLock MonitorLock
+        {
+            get { return _monitorLock; }
+        }
+
+        private sealed class NullThreadLock : ThreadLock
+        {
+            public override void Enter(object obj)
+            {
+                // Do nothing
+            }
+
+            public override void Exit(object obj)
+            {
+                // Do nothing
+            }
+        }
+
+        private sealed class MonitorThreadLock : ThreadLock
+        {
+            public override void Enter(object obj)
+            {
+                Monitor.Enter(obj);
+            }
+
+            public override void Exit(object obj)
+            {
+                Monitor.Exit(obj);
+            }
+        }
+    }
+}
\ No newline at end of file


Mime
View raw message