Subversion Repositories Kolibri OS

Rev

Rev 6540 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6540 Rev 9874
Line 19... Line 19...
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 22... Line 22...
22
 
22
 
-
 
23
#include 
23
#include 
24
#include 
24
#include 
25
#include 
Line 25... Line 26...
25
#include "gthr-kos32.h"
26
#include "gthr-kos32.h"
26
 
27
 
27
#define FUTEX_INIT      0
28
#define FUTEX_INIT      0
Line 33... Line 34...
33
  __atomic_exchange_4((ptr), (new), __ATOMIC_ACQUIRE)
34
  __atomic_exchange_4((ptr), (new), __ATOMIC_ACQUIRE)
Line 34... Line 35...
34
 
35
 
35
#define exchange_release(ptr, new) \
36
#define exchange_release(ptr, new) \
Line -... Line 37...
-
 
37
  __atomic_exchange_4((ptr), (new), __ATOMIC_RELEASE)
-
 
38
 
-
 
39
#define TLS_KEY_PID         0
-
 
40
#define TLS_KEY_TID         4
-
 
41
#define TLS_KEY_LOW_STACK   8
-
 
42
#define TLS_KEY_HIGH_STACK 12
-
 
43
#define TLS_KEY_LIBC       16
-
 
44
 
-
 
45
unsigned int tls_alloc(void);
-
 
46
int tls_free(unsigned int key);
-
 
47
 
-
 
48
static inline int tls_set(unsigned int key, void *val)
-
 
49
{
-
 
50
    int ret = -1;
-
 
51
    if(key < 4096)
-
 
52
    {
-
 
53
        __asm__ __volatile__(
-
 
54
        "movl %0, %%fs:(%1)"
-
 
55
        ::"r"(val),"r"(key));
-
 
56
        ret = 0;
-
 
57
    }
-
 
58
    return ret;
-
 
59
};
-
 
60
 
-
 
61
static inline void *tls_get(unsigned int key)
-
 
62
{
-
 
63
    void *val = (void*)-1;
-
 
64
    if(key < 4096)
-
 
65
    {
-
 
66
        __asm__ __volatile__(
-
 
67
        "movl %%fs:(%1), %0"
-
 
68
        :"=r"(val)
-
 
69
        :"r"(key));
-
 
70
    };
-
 
71
    return val;
-
 
72
}
36
  __atomic_exchange_4((ptr), (new), __ATOMIC_RELEASE)
73
 
37
 
74
 
38
int __gthr_kos32_once (__gthread_once_t *once, void (*func) (void))
75
int __gthr_kos32_once (__gthread_once_t *once, void (*func) (void))
39
{
76
{
Line 48... Line 85...
48
            once->done = 1;
85
            once->done = 1;
49
        }
86
        }
50
        else
87
        else
51
        {
88
        {
52
            while (! once->done)
89
            while (! once->done)
53
                yield();
90
                _ksys_thread_yield();
54
        }
91
        }
55
    }
92
    }
56
    return 0;
93
    return 0;
57
};
94
};
Line 90... Line 127...
90
    return tls_set(key, CONST_CAST2(void *, const void *, ptr));
127
    return tls_set(key, CONST_CAST2(void *, const void *, ptr));
91
}
128
}
Line 92... Line 129...
92
 
129
 
93
void __gthr_kos32_mutex_init_function (__gthread_mutex_t *mutex)
130
void __gthr_kos32_mutex_init_function (__gthread_mutex_t *mutex)
94
{
-
 
95
    int handle;
-
 
96
 
131
{
97
    mutex->lock = 0;
-
 
98
 
-
 
99
    __asm__ volatile(
-
 
100
    "int $0x40\t"
-
 
101
    :"=a"(handle)
-
 
102
    :"a"(77),"b"(FUTEX_INIT),"c"(mutex));
132
    mutex->lock = 0;
103
    mutex->handle = handle;
133
    mutex->handle  = _ksys_futex_create(mutex);
Line 104... Line 134...
104
}
134
}
105
 
135
 
106
void __gthr_kos32_mutex_destroy (__gthread_mutex_t *mutex)
136
void __gthr_kos32_mutex_destroy (__gthread_mutex_t *mutex)
107
{
-
 
108
    int retval;
-
 
109
 
-
 
110
    __asm__ volatile(
-
 
111
    "int $0x40\t"
137
{
112
    :"=a"(retval)
138
    int retval;
Line 113... Line 139...
113
    :"a"(77),"b"(FUTEX_DESTROY),"c"(mutex->handle));
139
    _ksys_futex_destroy(mutex->handle);
114
}
140
}
115
 
