Essentia  2.1-beta6-dev
threading.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_THREADING_H
21 #define ESSENTIA_THREADING_H
22 
23 
24 #ifdef OS_WIN32
25 # include <windows.h>
26 #else // OS_WIN32
27 # include <pthread.h>
28 #endif // OS_WIN32
29 
30 
31 namespace essentia {
32 
33 // if we were to use TBB for the scheduler, we would have:
34 /*
35 typedef tbb::spin_mutex Mutex;
36 typedef tbb::spin_mutex::scoped_lock MutexLocker;
37 typedef tbb::spin_mutex ForcedMutex;
38 typedef tbb::spin_mutex::scoped_lock ForcedMutexLocker;
39 */
40 
41 // The mutex in essentia only needs to be a real mutex when it is possible
42 // to call the algorithms in a multithreaded way.
43 // If not, it can be replaced with a no-op mutex for performance reasons.
44 
45 class Mutex {
46  public:
47  void lock() {}
48  void unlock() {}
49 };
50 
51 class MutexLocker {
52  public:
53  MutexLocker(Mutex& mutex) {}
54  void release() {}
55  void acquire(Mutex&) {}
56 };
57 
58 
59 // the ForcedMutex is a real Mutex, that should always lock properly
60 // (ex: in FFTW, the plan creation/destruction needs to be protected no matter what)
61 
62 # ifdef OS_WIN32
63 
64 // windows CriticalSection implementation
65 
66 class ForcedMutex {
67  protected:
68  CRITICAL_SECTION criticalSection;
69  public:
70  ForcedMutex() { InitializeCriticalSection(&criticalSection); }
71  ~ForcedMutex() { DeleteCriticalSection(&criticalSection); }
72  void lock() { EnterCriticalSection(&criticalSection); }
73  void unlock() { LeaveCriticalSection(&criticalSection); }
74 };
75 
76 # else // OS_WIN32
77 
78 // posix implementation for linux and osx
79 
80 class ForcedMutex {
81  protected:
82  pthread_mutex_t pthreadMutex;
83  public:
85  if (pthread_mutex_init(&pthreadMutex,0) != 0)
86  throw EssentiaException("can't create mutex type");
87  }
88  ~ForcedMutex() { pthread_mutex_destroy(&pthreadMutex); }
89  void lock() { pthread_mutex_lock(&pthreadMutex); }
90  void unlock() { pthread_mutex_unlock(&pthreadMutex); }
91 };
92 
93 # endif // OS_WIN32
94 
96  protected:
98  public:
101 };
102 
103 
104 } // namespace essentia
105 
106 #endif // ESSENTIA_THREADING_H
Definition: types.h:77
Definition: threading.h:95
~ForcedMutexLocker()
Definition: threading.h:100
ForcedMutex & _mutex
Definition: threading.h:97
ForcedMutexLocker(ForcedMutex &mutex)
Definition: threading.h:99
Definition: threading.h:80
void unlock()
Definition: threading.h:90
void lock()
Definition: threading.h:89
~ForcedMutex()
Definition: threading.h:88
pthread_mutex_t pthreadMutex
Definition: threading.h:82
ForcedMutex()
Definition: threading.h:84
Definition: threading.h:51
MutexLocker(Mutex &mutex)
Definition: threading.h:53
void release()
Definition: threading.h:54
void acquire(Mutex &)
Definition: threading.h:55
Definition: threading.h:45
void unlock()
Definition: threading.h:48
void lock()
Definition: threading.h:47
Definition: algorithm.h:28