~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

Viewing changes to storage/pbxt/src/pthread_xt.cc

  • Committer: Package Import Robot
  • Author(s): Otto Kekäläinen
  • Date: 2013-12-22 10:27:05 UTC
  • Revision ID: package-import@ubuntu.com-20131222102705-mndw7s12mz0szrcn
Tags: upstream-5.5.32
Import upstream version 5.5.32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2005 PrimeBase Technologies GmbH
 
2
 *
 
3
 * PrimeBase XT
 
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * 2006-03-22   Paul McCullagh
 
20
 *
 
21
 * H&G2JCtL
 
22
 *
 
23
 * This file contains windows specific code
 
24
 */
 
25
 
 
26
#include "xt_config.h"
 
27
 
 
28
#ifdef XT_WIN
 
29
#include <my_pthread.h>
 
30
#else
 
31
#include <sys/resource.h>
 
32
#endif
 
33
#include <errno.h>
 
34
#include <limits.h>
 
35
#include <string.h>
 
36
 
 
37
#include "pthread_xt.h"
 
38
#include "thread_xt.h"
 
39
 
 
40
#ifdef XT_WIN
 
41
 
 
42
xtPublic void xt_p_init_threading(void)
 
43
{
 
44
}
 
45
 
 
46
xtPublic int xt_p_set_normal_priority(pthread_t thr)
 
47
{
 
48
        return 0;
 
49
}
 
50
 
 
51
xtPublic int xt_p_set_low_priority(pthread_t thr)
 
52
{
 
53
        return 0;
 
54
}
 
55
 
 
56
xtPublic int xt_p_set_high_priority(pthread_t thr)
 
57
{
 
58
        return 0;
 
59
}
 
60
 
 
61
#define XT_RWLOCK_MAGIC 0x78AC390E
 
62
 
 
63
#ifdef XT_THREAD_LOCK_INFO
 
64
xtPublic int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr, const char *n)
 
65
#else
 
66
xtPublic int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr)
 
67
#endif
 
68
{
 
69
        InitializeCriticalSection(&mutex->mt_cs);
 
70
#ifdef XT_THREAD_LOCK_INFO
 
71
        xt_thread_lock_info_init(&mutex->mt_lock_info, mutex);
 
72
        mutex->mt_name = n;
 
73
#endif
 
74
        return 0;
 
75
}
 
76
 
 
77
xtPublic int xt_p_mutex_destroy(xt_mutex_type *mutex)
 
78
{
 
79
        DeleteCriticalSection(&mutex->mt_cs);
 
80
#ifdef XT_THREAD_LOCK_INFO
 
81
        xt_thread_lock_info_free(&mutex->mt_lock_info);
 
82
#endif
 
83
        return 0;
 
84
}
 
85
 
 
86
xtPublic int xt_p_mutex_lock(xt_mutex_type *mx)
 
87
{
 
88
        EnterCriticalSection(&mx->mt_cs);
 
89
#ifdef XT_THREAD_LOCK_INFO
 
90
        xt_thread_lock_info_add_owner(&mx->mt_lock_info);
 
91
#endif
 
92
        return 0;
 
93
}
 
94
 
 
95
xtPublic int xt_p_mutex_unlock(xt_mutex_type *mx)
 
96
{
 
97
        LeaveCriticalSection(&mx->mt_cs);
 
98
#ifdef XT_THREAD_LOCK_INFO
 
99
        xt_thread_lock_info_release_owner(&mx->mt_lock_info);
 
100
#endif
 
101
        return 0;
 
102
}
 
103
 
 
104
xtPublic int xt_p_mutex_trylock(xt_mutex_type *mutex)
 
105
{
 
106
#if(_WIN32_WINNT >= 0x0400)
 
107
        /* NOTE: MySQL bug! was using?!
 
108
         * pthread_mutex_trylock(A) (WaitForSingleObject((A), 0) == WAIT_TIMEOUT)
 
109
         */
 
110
        if (TryEnterCriticalSection(&mutex->mt_cs)) {
 
111
#ifdef XT_THREAD_LOCK_INFO
 
112
                xt_thread_lock_info_add_owner(&mutex->mt_lock_info);
 
113
#endif
 
114
                return 0;
 
115
        }
 
116
        return WAIT_TIMEOUT;
 
117
#else
 
118
        EnterCriticalSection(&mutex->mt_cs);
 
119
#ifdef XT_THREAD_LOCK_INFO
 
120
        xt_thread_lock_info_add_owner(&mutex->mt_lock_info);
 
121
#endif
 
122
        return 0;
 
123
#endif
 
124
}
 
