<commit_before>/*  $Id$
*  
*  Project: Swicli.Library - Two Way Interface for .NET and MONO to SWI-Prolog
*  Author:        <NAME>
*  E-mail:        <EMAIL>
*  WWW:           http://www.logicmoo.com
*  Copyright (C):  2010-2012 LogicMOO Developement
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*********************************************************/
#if USE_IKVM
using IKVM.Internal;
using ikvm.runtime;
using java.net;
using jpl;
using Hashtable = java.util.Hashtable;
using ClassLoader = java.lang.ClassLoader;
using Class = java.lang.Class;
using sun.reflect.misc;
#endif
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using SbsSW.SwiPlCs;
using SbsSW.SwiPlCs.Callback;
using SbsSW.SwiPlCs.Exceptions;
using CycFort = SbsSW.SwiPlCs.PlTerm;
using PrologCli = Swicli.Library.PrologClient;

namespace Swicli.Library
{
    public partial class PrologClient
    {
		static public bool ClientReady = false;
        static public object _ThreadRegLock = new object();
        static public object ThreadRegLock
        {
            get
            {
                return LockInfo.Watch(_ThreadRegLock);
            }
        }
        public static bool SaneThreadWorld = true;
        public static Dictionary<Thread, int> ThreadRegisterations = new Dictionary<Thread, int>();
        public static Dictionary<Thread, int> ForiegnFrameCounts = new Dictionary<Thread, int>();

        internal static int IncrementUseCount(Thread thread)
        {
            return IncrementUseCount(thread, ThreadRegisterations);
        }
        internal static int DecrementUseCount(Thread thread)
        {
            return DecrementUseCount(thread, ThreadRegisterations);
        }
        internal static int IncrementUseCount(Thread thread, Dictionary<Thread, int> registration)
        {
            lock (ThreadRegLock)
            {
                int regs = 0;
                if (!registration.TryGetValue(thread, out regs))
                {
                    registration[thread] = 1;
                }
                else
                {
                    registration[thread] = regs + 1;
                }
                return regs + 1;
            }
        }
        internal static int DecrementUseCount(Thread thread, Dictionary<Thread, int> registration)
        {
            lock (ThreadRegLock)
            {
                int regs = 0;
                if (!registration.TryGetValue(thread, out regs))
                {
                    registration[thread] = 0;
                }
                else
                {
                    registration[thread] = regs - 1;
                }
                return regs - 1;
            }
        }



        public static Dictionary<int, IntPtr> SafeThreads = new Dictionary<int, IntPtr>();
        public static Dictionary<int, Thread> engineToThread = new Dictionary<int, Thread>();
        public static Dictionary<int, int> threadToEngine = new Dictionary<int, int>();


        public static Dictionary<int, PlMtEngine> ThreadEngines = new Dictionary<int, PlMtEngine>();
        public static List<IntPtr> FreeEngines = new List<IntPtr>();
        public static bool UseEnginePool = true;

        public static void RegisterMainThread()
        {
            PingThreadFactories();
            lock (ThreadRegLock)
            {
                Application.ThreadExit += new EventHandler(OnThreadExit);
                var t = Thread.CurrentThread.ManagedThreadId;
                //libpl.PL_thread_at_exit((DelegateParameter0)PrologThreadAtExitGlobal, IntPtr.Zero, 1);
              //  SafeThreads.Add(t, new IntPtr(libpl.PL_ENGINE_MAIN));
               // int self = libpl.PL_thread_self();
              //  engineToThread.Add(self, t);
            }
        }

