1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_BASE_CRITICALSECTION_H__
29#define TALK_BASE_CRITICALSECTION_H__
30
31#ifdef WIN32
32#include "talk/base/win32.h"
33#endif
34
35#ifdef POSIX
36#include <pthread.h>
37#endif
38
39#ifdef _DEBUG
40#define CS_TRACK_OWNER 1
41#endif  // _DEBUG
42
43#if CS_TRACK_OWNER
44#define TRACK_OWNER(x) x
45#else  // !CS_TRACK_OWNER
46#define TRACK_OWNER(x)
47#endif  // !CS_TRACK_OWNER
48
49namespace talk_base {
50
51#ifdef WIN32
52class CriticalSection {
53public:
54  CriticalSection() {
55    InitializeCriticalSection(&crit_);
56    // Windows docs say 0 is not a valid thread id
57    TRACK_OWNER(thread_ = 0);
58  }
59  ~CriticalSection() {
60    DeleteCriticalSection(&crit_);
61  }
62  void Enter() {
63    EnterCriticalSection(&crit_);
64    TRACK_OWNER(thread_ = GetCurrentThreadId());
65  }
66  void Leave() {
67    TRACK_OWNER(thread_ = 0);
68    LeaveCriticalSection(&crit_);
69  }
70
71#if CS_TRACK_OWNER
72  bool CurrentThreadIsOwner() const { return thread_ == GetCurrentThreadId(); }
73#endif  // CS_TRACK_OWNER
74
75private:
76  CRITICAL_SECTION crit_;
77  TRACK_OWNER(DWORD thread_);  // The section's owning thread id
78};
79#endif // WIN32
80
81#ifdef POSIX
82class CriticalSection {
83public:
84  CriticalSection() {
85    pthread_mutexattr_t mutex_attribute;
86    pthread_mutexattr_init(&mutex_attribute);
87    pthread_mutexattr_settype(&mutex_attribute, PTHREAD_MUTEX_RECURSIVE);
88    pthread_mutex_init(&mutex_, &mutex_attribute);
89    pthread_mutexattr_destroy(&mutex_attribute);
90    TRACK_OWNER(thread_ = 0);
91  }
92  ~CriticalSection() {
93    pthread_mutex_destroy(&mutex_);
94  }
95  void Enter() {
96    pthread_mutex_lock(&mutex_);
97    TRACK_OWNER(thread_ = pthread_self());
98  }
99  void Leave() {
100    TRACK_OWNER(thread_ = 0);
101    pthread_mutex_unlock(&mutex_);
102  }
103
104#if CS_TRACK_OWNER
105  bool CurrentThreadIsOwner() const { return pthread_equal(thread_, pthread_self()); }
106#endif  // CS_TRACK_OWNER
107
108private:
109  pthread_mutex_t mutex_;
110  TRACK_OWNER(pthread_t thread_);
111};
112#endif // POSIX
113
114// CritScope, for serializing exection through a scope
115
116class CritScope {
117public:
118  CritScope(CriticalSection *pcrit) {
119    pcrit_ = pcrit;
120    pcrit_->Enter();
121  }
122  ~CritScope() {
123    pcrit_->Leave();
124  }
125private:
126  CriticalSection *pcrit_;
127};
128
129} // namespace talk_base
130
131#endif // TALK_BASE_CRITICALSECTION_H__
132