Пример #1
0
        private IProducer <T> OpenStream <T>(string streamName, ConsumerProducer <Message <BufferReader>, T> deserializer)
        {
            if (this.streams.TryGetValue(streamName, out object stream))
            {
                return((IProducer <T>)stream); // if the types don't match, invalid cast exception is the appropriate error
            }

            var meta = this.reader.OpenStream(streamName);

            if (meta.ActiveLifetime != null && !meta.ActiveLifetime.IsEmpty && meta.ActiveLifetime.IsFinite)
            {
                // propose a replay time that covers the stream lifetime
                this.pipeline.ProposeReplayTime(meta.ActiveLifetime, meta.OriginatingLifetime);
            }

            // register this stream with the store catalog
            this.pipeline.ConfigurationStore.Set(Store.StreamMetadataNamespace, streamName, meta);

            // create the deserialization sub-pipeline (and validate that we can deserialize this stream)
            var splitterOut = this.splitter.Add(meta.Id);

            deserializer.Out.Name = streamName;
            splitterOut.PipeTo(deserializer, DeliveryPolicy.Unlimited);
            this.streams[streamName] = deserializer.Out;
            return(deserializer.Out);
        }
Пример #2
0
 public IoRangeDescriptor(ResourceType resourceType, ulong minimum, ulong maximum,
                          ulong length, ulong alignment, ulong addressTranslationOffset,
                          bool minimumAddressIsFixed, bool maximumAddressIsFixed,
                          DecodeType decodeType, ConsumerProducer consumerProducer,
                          IoToMemoryTranslation ioToMemoryTranslation)
     : base(resourceType, minimum, maximum, length, alignment, addressTranslationOffset,
            minimumAddressIsFixed, maximumAddressIsFixed, decodeType, consumerProducer)
 {
     this.ioToMemoryTranslation = ioToMemoryTranslation;
 }
Пример #3
0
        public AddressSpaceDescriptor(ResourceType resourceType, ulong minimum, ulong maximum, ulong length, ulong alignment)
        {
            this.resourceType = resourceType;
            this.minimum      = minimum;
            this.maximum      = maximum;
            this.length       = length;
            this.alignment    = alignment;

            this.minimumAddressIsFixed    = false;
            this.maximumAddressIsFixed    = false;
            this.addressTranslationOffset = 0;
            this.decodeType       = DecodeType.BridgePositivelyDecodes;
            this.consumerProducer = ConsumerProducer.Consumes;
        }
Пример #4
0
 public MemoryRangeDescriptor(ResourceType resourceType, ulong minimum, ulong maximum,
                              ulong length, ulong alignment, ulong addressTranslationOffset,
                              bool minimumAddressIsFixed, bool maximumAddressIsFixed,
                              DecodeType decodeType, ConsumerProducer consumerProducer,
                              AcpiMemoryFlags acpiMemoryFlags, MemoryToIoTranslation memoryToIoTranslation,
                              AddressRangeAttribute addressRangeAttribute,
                              CacheableAttribute cacheableAttribute, WriteStatus writeStatus)
     : base(resourceType, minimum, maximum, length, alignment, addressTranslationOffset,
            minimumAddressIsFixed, maximumAddressIsFixed, decodeType, consumerProducer)
 {
     this.acpiMemoryFlags       = acpiMemoryFlags;
     this.memoryToIoTranslation = memoryToIoTranslation;
     this.addressRangeAttribute = addressRangeAttribute;
     this.cacheableAttribute    = cacheableAttribute;
     this.writeStatus           = writeStatus;
 }
