llx_scx.h 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (c) 2014, Siemens AG. All rights reserved.
 *
 * 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.
 */

27 28
#ifndef EMBB_CONTAINERS_INTERNAL_LLX_SCX_H_
#define EMBB_CONTAINERS_INTERNAL_LLX_SCX_H_
29 30 31

#include <embb/base/thread.h>
#include <embb/base/atomic.h>
32
#include <embb/base/function.h>
33
#include <embb/base/thread_specific_storage.h>
34 35
#include <embb/containers/object_pool.h>
#include <embb/containers/lock_free_tree_value_pool.h>
36
#include <embb/containers/internal/fixed_size_list.h>
37
#include <embb/containers/internal/hazard_pointer.h>
38 39 40 41 42 43 44 45

namespace embb { 
namespace containers {
namespace internal {

/**
 * SCX operation description. An SCX record contains all information
 * required to allow any process to complete a pending SCX operation.
46
 * This class is non-public.
47
 */
48
template< typename DataRecord >
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
class ScxRecord {
 
 private:
  typedef size_t cas_t;
  typedef ScxRecord< DataRecord > self_t;

 public:
  /**
   * Possible states of an LLX/SCX operation.
   */
  typedef enum {
    Undefined = 0,
    Comitted,
    Aborted,
    InProgress
  } OperationState;

 public:
  /**
   * Default constructor, creates sentinel instance of ScxRecord.
   */
  ScxRecord()
71 72 73 74 75 76 77 78
  : linked_data_records(0),
    finalize_data_records(0),
    new_value(0),
    old_value(0),
    scx_ops(0),
    state(OperationState::Comitted),
    all_frozen(false) {
    field = 0;
79 80 81 82 83 84
  }

  /**
   * Constructor.
   */
  ScxRecord(
85
    FixedSizeList<DataRecord *> &
86
      linked_data_records,
87
    FixedSizeList<DataRecord *> &
88
      finalize_data_records,
89
    embb::base::Atomic<cas_t> * target_field,
90 91
    cas_t new_value,
    cas_t old_value,
92
    FixedSizeList<self_t *> * scx_ops,
93
    OperationState operation_state)
94 95 96 97 98 99 100 101
  : linked_data_records(&linked_data_records),
    finalize_data_records(&finalize_data_records),
    new_value(new_value),
    old_value(old_value),
    scx_ops(scx_ops),
    state(operation_state),
    all_frozen(false) {
    field = target_field;
102 103
  }

104
 public:
105 106 107 108
  /**
   * Sequence of load-linked data records for this SCX operation.
   * Named 'V' in original publication.
   */
109
   const FixedSizeList<DataRecord *> *
110
    linked_data_records;
111 112 113 114 115

  /**
   * Sequence of data records to be finalized in this SCX operation.
   * Named 'R' in original publication.
   */
116
   const FixedSizeList<DataRecord *> *
117
    finalize_data_records;
118 119 120 121 122 123

  /**
   * Pointer to a mutable field of a data record in data_records the
   * new value is to be stored.
   * Named 'fld' in original publication.
   */
124
  embb::base::Atomic<cas_t> * field;
125 126 127 128 129

  /**
   * Value to be written in field referenced by field_index. 
   * Required to be compatible with atomic operations.
   */
130
  cas_t new_value;
131 132 133 134 135

  /**
   * Value previously read from field referenced by field_index.
   * Required to be compatible with atomic operations.
   */
136
  cas_t old_value;
137 138 139 140 141

  /**
   * List of SCX operation descriptions associated with data records
   * linked with this SCX operation.
   */
142
  FixedSizeList<self_t *> * scx_ops;
143 144 145 146

  /**
   * Current state of this SCX record.
   */
147
  OperationState state;
148 149 150 151 152 153

  /**
   * Whether all fields are currently frozen, initially false. 
   * Set to true after all data records in data_records V have 
   * been frozen for the SCX.
   */
154
  bool all_frozen;
155 156 157 158 159 160 161

}; /* class ScxRecord */

/**
 * Wraps user-defined data with fields required for LLX/SCX algorithm.
 * Mutable fields must each be contained in a single word.
 */
162
template< typename UserData >
163 164 165 166 167 168 169
class LlxScxRecord {

 private:
  typedef LlxScxRecord<UserData> self_t;
  typedef internal::ScxRecord<self_t> ScxRecord_t;
  typedef typename ScxRecord_t::OperationState OperationState;

170
 public:
171 172 173
  /**
   * The dummy SCX record always has state = Aborted.
   */
174
  static ScxRecord_t DummyScx;
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192

