CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
util_concurrency.cc
Go to the documentation of this file.
1 
5 #include "cvmfs_config.h"
6 #include "util_concurrency.h"
7 
8 #include <unistd.h>
9 
10 #include <cassert>
11 
12 #ifdef CVMFS_NAMESPACE_GUARD
13 namespace CVMFS_NAMESPACE_GUARD {
14 #endif
15 
16 unsigned int GetNumberOfCpuCores() {
17  const int numCPU = sysconf(_SC_NPROCESSORS_ONLN);
18 
19  if (numCPU <= 0) {
20  LogCvmfs(kLogSpooler, kLogWarning, "Unable to determine the available "
21  "number of processors in the system... "
22  "falling back to default '%d'",
24  return kFallbackNumberOfCpus;
25  }
26 
27  return static_cast<unsigned int>(numCPU);
28 }
29 
30 Signal::Signal() : fired_(false) {
31  int retval = pthread_mutex_init(&lock_, NULL);
32  assert(retval == 0);
33  retval = pthread_cond_init(&signal_, NULL);
34  assert(retval == 0);
35 }
36 
37 
39  assert(IsSleeping());
40  int res = pthread_cond_destroy(&signal_);
41  assert(0 == res);
42  res = pthread_mutex_destroy(&lock_);
43  assert(0 == res);
44 }
45 
46 
47 void Signal::Wait() {
48  MutexLockGuard guard(lock_);
49  while (!fired_) {
50  int retval = pthread_cond_wait(&signal_, &lock_);
51  assert(retval == 0);
52  }
53  fired_ = false;
54 }
55 
56 
58  MutexLockGuard guard(lock_);
59  fired_ = true;
60  int retval = pthread_cond_broadcast(&signal_);
61  assert(retval == 0);
62 }
63 
65  MutexLockGuard guard(lock_);
66  return fired_ == false;
67 }
68 
69 #ifdef CVMFS_NAMESPACE_GUARD
70 } // namespace CVMFS_NAMESPACE_GUARD
71 #endif
#define LogCvmfs(source, mask,...)
Definition: logging.h:20
unsigned int GetNumberOfCpuCores()
const int kLogWarning
pthread_cond_t signal_
void Wakeup()
assert((mem||(size==0))&&"Out Of Memory")
bool IsSleeping()
static const unsigned int kFallbackNumberOfCpus
void Wait()
pthread_mutex_t lock_