125
 
 
126
#ifdef XT_THREAD_LOCK_INFO
 
127
xtPublic int xt_p_rwlock_init(xt_rwlock_type *rwl, const pthread_condattr_t *attr, const char *n)
 
128
#else
 
129
xtPublic int xt_p_rwlock_init(xt_rwlock_type *rwl, const pthread_condattr_t *attr)
 
130
#endif
 
131
{
 
132
        int result;
 
133
 
 
134
        if (rwl == NULL)
 
135
                return ERROR_BAD_ARGUMENTS;
 
136
 
 
137
        rwl->rw_sh_count = 0;
 
138
        rwl->rw_ex_count = 0;
 
139
        rwl->rw_sh_complete_count = 0;
 
140
 
 
141
        result = xt_p_mutex_init_with_autoname(&rwl->rw_ex_lock, NULL);
 
142
        if (result != 0)
 
143
                goto failed;
 
144
 
 
145
        result = xt_p_mutex_init_with_autoname(&rwl->rw_sh_lock, NULL);
 
146
        if (result != 0)
 
147
                goto failed_2;
 
148
 
 
149
        result = pthread_cond_init(&rwl->rw_sh_cond, NULL);
 
150
        if (result != 0)
 
151
                goto failed_3;
 
152
 
 
153
        rwl->rw_magic = XT_RWLOCK_MAGIC;
 
154
#ifdef XT_THREAD_LOCK_INFO
 
155
        rwl->rw_name = n;
 
156
        xt_thread_lock_info_init(&rwl->rw_lock_info, rwl);
 
157
#endif
 
158
        return 0;
 
159
 
 
160
        failed_3:
 
161
        (void) xt_p_mutex_destroy(&rwl->rw_sh_lock);
 
162
 
 
163
        failed_2:
 
164
        (void) xt_p_mutex_destroy(&rwl->rw_ex_lock);
 
165
 
 
166
        failed:
 
167
        return result;
 
168
}
 
169
 
 
170
xtPublic int xt_p_rwlock_destroy(xt_rwlock_type *rwl)
 
171
{
 
172
        int result = 0, result1 = 0, result2 = 0;
 
173
 
 
174
        if (rwl == NULL)
 
175
                return ERROR_BAD_ARGUMENTS;
 
176
 
 
177
        if (rwl->rw_magic != XT_RWLOCK_MAGIC)
 
178
                return ERROR_BAD_ARGUMENTS;
 
179
 
 
180
        if ((result = xt_p_mutex_lock(&rwl->rw_ex_lock)) != 0)
 
181
                return result;
 
182
 
 
183
        if ((result = xt_p_mutex_lock(&rwl->rw_sh_lock)) != 0) {
 
184
                (void) xt_p_mutex_unlock(&rwl->rw_ex_lock);
 
185
                return result;
 
186
        }
 
187
 
 
188
        /*
 
189
         * Check whether any threads own/wait for the lock (wait for ex.access);
 
190
         * report "BUSY" if so.
 
191
         */
 
192
        if (rwl->rw_ex_count > 0 || rwl->rw_sh_count > rwl->rw_sh_complete_count) {
 
193
                result = xt_p_mutex_unlock(&rwl->rw_sh_lock);
 
194
                result1 = xt_p_mutex_unlock(&rwl->rw_ex_lock);
 
195
                result2 = ERROR_BUSY;
 
196
        }
 
197
        else {
 
198
                rwl->rw_magic = 0;
 
199
 
 
200
                if ((result = xt_p_mutex_unlock(&rwl->rw_sh_lock)) != 0)
 
201
                {
 
202
                        xt_p_mutex_unlock(&rwl->rw_ex_lock);
 
203
                        return result;
 
204
                }
 
205
 
 
206
                if ((result = xt_p_mutex_unlock(&rwl->rw_ex_lock)) != 0)
 
207
                        return result;
 
208
 
 
209
                result = pthread_cond_destroy(&rwl->rw_sh_cond);
 
210
                result1 = xt_p_mutex_destroy(&rwl->rw_sh_lock);
 
211
                result2 = xt_p_mutex_destroy(&rwl->rw_ex_lock);
 
212
        }
 
213
 
 
214
#ifdef XT_THREAD_LOCK_INFO
 
215
        xt_thread_lock_info_free(&rwl->rw_lock_info);
 
216
#endif
 
217
 
 
218
        return (result != 0) ? result : ((result1 != 0) ? result1 : result2);
 
219
}
 
