Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / PhysicsEffects / src / low_level / task / pfx_sync_components_pthreads.h
1 /*\r
2  Applied Research Associates Inc. (c)2011\r
3 \r
4  Redistribution and use in source and binary forms,\r
5    with or without modification, are permitted provided that the\r
6    following conditions are met:\r
7     * Redistributions of source code must retain the above copyright\r
8       notice, this list of conditions and the following disclaimer.\r
9     * Redistributions in binary form must reproduce the above copyright\r
10       notice, this list of conditions and the following disclaimer in the\r
11       documentation and/or other materials provided with the distribution.\r
12     * Neither the name of the Applied Research Associates Inc nor the names\r
13       of its contributors may be used to endorse or promote products derived\r
14       from this software without specific prior written permission.\r
15 \r
16    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
17    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
18    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
19    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
20    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
21    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
22    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
23    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
24    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
25    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
26    POSSIBILITY OF SUCH DAMAGE.\r
27 */\r
28 \r
29 #ifndef _SCE_PFX_SYNC_COMPONENTS_PTHREADS_H\r
30 #define _SCE_PFX_SYNC_COMPONENTS_PTHREADS_H\r
31 \r
32 #include "../../../include/physics_effects/base_level/base/pfx_common.h"\r
33 #include "../../../include/physics_effects/low_level/task/pfx_pthreads.h"\r
34 #include "../../../include/physics_effects/low_level/task/pfx_sync_components.h"\r
35 #include <pthread.h>\r
36 #include <semaphore.h>\r
37 \r
38 namespace sce {\r
39 namespace PhysicsEffects {\r
40 \r
41 //----------------------------------------------------------------------------\r
42 //  PfxPthreadsBarrier\r
43 //\r
44 /// Implementation of a barrier using pthreads. This version uses a mutex\r
45 /// and condition variable rather than using native pthreads barrier, which\r
46 /// enables it to be used on platforms that don't support pthreads barriers\r
47 /// (such as Android 2.3x)\r
48 //----------------------------------------------------------------------------\r
49 class PfxPthreadsBarrier : public PfxBarrier\r
50 {\r
51         public:\r
52                 PfxPthreadsBarrier();\r
53                 virtual ~PfxPthreadsBarrier();\r
54 \r
55                 // from PfxBarrier\r
56                 virtual void sync();\r
57                 virtual void setMaxCount(int n);\r
58                 virtual int  getMaxCount();\r
59 \r
60         private:\r
61                 pthread_mutex_t m_mutex;        ///< Mutex used to block worker threads\r
62                 pthread_cond_t m_cond;          ///< Condition variable\r
63         \r
64                 int m_maxThreads;                       ///< Maximum number of worker threads\r
65                 int     m_called;                               ///< Number of worker threads waiting at barrier\r
66 };\r
67 \r
68 //----------------------------------------------------------------------------\r
69 //  PfxPthreadsBarrier\r
70 //\r
71 /// Implementation of a critical section using pthreads.\r
72 //----------------------------------------------------------------------------\r
73 class PfxPthreadsCriticalSection : public PfxCriticalSection\r
74 {\r
75         public:\r
76                 PfxPthreadsCriticalSection();\r
77                 virtual ~PfxPthreadsCriticalSection();\r
78 \r
79                 // from PfxCriticalSection\r
80                 virtual PfxUInt32 getSharedParam(int i);\r
81                 virtual void setSharedParam(int i,PfxUInt32 p);\r
82 \r
83                 virtual void lock();\r
84                 virtual void unlock();\r
85 \r
86         private:\r
87                 pthread_mutex_t m_mutex;        ///< Mutex used to implement lock\r
88 };\r
89 \r
90 } //namespace PhysicsEffects\r
91 } //namespace sce\r
92 \r
93 #endif // _SCE_PFX_SYNC_COMPONENTS_PTHREADS_H\r