~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSMutex.cc

  • Committer: Monty Taylor
  • Date: 2008-09-16 00:00:48 UTC
  • mto: This revision was merged to the branch mainline in revision 391.
  • Revision ID: monty@inaugust.com-20080916000048-3rvrv3gv9l0ad3gs
Fixed copyright headers in drizzled/

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
2
 
 *
3
 
 * PrimeBase Media Stream for MySQL
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
 
 *
19
 
 * Original author: Paul McCullagh (H&G2JCtL)
20
 
 * Continued development: Barry Leslie
21
 
 *
22
 
 * 2007-06-06
23
 
 *
24
 
 * A basic mutex (mutual exclusion) object.
25
 
 *
26
 
 */
27
 
 
28
 
#include "CSConfig.h"
29
 
 
30
 
#include <assert.h>
31
 
#ifdef OS_WINDOWS
32
 
extern int gettimeofday(struct timeval *tv, struct timezone *tz);
33
 
#else
34
 
#include <sys/time.h>
35
 
#endif
36
 
 
37
 
 
38
 
#include "CSException.h"
39
 
#include "CSMutex.h"
40
 
#include "CSGlobal.h"
41
 
#include "CSLog.h"
42
 
 
43
 
/*
44
 
 * ---------------------------------------------------------------
45
 
 * A MUTEX ENTITY
46
 
 */
47
 
 
48
 
CSMutex::CSMutex()
49
 
#ifdef DEBUG
50
 
:
51
 
iLocker(NULL),
52
 
trace(false)
53
 
#endif
54
 
{
55
 
        int err;
56
 
 
57
 
        if ((err = pthread_mutex_init(&iMutex, NULL)))
58
 
                CSException::throwOSError(CS_CONTEXT, err);
59
 
}
60
 
 
61
 
CSMutex::~CSMutex()
62
 
{
63
 
        pthread_mutex_destroy(&iMutex);
64
 
}
65
 
 
66
 
void CSMutex::lock()
67
 
{
68
 
        int err;
69
 
 
70
 
        if ((err = pthread_mutex_lock(&iMutex)))
71
 
                CSException::throwOSError(CS_CONTEXT, err);
72
 
#ifdef DEBUG
73
 
        iLocker = CSThread::getSelf();
74
 
        if (trace)
75
 
                CSL.logf(iLocker, CSLog::Protocol, "Mutex locked\n");
76
 
#endif
77
 
}
78
 
 
79
 
void CSMutex::unlock()
80
 
{
81
 
#ifdef DEBUG
82
 
        if (trace)
83
 
                CSL.logf(iLocker, CSLog::Protocol, "Mutex unlocked\n");
84
 
        iLocker = NULL;
85
 
#endif
86
 
        pthread_mutex_unlock(&iMutex);
87
 
}
88
 
 
89
 
/*
90
 
 * ---------------------------------------------------------------
91
 
 * A LOCK ENTITY
92
 
 */
93
 
 
94
 
CSLock::CSLock():
95
 
CSMutex(),
96
 
iLockingThread(NULL),
97
 
iLockCount(0)
98
 
{
99
 
}
100
 
 
101
 
CSLock::~CSLock()
102
 
{
103
 
}
104
 
 
105
 
void CSLock::lock()
106
 
{
107
 
        int err;
108
 
 
109
 
        enter_();
110
 
        if (iLockingThread != self) {
111
 
                if ((err = pthread_mutex_lock(&iMutex)))
112
 
                        CSException::throwOSError(CS_CONTEXT, err);
113
 
                iLockingThread = self;
114
 
        }
115
 
        iLockCount++;
116
 
        exit_();
117
 
}
118
 
 
119
 
void CSLock::unlock()
120
 
{
121
 
        enter_();
122
 
        ASSERT(iLockingThread == self);
123
 
        if (!(--iLockCount)) {
124
 
                iLockingThread = NULL;
125
 
                pthread_mutex_unlock(&iMutex);
126
 
        }
127
 
        exit_();
128
 
}
129
 
 
130
 
bool CSLock::haveLock()
131
 
{
132
 
        enter_();
133
 
        return_(iLockingThread == self);
134
 
}
135
 
 
136
 
/*
137
 
 * ---------------------------------------------------------------
138
 
 * A SYNCRONISATION ENTITY
139
 
 */
140
 
 
141
 
CSSync::CSSync():
142
 
CSLock()
143
 
{
144
 
        int err;
145
 
 
146
 
        if ((err = pthread_cond_init(&iCondition, NULL)))
147
 
                CSException::throwOSError(CS_CONTEXT, err);
148
 
}
149
 
 
150
 
CSSync::~CSSync()
151
 
{
152
 
        pthread_cond_destroy(&iCondition);
153
 
}
154
 
 
155
 
void CSSync::wait()
156
 
{
157
 
        int err;
158
 
        int lock_count;
159
 
 
160
 
        enter_();
161
 
        ASSERT(iLockingThread == self);
162
 
        lock_count = iLockCount;
163
 
        iLockCount = 0;
164
 
        iLockingThread = NULL;
165
 
        err = pthread_cond_wait(&iCondition, &iMutex);
166
 
        iLockCount = lock_count;
167
 
        iLockingThread = self;
168
 
        if (err)
169
 
                CSException::throwOSError(CS_CONTEXT, err);
170
 
        exit_();
171
 
}
172
 
 
173
 
void CSSync::wait(time_t milli_sec)
174
 
{
175
 
        struct timespec abstime;
176
 
        int                             lock_count;
177
 
        int                             err;
178
 
        uint64_t                micro_sec;
179
 
 
180
 
        enter_();
181
 
        struct timeval  now;
182
 
 
183
 
        /* Get the current time in microseconds: */
184
 
        gettimeofday(&now, NULL);
185
 
        micro_sec = (uint64_t) now.tv_sec * (uint64_t) 1000000 + (uint64_t) now.tv_usec;
186
 
        
187
 
        /* Add the timeout which is in milli seconds */
188
 
        micro_sec += (uint64_t) milli_sec * (uint64_t) 1000;
189
 
 
190
 
        /* Setup the end time, which is in nano-seconds. */
191
 
        abstime.tv_sec = (long) (micro_sec / 1000000);                          /* seconds */
192
 
        abstime.tv_nsec = (long) ((micro_sec % 1000000) * 1000);        /* and nanoseconds */
193
 
 
194
 
        ASSERT(iLockingThread == self);
195
 
        lock_count = iLockCount;
196
 
        iLockCount = 0;
197
 
        iLockingThread = NULL;
198
 
        err = pthread_cond_timedwait(&iCondition, &iMutex, &abstime);
199
 
        iLockCount = lock_count;
200
 
        iLockingThread = self;
201
 
        if (err && err != ETIMEDOUT)
202
 
                CSException::throwOSError(CS_CONTEXT, err);
203
 
        exit_();
204
 
}
205
 
 
206
 
void CSSync::wakeup()
207
 
{
208
 
        int err;
209
 
 
210
 
        if ((err = pthread_cond_broadcast(&iCondition)))
211
 
                CSException::throwOSError(CS_CONTEXT, err);
212
 
}
213
 
 
214
 
 
215