1 | /* -*- mode: C; tab-width:8; c-basic-offset:8 -*- |
---|
2 | * vi:set ts=8: |
---|
3 | * |
---|
4 | * mutexlib.c |
---|
5 | */ |
---|
6 | |
---|
7 | #include <stdlib.h> |
---|
8 | #include <stdio.h> |
---|
9 | #include "al_mutexlib.h" |
---|
10 | |
---|
11 | #if defined(USE_POSIXTHREADING) |
---|
12 | |
---|
13 | MutexID _alCreateMutex( void ) |
---|
14 | { |
---|
15 | MutexID mutex = ( MutexID ) malloc( sizeof( *mutex ) ); |
---|
16 | if( mutex == NULL ) { |
---|
17 | return NULL; |
---|
18 | } |
---|
19 | if( pthread_mutex_init( mutex, NULL ) != 0 ) { |
---|
20 | free( mutex ); |
---|
21 | return NULL; |
---|
22 | } |
---|
23 | return mutex; |
---|
24 | } |
---|
25 | |
---|
26 | void _alDestroyMutex( MutexID mutex ) |
---|
27 | { |
---|
28 | if( pthread_mutex_destroy( mutex ) != 0 ) { |
---|
29 | fprintf( stderr, "mutex %p busy\n", ( void * ) mutex ); |
---|
30 | return; |
---|
31 | } |
---|
32 | free( mutex ); |
---|
33 | } |
---|
34 | |
---|
35 | void _alLockMutex( MutexID mutex ) |
---|
36 | { |
---|
37 | pthread_mutex_lock( mutex ); |
---|
38 | } |
---|
39 | |
---|
40 | int _alTryLockMutex( MutexID mutex ) |
---|
41 | { |
---|
42 | return ( pthread_mutex_trylock( mutex ) == 0 ) ? 0 : -1; |
---|
43 | } |
---|
44 | |
---|
45 | void _alUnlockMutex( MutexID mutex ) |
---|
46 | { |
---|
47 | pthread_mutex_unlock( mutex ); |
---|
48 | } |
---|
49 | |
---|
50 | #elif defined(USE_WINDOWSTHREADING) |
---|
51 | |
---|
52 | MutexID _alCreateMutex( void ) |
---|
53 | { |
---|
54 | MutexID mutex = ( MutexID ) malloc( sizeof( *mutex ) ); |
---|
55 | if( mutex == NULL ) { |
---|
56 | return NULL; |
---|
57 | } |
---|
58 | InitializeCriticalSection( mutex ); |
---|
59 | return mutex; |
---|
60 | } |
---|
61 | |
---|
62 | void _alDestroyMutex( MutexID mutex ) |
---|
63 | { |
---|
64 | DeleteCriticalSection( mutex ); |
---|
65 | } |
---|
66 | |
---|
67 | void _alLockMutex( MutexID mutex ) |
---|
68 | { |
---|
69 | EnterCriticalSection( mutex ); |
---|
70 | } |
---|
71 | |
---|
72 | int _alTryLockMutex( MutexID mutex ) |
---|
73 | { |
---|
74 | return ( TryEnterCriticalSection( mutex ) != 0 ) ? 0 : -1; |
---|
75 | } |
---|
76 | |
---|
77 | void _alUnlockMutex( MutexID mutex ) |
---|
78 | { |
---|
79 | LeaveCriticalSection( mutex ); |
---|
80 | } |
---|
81 | |
---|
82 | #elif defined(USE_MORPHOSTHREADING) |
---|
83 | |
---|
84 | #include <proto/exec.h> |
---|
85 | |
---|
86 | MutexID _alCreateMutex( void ) |
---|
87 | { |
---|
88 | MutexID mutex = ( MutexID ) AllocVec( sizeof( *mutex ), MEMF_PUBLIC ); |
---|
89 | if( mutex == NULL ) { |
---|
90 | return NULL; |
---|
91 | } |
---|
92 | InitSemaphore( mutex ); |
---|
93 | return mutex; |
---|
94 | } |
---|
95 | |
---|
96 | void _alDestroyMutex( MutexID mutex ) |
---|
97 | { |
---|
98 | FreeVec( mutex ); |
---|
99 | } |
---|
100 | |
---|
101 | void _alLockMutex( MutexID mutex ) |
---|
102 | { |
---|
103 | ObtainSemaphore( mutex ); |
---|
104 | } |
---|
105 | |
---|
106 | int _alTryLockMutex( MutexID mutex ) |
---|
107 | { |
---|
108 | return ( AttemptSemaphore( mutex ) != 0 ) ? 0 : -1; |
---|
109 | } |
---|
110 | |
---|
111 | void _alUnlockMutex( MutexID mutex ) |
---|
112 | { |
---|
113 | ReleaseSemaphore( mutex ); |
---|
114 | } |
---|
115 | |
---|
116 | #endif |
---|