This file is indexed.

/usr/include/OpenImageIO/thread.h is in libopenimageio-dev 1.6.17~dfsg0-1+b2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  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
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  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
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 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
 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
 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
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
/*
  Copyright 2008 Larry Gritz and the other authors and contributors.
  All Rights Reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * 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.
  * Neither the name of the software's owners nor the names of its
    contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.
  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
  OWNER 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.

  (This is the Modified BSD License)
*/


/////////////////////////////////////////////////////////////////////////
/// @file   thread.h
///
/// @brief  Wrappers and utilities for multithreading.
/////////////////////////////////////////////////////////////////////////


#ifndef OPENIMAGEIO_THREAD_H
#define OPENIMAGEIO_THREAD_H

#include <vector>

#include "oiioversion.h"
#include "platform.h"



#if OIIO_CPLUSPLUS_VERSION >= 11
# include <thread>
# include <mutex>
# include <atomic>
# define not_yet_OIIO_USE_STDATOMIC 1
#else   /* prior to C++11... */
  // Use Boost mutexes & guards when C++11 is not available
# include <boost/version.hpp>
# if defined(__GNUC__) && (BOOST_VERSION == 104500)
   // gcc reports errors inside some of the boost headers with boost 1.45
   // See: https://svn.boost.org/trac/boost/ticket/4818
#  pragma GCC diagnostic ignored "-Wunused-variable"
# endif
# include <boost/thread.hpp>
# if defined(__GNUC__) && (BOOST_VERSION == 104500)
   // can't restore via push/pop in all versions of gcc (warning push/pop implemented for 4.6+ only)
#  pragma GCC diagnostic error "-Wunused-variable"
# endif
#endif


#if defined(_MSC_VER)
   // N.B. including platform.h also included <windows.h>
#  pragma intrinsic (_InterlockedExchangeAdd)
#  pragma intrinsic (_InterlockedCompareExchange)
#  pragma intrinsic (_InterlockedCompareExchange64)
#  if defined(_WIN64)
#    pragma intrinsic(_InterlockedExchangeAdd64)
#  endif
// InterlockedExchangeAdd64 & InterlockedExchange64 are not available for XP
#  if defined(_WIN32_WINNT) && _WIN32_WINNT <= 0x0501
inline long long
InterlockedExchangeAdd64 (volatile long long *Addend, long long Value)
{
    long long Old;
    do {
        Old = *Addend;
    } while (_InterlockedCompareExchange64(Addend, Old + Value, Old) != Old);
    return Old;
}

inline long long
InterlockedExchange64 (volatile long long *Target, long long Value)
{
    long long Old;
    do {
        Old = *Target;
    } while (_InterlockedCompareExchange64(Target, Value, Old) != Old);
    return Old;
}
#  endif
#endif

#if defined(__GNUC__) && (defined(_GLIBCXX_ATOMIC_BUILTINS) || (__GNUC__ * 100 + __GNUC_MINOR__ >= 401))
#  define USE_GCC_ATOMICS
#  if !defined(__clang__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 408)
#    define OIIO_USE_GCC_NEW_ATOMICS
#  endif
#endif


// OIIO_THREAD_ALLOW_DCLP, if set to 0, prevents us from using a dodgy
// "double checked lock pattern" (DCLP).  We are very careful to construct
// it safely and correctly, and these uses improve thread performance for
// us.  But it confuses Thread Sanitizer, so this switch allows you to turn
// it off. Also set to 0 if you don't believe that we are correct in
// allowing this construct on all platforms.
#ifndef OIIO_THREAD_ALLOW_DCLP
#define OIIO_THREAD_ALLOW_DCLP 1
#endif



// Some helpful links:
//
// Descriptions of the "new" gcc atomic intrinsics:
//    https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
// Old gcc atomic intrinsics:
//    https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Atomic-Builtins.html
// C++11 and beyond std::atomic:
//    http://en.cppreference.com/w/cpp/atomic



