Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/thread/src/recursive_mutex.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 21.0 KB
Line 
1// Copyright (C) 2001-2003
2// William E. Kempf
3//
4//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7#include <boost/thread/detail/config.hpp>
8
9#include <boost/thread/recursive_mutex.hpp>
10#include <boost/thread/xtime.hpp>
11#include <boost/thread/thread.hpp>
12#include <boost/limits.hpp>
13#include <string>
14#include <stdexcept>
15#include <cassert>
16#include "timeconv.inl"
17
18#if defined(BOOST_HAS_WINTHREADS)
19#   include <new>
20#   include <boost/thread/once.hpp>
21#   include <windows.h>
22#   include <time.h>
23#   include "mutex.inl"
24#elif defined(BOOST_HAS_PTHREADS)
25#   include <errno.h>
26#elif defined(BOOST_HAS_MPTASKS)
27#   include <MacErrors.h>
28#   include "safe.hpp"
29#endif
30
31namespace boost {
32
33#if defined(BOOST_HAS_WINTHREADS)
34
35recursive_mutex::recursive_mutex()
36    : m_mutex(0)
37    , m_critical_section(false)
38    , m_count(0)
39{
40    m_critical_section = true;
41    if (m_critical_section)
42        m_mutex = new_critical_section();
43    else
44        m_mutex = new_mutex(0);
45}
46
47recursive_mutex::~recursive_mutex()
48{
49    if (m_critical_section)
50        delete_critical_section(m_mutex);
51    else
52        delete_mutex(m_mutex);
53}
54
55void recursive_mutex::do_lock()
56{
57    if (m_critical_section)
58        wait_critical_section_infinite(m_mutex);
59    else
60        wait_mutex(m_mutex, INFINITE);
61
62    if (++m_count > 1)
63    {
64        if (m_critical_section)
65            release_critical_section(m_mutex);
66        else
67            release_mutex(m_mutex);
68    }
69}
70
71void recursive_mutex::do_unlock()
72{
73    if (--m_count == 0)
74    {
75        if (m_critical_section)
76            release_critical_section(m_mutex);
77        else
78            release_mutex(m_mutex);
79    }
80}
81
82void recursive_mutex::do_lock(cv_state& state)
83{
84    if (m_critical_section)
85        wait_critical_section_infinite(m_mutex);
86    else
87        wait_mutex(m_mutex, INFINITE);
88
89    m_count = state;
90}
91
92void recursive_mutex::do_unlock(cv_state& state)
93{
94    state = m_count;
95    m_count = 0;
96
97    if (m_critical_section)
98        release_critical_section(m_mutex);
99    else
100        release_mutex(m_mutex);
101}
102
103recursive_try_mutex::recursive_try_mutex()
104    : m_mutex(0)
105    , m_critical_section(false)
106    , m_count(0)
107{
108    m_critical_section = has_TryEnterCriticalSection();
109    if (m_critical_section)
110        m_mutex = new_critical_section();
111    else
112        m_mutex = new_mutex(0);
113}
114
115recursive_try_mutex::~recursive_try_mutex()
116{
117    if (m_critical_section)
118        delete_critical_section(m_mutex);
119    else
120        delete_mutex(m_mutex);
121}
122
123void recursive_try_mutex::do_lock()
124{
125    if (m_critical_section)
126        wait_critical_section_infinite(m_mutex);
127    else
128        wait_mutex(m_mutex, INFINITE);
129
130    if (++m_count > 1)
131    {
132        if (m_critical_section)
133            release_critical_section(m_mutex);
134        else
135            release_mutex(m_mutex);
136    }
137}
138
139bool recursive_try_mutex::do_trylock()
140{
141    bool res = false;
142    if (m_critical_section)
143        res = wait_critical_section_try(m_mutex);
144    else
145        res = wait_mutex(m_mutex, 0) == WAIT_OBJECT_0;
146
147    if (res)
148    {
149        if (++m_count > 1)
150        {
151            if (m_critical_section)
152                release_critical_section(m_mutex);
153            else
154                release_mutex(m_mutex);
155        }
156        return true;
157    }
158    return false;
159}
160
161void recursive_try_mutex::do_unlock()
162{
163    if (--m_count == 0)
164    {
165        if (m_critical_section)
166            release_critical_section(m_mutex);
167        else
168            release_mutex(m_mutex);
169    }
170}
171
172void recursive_try_mutex::do_lock(cv_state& state)
173{
174    if (m_critical_section)
175        wait_critical_section_infinite(m_mutex);
176    else
177        wait_mutex(m_mutex, INFINITE);
178
179    m_count = state;
180}
181
182void recursive_try_mutex::do_unlock(cv_state& state)
183{
184    state = m_count;
185    m_count = 0;
186
187    if (m_critical_section)
188        release_critical_section(m_mutex);
189    else
190        release_mutex(m_mutex);
191}
192
193recursive_timed_mutex::recursive_timed_mutex()
194    : m_mutex(0)
195    , m_count(0)
196{
197    m_mutex = new_mutex(0);
198}
199
200recursive_timed_mutex::~recursive_timed_mutex()
201{
202    delete_mutex(m_mutex);
203}
204
205void recursive_timed_mutex::do_lock()
206{
207    wait_mutex(m_mutex, INFINITE);
208
209    if (++m_count > 1)
210        release_mutex(m_mutex);
211}
212
213bool recursive_timed_mutex::do_trylock()
214{
215    bool res = wait_mutex(m_mutex, 0) == WAIT_OBJECT_0;
216
217    if (res)
218    {
219        if (++m_count > 1)
220            release_mutex(m_mutex);
221        return true;
222    }
223    return false;
224}
225
226bool recursive_timed_mutex::do_timedlock(const xtime& xt)
227{
228    for (;;)
229    {
230        int milliseconds;
231        to_duration(xt, milliseconds);
232
233        unsigned int res = wait_mutex(m_mutex, milliseconds);
234
235        if (res == WAIT_TIMEOUT)
236        {
237            xtime cur;
238            xtime_get(&cur, TIME_UTC);
239            if (xtime_cmp(xt, cur) > 0)
240                continue;
241        }
242
243        if (res == WAIT_OBJECT_0)
244        {
245            if (++m_count > 1)
246                release_mutex(m_mutex);
247            return true;
248        }
249
250        return false;
251    }
252}
253
254void recursive_timed_mutex::do_unlock()
255{
256    if (--m_count == 0)
257        release_mutex(m_mutex);
258}
259
260void recursive_timed_mutex::do_lock(cv_state& state)
261{
262    wait_mutex(m_mutex, INFINITE);
263
264    m_count = state;
265}
266
267void recursive_timed_mutex::do_unlock(cv_state& state)
268{
269    state = m_count;
270    m_count = 0;
271
272    release_mutex(m_mutex);
273}
274
275#elif defined(BOOST_HAS_PTHREADS)
276
277recursive_mutex::recursive_mutex()
278    : m_count(0)
279#   if !defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
280    , m_valid_id(false)
281#   endif
282{
283    pthread_mutexattr_t attr;
284    int res = pthread_mutexattr_init(&attr);
285    assert(res == 0);
286
287#   if defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
288    res = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
289    assert(res == 0);
290#   endif
291
292    res = pthread_mutex_init(&m_mutex, &attr);
293    {
294        int res = 0;
295        res = pthread_mutexattr_destroy(&attr);
296        assert(res == 0);
297    }
298    if (res != 0)
299        throw thread_resource_error();
300
301#   if !defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
302    res = pthread_cond_init(&m_unlocked, 0);
303    if (res != 0)
304    {
305        pthread_mutex_destroy(&m_mutex);
306        throw thread_resource_error();
307    }
308#   endif
309}
310
311recursive_mutex::~recursive_mutex()
312{
313    int res = 0;
314    res = pthread_mutex_destroy(&m_mutex);
315    assert(res == 0);
316
317#   if !defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
318    res = pthread_cond_destroy(&m_unlocked);
319    assert(res == 0);
320#   endif
321}
322
323void recursive_mutex::do_lock()
324{
325    int res = 0;
326    res = pthread_mutex_lock(&m_mutex);
327    assert(res == 0);
328
329#   if defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
330    if (++m_count > 1)
331    {
332        res = pthread_mutex_unlock(&m_mutex);
333        assert(res == 0);
334    }
335#   else
336    pthread_t tid = pthread_self();
337    if (m_valid_id && pthread_equal(m_thread_id, tid))
338        ++m_count;
339    else
340    {
341        while (m_valid_id)
342        {
343            res = pthread_cond_wait(&m_unlocked, &m_mutex);
344            assert(res == 0);
345        }
346
347        m_thread_id = tid;
348        m_valid_id = true;
349        m_count = 1;
350    }
351
352    res = pthread_mutex_unlock(&m_mutex);
353    assert(res == 0);
354#   endif
355}
356
357void recursive_mutex::do_unlock()
358{
359#   if defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
360    if (--m_count == 0)
361    {
362        int res = 0;
363        res = pthread_mutex_unlock(&m_mutex);
364        assert(res == 0);
365    }
366#   else
367    int res = 0;
368    res = pthread_mutex_lock(&m_mutex);
369    assert(res == 0);
370
371    pthread_t tid = pthread_self();
372    if (m_valid_id && !pthread_equal(m_thread_id, tid))
373    {
374        res = pthread_mutex_unlock(&m_mutex);
375        assert(res == 0);
376        throw lock_error();
377    }
378
379    if (--m_count == 0)
380    {
381        assert(m_valid_id);
382        m_valid_id = false;
383
384        res = pthread_cond_signal(&m_unlocked);
385        assert(res == 0);
386    }
387
388    res = pthread_mutex_unlock(&m_mutex);
389    assert(res == 0);
390#   endif
391}
392
393void recursive_mutex::do_lock(cv_state& state)
394{
395#   if defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
396    m_count = state.count;
397#   else
398    int res = 0;
399
400    while (m_valid_id)
401    {
402        res = pthread_cond_wait(&m_unlocked, &m_mutex);
403        assert(res == 0);
404    }
405
406    m_thread_id = pthread_self();
407    m_valid_id = true;
408    m_count = state.count;
409
410    res = pthread_mutex_unlock(&m_mutex);
411    assert(res == 0);
412#   endif
413}
414
415void recursive_mutex::do_unlock(cv_state& state)
416{
417#   if !defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
418    int res = 0;
419    res = pthread_mutex_lock(&m_mutex);
420    assert(res == 0);
421
422    assert(m_valid_id);
423    m_valid_id = false;
424
425    res = pthread_cond_signal(&m_unlocked);
426    assert(res == 0);
427#   endif
428
429    state.pmutex = &m_mutex;
430    state.count = m_count;
431    m_count = 0;
432}
433
434recursive_try_mutex::recursive_try_mutex()
435    : m_count(0)
436#   if !defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
437    , m_valid_id(false)
438#   endif
439{
440    pthread_mutexattr_t attr;
441    int res = pthread_mutexattr_init(&attr);
442    assert(res == 0);
443
444#   if defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
445    res = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
446    assert(res == 0);
447#   endif
448
449    res = pthread_mutex_init(&m_mutex, &attr);
450    {
451        int res = 0;
452        res = pthread_mutexattr_destroy(&attr);
453        assert(res == 0);
454    }
455    if (res != 0)
456        throw thread_resource_error();
457
458#   if !defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
459    res = pthread_cond_init(&m_unlocked, 0);
460    if (res != 0)
461    {
462        pthread_mutex_destroy(&m_mutex);
463        throw thread_resource_error();
464    }
465#   endif
466}
467
468recursive_try_mutex::~recursive_try_mutex()
469{
470    int res = 0;
471    res = pthread_mutex_destroy(&m_mutex);
472    assert(res == 0);
473
474#   if !defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
475    res = pthread_cond_destroy(&m_unlocked);
476    assert(res == 0);
477#   endif
478}
479
480void recursive_try_mutex::do_lock()
481{
482    int res = 0;
483    res = pthread_mutex_lock(&m_mutex);
484    assert(res == 0);
485
486#   if defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
487    if (++m_count > 1)
488    {
489        res = pthread_mutex_unlock(&m_mutex);
490        assert(res == 0);
491    }
492#   else
493    pthread_t tid = pthread_self();
494    if (m_valid_id && pthread_equal(m_thread_id, tid))
495        ++m_count;
496    else
497    {
498        while (m_valid_id)
499        {
500            res = pthread_cond_wait(&m_unlocked, &m_mutex);
501            assert(res == 0);
502        }
503
504        m_thread_id = tid;
505        m_valid_id = true;
506        m_count = 1;
507    }
508
509    res = pthread_mutex_unlock(&m_mutex);
510    assert(res == 0);
511#   endif
512}
513
514bool recursive_try_mutex::do_trylock()
515{
516#   if defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
517    int res = 0;
518    res = pthread_mutex_trylock(&m_mutex);
519    assert(res == 0 || res == EBUSY);
520
521    if (res == 0)
522    {
523        if (++m_count > 1)
524        {
525            res = pthread_mutex_unlock(&m_mutex);
526            assert(res == 0);
527        }
528        return true;
529    }
530
531    return false;
532#   else
533    int res = 0;
534    res = pthread_mutex_lock(&m_mutex);
535    assert(res == 0);
536
537    bool ret = false;
538    pthread_t tid = pthread_self();
539    if (m_valid_id && pthread_equal(m_thread_id, tid))
540    {
541        ++m_count;
542        ret = true;
543    }
544    else if (!m_valid_id)
545    {
546        m_thread_id = tid;
547        m_valid_id = true;
548        m_count = 1;
549        ret = true;
550    }
551
552    res = pthread_mutex_unlock(&m_mutex);
553    assert(res == 0);
554    return ret;
555#   endif
556}
557
558void recursive_try_mutex::do_unlock()
559{
560#   if defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
561    if (--m_count == 0)
562    {
563        int res = 0;
564        res = pthread_mutex_unlock(&m_mutex);
565        assert(res == 0);
566    }
567#   else
568    int res = 0;
569    res = pthread_mutex_lock(&m_mutex);
570    assert(res == 0);
571
572    pthread_t tid = pthread_self();
573    if (m_valid_id && !pthread_equal(m_thread_id, tid))
574    {
575        res = pthread_mutex_unlock(&m_mutex);
576        assert(res == 0);
577        throw lock_error();
578    }
579
580    if (--m_count == 0)
581    {
582        assert(m_valid_id);
583        m_valid_id = false;
584
585        res = pthread_cond_signal(&m_unlocked);
586        assert(res == 0);
587    }
588
589    res = pthread_mutex_unlock(&m_mutex);
590    assert(res == 0);
591#   endif
592}
593
594void recursive_try_mutex::do_lock(cv_state& state)
595{
596#   if defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
597    m_count = state.count;
598#   else
599    int res = 0;
600
601    while (m_valid_id)
602    {
603        res = pthread_cond_wait(&m_unlocked, &m_mutex);
604        assert(res == 0);
605    }
606
607    m_thread_id = pthread_self();
608    m_valid_id = true;
609    m_count = state.count;
610
611    res = pthread_mutex_unlock(&m_mutex);
612    assert(res == 0);
613#   endif
614}
615
616void recursive_try_mutex::do_unlock(cv_state& state)
617{
618#   if !defined(BOOST_HAS_PTHREAD_MUTEXATTR_SETTYPE)
619    int res = 0;
620    res = pthread_mutex_lock(&m_mutex);
621    assert(res == 0);
622
623    assert(m_valid_id);
624    m_valid_id = false;
625
626    res = pthread_cond_signal(&m_unlocked);
627    assert(res == 0);
628#   endif
629
630    state.pmutex = &m_mutex;
631    state.count = m_count;
632    m_count = 0;
633}
634
635recursive_timed_mutex::recursive_timed_mutex()
636    : m_valid_id(false), m_count(0)
637{
638    int res = 0;
639    res = pthread_mutex_init(&m_mutex, 0);
640    if (res != 0)
641        throw thread_resource_error();
642
643    res = pthread_cond_init(&m_unlocked, 0);
644    if (res != 0)
645    {
646        pthread_mutex_destroy(&m_mutex);
647        throw thread_resource_error();
648    }
649}
650
651recursive_timed_mutex::~recursive_timed_mutex()
652{
653    int res = 0;
654    res = pthread_mutex_destroy(&m_mutex);
655    assert(res == 0);
656
657    res = pthread_cond_destroy(&m_unlocked);
658    assert(res == 0);
659}
660
661void recursive_timed_mutex::do_lock()
662{
663    int res = 0;
664    res = pthread_mutex_lock(&m_mutex);
665    assert(res == 0);
666
667    pthread_t tid = pthread_self();
668    if (m_valid_id && pthread_equal(m_thread_id, tid))
669        ++m_count;
670    else
671    {
672        while (m_valid_id)
673        {
674            res = pthread_cond_wait(&m_unlocked, &m_mutex);
675            assert(res == 0);
676        }
677
678        m_thread_id = tid;
679        m_valid_id = true;
680        m_count = 1;
681    }
682
683    res = pthread_mutex_unlock(&m_mutex);
684    assert(res == 0);
685}
686
687bool recursive_timed_mutex::do_trylock()
688{
689    int res = 0;
690    res = pthread_mutex_lock(&m_mutex);
691    assert(res == 0);
692
693    bool ret = false;
694    pthread_t tid = pthread_self();
695    if (m_valid_id && pthread_equal(m_thread_id, tid))
696    {
697        ++m_count;
698        ret = true;
699    }
700    else if (!m_valid_id)
701    {
702        m_thread_id = tid;
703        m_valid_id = true;
704        m_count = 1;
705        ret = true;
706    }
707
708    res = pthread_mutex_unlock(&m_mutex);
709    assert(res == 0);
710    return ret;
711}
712
713bool recursive_timed_mutex::do_timedlock(const xtime& xt)
714{
715    int res = 0;
716    res = pthread_mutex_lock(&m_mutex);
717    assert(res == 0);
718
719    bool ret = false;
720    pthread_t tid = pthread_self();
721    if (m_valid_id && pthread_equal(m_thread_id, tid))
722    {
723        ++m_count;
724        ret = true;
725    }
726    else
727    {
728        timespec ts;
729        to_timespec(xt, ts);
730
731        while (m_valid_id)
732        {
733            res = pthread_cond_timedwait(&m_unlocked, &m_mutex, &ts);
734            if (res == ETIMEDOUT)
735                break;
736            assert(res == 0);
737        }
738
739        if (!m_valid_id)
740        {
741            m_thread_id = tid;
742            m_valid_id = true;
743            m_count = 1;
744            ret = true;
745        }
746    }
747
748    res = pthread_mutex_unlock(&m_mutex);
749    assert(res == 0);
750    return ret;
751}
752
753void recursive_timed_mutex::do_unlock()
754{
755    int res = 0;
756    res = pthread_mutex_lock(&m_mutex);
757    assert(res == 0);
758
759    pthread_t tid = pthread_self();
760    if (m_valid_id && !pthread_equal(m_thread_id, tid))
761    {
762        res = pthread_mutex_unlock(&m_mutex);
763        assert(res == 0);
764        throw lock_error();
765    }
766
767    if (--m_count == 0)
768    {
769        assert(m_valid_id);
770        m_valid_id = false;
771
772        res = pthread_cond_signal(&m_unlocked);
773        assert(res == 0);
774    }
775
776    res = pthread_mutex_unlock(&m_mutex);
777    assert(res == 0);
778}
779
780void recursive_timed_mutex::do_lock(cv_state& state)
781{
782    int res = 0;
783
784    while (m_valid_id)
785    {
786        res = pthread_cond_wait(&m_unlocked, &m_mutex);
787        assert(res == 0);
788    }
789
790    m_thread_id = pthread_self();
791    m_valid_id = true;
792    m_count = state.count;
793
794    res = pthread_mutex_unlock(&m_mutex);
795    assert(res == 0);
796}
797
798void recursive_timed_mutex::do_unlock(cv_state& state)
799{
800    int res = 0;
801    res = pthread_mutex_lock(&m_mutex);
802    assert(res == 0);
803
804    assert(m_valid_id);
805    m_valid_id = false;
806
807    res = pthread_cond_signal(&m_unlocked);
808    assert(res == 0);
809
810    state.pmutex = &m_mutex;
811    state.count = m_count;
812    m_count = 0;
813}
814#elif defined(BOOST_HAS_MPTASKS)
815
816using threads::mac::detail::safe_enter_critical_region;
817
818
819recursive_mutex::recursive_mutex()
820    : m_count(0)
821{
822}
823
824recursive_mutex::~recursive_mutex()
825{
826}
827
828void recursive_mutex::do_lock()
829{
830    OSStatus lStatus = noErr;
831    lStatus = safe_enter_critical_region(m_mutex, kDurationForever,
832        m_mutex_mutex);
833    assert(lStatus == noErr);
834
835    if (++m_count > 1)
836    {
837        lStatus = MPExitCriticalRegion(m_mutex);
838        assert(lStatus == noErr);
839    }
840}
841
842void recursive_mutex::do_unlock()
843{
844    if (--m_count == 0)
845    {
846        OSStatus lStatus = noErr;
847        lStatus = MPExitCriticalRegion(m_mutex);
848        assert(lStatus == noErr);
849    }
850}
851
852void recursive_mutex::do_lock(cv_state& state)
853{
854    OSStatus lStatus = noErr;
855    lStatus = safe_enter_critical_region(m_mutex, kDurationForever,
856        m_mutex_mutex);
857    assert(lStatus == noErr);
858
859    m_count = state;
860}
861
862void recursive_mutex::do_unlock(cv_state& state)
863{
864    state = m_count;
865    m_count = 0;
866
867    OSStatus lStatus = noErr;
868    lStatus = MPExitCriticalRegion(m_mutex);
869    assert(lStatus == noErr);
870}
871
872recursive_try_mutex::recursive_try_mutex()
873    : m_count(0)
874{
875}
876
877recursive_try_mutex::~recursive_try_mutex()
878{
879}
880
881void recursive_try_mutex::do_lock()
882{
883    OSStatus lStatus = noErr;
884    lStatus = safe_enter_critical_region(m_mutex, kDurationForever,
885        m_mutex_mutex);
886    assert(lStatus == noErr);
887
888    if (++m_count > 1)
889    {
890        lStatus = MPExitCriticalRegion(m_mutex);
891        assert(lStatus == noErr);
892    }
893}
894
895bool recursive_try_mutex::do_trylock()
896{
897    OSStatus lStatus = noErr;
898    lStatus = MPEnterCriticalRegion(m_mutex, kDurationImmediate);
899    assert(lStatus == noErr || lStatus == kMPTimeoutErr);
900
901    if (lStatus == noErr)
902    {
903        if (++m_count > 1)
904        {
905            lStatus = MPExitCriticalRegion(m_mutex);
906            assert(lStatus == noErr);
907        }
908        return true;
909    }
910    return false;
911}
912
913void recursive_try_mutex::do_unlock()
914{
915    if (--m_count == 0)
916    {
917        OSStatus lStatus = noErr;
918        lStatus = MPExitCriticalRegion(m_mutex);
919        assert(lStatus == noErr);
920    }
921}
922
923void recursive_try_mutex::do_lock(cv_state& state)
924{
925    OSStatus lStatus = noErr;
926    lStatus = safe_enter_critical_region(m_mutex, kDurationForever,
927        m_mutex_mutex);
928    assert(lStatus == noErr);
929
930    m_count = state;
931}
932
933void recursive_try_mutex::do_unlock(cv_state& state)
934{
935    state = m_count;
936    m_count = 0;
937
938    OSStatus lStatus = noErr;
939    lStatus = MPExitCriticalRegion(m_mutex);
940    assert(lStatus == noErr);
941}
942
943recursive_timed_mutex::recursive_timed_mutex()
944    : m_count(0)
945{
946}
947
948recursive_timed_mutex::~recursive_timed_mutex()
949{
950}
951
952void recursive_timed_mutex::do_lock()
953{
954    OSStatus lStatus = noErr;
955    lStatus = safe_enter_critical_region(m_mutex, kDurationForever,
956        m_mutex_mutex);
957    assert(lStatus == noErr);
958
959    if (++m_count > 1)
960    {
961        lStatus = MPExitCriticalRegion(m_mutex);
962        assert(lStatus == noErr);
963    }
964}
965
966bool recursive_timed_mutex::do_trylock()
967{
968    OSStatus lStatus = noErr;
969    lStatus = MPEnterCriticalRegion(m_mutex, kDurationImmediate);
970    assert(lStatus == noErr || lStatus == kMPTimeoutErr);
971
972    if (lStatus == noErr)
973    {
974        if (++m_count > 1)
975        {
976            lStatus = MPExitCriticalRegion(m_mutex);
977            assert(lStatus == noErr);
978        }
979        return true;
980    }
981    return false;
982}
983
984bool recursive_timed_mutex::do_timedlock(const xtime& xt)
985{
986    int microseconds;
987    to_microduration(xt, microseconds);
988    Duration lDuration = kDurationMicrosecond * microseconds;
989
990    OSStatus lStatus = noErr;
991    lStatus = safe_enter_critical_region(m_mutex, lDuration, m_mutex_mutex);
992    assert(lStatus == noErr || lStatus == kMPTimeoutErr);
993
994    if (lStatus == noErr)
995    {
996        if (++m_count > 1)
997        {
998            lStatus = MPExitCriticalRegion(m_mutex);
999            assert(lStatus == noErr);
1000        }
1001        return true;
1002    }
1003    return false;
1004}
1005
1006void recursive_timed_mutex::do_unlock()
1007{
1008    if (--m_count == 0)
1009    {
1010        OSStatus lStatus = noErr;
1011        lStatus = MPExitCriticalRegion(m_mutex);
1012        assert(lStatus == noErr);
1013    }
1014}
1015
1016void recursive_timed_mutex::do_lock(cv_state& state)
1017{
1018    OSStatus lStatus = noErr;
1019    lStatus = safe_enter_critical_region(m_mutex, kDurationForever,
1020        m_mutex_mutex);
1021    assert(lStatus == noErr);
1022
1023    m_count = state;
1024}
1025
1026void recursive_timed_mutex::do_unlock(cv_state& state)
1027{
1028    state = m_count;
1029    m_count = 0;
1030
1031    OSStatus lStatus = noErr;
1032    lStatus = MPExitCriticalRegion(m_mutex);
1033    assert(lStatus == noErr);
1034}
1035#endif
1036
1037} // namespace boost
1038
1039// Change Log:
1040//   8 Feb 01  WEKEMPF Initial version.
Note: See TracBrowser for help on using the repository browser.