Subversion Repositories Kolibri OS

Rev

Rev 6520 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6520 Rev 6527
Line 18... Line 18...
18
You should have received a copy of the GNU General Public License and
18
You should have received a copy of the GNU General Public License and
19
a copy of the GCC Runtime Library Exception along with this program;
19
a copy of the GCC Runtime Library Exception along with this program;
20
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
20
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
21
.  */
21
.  */
Line -... Line 22...
-
 
22
 
22
 
23
#include 
Line 23... Line 24...
23
#include "gthr-kos32.h"
24
#include "gthr-kos32.h"
24
 
25
 
25
#define FUTEX_INIT      0
26
#define FUTEX_INIT      0
26
#define FUTEX_DESTROY   1
27
#define FUTEX_DESTROY   1
Line 27... Line -...
27
#define FUTEX_WAIT      2
-
 
28
#define FUTEX_WAKE      3
-
 
29
 
-
 
30
unsigned int tls_alloc(void);
-
 
31
int tls_free(unsigned int key);
-
 
32
void *tls_get(unsigned int key);
28
#define FUTEX_WAIT      2
33
void *tls_set(unsigned int key, void *val);
29
#define FUTEX_WAKE      3
Line 34... Line 30...
34
 
30
 
35
#define exchange_acquire(ptr, new) \
31
#define exchange_acquire(ptr, new) \
Line 36... Line -...
36
  __atomic_exchange_4((ptr), (new), __ATOMIC_ACQUIRE)
-
 
37
 
-
 
38
#define exchange_release(ptr, new) \
-
 
39
  __atomic_exchange_4((ptr), (new), __ATOMIC_RELEASE)
-
 
40
 
-
 
41
 
-
 
42
static inline void yield(void)
-
 
43
{
-
 
44
    __asm__ __volatile__(
-
 
45
    "int $0x40"
-
 
46
    ::"a"(68), "b"(1));
32
  __atomic_exchange_4((ptr), (new), __ATOMIC_ACQUIRE)
47
};
33
 
48
 
34
#define exchange_release(ptr, new) \
49
 
35
  __atomic_exchange_4((ptr), (new), __ATOMIC_RELEASE)
Line 93... Line 79...
93
    return tls_free(key);
79
    return tls_free(key);
94
}
80
}
Line 95... Line 81...
95
 
81
 
96
void* __gthr_kos32_getspecific (__gthread_key_t key)
82
void* __gthr_kos32_getspecific (__gthread_key_t key)
97
{
-
 
98
    void *ptr;
83
{
99
    ptr = tls_get(key);
-
 
100
    return ptr;
84
    return tls_get(key);
Line 101... Line 85...
101
}
85
}
102
 
86
 
103
int __gthr_kos32_setspecific (__gthread_key_t key, const void *ptr)
87
int __gthr_kos32_setspecific (__gthread_key_t key, const void *ptr)
104
{
-
 
105
    tls_set(key, CONST_CAST2(void *, const void *, ptr));
88
{
Line 106... Line 89...
106
    return 0;
89
    return tls_set(key, CONST_CAST2(void *, const void *, ptr));
107
}
90
}
108
 
91
 
Line 149... Line 132...
149
 
132
 
150
int __gthr_kos32_mutex_trylock (__gthread_mutex_t *mutex)
133
int __gthr_kos32_mutex_trylock (__gthread_mutex_t *mutex)
151
{
134
{
Line 152... Line 135...
152
    int zero = 0;
135
    int zero = 0;
153
 
136
 
Line 154... Line 137...
154
    return __atomic_compare_exchange_4(&mutex->lock, &zero, 1,0,__ATOMIC_ACQUIRE,__ATOMIC_RELAXED);
137
    return !__atomic_compare_exchange_4(&mutex->lock, &zero, 1,0,__ATOMIC_ACQUIRE,__ATOMIC_RELAXED);
155
}
138
}
156
 
139
 
Line 171... Line 154...
171
    return 0;
154
    return 0;
172
}
155
}
Line 173... Line 156...
173
 
156
 
174
void __gthr_kos32_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
157
void __gthr_kos32_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
-
 
158
{
-
 
159
    int handle;
175
{
160
 
-
 
161
    mutex->lock = 0;
-
 
162
 
-
 
163
    __asm__ volatile(
-
 
164
    "int $0x40\t"
-
 
165
    :"=a"(handle)
-
 
166
    :"a"(77),"b"(FUTEX_INIT),"c"(mutex));
-
 
167
    mutex->handle = handle;
176
//  mutex->counter = -1;
168
 
177
  mutex->depth = 0;
169
    mutex->depth = 0;
178
  mutex->owner = 0;
-
 
179
//  mutex->sema = CreateSemaphoreW (NULL, 0, 65535, NULL);
170
    mutex->owner = 0;
Line 180... Line -...
180
}
-
 
181
 
-
 
182
#if 0
171
}
183
int
172
 