 public:
  /**
   * Default constructor.
   */
  LlxScxRecord();

  /**
   * Constructor. Creates an instance of \c DataRecord_t wrapping a user
   * data object.
   */
  LlxScxRecord(const UserData & user_data);

  /**
   * Copy constructor
   */
  LlxScxRecord(const LlxScxRecord & other)
  : user_data_(other.user_data_) {
193
    scx_op_.Store(other.scx_op_.Load());
194 195 196 197 198 199 200
    marked_for_finalize_ = other.marked_for_finalize_;
  }

  /**
   * Assignment operator.
   */
  LlxScxRecord & operator=(const LlxScxRecord & rhs) {
201 202 203 204 205 206
    if (this != &rhs) {
      user_data_ = rhs.user_data_;
      scx_op_.Store(rhs.scx_op_.Load());
      marked_for_finalize_ = rhs.marked_for_finalize_;
    }
    return *this;
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  }

  /**
   * Destructor.
   */
  ~LlxScxRecord() {
  }
    
  /**
   * Returns list of \c NumMutableElements mutable fields in this 
   * data record.
   */
  UserData & Data() {
    return user_data_;
  }
222
/*
223 224 225 226 227 228 229
  UserData * operator*() {
    return &user_data_;
  }

  UserData * operator->() {
    return &user_data_;
  }
230
*/
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  /**
   * A data record r is frozen for an SCX-record U if r.info points to
   * U and either U.state is InProgress, or U.state is Committed and r
   * is marked.
   * While a data record r is frozen for an SCX record U, a mutable
   * field f of r can be changed only if f is the field pointed to by
   * U.fld, and it can only be changed by a process helping the SCX
   * that created U.
   */
  inline bool IsFrozenFor(const ScxRecord_t & scx) const {
    return scx_op_ == &scx &&
            (scx.State() == InProgress ||
            (scx.State() == Comitted && marked_));
  }

  /**
   * Returns the pointer to the SCX record holding exclusive access to
   * this data record.
   */
  inline embb::base::Atomic<ScxRecord_t *> & ScxInfo() {
    return scx_op_;
  }

  /** 
   * Mark this data record for finalizing.
   */
  inline void MarkForFinalize() {
    marked_for_finalize_ = true;
  }

  /** 
   * Mark this data record for finalizing.
   */
  inline bool IsMarkedForFinalize() const {
    return marked_for_finalize_;
  }
    
268
 private:
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
  /**
   * Instance of the user-defined data type containing mutable 
   * fields.
   */
  UserData user_data_;
  
  /**
   * Pointer to SCX record that describes the last SCX that accessed this
   * data record, initialized with dummy SCX record.
   */
  embb::base::Atomic<ScxRecord_t *> scx_op_;
    
  /**
   * Marked flag, whether this data record has been finalized.
   * The marked bit is initially false and only ever changes from false
   * to true.     
   */
  bool marked_for_finalize_;

};  // class LlxScxRecord

/** 
 * Multiword LL/SC
 * 
 * Implementation of the LLX/STX primitive as presented in 
 * "Pragmatic Primitives for Non-blocking Data Structures" 
 * (Brown et al., 2013).
 *
 * \tparam UserData Type containing mutable fields
298
 * \tparam ValuePool Type containing mutable fields
299
 */
300 301 302 303
template<
  typename UserData,
  typename ValuePool = embb::containers::LockFreeTreeValuePool< bool, false >
>
304 305
class LlxScx {

306
 private:
307
  typedef size_t cas_t;
308
  typedef LlxScx<UserData, ValuePool> self_t;
309
  typedef LlxScxRecord< UserData > DataRecord_t;
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
  typedef internal::ScxRecord< LlxScxRecord<UserData> > ScxRecord_t;
  typedef typename ScxRecord_t::OperationState OperationState;

 public:
  /**
   * Constructs a new instance of LlxScx.
   */
  LlxScx(
    size_t max_links
    /**< [IN] Maximum number of links depending on a single SCX operation */
  );

  /**
   * Destructor, frees memory.
   */
  ~LlxScx();

  /**
   * Tentatively performs an LLX (extended load-linked) operation on given 
   * LLX/SCX data record.
   * Returns true and stores result in given reference variable on success, 
   * otherwise returns false.
   */
  bool TryLoadLinked(
    DataRecord_t * const data_record,
    /**< [IN] Pointer to data record to load */
336
    UserData & data,
337
    /**< [OUT] Atomic snapshot of data record */
338 339 340
    bool & finalized
    /**< [OUT] Indicating whether requested fields have been finalized */
  );
341 342

