File: Mutex.h

package info (click to toggle)
pyxine 0.1alpha2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 852 kB
  • ctags: 2,017
  • sloc: ansic: 7,512; python: 2,224; cpp: 1,810; makefile: 160; sh: 40
file content (141 lines) | stat: -rw-r--r-- 3,019 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* -*-c++-*-
 *
 * $Id: Mutex.h,v 1.1.1.1 2003/02/08 00:42:21 dairiki Exp $
 *
 * Copyright (C) 2003 Geoffrey T. Dairiki
 *
 * This file is part of Pyxine, Python bindings for xine.
 *
 * Pyxine is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Pyxine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
#pragma interface

#ifndef _Mutex_H
#define _Mutex_H

#include "pxlib.h"

extern "C" {
# include <pthread.h>
}

BEGIN_PXLIB_NAMESPACE

class Mutex
{
private:
  pthread_mutex_t m;

public:
  Mutex() { pthread_mutex_init(&m, NULL); }
  ~Mutex() { pthread_mutex_destroy(&m); }
  operator pthread_mutex_t * () { return &m; }
};

class MutexLock
{
  struct lock_t {
    pthread_mutex_t * mp;
    int ref_cnt;

    lock_t(pthread_mutex_t * _mp)
      : mp(_mp), ref_cnt(1)
    { pthread_mutex_lock(mp); }

    ~lock_t() { pthread_mutex_unlock(mp); }
  };

  lock_t * lock;

public:
  MutexLock() : lock(0) {}
  MutexLock(pthread_mutex_t * m) : lock(new lock_t(m)) {}
  MutexLock(Mutex * m) : lock(new lock_t(*m)) {}
  MutexLock(const MutexLock& l) : lock(l.lock) {
    if (lock)
      lock->ref_cnt++;
  }
  MutexLock& operator= (const MutexLock& l) {
    if (lock && --lock->ref_cnt == 0)
      delete lock;
    lock = l.lock;
    if (lock)
      lock->ref_cnt++;
    return *this;
  }
  ~MutexLock () {
    if (lock && --lock->ref_cnt == 0)
      delete lock;
  }
};

/** A atomic data item.
 *
 * The data contained within is protected by a mutex, so that
 * access to the data is guaranteed to be atomic.
 */
template <class T>
class atomic
{
public:
  typedef T data_type;

private:
  data_type	value;
  mutable Mutex	mutex;

public:
  atomic() {}
  atomic(const data_type& x) : value(x) {}
  atomic(const atomic& a) : value(a) {}

  /** Assigment.
   *
   * Data only comes in with the mutex held.
   */
  atomic& operator= (const data_type& new_value) {
    MutexLock l(mutex);
    value = new_value;
    return *this;
  }

  /** Update value.
   *
   * Update the value.
   *
   * @return True iff the update changed the value.
   */
  bool update (const data_type& new_value) {
    MutexLock l(mutex);
    bool changed = value != new_value;
    if (changed)
      value = new_value;
    return changed;
  }

  /**
   * Conversion to data_type.
   *
   * This is the only way to access the data.
   */
  operator data_type () const {
    MutexLock l(mutex);
    return value;
  }
};

END_PXLIB_NAMESPACE

#endif // !_Mutex_H