Example #1
0
        private static void SvcWaitProcessWideKeyAtomic(Switch Ns, ARegisters Registers, AMemory Memory)
        {
            long MutexAddress   = (long)Registers.X0;
            long CondVarAddress = (long)Registers.X1;
            int  ThreadHandle   = (int)Registers.X2;
            long Timeout        = (long)Registers.X3;

            AThread Thread = Ns.Os.Handles.GetData <HThread>(ThreadHandle).Thread;

            if (Ns.Os.Mutexes.TryGetValue(MutexAddress, out Mutex M))
            {
                M.GiveUpLock(ThreadHandle);
            }

            CondVar Signal = new CondVar(Memory, CondVarAddress, Timeout);

            Signal = Ns.Os.CondVars.GetOrAdd(CondVarAddress, Signal);

            Signal.WaitForSignal(ThreadHandle);

            M = new Mutex(Memory, MutexAddress);

            M = Ns.Os.Mutexes.GetOrAdd(MutexAddress, M);

            //FIXME
            //M.WaitForLock(Thread, ThreadHandle);

            Memory.WriteInt32(MutexAddress, 0);

            Registers.X0 = (int)SvcResult.Success;
        }
Example #2
0
        static JSettings _Load(string file, Type type, bool useDefault)
        {
            JSettings R = null;

            if (AFile.ExistsAsAny(file))
            {
                try {
                    if (useDefault)
                    {
                        AFile.Delete(file);
                    }
                    else
                    {
                        var b   = AFile.LoadBytes(file);
                        var opt = new JsonSerializerOptions {
                            IgnoreNullValues = true, AllowTrailingCommas = true
                        };
                        R = JsonSerializer.Deserialize(b, type, opt) as JSettings;
                    }
                }
                catch (Exception ex) {
                    string es = ex.ToStringWithoutStack();
                    if (useDefault)
                    {
                        AOutput.Write($"Failed to delete settings file '{file}'. {es}");
                    }
                    else
                    {
                        string backup = file + ".backup";
                        try { AFile.Move(file, backup, IfExists.Delete); } catch { backup = "failed"; }
                        AOutput.Write(
                            $@"Failed to load settings from {file}. Will use default settings.
	{es}
	Backup: {backup}"    );
                    }
                }
            }
            R ??= Activator.CreateInstance(type) as JSettings;
            R._file   = file;
            R._loaded = true;

            //autosave
            if (Interlocked.Exchange(ref s_loadedOnce, 1) == 0)
            {
                AThread.Start(() => {
                    for (; ;)
                    {
                        Thread.Sleep(2000);
                        _SaveAllIfNeed();
                    }
                }, sta: false);

                AProcess.Exit += (unu, sed) => _SaveAllIfNeed();                 //info: Core does not call finalizers when process exits
            }
            lock (s_list) s_list.Add(R);

            return(R);
        }
Example #3
0
    protected void SelectDataFromTableAsync(SelectType p_selectType, bool p_selectAll, string p_tableName, string[] p_arraySelectKeys, Dictionary <string, string> p_dictWhere, Action <List <Dictionary <string, string> > > p_callbackFinish)
    {
        Debug.Log("SelectDataFromTableAsync");
        List <Dictionary <string, string> > __listDataLoaded = new List <Dictionary <string, string> >();

        AThread.StartNewThread(delegate
        {
            OpenDatabaseConnection();
            using (_dbTransaction = _dbConnection.BeginTransaction())
            {
                using (_dbCommand = _dbConnection.CreateCommand())
                {
                    Debug.Log(_dbConnection.ConnectionString);
                    Debug.Log(_dbConnection.State);
                    _dbCommand.Connection  = _dbConnection;
                    _dbCommand.Transaction = _dbTransaction;
                    _dbCommand.CommandText = BuildQuerySelect(p_selectType, p_selectAll, p_tableName, p_arraySelectKeys, p_dictWhere);
                    try
                    {
                        _dbReader = _dbCommand.ExecuteReader();
                        while (_dbReader.Read())
                        {
                            Dictionary <string, string> __dictRowLoaded = new Dictionary <string, string>();

                            for (int i = 0; i < p_arraySelectKeys.Length; i++)
                            {
                                string __stringRow = string.Empty;
                                if (typeof(DateTime) == _dbReader.GetFieldType(i))
                                {
                                    __stringRow = _dbReader.GetString(i);
                                }
                                else
                                {
                                    __stringRow = _dbReader.GetValue(i).ToString();
                                }
                                __dictRowLoaded.Add(p_arraySelectKeys[i], __stringRow);
                            }
                            __listDataLoaded.Add(__dictRowLoaded);
                        }
                        _dbReader.Close();
                    }
                    catch (Exception __sqliteException)
                    {
                        Debug.LogError("Sqlite Exception: " + __sqliteException);
                    }
                }
            }
            _dbConnection.Close();
        },
                               delegate
        {
            if (p_callbackFinish != null)
            {
                p_callbackFinish(__listDataLoaded);
            }
        });
    }
Example #4
0
    protected void UpdateListOfDataAsync(List <Dictionary <string, string> > p_listDictData, string p_tableName, string[] p_arrayWhereKeys, Action p_callbackFinish)
    {
        AThreadNodule __threadNodule = null;

        __threadNodule = AThread.StartNewThread(delegate
        {
            OpenDatabaseConnection();
            using (_dbTransaction = _dbConnection.BeginTransaction())
            {
                using (_dbCommand = _dbConnection.CreateCommand())
                {
                    _dbCommand.Connection  = _dbConnection;
                    _dbCommand.Transaction = _dbTransaction;
                    try
                    {
                        for (int i = 0; i < p_listDictData.Count; i++)
                        {
                            Dictionary <string, string> __dictRowEntries = new Dictionary <string, string>();
                            foreach (var k_key in p_listDictData[i].Keys)
                            {
                                __dictRowEntries.Add(k_key, p_listDictData[i][k_key]);
                            }
                            UpdateDataOnTable(__dictRowEntries, p_tableName, p_arrayWhereKeys);
                        }
                        _dbTransaction.Commit();
                    }
                    catch (Exception __sqliteException)
                    {
                        Debug.LogError("Sqlite Exception: " + __sqliteException);
                        _dbTransaction.Rollback();
                    }
                }
            }
            _dbConnection.Close();
            if (_cancelDataRequest == true)
            {
                _dbConnection.Close();
                __threadNodule.CancelThread(delegate
                {
                    if (_callbackCancelDataRequest != null)
                    {
                        _callbackCancelDataRequest();
                    }
                });
            }
        }, p_callbackFinish);
    }
Example #5
0
    protected void SelectDataFromTableAsync(List <string> p_listTableNames, Dictionary <string, string[]> p_dictTableArraySelectKeys, Dictionary <string, Dictionary <string, string> > p_dictTableWhereKeys, Action <List <Dictionary <string, string> > > p_finishCallback)
    {
        List <Dictionary <string, string> > __listDataLoaded = new List <Dictionary <string, string> >();

        AThread.StartNewThread(delegate
        {
            OpenDatabaseConnection();
            using (_dbTransaction = _dbConnection.BeginTransaction())
            {
                using (_dbCommand = _dbConnection.CreateCommand())
                {
                    _dbCommand.Connection  = _dbConnection;
                    _dbCommand.Transaction = _dbTransaction;
                    _dbCommand.CommandText = BuildQuerySelect(SelectType.NONE, false, p_listTableNames, p_dictTableArraySelectKeys, p_dictTableWhereKeys);
                    try
                    {
                        _dbReader = _dbCommand.ExecuteReader();
                        while (_dbReader.Read())
                        {
                            Dictionary <string, string> __dictRowLoaded = new Dictionary <string, string>();
                            for (int i = 0; i < p_listTableNames.Count; i++)
                            {
                                string __tableName = p_listTableNames[i];
                                for (int j = 0; j < p_dictTableArraySelectKeys[__tableName].Length; j++)
                                {
                                    string __stringCollumnValue = _dbReader.GetString(j);
                                    __dictRowLoaded.Add(p_dictTableArraySelectKeys[__tableName][j], __stringCollumnValue);
                                }
                            }
                            __listDataLoaded.Add(__dictRowLoaded);
                        }
                        _dbReader.Close();
                    }
                    catch (SqliteException __sqliteException)
                    {
                        Debug.Log("Sqlite Exception: " + __sqliteException);
                    }
                }
            }
            _dbConnection.Close();
        },
                               delegate
        {
            p_finishCallback(__listDataLoaded);
        });
    }
Example #6
0
        private static void SvcArbitrateLock(Switch Ns, ARegisters Registers, AMemory Memory)
        {
            int  OwnerThreadHandle      = (int)Registers.X0;
            long MutexAddress           = (long)Registers.X1;
            int  RequestingThreadHandle = (int)Registers.X2;

            AThread RequestingThread = Ns.Os.Handles.GetData <HThread>(RequestingThreadHandle).Thread;

            Mutex M = new Mutex(Memory, MutexAddress);

            M = Ns.Os.Mutexes.GetOrAdd(MutexAddress, M);

            //FIXME
            //M.WaitForLock(RequestingThread, RequestingThreadHandle);

            Memory.WriteInt32(MutexAddress, 0);

            Registers.X0 = (int)SvcResult.Success;
        }
Example #7
0
        public int MakeThread(
            long EntryPoint,
            long StackTop,
            long ArgsPtr,
            int Priority,
            int ProcessorId)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(nameof(Process));
            }

            AThread CpuThread = new AThread(GetTranslator(), Memory, EntryPoint);

            KThread Thread = new KThread(CpuThread, this, ProcessorId, Priority);

            Thread.LastPc = EntryPoint;

            int Handle = HandleTable.OpenHandle(Thread);

            long Tpidr = GetFreeTls();

            int ThreadId = (int)((Tpidr - MemoryManager.TlsIoRegionStart) / 0x200) + 1;

            CpuThread.ThreadState.ProcessId = ProcessId;
            CpuThread.ThreadState.ThreadId  = ThreadId;
            CpuThread.ThreadState.CntfrqEl0 = TickFreq;
            CpuThread.ThreadState.Tpidr     = Tpidr;

            CpuThread.ThreadState.X0  = (ulong)ArgsPtr;
            CpuThread.ThreadState.X1  = (ulong)Handle;
            CpuThread.ThreadState.X31 = (ulong)StackTop;

            CpuThread.ThreadState.Break     += BreakHandler;
            CpuThread.ThreadState.SvcCall   += SvcHandler.SvcCall;
            CpuThread.ThreadState.Undefined += UndefinedHandler;

            CpuThread.WorkFinished += ThreadFinished;

            Threads.TryAdd(CpuThread.ThreadState.Tpidr, Thread);

            return(Handle);
        }