220
 
 
221
 
 
222
xtPublic int xt_p_rwlock_rdlock(xt_rwlock_type *rwl)
 
223
{
 
224
        int result;
 
225
 
 
226
        if (rwl == NULL)
 
227
                return ERROR_BAD_ARGUMENTS;
 
228
 
 
229
        if (rwl->rw_magic != XT_RWLOCK_MAGIC)
 
230
                return ERROR_BAD_ARGUMENTS;
 
231
 
 
232
        if ((result = xt_p_mutex_lock(&rwl->rw_ex_lock)) != 0)
 
233
                return result;
 
234
 
 
235
        if (++rwl->rw_sh_count == INT_MAX) {
 
236
                if ((result = xt_p_mutex_lock(&rwl->rw_sh_lock)) != 0)
 
237
                {
 
238
                        (void) xt_p_mutex_unlock(&rwl->rw_ex_lock);
 
239
                        return result;
 
240
                }
 
241
 
 
242
                rwl->rw_sh_count -= rwl->rw_sh_complete_count;
 
243
                rwl->rw_sh_complete_count = 0;
 
244
 
 
245
                if ((result = xt_p_mutex_unlock(&rwl->rw_sh_lock)) != 0)
 
246
                {
 
247
                        (void) xt_p_mutex_unlock(&rwl->rw_ex_lock);
 
248
                        return result;
 
249
                }
 
250
        }
 
251
 
 
252
#ifdef XT_THREAD_LOCK_INFO
 
253
        xt_thread_lock_info_add_owner(&rwl->rw_lock_info);
 
254
#endif
 
255
 
 
256
        return (xt_p_mutex_unlock (&(rwl->rw_ex_lock)));
 
257
}
 
258
 
 
259
xtPublic int xt_p_rwlock_wrlock(xt_rwlock_type *rwl)
 
260
{
 
261
        int result;
 
262
 
 
263
        if (rwl == NULL)
 
264
                return ERROR_BAD_ARGUMENTS;
 
265
 
 
266
        if (rwl->rw_magic != XT_RWLOCK_MAGIC)
 
267
                return ERROR_BAD_ARGUMENTS;
 
268
 
 
269
        if ((result = xt_p_mutex_lock (&rwl->rw_ex_lock)) != 0)
 
270
                return result;
 
271
 
 
272
        if ((result = xt_p_mutex_lock (&rwl->rw_sh_lock)) != 0) {
 
273
                (void) xt_p_mutex_unlock (&rwl->rw_ex_lock);
 
274
                return result;
 
275
        }
 
276
 
 
277
        if (rwl->rw_ex_count == 0) {
 
278
                if (rwl->rw_sh_complete_count > 0) {
 
279
                        rwl->rw_sh_count -= rwl->rw_sh_complete_count;
 
280
                        rwl->rw_sh_complete_count = 0;
 
281
                }
 
282
 
 
283
                if (rwl->rw_sh_count > 0) {
 
284
                        rwl->rw_sh_complete_count = -rwl->rw_sh_count;
 
285
 
 
286
                        do {
 
287
                                result = pthread_cond_wait (&rwl->rw_sh_cond, &rwl->rw_sh_lock.mt_cs);
 
288
                        }
 
289
                        while (result == 0 && rwl->rw_sh_complete_count < 0);
 
290
 
 
291
                        if (result == 0)
 
292
                                rwl->rw_sh_count = 0;
 
293
                }
 
294
        }
 
295
 
 
296
        if (result == 0)
 
297
                rwl->rw_ex_count++;
 
298
 
 
299
#ifdef XT_THREAD_LOCK_INFO
 
300
        xt_thread_lock_info_add_owner(&rwl->rw_lock_info);
 
301
#endif
 
302
 
 
303
        return result;
 
304
}
 
305
 
 
306
xtPublic xtBool xt_p_rwlock_try_wrlock(xt_rwlock_type *rwl)
 