OIIO_NAMESPACE_BEGIN

/// Null mutex that can be substituted for a real one to test how much
/// overhead is associated with a particular mutex.
class null_mutex {
public:
    null_mutex () { }
    ~null_mutex () { }
    void lock () { }
    void unlock () { }
    void lock_shared () { }
    void unlock_shared () { }
    bool try_lock () { return true; }
};

/// Null lock that can be substituted for a real one to test how much
/// overhead is associated with a particular lock.
template<typename T>
class null_lock {
public:
    null_lock (T &m) { }
};



#ifdef NOTHREADS

// Definitions that we use for debugging to turn off all mutexes, locks,
// and atomics in order to test the performance hit of our thread safety.

typedef null_mutex mutex;
typedef null_mutex recursive_mutex;
typedef null_lock<mutex> lock_guard;
typedef null_lock<recursive_mutex> recursive_lock_guard;

#elif OIIO_CPLUSPLUS_VERSION >= 11

typedef std::mutex mutex;
typedef std::recursive_mutex recursive_mutex;
typedef std::lock_guard< mutex > lock_guard;
typedef std::lock_guard< recursive_mutex > recursive_lock_guard;
typedef std::thread thread;

#else

// Fairly modern Boost has all the mutex and lock types we need.

typedef boost::mutex mutex;
typedef boost::recursive_mutex recursive_mutex;
typedef boost::lock_guard< mutex > lock_guard;
typedef boost::lock_guard< recursive_mutex > recursive_lock_guard;
typedef boost::thread thread;

#endif



#if OIIO_USE_STDATOMIC
using std::memory_order;
#else
enum memory_order {
#if defined(OIIO_USE_GCC_NEW_ATOMICS)
    memory_order_relaxed = __ATOMIC_RELAXED,
    memory_order_consume = __ATOMIC_CONSUME,
    memory_order_acquire = __ATOMIC_ACQUIRE,
    memory_order_release = __ATOMIC_RELEASE,
    memory_order_acq_rel = __ATOMIC_ACQ_REL,
    memory_order_seq_cst = __ATOMIC_SEQ_CST
#else
    memory_order_relaxed,
    memory_order_consume,
    memory_order_acquire,
    memory_order_release,
    memory_order_acq_rel,
    memory_order_seq_cst
#endif
};
#endif



/// Atomic version of:  r = *at, *at += x, return r
/// For each of several architectures.
inline int
atomic_exchange_and_add (volatile int *at, int x,
                         memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    int r = *at;  *at += x;  return r;
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_fetch_add (at, x, order);
#elif defined(USE_GCC_ATOMICS)
    return __sync_fetch_and_add ((int *)at, x);
#elif defined(_MSC_VER)
    // Windows
    return _InterlockedExchangeAdd ((volatile LONG *)at, x);
#else
#   error No atomics on this platform.
#endif
}



inline long long
atomic_exchange_and_add (volatile long long *at, long long x,
                         memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    long long r = *at;  *at += x;  return r;
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_fetch_add (at, x, order);
#elif defined(USE_GCC_ATOMICS)
    return __sync_fetch_and_add (at, x);
#elif defined(_MSC_VER)
    // Windows
#  if defined(_WIN64)
    return _InterlockedExchangeAdd64 ((volatile LONGLONG *)at, x);
#  else
    return InterlockedExchangeAdd64 ((volatile LONGLONG *)at, x);
#  endif
#else
#   error No atomics on this platform.
#endif
}



/// Atomic version of:  r = *at, *at &= x, return r
/// For each of several architectures.
inline int
atomic_exchange_and_and (volatile int *at, int x,
                        memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    int r = *at;  *at &= x;  return r;
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_fetch_and (at, x, order);
#elif defined(USE_GCC_ATOMICS)
    return __sync_fetch_and_and ((int *)at, x);
#elif defined(_MSC_VER)
    // Windows
    return _InterlockedAnd ((volatile LONG *)at, x);
#else
#   error No atomics on this platform.
#endif
}



