1 /*
2 (c) Copyright 2001-2008 The world wide DirectFB Open Source Community (directfb.org)
3 (c) Copyright 2000-2004 Convergence (integrated media) GmbH
4
5 All rights reserved.
6
7 Written by Denis Oliver Kropp <dok@directfb.org>,
8 Andreas Hundt <andi@fischlustig.de>,
9 Sven Neumann <neo@directfb.org>,
10 Ville Syrjälä <syrjala@sci.fi> and
11 Claudio Ciccani <klan@users.sf.net>.
12
13 This library is free software; you can redistribute it and/or
14 modify it under the terms of the GNU Lesser General Public
15 License as published by the Free Software Foundation; either
16 version 2 of the License, or (at your option) any later version.
17
18 This library is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 Lesser General Public License for more details.
22
23 You should have received a copy of the GNU Lesser General Public
24 License along with this library; if not, write to the
25 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA.
27 */
28
29 #include <config.h>
30
31 #include <direct/mutex.h>
32 #include <direct/util.h>
33
34 /**********************************************************************************************************************/
35
36 DirectResult
direct_mutex_init(DirectMutex * mutex)37 direct_mutex_init( DirectMutex *mutex )
38 {
39 if (pthread_mutex_init( &mutex->lock, NULL ))
40 return errno2result( errno );
41
42 return DR_OK;
43 }
44
45 DirectResult
direct_recursive_mutex_init(DirectMutex * mutex)46 direct_recursive_mutex_init( DirectMutex *mutex )
47 {
48 DirectResult ret = DR_OK;
49 int result;
50 pthread_mutexattr_t attr;
51
52 pthread_mutexattr_init( &attr );
53 #if HAVE_DECL_PTHREAD_MUTEX_RECURSIVE
54 pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
55 #endif
56 result = pthread_mutex_init( &mutex->lock, &attr );
57 if (result) {
58 ret = errno2result( errno );
59 D_PERROR( "Direct/Mutex: Could not initialize recursive mutex!\n" );
60 }
61
62 pthread_mutexattr_destroy( &attr );
63
64 return (DirectResult) ret;
65 }
66
67 __attribute__((no_instrument_function))
68 DirectResult
direct_mutex_lock(DirectMutex * mutex)69 direct_mutex_lock( DirectMutex *mutex )
70 {
71 if (pthread_mutex_lock( &mutex->lock ))
72 return errno2result( errno );
73
74 return DR_OK;
75 }
76
77 __attribute__((no_instrument_function))
78 DirectResult
direct_mutex_unlock(DirectMutex * mutex)79 direct_mutex_unlock( DirectMutex *mutex )
80 {
81 if (pthread_mutex_unlock( &mutex->lock ))
82 return errno2result( errno );
83
84 return DR_OK;
85 }
86
87 DirectResult
direct_mutex_trylock(DirectMutex * mutex)88 direct_mutex_trylock( DirectMutex *mutex )
89 {
90 if (pthread_mutex_trylock( &mutex->lock ))
91 return errno2result( errno );
92
93 return DR_OK;
94 }
95
96 DirectResult
direct_mutex_deinit(DirectMutex * mutex)97 direct_mutex_deinit( DirectMutex *mutex )
98 {
99 if (pthread_mutex_destroy( &mutex->lock ))
100 return errno2result( errno );
101
102 return DR_OK;
103 }
104
105