307
{
 
308
        int result;
 
309
 
 
310
        if (rwl == NULL)
 
311
                return FALSE;
 
312
 
 
313
        if (rwl->rw_magic != XT_RWLOCK_MAGIC)
 
314
                return FALSE;
 
315
 
 
316
        if ((result = xt_p_mutex_trylock(&rwl->rw_ex_lock)) != 0)
 
317
                return FALSE;
 
318
 
 
319
        if ((result = xt_p_mutex_lock(&rwl->rw_sh_lock)) != 0) {
 
320
                (void) xt_p_mutex_unlock(&rwl->rw_ex_lock);
 
321
                return FALSE;
 
322
        }
 
323
 
 
324
        if (rwl->rw_ex_count == 0) {
 
325
                if (rwl->rw_sh_complete_count > 0) {
 
326
                        rwl->rw_sh_count -= rwl->rw_sh_complete_count;
 
327
                        rwl->rw_sh_complete_count = 0;
 
328
                }
 
329
 
 
330
                if (rwl->rw_sh_count > 0) {
 
331
                        rwl->rw_sh_complete_count = -rwl->rw_sh_count;
 
332
 
 
333
                        do {
 
334
                                result = pthread_cond_wait (&rwl->rw_sh_cond, &rwl->rw_sh_lock.mt_cs);
 
335
                        }
 
336
                        while (result == 0 && rwl->rw_sh_complete_count < 0);
 
337
 
 
338
                        if (result == 0)
 
339
                                rwl->rw_sh_count = 0;
 
340
                }
 
341
        }
 
342
 
 
343
        if (result == 0)
 
344
                rwl->rw_ex_count++;
 
345
 
 
346
#ifdef XT_THREAD_LOCK_INFO
 
347
        xt_thread_lock_info_add_owner(&rwl->rw_lock_info);
 
348
#endif
 
349
 
 
350
        return TRUE;
 
351
}
 
352
 
 
353
xtPublic int xt_p_rwlock_unlock(xt_rwlock_type *rwl)
 
354
{
 
355
        int result, result1;
 
356
 
 
357
        if (rwl == NULL)
 
358
                return (ERROR_BAD_ARGUMENTS);
 
359
 
 
360
        if (rwl->rw_magic != XT_RWLOCK_MAGIC)
 
361
                return ERROR_BAD_ARGUMENTS;
 
362
 
 
363
        if (rwl->rw_ex_count == 0) {
 
364
                if ((result = xt_p_mutex_lock(&rwl->rw_sh_lock)) != 0)
 
365
                        return result;
 
366
 
 
367
                if (++rwl->rw_sh_complete_count == 0)
 
368
                        result = pthread_cond_signal(&rwl->rw_sh_cond);
 
369
 
 
370
                result1 = xt_p_mutex_unlock(&rwl->rw_sh_lock);
 
371
        }
 
372
        else {
 
373
                rwl->rw_ex_count--;
 
374
 
 
375
                result = xt_p_mutex_unlock(&rwl->rw_sh_lock);
 
376
                result1 = xt_p_mutex_unlock(&rwl->rw_ex_lock);
 
377
        }
 
378
 
 
379
#ifdef XT_THREAD_LOCK_INFO
 
380
        xt_thread_lock_info_release_owner(&rwl->rw_lock_info);
 
381
#endif
 
382
 
 
383
        return ((result != 0) ? result : result1);
 
384
}
 
385
 
 
386
xtPublic int xt_p_cond_wait(xt_cond_type *cond, xt_mutex_type *mutex)
 
387
{
 
388
        return xt_p_cond_timedwait(cond, mutex, NULL);
 
389
}
 
390
 
 
391
xtPublic int xt_p_cond_timedwait(xt_cond_type *cond, xt_mutex_type *mt, struct timespec *abstime)
 
392
{
 
393
  return pthread_cond_timedwait(cond, &mt->mt_cs, abstime);
 
394
}
 
395
 
 
396
xtPublic int xt_p_join(pthread_t thread, void **value)
 
397
{
 
398
        pthread_join(thread, value);
 
399
        return 0;
 
400
}
 
401
 
 
402
#else // XT_WIN
 
403
 
 
404
#ifdef __darwin__
 
405
#define POLICY                  SCHED_RR
 
406
#else
 
407
#define POLICY                  pth_policy
 
408
#endif
 
