Example #1
0
        public bool[] GetBits(int count)
        {
            var c    = new AtomicInt32();
            var bits = new bool[count];

            for (var i = 0; i < count; i++)
            {
                var task = Task.Run(() => c.Increment());
                while (!task.IsCompleted)
                {
                    c.Increment();
                }
                bits[i] = c % 2 == 0;
            }
            return(bits);
        }
Example #2
0
        private async ValueTask <int> InitializeFromBackup()
        {
            if (_settings.BackupFile.IsNullOrWhiteSpace())
            {
                return(0);
            }
            var file = new FileInfo(_settings.BackupFile);

            if (file.Exists && file.Length > 0)
            {
                if (_settings.DataDir.IsNotNullOrWhiteSpace())
                {
                    using var zip = ZipFile.OpenRead(file.FullName);
                    await zip.ExtractToDirectoryAsync(_dataDir);

                    return(await InitializeFromData());
                }

                var count   = new AtomicInt32();
                var loaded  = new AtomicHashSet <string>();
                var threads = Environment.ProcessorCount / 2;
                await Task.WhenAll(Enumerable.Range(0, threads).Select(i => Task.Run(async() =>
                {
                    using var zip = ZipFile.OpenRead(file.FullName);
                    foreach (var entry in zip.Entries.OrderBy(e => e.Length).Where(e => loaded.TryAdd(e.FullName)).Watch(zip.Entries.Count / threads))
                    {
                        using var stream = entry.Open();
                        using var data   = new ZipArchive(stream, ZipArchiveMode.Read);
                        await InitializeFromZip(data);
                        count.Increment();
                    }
                })));

                return(count);
            }
            return(0);
        }