inline long long
atomic_exchange_and_and (volatile long long *at, long long x,
                        memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    long long r = *at;  *at &= x;  return r;
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_fetch_and (at, x, order);
#elif defined(USE_GCC_ATOMICS)
    return __sync_fetch_and_and (at, x);
#elif defined(_MSC_VER)
    // Windows
#  if defined(_WIN64)
    return _InterlockedAnd64 ((volatile LONGLONG *)at, x);
#  else
    return InterlockedAnd64 ((volatile LONGLONG *)at, x);
#  endif
#else
#   error No atomics on this platform.
#endif
}



/// Atomic version of:  r = *at, *at |= x, return r
/// For each of several architectures.
inline int
atomic_exchange_and_or (volatile int *at, int x,
                        memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    int r = *at;  *at |= x;  return r;
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_fetch_or (at, x, order);
#elif defined(USE_GCC_ATOMICS)
    return __sync_fetch_and_or ((int *)at, x);
#elif defined(_MSC_VER)
    // Windows
    return _InterlockedOr ((volatile LONG *)at, x);
#else
#   error No atomics on this platform.
#endif
}



inline long long
atomic_exchange_and_or (volatile long long *at, long long x,
                        memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    long long r = *at;  *at |= x;  return r;
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_fetch_or (at, x, order);
#elif defined(USE_GCC_ATOMICS)
    return __sync_fetch_and_or (at, x);
#elif defined(_MSC_VER)
    // Windows
#  if defined(_WIN64)
    return _InterlockedOr64 ((volatile LONGLONG *)at, x);
#  else
    return InterlockedOr64 ((volatile LONGLONG *)at, x);
#  endif
#else
#   error No atomics on this platform.
#endif
}



/// Atomic version of:  r = *at, *at ^= x, return r
/// For each of several architectures.
inline int
atomic_exchange_and_xor (volatile int *at, int x,
                         memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    int r = *at;  *at ^= x;  return r;
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_fetch_xor (at, x, order);
#elif defined(USE_GCC_ATOMICS)
    return __sync_fetch_and_xor ((int *)at, x);
#elif defined(_MSC_VER)
    // Windows
    return _InterlockedXor ((volatile LONG *)at, x);
#else
#   error No atomics on this platform.
#endif
}



inline long long
atomic_exchange_and_xor (volatile long long *at, long long x,
                         memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    long long r = *at;  *at ^= x;  return r;
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_fetch_xor (at, x, order);
#elif defined(USE_GCC_ATOMICS)
    return __sync_fetch_and_xor (at, x);
#elif defined(_MSC_VER)
    // Windows
#  if defined(_WIN64)
    return _InterlockedXor64 ((volatile LONGLONG *)at, x);
#  else
    return InterlockedXor64 ((volatile LONGLONG *)at, x);
#  endif
#else
#   error No atomics on this platform.
#endif
}



/// Atomic version of:
///    if (*at == compareval) {
///        *at = newval;  return true;
///    } else {
///        return false;
///    }
inline bool
atomic_compare_and_exchange (volatile int *at, int compareval, int newval,
                             bool weak = false,
                             memory_order success = memory_order_seq_cst,
                             memory_order failure = memory_order_seq_cst)
{
#ifdef NOTHREADS
    if (*at == compareval) {
        *at = newval;  return true;
    } else {
        return false;
    }
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_compare_exchange_n (at, &compareval, newval, weak,
                                        success, failure);
#elif defined(USE_GCC_ATOMICS)
    return __sync_bool_compare_and_swap (at, compareval, newval);
#elif defined(_MSC_VER)
    return (_InterlockedCompareExchange ((volatile LONG *)at, newval, compareval) == compareval);
#else
#   error No atomics on this platform.
#endif
}



