summaryrefslogtreecommitdiff
path: root/Build/source/libs/freetype/freetype-1.5/lib/arch/beos/ttmutex.c
blob: 2df23d34161977e2cdb856f57165ea51f1c89fab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************
 *
 *  ttmutex.c                                                1.0
 *
 *    Mutual exclusion object, BeOS implementation
 *
 *  Copyright 1999-2001 by
 *  Catharon Productions, Inc.
 *
 *  This file is part of the FreeType project, and may only be used
 *  modified and distributed under the terms of the FreeType project
 *  license, LICENSE.TXT.  By continuing to use, modify, or distribute
 *  this file you indicate that you have read the license and
 *  understand and accept it fully.
 *
 ******************************************************************/

#include "ttmutex.h"

#include <kernel/OS.h>
#include <support/SupportDefs.h>
#include <stdio.h>

/* required by the tracing mode */
#undef  TT_COMPONENT
#define TT_COMPONENT  trace_mutex


#ifdef TT_CONFIG_OPTION_THREAD_SAFE

/* New and improved BeOS mutex in the form of a "benaphore". */

typedef struct
{
  sem_id  _sem;
  int32   _atom;
} beos_mutex_t;

/* Counter so the semaphores have unique names; access this with */
/* atomic_*() functions only!                                    */

static int32  _mutex_counter = 0;

/* Allocate and initialize the mutex. */

FT_INTERNAL_FUNC( void )
TT_Mutex_Create( TMutex*  mutex )
{
  beos_mutex_t*  m = NULL;


  m = (beos_mutex_t *)malloc( sizeof( beos_mutex_t ) );
  if ( m )
  {
    char   mutex_name[B_OS_NAME_LENGTH];
    int32  prev = atomic_add( &_mutex_counter, 1 );


    sprintf( mutex_name, "FreeType mutex (%d)", (int)prev );

    m->_atom = 0;
    m->_sem  = create_sem( 0, mutex_name );
    
    if ( m->_sem < B_NO_ERROR )
    {
      /* Can't get a semaphore, so we're screwed. */
      free( m );
      m = NULL;
    }
  }

  /* Send back the new mutex. */
  *mutex = (TMutex)m;
}


/* Delete a mutex. */

FT_INTERNAL_FUNC( void )
TT_Mutex_Delete( TMutex*  mutex )
{
  if ( mutex )
  {
    beos_mutex_t*  m = (beos_mutex_t *)*mutex;

    
    if ( m->_sem >= B_NO_ERROR )
    {
      status_t  retval = delete_sem( m->_sem );

      
      /* Make the compiler shut up... */
      retval = retval;
    }

    free( m );
  }
}


/* Attempt to lock a mutex; blocks until we can lock it or the semaphore */
/* is destroyed.                                                         */

FT_INTERNAL_FUNC( void )
TT_Mutex_Lock( TMutex*  mutex )
{
  int32          prev  = 0;
  beos_mutex_t*  m     = (beos_mutex_t *)*mutex;


  if ( !mutex )
    return;

  prev = atomic_add( &(m->_atom), 1 );
  if ( prev > 0 )
  {
    status_t  retval = acquire_sem( m->_sem );

      
    /* Make the compiler shut up... */
    retval = retval;
  }
}


/* Release a mutex. */

FT_INTERNAL_FUNC( void )
TT_Mutex_Release( TMutex*  mutex )
{
  int32          prev = 0;
  beos_mutex_t*  m    = (beos_mutex_t *)*mutex;


  if ( !mutex )
    return;

  prev = atomic_add( &(m->_atom), -1 );
  if ( prev > 1 ) {
    status_t  retval = release_sem( m->_sem );
      
    /* Make the compiler shut up... */
    retval = retval;
  }
}

#endif /* TT_CONFIG_OPTION_THREAD_SAFE */


/* END */