foobar2000 SDK  2015-08-03
synchro_nix.h
Go to the documentation of this file.
1 #include <pthread.h>
2 
3 namespace pfc {
4  class mutexAttr {
5  public:
6  mutexAttr() {pthread_mutexattr_init(&attr);}
7  ~mutexAttr() {pthread_mutexattr_destroy(&attr);}
8  void setType(int type) {pthread_mutexattr_settype(&attr, type);}
9  int getType() const {int rv = 0; pthread_mutexattr_gettype(&attr, &rv); return rv; }
10  void setRecursive() {setType(PTHREAD_MUTEX_RECURSIVE);}
11  bool isRecursive() {return getType() == PTHREAD_MUTEX_RECURSIVE;}
12  void setProcessShared() {pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);}
13  operator const pthread_mutexattr_t & () const {return attr;}
14  operator pthread_mutexattr_t & () {return attr;}
15  pthread_mutexattr_t attr;
16  private:
17  mutexAttr(const mutexAttr&); void operator=(const mutexAttr&);
18  };
19 
20  class mutexBase {
21  public:
22  void lock() throw() {pthread_mutex_lock(&obj);}
23  void unlock() throw() {pthread_mutex_unlock(&obj);}
24 
25  void enter() throw() {lock();}
26  void leave() throw() {unlock();}
27 
28  void create( const pthread_mutexattr_t * attr );
29  void create( const mutexAttr & );
30  void createRecur();
31  void destroy();
32  protected:
33  mutexBase() {}
35  private:
36  pthread_mutex_t obj;
37 
38  void operator=( const mutexBase & );
39  mutexBase( const mutexBase & );
40  };
41 
42 
43 
44  class mutex : public mutexBase {
45  public:
46  mutex() {create(NULL);}
47  ~mutex() {destroy();}
48  };
49 
50  class mutexRecur : public mutexBase {
51  public:
52  mutexRecur() {createRecur();}
53  ~mutexRecur() {destroy();}
54  };
55 
56 
57  class mutexRecurStatic : public mutexBase {
58  public:
59  mutexRecurStatic() {createRecur();}
60  };
61 
62 
63  class mutexScope {
64  public:
65  mutexScope( mutexBase * m ) throw() : m_mutex(m) { m_mutex->enter(); }
66  mutexScope( mutexBase & m ) throw() : m_mutex(&m) { m_mutex->enter(); }
67  ~mutexScope( ) throw() {m_mutex->leave();}
68  private:
69  void operator=( const mutexScope & ); mutexScope( const mutexScope & );
71  };
72 
73 
75  public:
76  readWriteLockAttr() {pthread_rwlockattr_init( & attr ); }
77  ~readWriteLockAttr() {pthread_rwlockattr_destroy( & attr ) ;}
78 
79  void setProcessShared( bool val = true ) {
80  pthread_rwlockattr_setpshared( &attr, val ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE );
81  }
82 
83  pthread_rwlockattr_t attr;
84 
85  private:
87  };
88 
90  public:
91  void create( const pthread_rwlockattr_t * attr );
92  void create( const readWriteLockAttr & );
93  void destroy() {pthread_rwlock_destroy( & obj ); }
94 
95  void enterRead() {pthread_rwlock_rdlock( &obj ); }
96  void enterWrite() {pthread_rwlock_wrlock( &obj ); }
97  void leaveRead() {pthread_rwlock_unlock( &obj ); }
98  void leaveWrite() {pthread_rwlock_unlock( &obj ); }
99  protected:
102  private:
103  pthread_rwlock_t obj;
104 
106  };
107 
108 
110  public:
112  ~readWriteLock() {destroy();}
113  };
114 
115 
117  public:
118  _readWriteLock_scope_read( readWriteLockBase & lock ) : m_lock( lock ) { m_lock.enterRead(); }
119  ~_readWriteLock_scope_read() {m_lock.leaveRead();}
120  private:
122  void operator=( const _readWriteLock_scope_read &);
124  };
126  public:
127  _readWriteLock_scope_write( readWriteLockBase & lock ) : m_lock( lock ) { m_lock.enterWrite(); }
128  ~_readWriteLock_scope_write() {m_lock.leaveWrite();}
129  private:
131  void operator=( const _readWriteLock_scope_write &);
133  };
134 }
135 
136 
137 
141 
142 #define insync(X) c_insync blah____sync(X)
143 
144 
145 #define inReadSync( X ) ::pfc::_readWriteLock_scope_read _asdf_l_readWriteLock_scope_read( X )
146 #define inWriteSync( X ) ::pfc::_readWriteLock_scope_write _asdf_l_readWriteLock_scope_write( X )
pfc::mutexRecurStatic critical_section_static
Definition: synchro_nix.h:139
bool isRecursive()
Definition: synchro_nix.h:11
mutexBase * m_mutex
Definition: synchro_nix.h:70
pthread_mutexattr_t attr
Definition: synchro_nix.h:15
pthread_rwlock_t obj
Definition: synchro_nix.h:103
readWriteLockBase & m_lock
Definition: synchro_nix.h:123
void setProcessShared()
Definition: synchro_nix.h:12
::c_insync mutexScope
Definition: synchro_win.h:132
pfc::mutexRecur critical_section
Definition: synchro_nix.h:138
void setProcessShared(bool val=true)
Definition: synchro_nix.h:79
pthread_mutex_t obj
Definition: synchro_nix.h:36
void setRecursive()
Definition: synchro_nix.h:10
pfc::mutexScope c_insync
Definition: synchro_nix.h:140
readWriteLockBase & m_lock
Definition: synchro_nix.h:132
int getType() const
Definition: synchro_nix.h:9
void operator=(const mutexAttr &)
void create(pfc::string_formatter &p_out, const t_entry_list &p_data)
Definition: cue_creator.cpp:44
_readWriteLock_scope_read(readWriteLockBase &lock)
Definition: synchro_nix.h:118
_readWriteLock_scope_write(readWriteLockBase &lock)
Definition: synchro_nix.h:127
void setType(int type)
Definition: synchro_nix.h:8
mutexScope(mutexBase *m)
Definition: synchro_nix.h:65
pthread_rwlockattr_t attr
Definition: synchro_nix.h:83
mutexScope(mutexBase &m)
Definition: synchro_nix.h:66