inline bool
atomic_compare_and_exchange (volatile long long *at, long long compareval, long long newval,
                             bool weak = false,
                             memory_order success = memory_order_seq_cst,
                             memory_order failure = memory_order_seq_cst)
{
#ifdef NOTHREADS
    if (*at == compareval) {
        *at = newval;  return true;
    } else {
        return false;
    }
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_compare_exchange_n (at, &compareval, newval, weak,
                                        success, failure);
#elif defined(USE_GCC_ATOMICS)
    return __sync_bool_compare_and_swap (at, compareval, newval);
#elif defined(_MSC_VER)
    return (_InterlockedCompareExchange64 ((volatile LONGLONG *)at, newval, compareval) == compareval);
#else
#   error No atomics on this platform.
#endif
}



/// Atomic version of:  r = *at, *at = x, return r
/// For each of several architectures.
inline int
atomic_exchange (volatile int *at, int x,
                 memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    int r = *at;  *at = x;  return r;
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_exchange_n (at, x, order);
#elif defined(USE_GCC_ATOMICS)
    // No __sync version of atomic exchange! Do it the hard way:
    while (1) {
        int old = *at;
        if (atomic_compare_and_exchange (at, old, x))
            return old;
    }
    return 0; // can never happen
#elif defined(_MSC_VER)
    // Windows
    return _InterlockedExchange ((volatile LONG *)at, x);
#else
#   error No atomics on this platform.
#endif
}



inline long long
atomic_exchange (volatile long long *at, long long x,
                 memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    long long r = *at;  *at = x;  return r;
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    return __atomic_exchange_n (at, x, order);
#elif defined(USE_GCC_ATOMICS)
    // No __sync version of atomic exchange! Do it the hard way:
    while (1) {
        long long old = *at;
        if (atomic_compare_and_exchange (at, old, x))
            return old;
    }
    return 0; // can never happen
#elif defined(_MSC_VER)
    // Windows
#  if defined(_WIN64)
    return _InterlockedExchange64 ((volatile LONGLONG *)at, x);
#  else
    return InterlockedExchange64 ((volatile LONGLONG *)at, x);
#  endif
#else
#   error No atomics on this platform.
#endif
}



/// Memory fence / synchronization barrier
OIIO_FORCEINLINE void
atomic_thread_fence (memory_order order = memory_order_seq_cst)
{
#ifdef NOTHREADS
    // nothing
#elif OIIO_USE_STDATOMIC
    std::__atomic_thread_fence (order);
#elif defined(OIIO_USE_GCC_NEW_ATOMICS)
    __atomic_thread_fence (order);
#elif defined(USE_GCC_ATOMICS)
    __sync_synchronize ();
#elif defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
    __asm__ __volatile__ ("":::"memory");
#elif defined(_MSC_VER)
    MemoryBarrier ();
#else
#   error No atomics on this platform.
#endif
}



/// Yield the processor for the rest of the timeslice.
///
inline void
yield ()
{
#if defined(__GNUC__)
    sched_yield ();
#elif defined(_MSC_VER)
    SwitchToThread ();
#else
#   error No yield on this platform.
#endif
}



// Slight pause
inline void
pause (int delay)
{
#if defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
    for (int i = 0; i < delay; ++i)
        __asm__ __volatile__("pause;");

#elif defined(__GNUC__) && (defined(__arm__) || defined(__s390__))
    for (int i = 0; i < delay; ++i)
        __asm__ __volatile__("NOP;");

#elif defined(_MSC_VER)
    for (int i = 0; i < delay; ++i) {
#if defined (_WIN64)
        YieldProcessor();
#else
        _asm  pause
#endif /* _WIN64 */
    }

#else
    // No pause on this platform, just punt
    for (int i = 0; i < delay; ++i) ;
#endif
}



// Helper class to deliver ever longer pauses until we yield our timeslice.
class atomic_backoff {
public:
    atomic_backoff () : m_count(1) { }

