// ----------------------------------------------------------------------- // // // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to You under the Apache License, Version 2.0 // (the "License"); you may not use this file except in compliance with // the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // // ----------------------------------------------------------------------- namespace Lucene.Net.Support.Threading { using System; using System.Collections.Generic; using System.Linq; using System.Text; /// /// Encapsulates a memory slot to store local data. This class cannot be inherited. /// This currently only supports Threading Slots. /// public sealed class LocalDataStoreSlot { private static readonly object syncRoot = new object(); private static bool[] bitmap; /// /// Initializes a new instance of the class. /// /// if set to true [in thread]. internal LocalDataStoreSlot(bool isThreadLocal) { if (!isThreadLocal) throw new NotImplementedException("Only saving to a thread is currently supported"); this.IsThreadLocal = true; lock (syncRoot) { int i; bool[] bitmapCopy = bitmap; if (bitmapCopy != null) { // find a slot that has been closed, assign the index for (i = 0; i < bitmapCopy.Length; ++i) { if (!bitmapCopy[i]) { this.SlotId = i; bitmapCopy[i] = true; return; } } // if a slot was not open, expand bitmap 2 places bool[] newBitmap = new bool[i + 2]; Array.Copy(bitmapCopy, newBitmap, newBitmap.Length); bitmapCopy = newBitmap; } else { // create a new bitmap bitmapCopy = new bool[2]; i = 0; } // assign slot bitmapCopy[i] = true; this.SlotId = i; bitmap = bitmapCopy; } } /// /// Finalizes an instance of the class. /// Releases unmanaged resources and performs other cleanup operations before the /// is reclaimed by garbage collection. /// ~LocalDataStoreSlot() { lock (syncRoot) { ThreadData.FreeLocalSlotData(this.SlotId, this.IsThreadLocal); bitmap[this.SlotId] = false; } } /// /// Gets or sets the slot index. /// /// The index of the slot. internal int SlotId { get; set; } /// /// Gets or sets a value indicating whether this instance is thread local. /// /// /// true if this instance is thread local; otherwise, false. /// internal bool IsThreadLocal { get; set; } } }