hazard_pointer-inl.h 13.7 KB
Newer Older
1
/*
Marcus Winter committed
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef EMBB_CONTAINERS_INTERNAL_HAZARD_POINTER_INL_H_
#define EMBB_CONTAINERS_INTERNAL_HAZARD_POINTER_INL_H_

namespace embb {
namespace containers {
namespace internal {
33 34 35 36 37 38 39 40 41 42 43 44
// Visual Studio is complaining, that the return in the last line of this
// function is not reachable. This is true, as long as exceptions are enabled.
// Otherwise, the exception becomes an assertion and with disabling assertions,
// the code becomes reachable. So, disabling this warning.
#ifdef EMBB_PLATFORM_COMPILER_MSVC
#pragma warning(push)
#pragma warning(disable:4702)
#endif
  template< typename GuardType >
  unsigned int HazardPointer< GuardType >::GetObjectLocalThreadIndex() {
    // first, get the EMBB native thread id.
    unsigned int embb_thread_index;
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    int return_val = embb_internal_thread_index(&embb_thread_index);

    if (return_val != EMBB_SUCCESS) {
      EMBB_THROW(embb::base::ErrorException, "Could not get thread id");
    }

    // iterate over the mappings array
    for (unsigned int i = 0; i != max_accessors_count_; ++i) {
      // end of mappings? then we need to write our id
      if (thread_id_mapping_[i] == -1) {
        // try to CAS the initial value with out thread id
        int expected = -1;
        if (thread_id_mapping_[i].CompareAndSwap(expected,
          static_cast<int>(embb_thread_index))) {
          //successful, return our mapping
          return i;
        }
      }

      if (thread_id_mapping_[i] == static_cast<int>(embb_thread_index)) {
        // found our mapping!
        return i;
      }
    }

    // when we reach this point, we have too many accessors
    // (no mapping possible)
    EMBB_THROW(embb::base::ErrorException, "Too many accessors");
74

75
    return 0;
76
  }
77 78
#ifdef EMBB_PLATFORM_COMPILER_MSVC
#pragma warning(pop)
79
#endif
80

81 82 83 84 85 86 87 88 89 90 91
  template< typename GuardType >
  void HazardPointer< GuardType >::RemoveGuard(int guard_position) {
    const unsigned int my_thread_id = GetObjectLocalThreadIndex();

    // check invariants...
    assert(guard_position < max_guards_per_thread_);
    assert(my_thread_id < max_accessors_count_);

    // set guard
    guards_[guard_position*max_accessors_count_ + my_thread_id] =
      undefined_guard_;
92 93
  }

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  template< typename GuardType >
  HazardPointer< GuardType >::HazardPointer(
    embb::base::Function<void, GuardType> freeGuardCallback,
    GuardType undefined_guard, int guardsPerThread, int accessors) :
    max_accessors_count_(accessors < 0 ?
    embb::base::Thread::GetThreadsMaxCount() : accessors),
    undefined_guard_(undefined_guard),
    max_guards_per_thread_(guardsPerThread),
    release_object_callback_(freeGuardCallback),
    thread_id_mapping_(static_cast<embb::base::Atomic<int>*>(
      embb::base::Allocation::Allocate(sizeof(embb::base::Atomic<int>)
      *max_accessors_count_))),
    guards_(static_cast<embb::base::Atomic< GuardType >*>
      (embb::base::Allocation::Allocate(
      sizeof(embb::base::Atomic< GuardType >) * max_guards_per_thread_ *
      max_accessors_count_))),
    thread_local_retired_lists_temp_(static_cast<GuardType*>
      (embb::base::Allocation::Allocate(
      sizeof(GuardType) * max_guards_per_thread_ * max_accessors_count_ *
      max_accessors_count_
      ))),
    thread_local_retired_lists_(static_cast<GuardType*>
      (embb::base::Allocation::Allocate(
      sizeof(GuardType) * max_guards_per_thread_ * max_accessors_count_ *
      max_accessors_count_
      ))) {
    const unsigned int count_guards =
      max_guards_per_thread_ * max_accessors_count_;

    const unsigned int count_ret_elements =
      count_guards * max_accessors_count_;

    for (unsigned int i = 0; i != max_accessors_count_; ++i) {
      //in-place new for each cell
      new (&thread_id_mapping_[i]) embb::base::Atomic < int >(-1);
    }
130

131 132 133 134 135 136 137 138 139 140 141 142 143
    for (unsigned int i = 0; i != count_guards; ++i) {
      //in-place new for each cell
      new (&guards_[i]) embb::base::Atomic < GuardType >(undefined_guard);
    }

    for (unsigned int i = 0; i != count_ret_elements; ++i) {
      //in-place new for each cell
      new (&thread_local_retired_lists_temp_[i]) GuardType(undefined_guard);
    }

    for (unsigned int i = 0; i != count_ret_elements; ++i) {
      //in-place new for each cell
      new (&thread_local_retired_lists_[i]) GuardType(undefined_guard);
144 145
    }
  }
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

  template< typename GuardType >
  HazardPointer< GuardType >::~HazardPointer() {
    const unsigned int count_guards =
      max_guards_per_thread_ * max_accessors_count_;

    const unsigned int count_ret_elements =
      count_guards * max_accessors_count_;

    // Release references from all retired lists. Note that for this to work,
    // the data structure using hazard pointer has still to be active... So
    // first, the hazard pointer class shall be destructed, then the memory
    // management class (e.g. some pool). Otherwise, the hazard pointer class
    // would try to return memory to an already destructed memory manager.
    for (unsigned int i = 0; i != count_ret_elements; ++i) {
      GuardType pointerToFree =
          thread_local_retired_lists_[i];
      if (pointerToFree == undefined_guard_) {
        break;
165
      }
166
      release_object_callback_(pointerToFree);
167
    }
168

169 170 171 172 173 174 175 176
    for (unsigned int i = 0; i != max_accessors_count_; ++i) {
      thread_id_mapping_[i].~Atomic();
    }

    embb::base::Allocation::Free(thread_id_mapping_);

    for (unsigned int i = 0; i != count_guards; ++i) {
      guards_[i].~Atomic();
177
    }
178 179 180 181 182 183 184 185 186 187 188 189 190 191

    embb::base::Allocation::Free(guards_);

    for (unsigned int i = 0; i != count_ret_elements; ++i) {
      thread_local_retired_lists_temp_[i].~GuardType();
    }

    embb::base::Allocation::Free(thread_local_retired_lists_temp_);

    for (unsigned int i = 0; i != count_ret_elements; ++i) {
      thread_local_retired_lists_[i].~GuardType();
    }

    embb::base::Allocation::Free(thread_local_retired_lists_);
192 193
  }

194 195 196 197 198 199 200 201 202 203 204
  template< typename GuardType >
  void HazardPointer< GuardType >::Guard(int guardPosition,
    GuardType guardedElement) {
    const unsigned int my_thread_id = GetObjectLocalThreadIndex();

    // check invariants...
    assert(guardPosition < max_guards_per_thread_);
    assert(my_thread_id < max_accessors_count_);

    // set guard
    guards_[guardPosition*max_accessors_count_ + my_thread_id] = guardedElement;
205
  }
206

207 208 209 210 211 212 213 214 215
  template< typename GuardType >
  size_t HazardPointer< GuardType >::ComputeMaximumRetiredObjectCount(
  size_t guardsPerThread, int accessors) {
    unsigned int accessorCount = (accessors == -1 ?
      embb::base::Thread::GetThreadsMaxCount() :
      accessors);

    return static_cast<size_t>(
        guardsPerThread * accessorCount * accessorCount);
216
  }
217

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  /**
   * Remark: it might be faster to just swap pointers for temp retired list and
   * retired list. However, with the current implementation (one array for all
   * retired and retired temp lists, respectively) this is not possible. This is
   * not changed until this copying accounts for a performance problem. The
   * copying is not the bottleneck currently.
   */
  template< typename GuardType >
  void HazardPointer< GuardType >::CopyRetiredList(GuardType* sourceList,
    GuardType* targetList, unsigned int retiredListSize,
    GuardType undefinedGuard) {
    bool done = false;
    for (unsigned int ii = 0; ii != retiredListSize; ++ii) {
      if (!done) {
        GuardType guardToCopy = sourceList[ii];

        if (guardToCopy == undefinedGuard) {
          done = true;

          if (targetList[ii] == undefinedGuard) {
            // end of target list
            break;
          }
        }
        targetList[ii] = guardToCopy;
      } else {
        // we copied the whole source list, remaining values in the target
        // have to be zeroed.
        if (targetList[ii] == undefinedGuard) {
          // end of target list
          break;
        } else {
          targetList[ii] = undefinedGuard;
        }
      }
    }
254
  }
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

  template< typename GuardType >
  void HazardPointer< GuardType >::UpdateRetiredList(GuardType* retired_list,
    GuardType* updated_retired_list, unsigned int retired_list_size,
    GuardType guarded_element, GuardType considered_hazard,
    GuardType undefined_guard) {
    // no hazard set here
    if (considered_hazard == undefined_guard)
        return;

    // if this hazard is currently in the union of
    // threadLocalRetiredLists and pointerToRetire, but not yet in
    // threadLocalRetiredListsTemp, add it to that list
    bool contained_in_union = false;

    // first iterate over our retired list
    for (unsigned int i = 0; i != retired_list_size; ++i) {
      // when reaching 0, we can stop iterating (end of the "list")
      if (retired_list[i] == 0)
          break;

      // the hazard is contained in the retired list... it shall go
      // into the temp list, if not already there
      if (retired_list[i] == considered_hazard) {
        contained_in_union = true;
        break;
      }
    }

    // the union also contains pointerToRetire
    if (!contained_in_union) {
      contained_in_union = (considered_hazard == guarded_element);
    }

    // add the pointer to temp. retired list, if not already there
    if (contained_in_union) {
      for (unsigned int ii = 0; ii != retired_list_size; ++ii) {
        // is it already there?
        if (updated_retired_list[ii] == considered_hazard)
            break;

        // end of the list
        if (updated_retired_list[ii] == undefined_guard) {
          // add hazard
          updated_retired_list[ii] = considered_hazard;

          // we are done here...
          break;
        }
      }
    }
306 307
  }

