~serge-hallyn/ubuntu/raring/spice/spice-fixed2

« back to all changes in this revision

Viewing changes to client/threads.h

  • Committer: Package Import Robot
  • Author(s): Liang Guo
  • Date: 2011-07-23 12:21:04 UTC
  • Revision ID: package-import@ubuntu.com-20110723122104-gvv70gd6ui6213qd
Tags: upstream-0.8.2
ImportĀ upstreamĀ versionĀ 0.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2009 Red Hat, Inc.
 
3
 
 
4
   This library is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU Lesser General Public
 
6
   License as published by the Free Software Foundation; either
 
7
   version 2.1 of the License, or (at your option) any later version.
 
8
 
 
9
   This library is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
   Lesser General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU Lesser General Public
 
15
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 
16
*/
 
17
 
 
18
#ifndef _H_THREADS
 
19
#define _H_THREADS
 
20
 
 
21
#include "pthread.h"
 
22
#include "atomic_count.h"
 
23
 
 
24
class Thread {
 
25
public:
 
26
    typedef void* (*thread_main_t)(void*);
 
27
 
 
28
    Thread(thread_main_t thread_main, void* opaque);
 
29
    void join();
 
30
 
 
31
private:
 
32
    pthread_t _thread;
 
33
};
 
34
 
 
35
class Mutex {
 
36
public:
 
37
    enum Type {
 
38
        NORMAL,
 
39
        RECURSIVE,
 
40
    };
 
41
 
 
42
    Mutex(Type = NORMAL);
 
43
    virtual ~Mutex();
 
44
 
 
45
private:
 
46
    friend class Lock;
 
47
    pthread_mutex_t* get() {return &_mutex;}
 
48
 
 
49
private:
 
50
    pthread_mutex_t _mutex;
 
51
};
 
52
 
 
53
class Lock {
 
54
public:
 
55
    Lock(Mutex& mutex)
 
56
        : _locked (true)
 
57
        , _mutex (mutex)
 
58
    {
 
59
        pthread_mutex_lock(_mutex.get());
 
60
    }
 
61
 
 
62
    Lock(Mutex& mutex, uint64_t timout_nano)
 
63
        : _mutex (mutex)
 
64
    {
 
65
        if (!pthread_mutex_trylock(_mutex.get())) {
 
66
            _locked = true;
 
67
            return;
 
68
        }
 
69
        timed_lock(timout_nano);
 
70
    }
 
71
 
 
72
    ~Lock()
 
73
    {
 
74
        unlock();
 
75
    }
 
76
 
 
77
    void unlock()
 
78
    {
 
79
        if (_locked) {
 
80
            pthread_mutex_unlock(_mutex.get());
 
81
            _locked = false;
 
82
        }
 
83
    }
 
84
 
 
85
    bool is_locked() { return _locked;}
 
86
 
 
87
private:
 
88
    friend class Condition;
 
89
    pthread_mutex_t* get() {return _mutex.get();}
 
90
    void timed_lock(uint64_t timout_nano);
 
91
 
 
92
private:
 
93
    bool _locked;
 
94
    Mutex& _mutex;
 
95
};
 
96
 
 
97
class RecurciveMutex: public Mutex {
 
98
public:
 
99
    RecurciveMutex() : Mutex(Mutex::RECURSIVE) {}
 
100
};
 
101
 
 
102
typedef Lock RecurciveLock;
 
103
class Condition {
 
104
public:
 
105
    Condition();
 
106
 
 
107
    ~Condition()
 
108
    {
 
109
        pthread_cond_destroy(&_condition);
 
110
    }
 
111
 
 
112
    void notify_one()
 
113
    {
 
114
        pthread_cond_signal(&_condition);
 
115
    }
 
116
 
 
117
    void notify_all()
 
118
    {
 
119
        pthread_cond_broadcast(&_condition);
 
120
    }
 
121
 
 
122
    void wait(Lock& lock)
 
123
    {
 
124
        pthread_cond_wait(&_condition, lock.get());
 
125
    }
 
126
 
 
127
    bool timed_wait(Lock& lock, uint64_t nano);
 
128
 
 
129
private:
 
130
    pthread_cond_t _condition;
 
131
};
 
132
 
 
133
 
 
134
#endif
 
135