File: sync.m

package info (click to toggle)
gnustep-base 1.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 27,260 kB
  • sloc: objc: 204,878; ansic: 32,160; sh: 157; makefile: 128; cpp: 122; xml: 32
file content (229 lines) | stat: -rw-r--r-- 4,926 bytes parent folder | download | duplicates (4)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* 
   The implementation of synchronization primitives for Objective-C.
   Copyright (C) 2008 Free Software Foundation, Inc.

   Gregory Casamento <greg.casamento@gmail.com>

   This file is part of the GNUstep Base Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02111 USA.
*/

#include <stdlib.h>
#include "objc/objc.h"
#include "objc/objc-api.h"
#include "objc/thr.h"

/*
 * Node structure...
 */
typedef struct lock_node {
  id obj;
  objc_mutex_t lock;
  struct lock_node *next;
  struct lock_node *prev;
} lock_node_t;

/*
 * Return types for the locks...
 */
typedef enum { 
  OBJC_SYNC_SUCCESS = 0,
  OBJC_SYNC_NOT_OWNING_THREAD_ERROR = -1,
  OBJC_SYNC_TIMED_OUT = -2,
  OBJC_SYNC_NOT_INITIALIZED = -3		
} sync_return_t;

static lock_node_t *lock_list = NULL;
static objc_mutex_t table_lock = NULL; 

/**
 * Initialize the table lock.
 */
static void
sync_lock_init()
{
  if (table_lock == NULL)
    {
      table_lock = objc_mutex_allocate();
    }
}

/**
 * Find the node in the list.
 */
static lock_node_t*
sync_find_node(id obj)
{
  lock_node_t *current = lock_list;

  if (lock_list != NULL)
    {
      // iterate over the list looking for the end...
      while (current != NULL)
	{
	  // if the current object is the one, breal and
	  // return that node.
	  if (current->obj == obj)
	    {
	      break;
	    }

	  // get the next one...
	  current = current->next;
	}
    }
  return current;
}

/**
 * Add a node for the object, if one doesn't already exist.
 */
static lock_node_t*
sync_add_node(id obj)
{
  lock_node_t *current = NULL;

  // get the lock...
  sync_lock_init();
  
  // if the list hasn't been initialized, initialize it.
  if (lock_list == NULL)
    {
      // instantiate the new node and set the list...
      lock_list = malloc(sizeof(lock_node_t));

      // set the current node to the last in the list...
      current = lock_list;

      // set next and prev...
      current->prev = NULL;
      current->next = NULL;
    }
  else 
    {
      lock_node_t *new_node = NULL;
      current = lock_list;

      // look for the end of the list.
      while (current->next)
	{
	  current = current->next;
	}

      // instantiate the new node...
      new_node = malloc(sizeof(lock_node_t));

      if (new_node != NULL)
	{
	  // set next and prev...
	  current->next = new_node;
	  new_node->prev = current;
	  new_node->next = NULL;
	  
	  // set the current node to the last in the list...
	  current = new_node;
	}
    }

  if (current != NULL)
    {
      // add the object and it's lock
      current->obj = obj;
      current->lock = objc_mutex_allocate();
    }

  return current;
}

/**
 * Add a lock for the object.
 */ 
int
objc_sync_enter(id obj)
{
  lock_node_t *node = NULL;
  int status = 0;

  // lock access to the table until we're done....
  sync_lock_init();
  objc_mutex_lock(table_lock);

  node = sync_find_node(obj);
  if (node == NULL)
    {
      node = sync_add_node(obj);
      if (node == NULL)
	{
	  // unlock the table....
	  objc_mutex_unlock(table_lock);  
	  return OBJC_SYNC_NOT_INITIALIZED;
	}
    }

  // unlock the table....
  objc_mutex_unlock(table_lock);  

  status = objc_mutex_lock(node->lock);

  // if the status is more than one, then another thread
  // has this section locked, so we abort.  A status of -1
  // indicates that an error occurred.
  if (status > 1 || status == -1)
    {
      return OBJC_SYNC_NOT_OWNING_THREAD_ERROR;
    }

  return OBJC_SYNC_SUCCESS;
}

/**
 * Remove a lock for the object.
 */
int
objc_sync_exit(id obj)
{
  lock_node_t *node = NULL;
  int status = 0;

  // lock access to the table until we're done....
  sync_lock_init();
  objc_mutex_lock(table_lock);

  node = sync_find_node(obj);
  if (node == NULL)
    {
      // unlock the table....
      objc_mutex_unlock(table_lock);  
      return OBJC_SYNC_NOT_INITIALIZED;
    }

  status = objc_mutex_unlock(node->lock);

  // unlock the table....
  objc_mutex_unlock(table_lock);  

  // if the status is not zero, then we are not the sole
  // owner of this node.  Also if -1 is returned, this indicates and error
  // condition.
  if (status > 0 || status == -1)
    {
      return OBJC_SYNC_NOT_OWNING_THREAD_ERROR;      
    }
 
  return OBJC_SYNC_SUCCESS;  
}