Example #8
0
 protected void InsertListOfData(List <Dictionary <string, string> > p_listDictData, string p_tableName, Action p_callbackFinish)
 {
     AThread.StartNewThread(delegate
     {
         OpenDatabaseConnection();
         using (_dbTransaction = _dbConnection.BeginTransaction())
         {
             using (_dbCommand = _dbConnection.CreateCommand())
             {
                 _dbCommand.Connection  = _dbConnection;
                 _dbCommand.Transaction = _dbTransaction;
                 try
                 {
                     for (int i = 0; i < p_listDictData.Count; i++)
                     {
                         Dictionary <string, string> __dictRowEntries = new Dictionary <string, string>();
                         foreach (var k_key in p_listDictData[i].Keys)
                         {
                             __dictRowEntries.Add(k_key, p_listDictData[i][k_key]);
                         }
                         InsertDataOnTable(__dictRowEntries, p_tableName);
                     }
                     _dbTransaction.Commit();
                 }
                 catch (SqliteException __sqliteException)
                 {
                     Debug.LogError(__sqliteException);
                     _dbTransaction.Rollback();
                 }
             }
         }
         _dbConnection.Close();
     },
                            delegate
     {
         if (p_callbackFinish != null)
         {
             p_callbackFinish();
         }
     });
 }
Example #9
0
        public void WaitForLock(AThread RequestingThread, int RequestingThreadHandle)
        {
            lock (EnterWaitLock)
            {
                int CurrentThreadHandle = Memory.ReadInt32(MutexAddress) & ~MutexHasListenersMask;

                if (CurrentThreadHandle == RequestingThreadHandle ||
                    CurrentThreadHandle == 0)
                {
                    return;
                }

                if (CurrRequestingThreadHandle == 0 || RequestingThread.Priority < HighestPriority)
                {
                    CurrRequestingThreadHandle = RequestingThreadHandle;

                    HighestPriority = RequestingThread.Priority;
                }
            }

            ThreadEvent.Reset();
            ThreadEvent.WaitOne();
        }