• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

openmc-dev / openmc / 10406990930

15 Aug 2024 04:22PM UTC coverage: 84.721% (-0.2%) from 84.9%
10406990930

Pull #3112

github

web-flow
Merge dd7896694 into 10c511a0e
Pull Request #3112: Revamp CI with dependency and Python caching for efficient installs

49440 of 58356 relevant lines covered (84.72%)

32153493.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/include/openmc/openmp_interface.h
1
#ifndef OPENMC_OPENMP_INTERFACE_H
2
#define OPENMC_OPENMP_INTERFACE_H
3

4
#ifdef _OPENMP
5
#include <omp.h>
6
#endif
7

8
namespace openmc {
9

10
//==============================================================================
11
//! Accessor functions related to number of threads and thread number
12
//==============================================================================
13
inline int num_threads()
14
{
15
#ifdef _OPENMP
16
  return omp_get_max_threads();
17
#else
18
  return 1;
19
#endif
20
}
21

22
inline int thread_num()
70✔
23
{
24
#ifdef _OPENMP
25
  return omp_get_thread_num();
40✔
26
#else
27
  return 0;
30✔
28
#endif
29
}
30

31
//==============================================================================
32
//! An object used to prevent concurrent access to a piece of data.
33
//
34
//! This type meets the C++ "Lockable" requirements.
35
//==============================================================================
36

37
class OpenMPMutex {
38
public:
39
  OpenMPMutex()
×
40
  {
41
#ifdef _OPENMP
42
    omp_init_lock(&mutex_);
43
#endif
44
  }
45

46
  ~OpenMPMutex()
×
47
  {
48
#ifdef _OPENMP
49
    omp_destroy_lock(&mutex_);
50
#endif
51
  }
52

53
  // omp_lock_t objects cannot be deep copied, they can only be shallow
54
  // copied. Thus, while shallow copying of an omp_lock_t object is
55
  // completely valid (provided no race conditions exist), true copying
56
  // of an OpenMPMutex object is not valid due to the action of the
57
  // destructor. However, since locks are fungible, we can simply replace
58
  // copying operations with default construction. This allows storage of
59
  // OpenMPMutex objects within containers that may need to move/copy them
60
  // (e.g., std::vector). It is left to the caller to understand that
61
  // copying of OpenMPMutex does not produce two handles to the same mutex,
62
  // rather, it produces two different mutexes.
63

64
  // Copy constructor
65
  OpenMPMutex(const OpenMPMutex& other) { OpenMPMutex(); }
×
66

67
  // Copy assignment operator
68
  OpenMPMutex& operator=(const OpenMPMutex& other)
69
  {
70
    OpenMPMutex();
71
    return *this;
72
  }
73

74
  //! Lock the mutex.
75
  //
76
  //! This function blocks execution until the lock succeeds.
77
  void lock()
78
  {
79
#ifdef _OPENMP
80
    omp_set_lock(&mutex_);
81
#endif
82
  }
83

84
  //! Try to lock the mutex and indicate success.
85
  //
86
  //! This function does not block.  It returns immediately and gives false if
87
  //! the lock is unavailable.
88
  bool try_lock() noexcept
89
  {
90
#ifdef _OPENMP
91
    return omp_test_lock(&mutex_);
92
#else
93
    return true;
94
#endif
95
  }
96

97
  //! Unlock the mutex.
98
  void unlock() noexcept
99
  {
100
#ifdef _OPENMP
101
    omp_unset_lock(&mutex_);
102
#endif
103
  }
104

105
private:
106
#ifdef _OPENMP
107
  omp_lock_t mutex_;
108
#endif
109
};
110

111
} // namespace openmc
112
#endif // OPENMC_OPENMP_INTERFACE_H
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc