Пример #1
0
        public static SharedBuffer CreateBuffer(Guid id, Type elementType, long bufferSize)
        {
            var sb = new SharedBuffer();
            sb.Id = id;
            sb.ElementTypeString = elementType.FullName;
            sb.ElementSize = Marshal.SizeOf(elementType);
            sb.ElementCount = bufferSize;

            if (_blockPath != string.Empty)
            {
                var fileStream = System.IO.File.Create(string.Format(@"{0}\{1}", _blockPath, id.ToString()), 
                    (int)(bufferSize * sb.ElementSize), System.IO.FileOptions.DeleteOnClose);

                var mmf = MemoryMappedFile.CreateFromFile(fileStream, id.ToString(), 
                    (int)(bufferSize * sb.ElementSize),
                    MemoryMappedFileAccess.ReadWrite, null, System.IO.HandleInheritability.None, false);

                _cacheBuffers.TryAdd(id, sb);
                _cacheMmfs.TryAdd(id, mmf);
                _cacheFileStreams.TryAdd(id, fileStream);
            }
            else
            { 
                var mmf = MemoryMappedFile.CreateNew(id.ToString(), 
                    bufferSize * sb.ElementSize);
                _cacheBuffers.TryAdd(id, sb);
                _cacheMmfs.TryAdd(id, mmf);
            }

            return sb;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sb"></param>
        public void PrefetchBuffer(SharedBuffer sb)
        {
            if ((!_sbStack.Any(sbOther => sbOther.Id.CompareTo(sb.Id) == 0))
                && (!_sbDone.Any(sbOther => sbOther.Id.CompareTo(sb.Id) == 0)))
                _sbStack.Push(sb);

            if (_sbPrefetchTask == null
                || _sbPrefetchTask.Status != System.Threading.Tasks.TaskStatus.Running)
            {
                Action prefetchAction = () =>
                    {
                        try
                        {
                            while (true)
                            {
                                SharedBuffer sbLocal;
                                if (_sbStack.TryPop(out sbLocal))
                                {
//#if SLOW_POKE
//                                var handle = sbLocal.GetHandle();
//                                const long STEP_SIZE = 4096;
//                                long numSteps = (long)handle.ByteLength / STEP_SIZE;
//                                System.Threading.Tasks.Parallel.For(0, numSteps,
//                                    at => handle.Read<byte>((ulong)(at * STEP_SIZE)));
//                                sbLocal.ReleaseHandle();
//#else
//                                    BufferRepository.ReadFile(sbLocal.Id);
//#endif
                                    _sbDone.Add(sbLocal);
                                }
                            }
                        }
                        finally
                        { 
                            System.Console.WriteLine("Exiting prefetch...");
                        }
                    };

                _sbPrefetchTask = new System.Threading.Tasks.Task(prefetchAction);
                _sbPrefetchTask.Start();
            }
        }