Пример #5
0
        public AddressSpaceDescriptor(ResourceType resourceType, ulong minimum, ulong maximum,
                                      ulong length, ulong alignment, ulong addressTranslationOffset,
                                      bool minimumAddressIsFixed, bool maximumAddressIsFixed,
                                      DecodeType decodeType, ConsumerProducer consumerProducer)
        {
            this.resourceType = resourceType;
            this.minimum      = minimum;
            this.maximum      = maximum;
            this.length       = length;
            this.alignment    = alignment;

            this.minimumAddressIsFixed    = minimumAddressIsFixed;
            this.maximumAddressIsFixed    = maximumAddressIsFixed;
            this.addressTranslationOffset = addressTranslationOffset;
            this.decodeType       = decodeType;
            this.consumerProducer = consumerProducer;
        }
        public async Task <int> AddThreadAsync()
        {
            CancellationTokenSource source = new CancellationTokenSource();
            CancellationToken       token  = source.Token;

            Task producer = Task.Run(async() => {
                while (!token.IsCancellationRequested)
                {
                    if (_documents.TryDequeue(out var file))
                    {
                        var numbers = new List <int>();

                        string line;
                        while ((line = file.Stream.ReadLine()) != null)
                        {
                            numbers.Add(Int32.Parse(line));
                        }

                        _channel.Writer.TryWrite(new ChannelData()
                        {
                            FileName = file.FileName, FileData = numbers
                        });
                    }
                    else
                    {
                        _channel.Writer.TryComplete();
                    }
                }
            }, token);

            Task consumer = Task.Run(async() => {
                while (!token.IsCancellationRequested && await _channel.Reader.WaitToReadAsync())
                {
                    if (_channel.Reader.TryRead(out var file))
                    {
                        var count = file.FileData.Count;
                        foreach (var number in file.FileData)
                        {
                            var isPrime = PrimeHelper.IsPrime(number);
                            if (isPrime)
                            {
                                _logger.LogInformation(file.FileName + " : " + number.ToString() + " : " + count);

                                if (_minPrime > number)
                                {
                                    ChangeMin(number);
                                }
                                if (_maxPrime < number)
                                {
                                    ChangeMax(number);
                                }
                            }

                            //Otherwise all the files are checked too fast
                            await Task.Delay(DelayTime);
                            count--;

                            ChangeFileCompleted(file.FileName, file.FileData.Count, count);
                        }
                    }
                    IncrementTotalFilesDone();
                }
            }, token);

            var consumerProducer = new ConsumerProducer {
                Producer = producer, Consumer = consumer
            };

            _threads.Add(source, consumerProducer);

            return(await Task.FromResult(_threads.Count));
        }
Пример #7
0
        private IProducer <T> OpenStream <T>(string streamName, ConsumerProducer <Message <BufferReader>, T> deserializer, bool checkType = true)
        {
            if (this.streams.TryGetValue(streamName, out object stream))
            {
                return((IProducer <T>)stream); // if the types don't match, invalid cast exception is the appropriate error
            }

            var meta = this.reader.OpenStream(streamName);

            if (checkType)
            {
                // check that the requested type matches the stream type
                var streamType    = meta.TypeName;
                var requestedType = TypeSchema.GetContractName(typeof(T), this.serializers.RuntimeVersion);
                if (streamType != requestedType)
                {
                    // check if the handler maps the stream type to the requested type
                    var handler = this.serializers.GetHandler <T>();
                    if (handler.Name != streamType)
                    {
                        if (this.serializers.Schemas.TryGetValue(streamType, out var streamTypeSchema) &&
                            this.serializers.Schemas.TryGetValue(requestedType, out var requestedTypeSchema))
                        {
                            // validate compatibility - will throw if types are incompatible
                            streamTypeSchema.ValidateCompatibleWith(requestedTypeSchema);
                        }
                    }
                }
            }

            if (meta.OriginatingLifetime != null && !meta.OriginatingLifetime.IsEmpty && meta.OriginatingLifetime.IsFinite)
            {
                // propose a replay time that covers the stream lifetime
                this.ProposeReplayTime(meta.OriginatingLifetime);
            }

            // register this stream with the store catalog
            this.pipeline.ConfigurationStore.Set(Store.StreamMetadataNamespace, streamName, meta);

            // collate the raw messages by their stream IDs
            var splitterOut = this.splitter.Add(meta.Id);

            splitterOut.PipeTo(deserializer, DeliveryPolicy.Unlimited);

            // preserve the envelope of the deserialized message in the output connector
            var outConnector = new Connector <T>(this, this.pipeline, $"connectorOut{streamName}", preserveEnvelope: true);

            deserializer
            .Process <T, T>(
                (msg, env, emitter) =>
            {
                // do not deliver messages past the stream closing time
                if (meta.Closed == default || env.OriginatingTime <= meta.Closed)
                {
                    // call Deliver rather than Post to preserve the original envelope
                    emitter.Deliver(msg, env);
                }
            }, DeliveryPolicy.SynchronousOrThrottle)
            .PipeTo(outConnector, DeliveryPolicy.SynchronousOrThrottle);

            outConnector.Out.Name    = streamName;
            this.streams[streamName] = outConnector.Out;
            return(outConnector.Out);
        }