    void operator() () {
        if (m_count <= 16) {
            pause (m_count);
            m_count *= 2;
        } else {
            yield();
        }
    }

private:
    int m_count;
};



/// Atomic integer.  Increment, decrement, add, and subtract in a
/// totally thread-safe manner.
template<class T>
class atomic {
public:
    /// Construct with initial value.
    ///
    atomic (T val=0) : m_val(val) { }

    ~atomic () { }

    /// Retrieve value
    ///
    T load (memory_order order = memory_order_seq_cst) const {
        return atomic_exchange_and_add (&m_val, 0, order);
    }

    /// Retrieve value
    ///
    T operator() () const { return load(); }

    /// Retrieve value
    ///
    operator T() const { return load(); }

    /// Fast retrieval of value, no interchange, don't care about memory
    /// fences. Use with extreme caution!
    T fast_value () const { return m_val; }

    /// Assign new value, atomically.
    void store (T x, memory_order order = memory_order_seq_cst) {
        atomic_exchange (&m_val, x, order);
    }

    /// Atomic exchange
    T exchange (T x, memory_order order = memory_order_seq_cst) {
        return atomic_exchange (&m_val, x, order);
    }

    /// Atomic fetch-and-add: add x and return the old value.
    T fetch_add (T x, memory_order order = memory_order_seq_cst) {
        return atomic_exchange_and_add (&m_val, x, order);
    }
    /// Atomic fetch-and-subtract: subtract x and return the old value.
    T fetch_sub (T x, memory_order order = memory_order_seq_cst) {
        return atomic_exchange_and_add (&m_val, -x, order);
    }
    /// Atomic fetch-and-and: bitwise and with x and return the old value.
    T fetch_and (T x, memory_order order = memory_order_seq_cst) {
        return atomic_exchange_and_and (&m_val, x, order);
    }
    /// Atomic fetch-and-or: bitwise or with x and return the old value.
    T fetch_or (T x, memory_order order = memory_order_seq_cst) {
        return atomic_exchange_and_or (&m_val, x, order);
    }
    /// Atomic fetch-and-xor: bitwise xor with x and return the old value.
    T fetch_xor (T x, memory_order order = memory_order_seq_cst) {
        return atomic_exchange_and_xor (&m_val, x, order);
    }

    /// Assign new value.
    ///
    T operator= (T x) { store(x); return x; }

    /// Pre-increment:  ++foo
    ///
    T operator++ () { return fetch_add(1) + 1; }

    /// Post-increment:  foo++
    ///
    T operator++ (int) {  return fetch_add(1); }

    /// Pre-decrement:  --foo
    ///
    T operator-- () {  return fetch_sub(1) - 1; }

    /// Post-decrement:  foo--
    ///
    T operator-- (int) {  return fetch_sub(1); }

    /// Add to the value, return the new result
    ///
    T operator+= (T x) { return fetch_add(x) + x; }

    /// Subtract from the value, return the new result
    ///
    T operator-= (T x) { return fetch_sub(x) - x; }

    /// Logical and, return the new result
    ///
    T operator&= (T x) { return fetch_and(x) & x; }

    /// Logical or, return the new result
    ///
    T operator|= (T x) { return fetch_or(x) | x; }

    /// Logical xor, return the new result
    ///
    T operator^= (T x) { return fetch_xor(x) ^ x; }

    bool bool_compare_and_swap (T compareval, T newval) {
        return atomic_compare_and_exchange (&m_val, compareval, newval);
    }

    T operator= (const atomic &x) {
        T r = x();
        *this = r;
        return r;
    }

private:
#ifdef __arm__
    OIIO_ALIGN(8)
#endif 
    volatile mutable T m_val;

    // Disallow copy construction by making private and unimplemented.
    atomic (atomic const &);
};



#ifdef NOTHREADS

typedef int atomic_int;
typedef long long atomic_ll;

#else

typedef atomic<int> atomic_int;
typedef atomic<long long> atomic_ll;