308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
  template< typename GuardType >
  void HazardPointer< GuardType >::EnqueueForDeletion(GuardType toRetire) {
    unsigned int my_thread_id = GetObjectLocalThreadIndex();

    // check for invariant
    assert(my_thread_id < max_accessors_count_);

    const unsigned int retired_list_size = max_accessors_count_ *
      max_guards_per_thread_;

    const unsigned int count_guards = max_accessors_count_ *
      max_guards_per_thread_;

    GuardType* retired_list =
      &thread_local_retired_lists_[my_thread_id * retired_list_size];

    GuardType* retired_list_temp =
      &thread_local_retired_lists_temp_[my_thread_id * retired_list_size];

    // wipe my temp. retired list...
    for (unsigned int i = 0; i < retired_list_size; ++i) {
      // the list is filled always from left to right, so occurring the first
      // undefinedGuard, the remaining ones are also undefinedGuard...
      if (retired_list_temp[i] == undefined_guard_)
          break;

      retired_list_temp[i] = undefined_guard_;
    }

    // we test each hazard if it is in the union of retiredList and
    // guardedElement. If it is, it goes into the new retired list...
    for (unsigned int i = 0; i != count_guards; ++i) {
      // consider each current active guard
      GuardType considered_hazard = guards_[i].Load();
      UpdateRetiredList(retired_list, retired_list_temp, retired_list_size,
        toRetire, considered_hazard, undefined_guard_);
    }

    int retired_list_size_signed = static_cast<int>(retired_list_size);
    assert(retired_list_size_signed >= 0);

    // now we created a a new retired list... the elements that are "removed"
    // from the old retired list can be safely deleted now...
    for (int i = -1; i != retired_list_size_signed; ++i) {
      // we iterate over the current retired list... -1 is used as dummy element
      // in the iteration, to also iterate over the pointerToRetire, which is
      // logically also part of the current retired list...

      // end of the list, stop iterating
      if (i >= 0 && retired_list[i] == undefined_guard_)
          break;

      GuardType to_check_if_in_new_list = undefined_guard_;

      to_check_if_in_new_list = (i == -1 ? toRetire : retired_list[i]);

      // still in the new retired list?
      bool still_in_list = false;
      for (unsigned int ii = 0; ii != retired_list_size; ++ii) {
        // end of list
        if (retired_list_temp[ii] == undefined_guard_)
            break;

        if (to_check_if_in_new_list == retired_list_temp[ii]) {
          // still in list, cannot delete element!
          still_in_list = true;
          break;
        }
      }

      if (!still_in_list) {
        this->release_object_callback_(to_check_if_in_new_list);
      }
    }

    // copy the updated retired list (temp) to the retired list...
    CopyRetiredList(retired_list_temp, retired_list, retired_list_size,
      undefined_guard_);
  }
387 388 389 390 391
} // namespace internal
} // namespace containers
} // namespace embb

#endif  // EMBB_CONTAINERS_INTERNAL_HAZARD_POINTER_INL_H_