comparison lphobos/internal/critical.d @ 473:373489eeaf90

Applied downs' lphobos update
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Mon, 04 Aug 2008 19:28:49 +0200
parents
children
comparison
equal deleted inserted replaced
472:15c804b6ce77 473:373489eeaf90
1 module internal.critical;
2 extern(C):
3
4 import std.c.linux.linux, std.c.stdlib:ccalloc=calloc, cmalloc=malloc, cfree=free;
5
6 struct CritSec {
7 pthread_mutex_t* p;
8 }
9
10 const PTHREAD_MUTEX_RECURSIVE = 1, PTHREAD_MUTEX_ERRORCHECK=2;
11
12 extern(C) int pthread_self();
13
14 void _d_criticalenter(CritSec* cs) {
15 if (!cs.p) {
16 auto newp = cast(pthread_mutex_t*) cmalloc(pthread_mutex_t.sizeof);
17 auto cspp = &cs.p;
18 pthread_mutexattr_t mt; pthread_mutexattr_init(&mt);
19 pthread_mutexattr_settype(&mt, PTHREAD_MUTEX_RECURSIVE);
20 printf("Create -> %i\n", pthread_mutex_init(newp, &mt));
21 asm { xor EAX, EAX; mov ECX, newp; mov EDX, cspp; lock; cmpxchg int ptr [EDX], ECX; }
22 if (cs.p != newp) pthread_mutex_destroy(newp);
23 }
24 auto count = (cast(uint*) cs.p)[1];
25 // printf("%i ::%u\n", pthread_self(), count);
26 //printf("%i: Lock %p -> %i\n", pthread_self(), cs.p,
27 pthread_mutex_lock(cs.p);//);
28 }
29
30 void _d_criticalexit(CritSec* cs) {
31 //printf("%i: Unlock %p -> %i\n", pthread_self(), cs.p,
32 pthread_mutex_unlock(cs.p);//);
33 }
34
35 void _d_monitorenter(Object h)
36 {
37 _d_criticalenter(cast(CritSec*) &h.__monitor);
38 }
39
40 void _d_monitorexit(Object h)
41 {
42 _d_criticalexit(cast(CritSec*) &h.__monitor);
43 }
44
45 void _STI_monitor_staticctor() { }
46 void _STI_critical_init() { }
47 void _STI_critical_term() { }
48 void _STD_monitor_staticdtor() { }
49 void _STD_critical_term() { }