409
 
 
410
static int pth_policy;
 
411
static int pth_normal_priority;
 
412
static int pth_min_priority;
 
413
static int pth_max_priority;
 
414
 
 
415
/* Return zero if the priority was set OK,
 
416
 * else errno.
 
417
 */
 
418
static int pth_set_priority(pthread_t thread, int priority)
 
419
{
 
420
        struct sched_param      sp;
 
421
 
 
422
        memset(&sp, 0, sizeof(struct sched_param));
 
423
        sp.sched_priority = priority;
 
424
        return pthread_setschedparam(thread, POLICY, &sp);
 
425
}
 
426
 
 
427
static void pth_get_priority_limits(void)
 
428
{
 
429
        XTThreadPtr                     self = NULL;
 
430
        struct sched_param      sp;
 
431
        int                                     err;
 
432
        int                                     start;
 
433
 
 
434
        /* Save original priority: */
 
435
        err = pthread_getschedparam(pthread_self(), &pth_policy, &sp);
 
436
        if (err) {
 
437
                xt_throw_errno(XT_CONTEXT, err);
 
438
                return;
 
439
        }
 
440
        pth_normal_priority = sp.sched_priority;
 
441
 
 
442
        start = sp.sched_priority;
 
443
 
 
444
#ifdef XT_FREEBSD 
 
445
        pth_min_priority = sched_get_priority_min(sched_getscheduler(0));
 
446
        pth_max_priority = sched_get_priority_max(sched_getscheduler(0));
 
447
#else
 
448
        /* Search for the minimum priority: */
 
449
        pth_min_priority = start;
 
450
        for (;;) {
 
451
                /* 2007-03-01: Corrected, pth_set_priority returns the error code
 
452
                 * (thanks to Hakan for pointing out this bug!)
 
453
                 */
 
454
                if (pth_set_priority(pthread_self(), pth_min_priority-1) != 0)
 
455
                        break;
 
456
                pth_min_priority--;
 
457
        }
 
458
 
 
459
        /* Search for the maximum priority: */
 
460
        pth_max_priority = start;
 
461
        for (;;) {
 
462
                if (pth_set_priority(pthread_self(), pth_max_priority+1) != 0)
 
463
                        break;
 
464
                pth_max_priority++;
 
465
        }
 
466
 
 
467
        /* Restore original priority: */
 
468
        pthread_setschedparam(pthread_self(), pth_policy, &sp);
 
469
#endif
 
470
}
 
471
 
 
472
xtPublic void xt_p_init_threading(void)
 
473
{
 
474
        pth_get_priority_limits();
 
475
}
 
476
 
 
477
xtPublic int xt_p_set_low_priority(pthread_t thr)
 
478
{
 
479
        if (pth_min_priority != pth_max_priority)
 
480
          return pth_set_priority(thr, pth_min_priority);
 
481
        return 0;
 
482
}
 
483
 
 
484
xtPublic int xt_p_set_normal_priority(pthread_t thr)
 
485
{
 
486
        if (pth_min_priority != pth_max_priority)
 
487
          return pth_set_priority(thr, pth_normal_priority);
 
488
        return 0;
 
489
}
 
490
 
 
491
xtPublic int xt_p_set_high_priority(pthread_t thr)
 
492
{
 
493
        if (pth_min_priority != pth_max_priority)
 
494
          return pth_set_priority(thr, pth_max_priority);
 
495
        return 0;
 
496
}
 
497
 
 
498
#ifdef DEBUG_LOCKING
 
499
 
 
500
xtPublic int xt_p_mutex_lock(xt_mutex_type *mutex, u_int line, const char *file)
 
501
{
 
502
        XTThreadPtr self = xt_get_self();
 
503
        int                     r;
 
504
 
 
505
        ASSERT_NS(mutex->mu_init == 12345);
 
506
        r = pthread_mutex_lock(&mutex->mu_plock);
 
507
        if (r == 0) {
 
508
                if (mutex->mu_trace)
 
509
                        printf("==LOCK mutex %d %s:%d\n", (int) mutex->mu_trace, file, (int) line);
 
510
                ASSERT_NS(!mutex->mu_locker);
 
511
                mutex->mu_locker = self;
 
512
                mutex->mu_line = line;
 
513
                mutex->mu_file = file;
 
514
        }
 
515
#ifdef XT_THREAD_LOCK_INFO
 
516
        xt_thread_lock_info_add_owner(&mutex->mu_lock_info);
 
517
#endif
 
518
        return r;
 
519
}
 