#endif



#ifdef NOTHREADS

typedef null_mutex spin_mutex;
typedef null_lock<spin_mutex> spin_lock;

#else

// Define our own spin locks.


/// A spin_mutex is semantically equivalent to a regular mutex, except
/// for the following:
///  - A spin_mutex is just 4 bytes, whereas a regular mutex is quite
///    large (44 bytes for pthread).
///  - A spin_mutex is extremely fast to lock and unlock, whereas a regular
///    mutex is surprisingly expensive just to acquire a lock.
///  - A spin_mutex takes CPU while it waits, so this can be very
///    wasteful compared to a regular mutex that blocks (gives up its
///    CPU slices until it acquires the lock).
///
/// The bottom line is that mutex is the usual choice, but in cases where
/// you need to acquire locks very frequently, but only need to hold the
/// lock for a very short period of time, you may save runtime by using
/// a spin_mutex, even though it's non-blocking.
///
/// N.B. A spin_mutex is only the size of an int.  To avoid "false
/// sharing", be careful not to put two spin_mutex objects on the same
/// cache line (within 128 bytes of each other), or the two mutexes may
/// effectively (and wastefully) lock against each other.
///
class spin_mutex {
public:
    /// Default constructor -- initialize to unlocked.
    ///
    spin_mutex (void) { m_locked = 0; }

    ~spin_mutex (void) { }

    /// Copy constructor -- initialize to unlocked.
    ///
    spin_mutex (const spin_mutex &) { m_locked = 0; }

    /// Assignment does not do anything, since lockedness should not
    /// transfer.
    const spin_mutex& operator= (const spin_mutex&) { return *this; }

    /// Acquire the lock, spin until we have it.
    ///
    void lock () {
        // To avoid spinning too tightly, we use the atomic_backoff to
        // provide increasingly longer pauses, and if the lock is under
        // lots of contention, eventually yield the timeslice.
        atomic_backoff backoff;

        // Try to get ownership of the lock. Though experimentation, we
        // found that OIIO_UNLIKELY makes this just a bit faster on 
        // gcc x86/x86_64 systems.
        while (! OIIO_UNLIKELY(try_lock())) {
#if OIIO_THREAD_ALLOW_DCLP
            // The full try_lock() involves a compare_and_swap, which
            // writes memory, and that will lock the bus.  But a normal
            // read of m_locked will let us spin until the value
            // changes, without locking the bus. So it's faster to
            // check in this manner until the mutex appears to be free.
            // HOWEVER... Thread Sanitizer things this is an instance of
            // an unsafe "double checked lock pattern" (DCLP) and flags it
            // as an error. I think it's a false negative, because the
            // outer loop is still an atomic check, the inner non-atomic
            // loop only serves to delay, and can't lead to a true data
            // race. But we provide this build-time switch to, at least,
            // give a way to use tsan for other checks.
            do {
                backoff();
            } while (m_locked);
#else
            backoff();
#endif
        }
    }

    /// Release the lock that we hold.
    ///
    void unlock () {
        // Fastest way to do it is with a store with "release" semantics
#if defined(OIIO_USE_GCC_NEW_ATOMICS)
        __atomic_clear (&m_locked, __ATOMIC_RELEASE);
#elif defined(USE_GCC_ATOMICS)
        __sync_lock_release (&m_locked);
        //   Equivalent, x86 specific code:
        //   __asm__ __volatile__("": : :"memory");
        //   m_locked = 0;
#elif defined(_MSC_VER)
        MemoryBarrier ();
        m_locked = 0;
#else
        // Otherwise, just assign zero to the atomic (but that's a full 
        // memory barrier).
        *(atomic_int *)&m_locked = 0;
#endif
    }

