• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright(c) 2011 Trusted Logic.   All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *  * Neither the name Trusted Logic nor the names of its
15  *    contributors may be used to endorse or promote products derived
16  *    from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef __LIB_MUTEX_H__
32 #define __LIB_MUTEX_H__
33 
34 /**
35  * This libray defines a type of simple non-recursive mutex that can
36  * be statically initialized. The interface is very similar to pthread
37  * mutexes.
38  **/
39 
40 #ifdef WIN32
41 
42 #include <windows.h>
43 #include "s_type.h"
44 
45 /* Windows API: use a critical section with a state describing
46    whether it has been initialized or not or is being initialized.
47    We use interlocked operations on the state to synchronize between
48    multiple threads competing to initialize the critical section */
49 typedef struct
50 {
51    volatile uint32_t nInitState;
52    CRITICAL_SECTION sCriticalSection;
53 }
54 LIB_MUTEX;
55 
56 #define LIB_MUTEX_WIN32_STATE_UNINITIALIZED 0
57 #define LIB_MUTEX_WIN32_STATE_INITIALIZING  1
58 #define LIB_MUTEX_WIN32_STATE_INITIALIZED   2
59 
60 #define LIB_MUTEX_INITIALIZER { 0 }
61 
62 #endif  /* WIN32 */
63 
64 #ifdef LINUX
65 
66 #include <pthread.h>
67 #include "s_type.h"
68 
69 /* Linux: directly use a pthread mutex. */
70 typedef pthread_mutex_t LIB_MUTEX;
71 #define LIB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
72 
73 #endif /* LINUX */
74 
75 /**
76  * Initialize a mutex. Note that thie function cannot fail. If there
77  * is not enough resource to initialize the mutex, the implementation
78  * resort to active-wait
79  **/
80 void libMutexInit(LIB_MUTEX* pMutex);
81 
82 /**
83  * Lock the mutex
84  */
85 void libMutexLock(LIB_MUTEX* pMutex);
86 
87 /**
88  * Unlock the mutex
89  */
90 void libMutexUnlock(LIB_MUTEX* pMutex);
91 
92 /**
93  * Destroy the mutex
94  */
95 void libMutexDestroy(LIB_MUTEX* pMutex);
96 
97 #endif /* __LIB_MUTEX_H__ */
98