        public static bool NoTestThreadFActory = false;
        public static void PingThreadFactories()
        {
            try
            {

                if (NoTestThreadFActory) return;
                Assembly assem = AssemblyLoad("MushDLR223");<commit_msg>Remove (yet another) reference to System.Windows.Forms.Application class - for easier building<commit_after>/*  $Id$
*  
*  Project: Swicli.Library - Two Way Interface for .NET and MONO to SWI-Prolog
*  Author:        <NAME>
*  E-mail:        <EMAIL>
*  WWW:           http://www.logicmoo.com
*  Copyright (C):  2010-2012 LogicMOO Developement
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*********************************************************/
#if USE_IKVM
using IKVM.Internal;
using ikvm.runtime;
using java.net;
using jpl;
using Hashtable = java.util.Hashtable;
using ClassLoader = java.lang.ClassLoader;
using Class = java.lang.Class;
using sun.reflect.misc;
#endif
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using SbsSW.SwiPlCs;
using SbsSW.SwiPlCs.Callback;
using SbsSW.SwiPlCs.Exceptions;
using CycFort = SbsSW.SwiPlCs.PlTerm;
using PrologCli = Swicli.Library.PrologClient;

namespace Swicli.Library
{
    public partial class PrologClient
    {
		static public bool ClientReady = false;
        static public object _ThreadRegLock = new object();
        static public object ThreadRegLock
        {
            get
            {
                return LockInfo.Watch(_ThreadRegLock);
            }
        }
        public static bool SaneThreadWorld = true;
        public static Dictionary<Thread, int> ThreadRegisterations = new Dictionary<Thread, int>();
        public static Dictionary<Thread, int> ForiegnFrameCounts = new Dictionary<Thread, int>();

        internal static int IncrementUseCount(Thread thread)
        {
            return IncrementUseCount(thread, ThreadRegisterations);
        }
        internal static int DecrementUseCount(Thread thread)
        {
            return DecrementUseCount(thread, ThreadRegisterations);
        }
        internal static int IncrementUseCount(Thread thread, Dictionary<Thread, int> registration)
        {
            lock (ThreadRegLock)
            {
                int regs = 0;
                if (!registration.TryGetValue(thread, out regs))
                {
                    registration[thread] = 1;
                }
                else
                {
                    registration[thread] = regs + 1;
                }
                return regs + 1;
            }
        }
        internal static int DecrementUseCount(Thread thread, Dictionary<Thread, int> registration)
        {
            lock (ThreadRegLock)
            {
                int regs = 0;
                if (!registration.TryGetValue(thread, out regs))
                {
                    registration[thread] = 0;
                }
                else
                {
                    registration[thread] = regs - 1;
                }
                return regs - 1;
            }
        }



        public static Dictionary<int, IntPtr> SafeThreads = new Dictionary<int, IntPtr>();
        public static Dictionary<int, Thread> engineToThread = new Dictionary<int, Thread>();
        public static Dictionary<int, int> threadToEngine = new Dictionary<int, int>();


        public static Dictionary<int, PlMtEngine> ThreadEngines = new Dictionary<int, PlMtEngine>();
        public static List<IntPtr> FreeEngines = new List<IntPtr>();
        public static bool UseEnginePool = true;

        public static void RegisterMainThread()
        {
            PingThreadFactories();
            lock (ThreadRegLock)
            {
                var fa = Type.GetType("System.Windows.Forms.Application");
                if (fa != null)
                {
                    var evinfo = fa.GetEvent("ThreadExit");
                    if (evinfo != null)
                    {
                        evinfo.AddEventHandler(null, new EventHandler(OnThreadExit));
                    }
                } else
                {
                    ConsoleWriteLine("Cannot install hook ThreadExit to Mono");
                }
                var t = Thread.CurrentThread.ManagedThreadId;
                //libpl.PL_thread_at_exit((DelegateParameter0)PrologThreadAtExitGlobal, IntPtr.Zero, 1);
              //  SafeThreads.Add(t, new IntPtr(libpl.PL_ENGINE_MAIN));
               // int self = libpl.PL_thread_self();
              //  engineToThread.Add(self, t);
            }
        }

        public static bool NoTestThreadFActory = false;
        public static void PingThreadFactories()
        {
            try
            {

                if (NoTestThreadFActory) return;
                Assembly assem = AssemblyLoad("MushDLR223");
{'fraction_non_alphanumeric': 0.08905123901668477, 'fraction_numerical': 0.007305755750814493, 'mean_word_length': 2.056729028364514, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6943662', 'n_tokens_mistral': 2739, 'n_tokens_neox': 2567, 'n_words': 789}