    /// Try to acquire the lock.  Return true if we have it, false if
    /// somebody else is holding the lock.
    bool try_lock () {
#if defined(OIIO_USE_GCC_NEW_ATOMICS)
        return __atomic_test_and_set (&m_locked, __ATOMIC_ACQUIRE) == 0;
#elif defined(USE_GCC_ATOMICS)
        // GCC gives us an intrinsic that is even better -- an atomic
        // exchange with "acquire" barrier semantics.
        return __sync_lock_test_and_set (&m_locked, 1) == 0;
#else
        // Our compare_and_swap returns true if it swapped
        return atomic_compare_and_exchange (&m_locked, 0, 1);
#endif
    }

    /// Helper class: scoped lock for a spin_mutex -- grabs the lock upon
    /// construction, releases the lock when it exits scope.
    class lock_guard {
    public:
        lock_guard (spin_mutex &fm) : m_fm(fm) { m_fm.lock(); }
        ~lock_guard () { m_fm.unlock(); }
    private:
        lock_guard(); // Do not implement
        lock_guard(const lock_guard& other); // Do not implement
        lock_guard& operator = (const lock_guard& other); // Do not implement
        spin_mutex & m_fm;
    };

private:
#if defined(OIIO_USE_GCC_NEW_ATOMICS)
    // Using the gcc >= 4.8 new atomics, we can easily do a single byte flag
    volatile char m_locked; ///< Atomic counter is zero if nobody holds the lock
#else
    // Otherwise, fall back on it being an int
    volatile int m_locked;  ///< Atomic counter is zero if nobody holds the lock
#endif
};


typedef spin_mutex::lock_guard spin_lock;

#endif



/// Spinning reader/writer mutex.  This is just like spin_mutex, except
/// that there are separate locking mechanisms for "writers" (exclusive
/// holders of the lock, presumably because they are modifying whatever
/// the lock is protecting) and "readers" (non-exclusive, non-modifying
/// tasks that may access the protectee simultaneously).
class spin_rw_mutex {
public:
    /// Default constructor -- initialize to unlocked.
    ///
    spin_rw_mutex (void) { m_readers = 0; }

    ~spin_rw_mutex (void) { }

    /// Copy constructor -- initialize to unlocked.
    ///
    spin_rw_mutex (const spin_rw_mutex &) { m_readers = 0; }

    /// Assignment does not do anything, since lockedness should not
    /// transfer.
    const spin_rw_mutex& operator= (const spin_rw_mutex&) { return *this; }

    /// Acquire the reader lock.
    ///
    void read_lock () {
        // Spin until there are no writers active
        m_locked.lock();
        // Register ourself as a reader
        ++m_readers;
        // Release the lock, to let other readers work
        m_locked.unlock();
    }

    /// Release the reader lock.
    ///
    void read_unlock () {
        --m_readers;  // it's atomic, no need to lock to release
    }

    /// Acquire the writer lock.
    ///
    void write_lock () {
        // Make sure no new readers (or writers) can start
        m_locked.lock();
        // Spin until the last reader is done, at which point we will be
        // the sole owners and nobody else (reader or writer) can acquire
        // the resource until we release it.
#if OIIO_THREAD_ALLOW_DCLP
        while (*(volatile int *)&m_readers > 0)
                ;
#else
        while (m_readers > 0)
                ;
#endif
    }

    /// Release the writer lock.
    ///
    void write_unlock () {
        // Let other readers or writers get the lock
        m_locked.unlock ();
    }

    /// Acquire an exclusive ("writer") lock.
    void lock () { write_lock(); }

    /// Release an exclusive ("writer") lock.
    void unlock () { write_unlock(); }

    /// Acquire a shared ("reader") lock.
    void lock_shared () { read_lock(); }

    /// Release a shared ("reader") lock.
    void unlock_shared () { read_unlock(); }