520
 
 
521
xtPublic int xt_p_mutex_unlock(xt_mutex_type *mutex)
 
522
{
 
523
        XTThreadPtr self = xt_get_self();
 
524
 
 
525
        ASSERT_NS(mutex->mu_init == 12345);
 
526
        ASSERT_NS(mutex->mu_locker == self);
 
527
        mutex->mu_locker = NULL;
 
528
        if (mutex->mu_trace)
 
529
                printf("UNLOCK mutex %d\n", (int) mutex->mu_trace);
 
530
#ifdef XT_THREAD_LOCK_INFO
 
531
        xt_thread_lock_info_release_owner(&mutex->mu_lock_info);
 
532
#endif
 
533
        return pthread_mutex_unlock(&mutex->mu_plock);
 
534
}
 
535
 
 
536
xtPublic int xt_p_mutex_destroy(xt_mutex_type *mutex)
 
537
{
 
538
        //ASSERT_NS(mutex->mu_init == 12345);
 
539
        mutex->mu_init = 11111;
 
540
#ifdef XT_THREAD_LOCK_INFO
 
541
        xt_thread_lock_info_free(&mutex->mu_lock_info);
 
542
#endif
 
543
        return pthread_mutex_destroy(&mutex->mu_plock);
 
544
}
 
545
 
 
546
xtPublic int xt_p_mutex_trylock(xt_mutex_type *mutex)
 
547
{
 
548
        XTThreadPtr self = xt_get_self();
 
549
        int                     r;
 
550
 
 
551
        ASSERT_NS(mutex->mu_init == 12345);
 
552
        r = pthread_mutex_trylock(&mutex->mu_plock);
 
553
        if (r == 0) {
 
554
                ASSERT_NS(!mutex->mu_locker);
 
555
                mutex->mu_locker = self;
 
556
#ifdef XT_THREAD_LOCK_INFO
 
557
                xt_thread_lock_info_add_owner(&mutex->mu_lock_info);
 
558
#endif
 
559
        }
 
560
        return r;
 
561
}
 
562
 
 
563
#ifdef XT_THREAD_LOCK_INFO
 
564
xtPublic int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr, const char *n)
 
565
#else
 
566
xtPublic int xt_p_mutex_init(xt_mutex_type *mutex, const pthread_mutexattr_t *attr)
 
567
#endif
 
568
{
 
569
        mutex->mu_init = 12345;
 
570
        mutex->mu_trace = FALSE;
 
571
        mutex->mu_locker = NULL;
 
572
#ifdef XT_THREAD_LOCK_INFO
 
573
        mutex->mu_name = n;
 
574
        xt_thread_lock_info_init(&mutex->mu_lock_info, mutex);
 
575
#endif
 
576
        return pthread_mutex_init(&mutex->mu_plock, attr);
 
577
}
 
578
 
 
579
xtPublic int xt_p_cond_wait(xt_cond_type *cond, xt_mutex_type *mutex)
 
580
{
 
581
        XTThreadPtr self = xt_get_self();
 
582
        int                     r;
 
583
 
 
584
        ASSERT_NS(mutex->mu_init == 12345);
 
585
        ASSERT_NS(mutex->mu_locker == self);
 
586
        mutex->mu_locker = NULL;
 
587
        r = pthread_cond_wait(cond, &mutex->mu_plock);
 
588
        ASSERT_NS(!mutex->mu_locker);
 
589
        mutex->mu_locker = self;
 
590
        return r;
 
591
}
 
592
 
 
593
xtPublic int xt_p_cond_timedwait(xt_cond_type *cond, xt_mutex_type *mutex, const struct timespec *abstime)
 
594
{
 
595
        XTThreadPtr self = xt_get_self();
 
596
        int                     r;
 
597
 
 
598
        ASSERT_NS(mutex->mu_init == 12345);
 
599
        ASSERT_NS(mutex->mu_locker == self);
 
600
        mutex->mu_locker = NULL;
 
601
        r = pthread_cond_timedwait(cond, &mutex->mu_plock, abstime);
 
602
        ASSERT_NS(!mutex->mu_locker);
 
603
        mutex->mu_locker = self;
 
604
        return r;
 
605
}
 
