Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/openal-0.0.8/src/al_mutexlib.c @ 17

Last change on this file since 17 was 17, checked in by landauf, 16 years ago

added openal

File size: 1.9 KB
Line 
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
13MutexID _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
26void _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
35void _alLockMutex( MutexID mutex )
36{
37        pthread_mutex_lock( mutex );
38}
39
40int _alTryLockMutex( MutexID mutex )
41{
42        return ( pthread_mutex_trylock( mutex ) == 0 ) ? 0 : -1;
43}
44
45void _alUnlockMutex( MutexID mutex )
46{
47        pthread_mutex_unlock( mutex );
48}
49
50#elif defined(USE_WINDOWSTHREADING)
51
52MutexID _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
62void _alDestroyMutex( MutexID mutex )
63{
64        DeleteCriticalSection( mutex );
65}
66
67void _alLockMutex( MutexID mutex )
68{
69        EnterCriticalSection( mutex );
70}
71
72int _alTryLockMutex( MutexID mutex )
73{
74        return ( TryEnterCriticalSection( mutex ) != 0 ) ? 0 : -1;
75}
76
77void _alUnlockMutex( MutexID mutex )
78{
79        LeaveCriticalSection( mutex );
80}
81
82#elif defined(USE_MORPHOSTHREADING)
83
84#include <proto/exec.h>
85
86MutexID _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
96void _alDestroyMutex( MutexID mutex )
97{
98        FreeVec( mutex );
99}
100
101void _alLockMutex( MutexID mutex )
102{
103        ObtainSemaphore( mutex );
104}
105
106int _alTryLockMutex( MutexID mutex )
107{
108        return ( AttemptSemaphore( mutex ) != 0 ) ? 0 : -1;
109}
110
111void _alUnlockMutex( MutexID mutex )
112{
113        ReleaseSemaphore( mutex );
114}
115
116#endif
Note: See TracBrowser for help on using the repository browser.