141
 
Line 120... Line 146...
120
    if( __sync_fetch_and_add(&mutex->lock, 1) == 0)
146
    if( __sync_fetch_and_add(&mutex->lock, 1) == 0)
121
        return 0;
147
        return 0;
Line 122... Line 148...
122
 
148
 
123
    while (exchange_acquire (&mutex->lock, 2) != 0)
149
    while (exchange_acquire (&mutex->lock, 2) != 0)
124
    {
-
 
125
        __asm__ volatile(
-
 
126
        "int $0x40\t\n"
-
 
127
        :"=a"(tmp)
-
 
128
        :"a"(77),"b"(FUTEX_WAIT),
150
    {
129
        "c"(mutex->handle),"d"(2),"S"(0));
151
        _ksys_futex_wait(mutex->handle, 2 , 0);
130
   }
152
    }
131
   return 0;
153
    return 0;
Line 132... Line 154...
132
}
154
}
Line 144... Line 166...
144
 
166
 
Line 145... Line 167...
145
    prev = exchange_release (&mutex->lock, 0);
167
    prev = exchange_release (&mutex->lock, 0);
146
 
168
 
147
    if (prev != 1)
-
 
148
    {
-
 
149
        __asm__ volatile(
-
 
150
        "int $0x40\t"
-
 
151
        :"=a"(prev)
169
    if (prev != 1)
152
        :"a"(77),"b"(FUTEX_WAKE),
170
    {
153
        "c"(mutex->handle),"d"(1));
171
        _ksys_futex_wake(mutex->handle, 1);
154
    };
172
    };
Line 155... Line 173...
155
    return 0;
173
    return 0;
156
}
174
}
157
 
175
 
158
void __gthr_kos32_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
-
 
159
{
176
void __gthr_kos32_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
Line 160... Line -...
160
    int handle;
-
 
161
 
-
 
162
    mutex->lock = 0;
-
 
163
 
-
 
164
    __asm__ volatile(
177
{
Line 165... Line 178...
165
    "int $0x40\t"
178
    int handle;
166
    :"=a"(handle)
179
    mutex->lock = 0;
167
    :"a"(77),"b"(FUTEX_INIT),"c"(mutex));
180
 
Line 188... Line 201...
188
        __sync_fetch_and_sub(&mutex->lock, 1);
201
        __sync_fetch_and_sub(&mutex->lock, 1);
189
        ++(mutex->depth);
202
        ++(mutex->depth);
190
    }
203
    }
191
    else while (exchange_acquire (&mutex->lock, 2) != 0)
204
    else while (exchange_acquire (&mutex->lock, 2) != 0)
192
    {
205
    {
193
        __asm__ volatile(
-
 
194
        "int $0x40\t\n"
-
 
195
        :"=a"(tmp)
-
 
196
        :"a"(77),"b"(FUTEX_WAIT),
-
 
197
        "c"(mutex->handle),"d"(2),"S"(0));
206
        _ksys_futex_wait(mutex->handle, 2, 0);
198
        mutex->depth = 1;
207
        mutex->depth = 1;
199
        mutex->owner = me;
208
        mutex->owner = me;
200
    };
209
    };
Line 201... Line 210...
201
 
210
 
Line 230... Line 239...
230
 
239
 
Line 231... Line 240...
231
        prev = exchange_release (&mutex->lock, 0);
240
        prev = exchange_release (&mutex->lock, 0);
232
 
241
 
233
        if (prev != 1)
-
 
234
        {
-
 
235
            __asm__ volatile(
-
 
236
            "int $0x40\t"
-
 
237
            :"=a"(prev)
242
        if (prev != 1)
238
            :"a"(77),"b"(FUTEX_WAKE),
243
        {
239
            "c"(mutex->handle),"d"(1));
244
            _ksys_futex_wake(mutex->handle, 1);
240
        };
245
        };
Line 241... Line 246...
241
        mutex->owner = 0;
246
        mutex->owner = 0;
242
    };
247
    };
Line 243... Line 248...
243
 
248
 
244
    return 0;
249
    return 0;
245
}
250
}
246
 
-
 
247
int __gthr_kos32_recursive_mutex_destroy (__gthread_recursive_mutex_t *mutex)
-
 
248
{
-
 
249
    int retval;
-
 
250
 
251
 
251
    __asm__ volatile(
-
 
252
    "int $0x40\t"
252
int __gthr_kos32_recursive_mutex_destroy (__gthread_recursive_mutex_t *mutex)
253
    :"=a"(retval)
253
{