  /**
343 344 345 346 347 348
   * Clears the calling thread's active links previously established using
   * \c TryLoadLinked.
   */
  void ClearLinks();

  /**
349 350
   * Actual implementation of StoreConditional operating on unified fields/values
   * of type cas_t.
351 352 353 354 355
   * Tentatively performs a single-record Store-Conditional operation on 
   * given LLX/SCX data record.
   * Returns true if the given value has been stored successfully, otherwise
   * false.
   */
356
  bool TryStoreConditional(
357
    embb::base::Atomic<cas_t> * cas_field,
358
    /**< [IN] Pointer to the field the value will be stored into */
359
    cas_t cas_value,
360 361 362 363 364
    /**< [IN] Value to store */
    embb::containers::internal::FixedSizeList<DataRecord_t *> & linked_deps,
    /**< [IN] Data records linked to this store operation */
    embb::containers::internal::FixedSizeList<DataRecord_t *> & finalize_deps
    /**< [IN] Data records to be finalized in this store operation */
365
  );
366

367
  /**
368 369 370
   * Performs a VLX (extended validate link) operation on list of given LLX
   * data records.
   * Before calling this method, the given LLX/SCX records must have been
371
   * linked via \c TryLoadLinked.
372
   *
373 374
   * \returns True if the calling thread's links obtained by its most recent
   *          invocations of SCX is still valid.
375 376
   */
  bool TryValidateLink(
377 378
    embb::containers::internal::FixedSizeList<DataRecord_t *> & linked_deps
    /**< [IN] Linked data records to validate */
379
  );
380
  
381
 private:
382 383 384 385
  /**
   * Result of a Load-Linked operation, to be stored in thread-specific
   * array range within thread_llx_results_.
   */
386 387 388 389 390
  typedef struct {
    DataRecord_t * data_record;
    ScxRecord_t * scx_record;
    UserData user_data;
  } LlxResult;
391
  
392 393 394 395
  /**
   * Resolves the calling thread's Id.
   */
  unsigned int ThreadId();
396 397 398 399 400
    
  /**
   * Help complete an SCX operation referenced by the given SCX record
   */
  bool Help(ScxRecord_t * scx);
401 402

  /**
403 404 405 406 407 408
   * The callback function, used to cleanup non-hazardous pointers.
   * \see delete_pointer_callback
   */
  void DeleteOperationCallback(ScxRecord_t * scx_record);

  /**
409 410 411 412 413
   * Maximum number of active links created via TryLoadLinked per thread.
   */
  size_t max_links_;

  /**
414 415 416 417 418
   * Maximum number of threads engaging in operations on this LLX/SCX instance.
   */
  unsigned int max_threads_;

  /**
419 420
   * Shared table containing for each r in V, a copy of r's info value in this
   * thread's local table of LLX results.
421 422
   */
  embb::containers::ObjectPool<
423
    embb::containers::internal::FixedSizeList< ScxRecord_t * >, ValuePool >
424
      scx_record_list_pool_;
425

426 427 428
  /**
   * Pool for SCX records allocated in TryStoreConditional
   */
429 430
  embb::containers::ObjectPool< ScxRecord_t, ValuePool > scx_record_pool_;

431 432 433
  /**
   * Thread-specific list of LLX results performed by the thread.
   */
434
  embb::containers::internal::FixedSizeList< LlxResult > **
435
    thread_llx_results_;
436 437 438 439 440 441 442 443 444 445 446
  
  /**
   * Callback to the method that is called by hazard pointers if a pointer is
   * not hazardous anymore, i.e., can safely be reused.
   */
  embb::base::Function < void, ScxRecord_t * > delete_operation_callback;

  /**
   * The hazard pointer object, used for memory management.
   */
  embb::containers::internal::HazardPointer< ScxRecord_t * > hp;
447 448 449 450 451

  /**
   * Prevent default construction.
   */
  LlxScx();
452

453 454 455 456
  /**
   * Prevent copy construction.
   */
  LlxScx(const LlxScx &);
457

458 459 460 461 462 463 464 465 466 467 468
  /**
   * Prevent assignment.
   */
  LlxScx & operator=(const LlxScx &);

};

} // namespace primitives
} // namespace containers
} // namespace embb

469
#include <embb/containers/internal/llx_scx-inl.h>
470

471
#endif  // EMBB_CONTAINERS_INTERNAL_LLX_SCX_H_