Пример #8
0
        public static ResourceDescriptor[] Parse(byte[] resourceBuffer)
        {
            ResourceDescriptorList result = new ResourceDescriptorList();

            for (int start = 0; start < resourceBuffer.Length;)
            {
                if (resourceBuffer.Length < 1)
                {
                    throw new ArgumentException("resourceBuffer must have length at least 1");
                }
                if ((resourceBuffer[start] & 0x80) == 0)
                {
                    // Small item
                    int lengthBytes = resourceBuffer[start] & 0x7;

                    switch ((SmallResourceItemName)((resourceBuffer[start] >> 3) & 0x0F))
                    {
                    case SmallResourceItemName.IrqFormatDescriptor: {
                        Debug.Assert(lengthBytes == 2 || lengthBytes == 3);
                        IntList interruptNumbers = new IntList();
                        for (int i = 0; i < 8; i++)
                        {
                            if ((resourceBuffer[start + 1] & (1 << i)) != 0)
                            {
                                interruptNumbers.Add(i);
                            }
                            if ((resourceBuffer[start + 2] & (1 << i)) != 0)
                            {
                                interruptNumbers.Add(i + 8);
                            }
                        }

                        // From spec: "If byte 3 is not included, High true,
                        // edge sensitive, non-shareable is assumed."
                        bool sharable = false;
                        IrqDescriptor.Polarity polarity = IrqDescriptor.Polarity.ActiveHigh;
                        IrqDescriptor.Mode     mode     = IrqDescriptor.Mode.EdgeTriggered;

                        if (lengthBytes == 3)
                        {
                            byte flags = resourceBuffer[start + 3];
                            sharable = (flags & (1 << 4)) != 0;
                            polarity = (IrqDescriptor.Polarity)((flags >> 3) & 1);
                            mode     = (IrqDescriptor.Mode)(flags & 1);
                        }

                        result.Add(new IrqDescriptor(interruptNumbers.ToArray(),
                                                     sharable, polarity, mode));
                        break;
                    }

                    case SmallResourceItemName.DmaFormatDescriptor: {
                        Debug.Assert(lengthBytes == 2);
                        IntList dmaChannelNumbers = new IntList();
                        for (int i = 0; i < 8; i++)
                        {
                            if ((resourceBuffer[start + 1] & (1 << i)) != 0)
                            {
                                dmaChannelNumbers.Add(i);
                            }
                        }

                        byte flags = resourceBuffer[start + 2];
                        DmaDescriptor.ChannelSpeed channelSpeed =
                            (DmaDescriptor.ChannelSpeed)((flags >> 5) & 0x3);
                        DmaDescriptor.LogicalDeviceBusMasterStatus logicalDeviceBusMasterStatus =
                            (DmaDescriptor.LogicalDeviceBusMasterStatus)((flags >> 2) & 0x1);
                        DmaDescriptor.TransferTypePreference transferTypePreference =
                            (DmaDescriptor.TransferTypePreference)(flags & 0x3);

                        result.Add(new DmaDescriptor(dmaChannelNumbers.ToArray(),
                                                     channelSpeed, logicalDeviceBusMasterStatus, transferTypePreference));
                        break;
                    }

                    case SmallResourceItemName.StartDependentFunctionsDescriptor:
                        throw new ResourceDescriptionParseException("Unimplemented ACPI resource descriptor");

                    case SmallResourceItemName.EndDependentFunctionsDescriptor:
                        throw new ResourceDescriptionParseException("Unimplemented ACPI resource descriptor");

                    case SmallResourceItemName.IoPortDescriptor: {
                        UInt16 minimum   = BuildUInt16(resourceBuffer, start + 2);
                        UInt16 maximum   = BuildUInt16(resourceBuffer, start + 4);
                        byte   alignment = resourceBuffer[start + 6];
                        byte   length    = resourceBuffer[start + 7];
                        result.Add(new IoRangeDescriptor(minimum, maximum, length, alignment));
                        break;
                    }

                    case SmallResourceItemName.FixedLocationIoPortDescriptor: {
                        UInt16 baseAddress = BuildUInt16(resourceBuffer, start + 1);
                        baseAddress &= 0x3FF;
                        byte length = resourceBuffer[start + 3];
                        result.Add(new IoRangeDescriptor(baseAddress, baseAddress, length, /*alignment*/ 1));
                        break;
                    }

                    case SmallResourceItemName.VendorDefinedDescriptor: {
                        byte[] data = new byte[7];
                        Array.Copy(resourceBuffer, start + 1, data, 0, 7);
                        result.Add(new VendorDefinedDescriptor(data));
                        break;
                    }

                    case SmallResourceItemName.EndTagDescriptor:
                        // Used to check it was at the end here, but apparently it can occur elsewhere
                        break;

                    default:
                        // Just ignore it and skip over it
                        break;
                    }

                    start += 1 + lengthBytes; // 1 for the tag byte 0
                }
                else
                {
                    // Large item
                    UInt16 lengthBytes             = BuildUInt16(resourceBuffer, start + 1);
                    LargeResourceItemName itemName = (LargeResourceItemName)(resourceBuffer[start] & 0x7F);

                    switch (itemName)
                    {
                    case LargeResourceItemName.MemoryRangeDescriptor24Bit: {
                        MemoryRangeDescriptor.WriteStatus writeStatus =
                            (MemoryRangeDescriptor.WriteStatus)(resourceBuffer[start + 3] & 1);
                        UInt16 minimum = BuildUInt16(resourceBuffer, start + 3);
                        minimum *= 256;
                        UInt16 maximum = BuildUInt16(resourceBuffer, start + 6);
                        maximum *= 256;
                        UInt16 alignment = BuildUInt16(resourceBuffer, start + 8);
                        UInt16 length    = BuildUInt16(resourceBuffer, start + 10);
                        length *= 256;
                        result.Add(new MemoryRangeDescriptor(minimum, maximum, length, alignment, writeStatus));
                        break;
                    }

                    case LargeResourceItemName.GenericRegisterDescriptor: {
                        GenericRegisterDescriptor.AddressSpace addressSpace =
                            (GenericRegisterDescriptor.AddressSpace)resourceBuffer[3];
                        byte registerBitWidth  = resourceBuffer[start + 4];
                        byte registerBitOffset = resourceBuffer[start + 5];
                        GenericRegisterDescriptor.AddressSize addressSize =
                            (GenericRegisterDescriptor.AddressSize)resourceBuffer[6];
                        ulong registerAddress = BuildUInt64(resourceBuffer, start + 7);

                        result.Add(new GenericRegisterDescriptor(
                                       addressSpace, registerBitWidth, registerBitOffset, addressSize, registerAddress));
                        break;
                    }

                    case LargeResourceItemName.VendorDefinedDescriptor: {
                        byte[] guidBytes = new byte[16];
                        Array.Copy(resourceBuffer, start + 4, guidBytes, 0, 16);
                        byte[] data = new byte[lengthBytes - 16 - 1];
                        Array.Copy(resourceBuffer, start + 20, data, 0, data.Length);
                        result.Add(new VendorDefinedDescriptor(new Guid(guidBytes), resourceBuffer[start + 3], data));
                        break;
                    }

                    case LargeResourceItemName.MemoryRangeDescriptor32Bit: {
                        MemoryRangeDescriptor.WriteStatus writeStatus =
                            (MemoryRangeDescriptor.WriteStatus)(resourceBuffer[start + 3] & 1);
                        UInt32 minimum   = BuildUInt32(resourceBuffer, start + 6);
                        UInt32 maximum   = BuildUInt32(resourceBuffer, start + 10);
                        UInt32 alignment = BuildUInt32(resourceBuffer, start + 14);
                        UInt32 length    = BuildUInt32(resourceBuffer, start + 18);
                        result.Add(new MemoryRangeDescriptor(minimum, maximum, length, alignment, writeStatus));
                        break;
                    }

                    case LargeResourceItemName.FixedLocationMemoryRangeDescriptor32Bit: {
                        MemoryRangeDescriptor.WriteStatus writeStatus =
                            (MemoryRangeDescriptor.WriteStatus)(resourceBuffer[start + 3] & 1);
                        UInt32 baseAddress = BuildUInt32(resourceBuffer, start + 4);
                        UInt32 length      = BuildUInt32(resourceBuffer, start + 8);
                        result.Add(new MemoryRangeDescriptor(baseAddress, baseAddress, length, 1, writeStatus));
                        break;
                    }

                    case LargeResourceItemName.WordAddressSpaceDescriptor:
                    case LargeResourceItemName.DwordAddressSpaceDescriptor:
                    case LargeResourceItemName.QwordAddressSpaceDescriptor:
                    case LargeResourceItemName.ExtendedAddressSpaceDescriptor: {
                        AddressSpaceDescriptor.ResourceType resourceType =
                            (AddressSpaceDescriptor.ResourceType)resourceBuffer[start + 3];

                        byte flags = resourceBuffer[start + 4];
                        bool maximumAddressIsFixed = (flags & (1 << 3)) != 0;
                        bool minimumAddressIsFixed = (flags & (1 << 2)) != 0;
                        AddressSpaceDescriptor.DecodeType decodeType =
                            (AddressSpaceDescriptor.DecodeType)((flags >> 1) & 1);
                        ConsumerProducer consumerProducer =
                            (ConsumerProducer)(flags & 1);

                        // alignment same thing as granularity in spec
                        ulong minimum, maximum, length, alignment, addressTranslationOffset;
                        if (itemName == LargeResourceItemName.WordAddressSpaceDescriptor)
                        {
                            minimum   = BuildUInt16(resourceBuffer, start + 8);
                            maximum   = BuildUInt16(resourceBuffer, start + 10);
                            alignment = BuildUInt16(resourceBuffer, start + 6);
                            length    = BuildUInt16(resourceBuffer, start + 14);
                            addressTranslationOffset = BuildUInt16(resourceBuffer, start + 12);
                        }
                        else if (itemName == LargeResourceItemName.DwordAddressSpaceDescriptor)
                        {
                            minimum   = BuildUInt32(resourceBuffer, start + 10);
                            maximum   = BuildUInt32(resourceBuffer, start + 14);
                            alignment = BuildUInt32(resourceBuffer, start + 6);
                            length    = BuildUInt32(resourceBuffer, start + 22);
                            addressTranslationOffset = BuildUInt32(resourceBuffer, start + 18);
                        }
                        else if (itemName == LargeResourceItemName.QwordAddressSpaceDescriptor)
                        {
                            minimum   = BuildUInt64(resourceBuffer, start + 14);
                            maximum   = BuildUInt64(resourceBuffer, start + 22);
                            alignment = BuildUInt64(resourceBuffer, start + 6);
                            length    = BuildUInt64(resourceBuffer, start + 38);
                            addressTranslationOffset = BuildUInt64(resourceBuffer, start + 30);
                        }
                        else     /* if (itemName == LargeResourceItemName.ExtendedAddressSpaceDescriptor) */
                        {
                            minimum   = BuildUInt64(resourceBuffer, start + 16);
                            maximum   = BuildUInt64(resourceBuffer, start + 24);
                            alignment = BuildUInt64(resourceBuffer, start + 8);
                            length    = BuildUInt64(resourceBuffer, start + 40);
                            addressTranslationOffset = BuildUInt64(resourceBuffer, start + 32);
                        }

                        byte typeSpecificFlags = resourceBuffer[start + 4];
                        if (resourceType == AddressSpaceDescriptor.ResourceType.MemoryRange)
                        {
                            MemoryRangeDescriptor.AcpiMemoryFlags acpiMemoryFlags =
                                (itemName == LargeResourceItemName.ExtendedAddressSpaceDescriptor) ?
                                (MemoryRangeDescriptor.AcpiMemoryFlags)BuildUInt16(resourceBuffer, start + 48) :
                                MemoryRangeDescriptor.AcpiMemoryFlags.None;
                            MemoryRangeDescriptor.MemoryToIoTranslation memoryToIoTranslation =
                                (MemoryRangeDescriptor.MemoryToIoTranslation)((typeSpecificFlags >> 5) & 1);
                            MemoryRangeDescriptor.AddressRangeAttribute addressRangeAttribute =
                                (MemoryRangeDescriptor.AddressRangeAttribute)((typeSpecificFlags >> 3) & 3);
                            MemoryRangeDescriptor.CacheableAttribute cacheableAttribute =
                                (MemoryRangeDescriptor.CacheableAttribute)((typeSpecificFlags >> 1) & 3);
                            MemoryRangeDescriptor.WriteStatus writeStatus =
                                (MemoryRangeDescriptor.WriteStatus)(typeSpecificFlags & 1);

                            result.Add(new MemoryRangeDescriptor(resourceType, minimum, maximum, length,
                                                                 alignment, addressTranslationOffset,
                                                                 minimumAddressIsFixed, maximumAddressIsFixed,
                                                                 decodeType, consumerProducer,
                                                                 acpiMemoryFlags, memoryToIoTranslation,
                                                                 addressRangeAttribute, cacheableAttribute,
                                                                 writeStatus));
                        }
                        else if (resourceType == AddressSpaceDescriptor.ResourceType.IoRange)
                        {
                            bool translation = ((typeSpecificFlags >> 4) & 1) != 0;
                            bool sparse      = ((typeSpecificFlags >> 5) & 1) != 0;

                            IoRangeDescriptor.IoToMemoryTranslation ioToMemoryTranslation;
                            if (!translation)
                            {
                                ioToMemoryTranslation = IoRangeDescriptor.IoToMemoryTranslation.TypeStatic;
                            }
                            else if (sparse)
                            {
                                ioToMemoryTranslation = IoRangeDescriptor.IoToMemoryTranslation.TypeTranslationSparse;
                            }
                            else
                            {
                                ioToMemoryTranslation = IoRangeDescriptor.IoToMemoryTranslation.TypeTranslationDense;
                            }

                            result.Add(new IoRangeDescriptor(resourceType, minimum, maximum, length,
                                                             alignment, addressTranslationOffset,
                                                             minimumAddressIsFixed, maximumAddressIsFixed,
                                                             decodeType, consumerProducer,
                                                             ioToMemoryTranslation));
                        }
                        else
                        {
                            result.Add(new AddressSpaceDescriptor(resourceType, minimum, maximum, length,
                                                                  alignment, addressTranslationOffset,
                                                                  minimumAddressIsFixed, maximumAddressIsFixed,
                                                                  decodeType, consumerProducer));
                        }
                        break;
                    }

                    case LargeResourceItemName.ExtendedIrqDescriptor: {
                        byte flags    = resourceBuffer[start + 3];
                        bool sharable = (flags & (1 << 4)) != 0;
                        IrqDescriptor.Polarity polarity = (IrqDescriptor.Polarity)((flags >> 3) & 1);
                        IrqDescriptor.Mode     mode     = (IrqDescriptor.Mode)(flags & 1);

                        int[] interruptTable = new int[resourceBuffer[start + 4]];
                        for (int i = 0; i < interruptTable.Length; i++)
                        {
                            interruptTable[i] = resourceBuffer[start + 5 + i];
                        }

                        // TODO: Currently we don't extract the Resource Source, if present

                        result.Add(new IrqDescriptor(interruptTable, sharable, polarity, mode));
                        break;
                    }

                    default:
                        // Just ignore it and skip over it
                        break;
                    }

                    start += 1 + 2 + lengthBytes; // 1 for the tag byte, 2 for length bytes
                }
            }

            return(result.ToArray());
        }