606
 
 
607
xtPublic int xt_p_rwlock_rdlock(xt_rwlock_type *rwlock)
 
608
{
 
609
        int r;
 
610
 
 
611
        ASSERT_NS(rwlock->rw_init == 67890);
 
612
        r = pthread_rwlock_rdlock(&rwlock->rw_plock);
 
613
#ifdef XT_THREAD_LOCK_INFO
 
614
        xt_thread_lock_info_add_owner(&rwlock->rw_lock_info);
 
615
#endif
 
616
        return r;
 
617
}
 
618
 
 
619
xtPublic int xt_p_rwlock_wrlock(xt_rwlock_type *rwlock)
 
620
{
 
621
        XTThreadPtr self = xt_get_self();
 
622
        int                     r;
 
623
 
 
624
        ASSERT_NS(rwlock->rw_init == 67890);
 
625
        r = pthread_rwlock_wrlock(&rwlock->rw_plock);
 
626
        if (r == 0) {
 
627
                ASSERT_NS(!rwlock->rw_locker);
 
628
                rwlock->rw_locker = self;
 
629
        }
 
630
#ifdef XT_THREAD_LOCK_INFO
 
631
        xt_thread_lock_info_add_owner(&rwlock->rw_lock_info);
 
632
#endif
 
633
        return r;
 
634
}
 
635
 
 
636
xtPublic xtBool xt_p_rwlock_try_wrlock(xt_rwlock_type *rwlock)
 
637
{
 
638
        XTThreadPtr self = xt_get_self();
 
639
        int                     r;
 
640
 
 
641
        ASSERT_NS(rwlock->rw_init == 67890);
 
642
        r = pthread_rwlock_trywrlock(&rwlock->rw_plock);
 
643
        if (r == 0) {
 
644
                ASSERT_NS(!rwlock->rw_locker);
 
645
                rwlock->rw_locker = self;
 
646
#ifdef XT_THREAD_LOCK_INFO
 
647
                xt_thread_lock_info_add_owner(&rwlock->rw_lock_info);
 
648
#endif
 
649
        }
 
650
        return r == 0;
 
651
}
 
652
 
 
653
xtPublic int xt_p_rwlock_unlock(xt_rwlock_type *rwlock)
 
654
{
 
655
        XTThreadPtr self = xt_get_self();
 
656
 
 
657
        ASSERT_NS(rwlock->rw_init == 67890);
 
658
        if (rwlock->rw_locker) {
 
659
                ASSERT_NS(rwlock->rw_locker == self);
 
660
                rwlock->rw_locker = NULL;
 
661
        }
 
662
#ifdef XT_THREAD_LOCK_INFO
 
663
        xt_thread_lock_info_release_owner(&rwlock->rw_lock_info);
 
664
#endif
 
665
        return pthread_rwlock_unlock(&rwlock->rw_plock);
 
666
}
 
667
 
 
668
xtPublic int xt_p_rwlock_destroy(xt_rwlock_type *rwlock)
 
669
{
 
670
        ASSERT_NS(rwlock->rw_init == 67890);
 
671
        rwlock->rw_init = 0;
 
672
#ifdef XT_THREAD_LOCK_INFO
 
673
        xt_thread_lock_info_free(&rwlock->rw_lock_info);
 
674
#endif
 
675
        return pthread_rwlock_destroy(&rwlock->rw_plock);
 
676
}
 
677
 
 
678
#ifdef XT_THREAD_LOCK_INFO
 
679
xtPublic int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_rwlockattr_t *attr, const char *n)
 
680
#else
 
681
xtPublic int xt_p_rwlock_init(xt_rwlock_type *rwlock, const pthread_rwlockattr_t *attr)
 
682
#endif
 
683
{
 
684
        rwlock->rw_init = 67890;
 
685
        rwlock->rw_readers = 0;
 
686
        rwlock->rw_locker = NULL;
 
687
#ifdef XT_THREAD_LOCK_INFO
 
688
        rwlock->rw_name = n;
 
689
        xt_thread_lock_info_init(&rwlock->rw_lock_info, rwlock);
 
690
#endif
 
691
        return pthread_rwlock_init(&rwlock->rw_plock, attr);
 
692
}
 
693
 
 
694
#endif // DEBUG_LOCKING
 
695
 
 
696
#endif // XT_WIN
 
697