    /// Helper class: scoped read lock for a spin_rw_mutex -- grabs the
    /// read lock upon construction, releases the lock when it exits scope.
    class read_lock_guard {
    public:
        read_lock_guard (spin_rw_mutex &fm) : m_fm(fm) { m_fm.read_lock(); }
        ~read_lock_guard () { m_fm.read_unlock(); }
    private:
        read_lock_guard(); // Do not implement
        read_lock_guard(const read_lock_guard& other); // Do not implement
        read_lock_guard& operator = (const read_lock_guard& other); // Do not implement
        spin_rw_mutex & m_fm;
    };

    /// Helper class: scoped write lock for a spin_rw_mutex -- grabs the
    /// read lock upon construction, releases the lock when it exits scope.
    class write_lock_guard {
    public:
        write_lock_guard (spin_rw_mutex &fm) : m_fm(fm) { m_fm.write_lock(); }
        ~write_lock_guard () { m_fm.write_unlock(); }
    private:
        write_lock_guard(); // Do not implement
        write_lock_guard(const write_lock_guard& other); // Do not implement
        write_lock_guard& operator = (const write_lock_guard& other); // Do not implement
        spin_rw_mutex & m_fm;
    };

private:
    OIIO_CACHE_ALIGN
    spin_mutex m_locked;   // write lock
    char pad1_[OIIO_CACHE_LINE_SIZE-sizeof(spin_mutex)];
    OIIO_CACHE_ALIGN
    atomic_int m_readers;  // number of readers
    char pad2_[OIIO_CACHE_LINE_SIZE-sizeof(atomic_int)];
};


typedef spin_rw_mutex::read_lock_guard spin_rw_read_lock;
typedef spin_rw_mutex::write_lock_guard spin_rw_write_lock;



/// Mutex pool. Sometimes, we have lots of objects that need to be
/// individually locked for thread safety, but two separate objects don't
/// need to lock against each other. If there are many more objects than
/// threads, it's wasteful for each object to contain its own mutex. So a
/// solution is to make a mutex_pool -- a collection of several mutexes.
/// Each object uses a hash to choose a consistent mutex for itself, but
/// which will be unlikely to be locked simultaneously by different object.
/// Semantically, it looks rather like an associative array of mutexes. We
/// also ensure that the mutexes are all on different cache lines, to ensure
/// that they don't exhibit false sharing. Try to choose Bins larger than
/// the expected number of threads that will be simultaneously locking
/// mutexes.
template<class Mutex, class Key, class Hash, size_t Bins=16>
class mutex_pool
{
public:
    mutex_pool () { }
    Mutex& operator[] (const Key &key) {
        return m_mutex[m_hash(key) % Bins].m;
    }
private:
    // Helper type -- force cache line alignment. This should make an array
    // of these also have padding so that each individual mutex is aligned
    // to its own cache line, thus eliminating any "false sharing."
    struct AlignedMutex {
        OIIO_CACHE_ALIGN Mutex m;
    };

    AlignedMutex m_mutex[Bins];
    Hash m_hash;
};



/// Simple thread group class. This is just as good as boost::thread_group,
/// for the limited functionality that we use.
class thread_group {
public:
    thread_group () {}
    ~thread_group () {
        for (size_t i = 0, e = m_threads.size(); i < e; ++i)
            delete m_threads[i];
    }
    void add_thread (thread *t) {
        if (t) {
            lock_guard lock (m_mutex);
            m_threads.push_back (t);
        }
    }
    template<typename FUNC>
    thread *create_thread (FUNC func) {
        lock_guard lock (m_mutex);
        thread *t = new thread (func);
        m_threads.push_back (t);
        return t;
    }
    void join_all () {
        lock_guard lock (m_mutex);
        for (size_t i = 0, e = m_threads.size(); i < e; ++i) {
            if (m_threads[i]->joinable())
                m_threads[i]->join();
        }
    }
    size_t size () {
        lock_guard lock (m_mutex);
        return m_threads.size();
    }
private:
    mutex m_mutex;
    std::vector<thread *> m_threads;
};


OIIO_NAMESPACE_END

#endif // OPENIMAGEIO_THREAD_H