-
 
173
int __gthr_kos32_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
-
 
174
{
184
__gthr_win32_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
175
    int tmp;
-
 
176
 
185
{
177
    unsigned long me = (unsigned long)tls_get(TLS_KEY_LOW_STACK);
186
  DWORD me = GetCurrentThreadId();
178
 
187
  if (InterlockedIncrement (&mutex->counter) == 0)
179
    if( __sync_fetch_and_add(&mutex->lock, 1) == 0)
188
    {
180
    {
-
 
181
        mutex->depth = 1;
189
      mutex->depth = 1;
182
        mutex->owner = me;
190
      mutex->owner = me;
183
        return 0;
191
    }
184
    }
192
  else if (mutex->owner == me)
185
    else if (mutex->owner == me)
193
    {
186
    {
194
      InterlockedDecrement (&mutex->counter);
187
        __sync_fetch_and_sub(&mutex->lock, 1);
195
      ++(mutex->depth);
188
        ++(mutex->depth);
196
    }
189
    }
-
 
190
    else while (exchange_acquire (&mutex->lock, 2) != 0)
-
 
191
    {
-
 
192
        __asm__ volatile(
-
 
193
        "int $0x40\t\n"
-
 
194
        :"=a"(tmp)
197
  else if (WaitForSingleObject (mutex->sema, INFINITE) == WAIT_OBJECT_0)
195
        :"a"(77),"b"(FUTEX_WAIT),
198
    {
196
        "c"(mutex->handle),"d"(2),"S"(0));
199
      mutex->depth = 1;
197
        mutex->depth = 1;
200
      mutex->owner = me;
-
 
201
    }
-
 
202
  else
-
 
203
    {
-
 
204
      /* WaitForSingleObject returns WAIT_FAILED, and we can only do
-
 
205
         some best-effort cleanup here.  */
-
 
206
      InterlockedDecrement (&mutex->counter);
198
        mutex->owner = me;
207
      return 1;
199
    };
208
    }
200
 
Line 209... Line -...
209
  return 0;
-
 
210
}
201
    return 0;
211
 
202
}
-
 
203
 
212
int
204
int __gthr_kos32_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
-
 
205
{
213
__gthr_win32_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
206
    unsigned long me = (unsigned long)tls_get(TLS_KEY_LOW_STACK);
214
{
207
    int zero = 0;
215
  DWORD me = GetCurrentThreadId();
208
 
216
  if (__GTHR_W32_InterlockedCompareExchange (&mutex->counter, 0, -1) < 0)
209
    if(__atomic_compare_exchange_4(&mutex->lock, &zero, 1,0,__ATOMIC_ACQUIRE,__ATOMIC_RELAXED))
217
    {
210
    {
218
      mutex->depth = 1;
211
        mutex->depth = 1;
Line 224... Line 217...
224
    return 1;
217
        return 1;
Line 225... Line 218...
225
 
218
 
226
  return 0;
219
    return 0;
Line 227... Line -...
227
}
-
 
228
 
220
}
229
int
221
 
230
__gthr_win32_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
222
int __gthr_kos32_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
-
 
223
{
231
{
224
    --(mutex->depth);
232
  --(mutex->depth);
225
 
233
  if (mutex->depth == 0)
226
    if (mutex->depth == 0)
Line 234... Line 227...
234
    {
227
    {
-
 
228
        int prev;
-
 
229
 
-
 
230
        prev = exchange_release (&mutex->lock, 0);
-
 
231
 
-
 
232
        if (prev != 1)
-
 
233
        {
-
 
234
            __asm__ volatile(
235
      mutex->owner = 0;
235
            "int $0x40\t"
-
 
236
            :"=a"(prev)
-
 
237
            :"a"(77),"b"(FUTEX_WAKE),
236
 
238
            "c"(mutex->handle),"d"(1));
Line 237... Line 239...
237
      if (InterlockedDecrement (&mutex->counter) >= 0)
239
        };
238
	return ReleaseSemaphore (mutex->sema, 1, NULL) ? 0 : 1;
240
        mutex->owner = 0;
Line 239... Line -...
239
    }
-
 
240
 
241
    };
241
  return 0;
242
 
-
 
243
    return 0;
-
 
244
}
-
 
245
 
-
 
246
int __gthr_kos32_recursive_mutex_destroy (__gthread_recursive_mutex_t *mutex)
-
 
247
{
242
}
248
    int retval;
-
 
249
 
243
 
250
    __asm__ volatile(
244
int
251
    "int $0x40\t"
Line 245... Line -...
245
__gthr_win32_recursive_mutex_destroy (__gthread_recursive_mutex_t *mutex)
-