Directory: | cvmfs/ |
---|---|
File: | cvmfs/util/concurrency.cc |
Date: | 2025-07-13 02:35:07 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 34 | 36 | 94.4% |
Branches: | 11 | 20 | 55.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | */ | ||
4 | |||
5 | |||
6 | #include "util/concurrency.h" | ||
7 | |||
8 | #include <pthread.h> | ||
9 | #include <unistd.h> | ||
10 | |||
11 | #include <cassert> | ||
12 | #include <cstddef> | ||
13 | |||
14 | #include "util/logging.h" | ||
15 | #include "util/mutex.h" | ||
16 | |||
17 | #ifdef CVMFS_NAMESPACE_GUARD | ||
18 | namespace CVMFS_NAMESPACE_GUARD { | ||
19 | #endif | ||
20 | |||
21 | 728 | unsigned int GetNumberOfCpuCores() { | |
22 | 728 | const int numCPU = sysconf(_SC_NPROCESSORS_ONLN); | |
23 | |||
24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 728 times.
|
728 | if (numCPU <= 0) { |
25 | ✗ | LogCvmfs(kLogSpooler, kLogWarning, | |
26 | "Unable to determine the available " | ||
27 | "number of processors in the system... " | ||
28 | "falling back to default '%d'", | ||
29 | kFallbackNumberOfCpus); | ||
30 | ✗ | return kFallbackNumberOfCpus; | |
31 | } | ||
32 | |||
33 | 728 | return static_cast<unsigned int>(numCPU); | |
34 | } | ||
35 | |||
36 | 60318 | Signal::Signal() : fired_(false) { | |
37 | 60318 | int retval = pthread_mutex_init(&lock_, NULL); | |
38 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60318 times.
|
60318 | assert(retval == 0); |
39 | 60318 | retval = pthread_cond_init(&signal_, NULL); | |
40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60318 times.
|
60318 | assert(retval == 0); |
41 | 60318 | } | |
42 | |||
43 | |||
44 | 60325 | Signal::~Signal() { | |
45 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 60318 times.
|
60325 | assert(IsSleeping()); |
46 | 60318 | int res = pthread_cond_destroy(&signal_); | |
47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60325 times.
|
60325 | assert(0 == res); |
48 | 60325 | res = pthread_mutex_destroy(&lock_); | |
49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60318 times.
|
60318 | assert(0 == res); |
50 | 60318 | } | |
51 | |||
52 | |||
53 | 60927 | void Signal::Wait() { | |
54 | 60927 | const MutexLockGuard guard(lock_); | |
55 |
2/2✓ Branch 0 taken 60196 times.
✓ Branch 1 taken 60927 times.
|
121123 | while (!fired_) { |
56 |
1/2✓ Branch 1 taken 60196 times.
✗ Branch 2 not taken.
|
60196 | const int retval = pthread_cond_wait(&signal_, &lock_); |
57 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60196 times.
|
60196 | assert(retval == 0); |
58 | } | ||
59 | 60927 | fired_ = false; | |
60 | 60927 | } | |
61 | |||
62 | |||
63 | 60927 | void Signal::Wakeup() { | |
64 | 60927 | const MutexLockGuard guard(lock_); | |
65 | 60927 | fired_ = true; | |
66 | 60927 | const int retval = pthread_cond_broadcast(&signal_); | |
67 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60927 times.
|
60927 | assert(retval == 0); |
68 | 60927 | } | |
69 | |||
70 | 60705 | bool Signal::IsSleeping() { | |
71 | 60705 | const MutexLockGuard guard(lock_); | |
72 | 121417 | return fired_ == false; | |
73 | 60712 | } | |
74 | |||
75 | #ifdef CVMFS_NAMESPACE_GUARD | ||
76 | } // namespace CVMFS_NAMESPACE_GUARD | ||
77 | #endif | ||
78 |