diff --git a/libs/pthread.go b/libs/pthread.go index 4d4dab6..9aa7784 100644 --- a/libs/pthread.go +++ b/libs/pthread.go @@ -19,11 +19,6 @@ func init() { onceT := types.NamedTGo("pthread_once_t", "sync.Once", c.MethStructT(map[string]*types.FuncType{ "Do": c.FuncTT(nil, c.FuncTT(nil)), })) - threadT := types.NamedTGo("pthread_t", "pthread.Thread", c.MethStructT(map[string]*types.FuncType{ - "Join": c.FuncTT(intT, c.PtrT(retT)), - "TimedJoinNP": c.FuncTT(intT, c.PtrT(retT), c.PtrT(timespecT)), - })) - threadAttrT := types.NamedTGo("pthread_attr_t", "pthread.Attr", c.MethStructT(map[string]*types.FuncType{})) mutexAttrT := types.NamedTGo("pthread_mutexattr_t", "pthread.MutexAttr", c.MethStructT(map[string]*types.FuncType{ "Init": c.FuncTT(intT), "SetType": c.FuncTT(intT, intT), @@ -32,11 +27,23 @@ func init() { mutexT := types.NamedTGo("pthread_mutex_t", "pthread.Mutex", c.MethStructT(map[string]*types.FuncType{ "Init": c.FuncTT(intT, c.PtrT(mutexAttrT)), "Destroy": c.FuncTT(intT), - "Lock": c.FuncTT(intT), + "CLock": c.FuncTT(intT), "TryLock": c.FuncTT(intT), "TimedLock": c.FuncTT(intT, c.PtrT(timespecT)), - "Unlock": c.FuncTT(intT), + "CUnlock": c.FuncTT(intT), + })) + condAttrT := types.NamedTGo("pthread_condattr_t", "pthread.CondAttr", types.StructT(nil)) + condT := types.NamedTGo("pthread_cond_t", "sync.Cond", types.StructT([]*types.Field{ + {Name: types.NewIdent("L", c.PtrT(mutexT))}, + {Name: types.NewIdent("Wait", c.FuncTT(nil))}, + {Name: types.NewIdent("Signal", c.FuncTT(nil))}, + {Name: types.NewIdent("Broadcast", c.FuncTT(nil))}, })) + threadT := types.NamedTGo("pthread_t", "pthread.Thread", c.MethStructT(map[string]*types.FuncType{ + "Join": c.FuncTT(intT, c.PtrT(retT)), + "TimedJoinNP": c.FuncTT(intT, c.PtrT(retT), c.PtrT(timespecT)), + })) + threadAttrT := types.NamedTGo("pthread_attr_t", "pthread.Attr", c.MethStructT(map[string]*types.FuncType{})) return &Library{ Imports: map[string]string{ "sync": "sync", @@ -46,6 +53,8 @@ func init() { "pthread_t_": threadT, "pthread_t": c.PtrT(threadT), "pthread_once_t": onceT, + "pthread_cond_t": condT, + "pthread_condattr_t": condAttrT, "pthread_attr_t": threadAttrT, "pthread_mutex_t": mutexT, "pthread_mutexattr_t": mutexAttrT, @@ -53,6 +62,8 @@ func init() { Idents: map[string]*types.Ident{ "PTHREAD_MUTEX_RECURSIVE": c.NewIdent("PTHREAD_MUTEX_RECURSIVE", "pthread.MUTEX_RECURSIVE", pthread.MUTEX_RECURSIVE, gintT), "pthread_create": c.NewIdent("pthread_create", "pthread.Create", pthread.Create, c.FuncTT(intT, c.PtrT(c.PtrT(threadT)), c.PtrT(threadAttrT), c.FuncTT(retT, argT), argT)), + "pthread_cond_init": c.NewIdent("pthread_cond_init", "pthread.CondInit", pthread.CondInit, c.FuncTT(intT, c.PtrT(condT), c.PtrT(condAttrT))), + "pthread_cond_destroy": c.NewIdent("pthread_cond_destroy", "pthread.CondFree", pthread.CondFree, c.FuncTT(intT, c.PtrT(condT))), }, // TODO Header: ` @@ -61,6 +72,8 @@ func init() { const _cxgo_go_int PTHREAD_MUTEX_RECURSIVE = 1; +typedef struct pthread_mutex_t pthread_mutex_t; + typedef struct pthread_attr_t {} pthread_attr_t; typedef struct { @@ -69,17 +82,23 @@ typedef struct { #define PTHREAD_ONCE_INIT {0} #define pthread_once(o,f) (o)->Do(f) -typedef struct {} pthread_cond_t; +typedef struct { + pthread_mutex_t* L; + void (*Wait)(void); + void (*Signal)(void); + void (*Broadcast)(void); +} pthread_cond_t; typedef struct {} pthread_condattr_t; int pthread_cond_destroy(pthread_cond_t *cond); int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t * attr); -int pthread_cond_broadcast(pthread_cond_t *cond); -int pthread_cond_signal(pthread_cond_t *cond); +#define pthread_cond_broadcast(c) (c)->Broadcast() +#define pthread_cond_signal(c) (c)->Signal() #define PTHREAD_COND_INITIALIZER {0} typedef struct {} pthread_mutex_t; int pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, const struct timespec * abstime); -int pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex); +//int pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex); +#define pthread_cond_wait(c,m) {(c)->L = m; (c)->Wait();} typedef struct{ _cxgo_sint32 (*Join)(void **retval); @@ -101,16 +120,16 @@ typedef struct pthread_mutexattr_t { typedef struct pthread_mutex_t { _cxgo_sint32 (*Destroy)(void); _cxgo_sint32 (*Init)(const pthread_mutexattr_t *restrict attr); - _cxgo_sint32 (*Lock)(void); + _cxgo_sint32 (*CLock)(void); _cxgo_sint32 (*TryLock)(void); - _cxgo_sint32 (*Unlock)(void); + _cxgo_sint32 (*CUnlock)(void); _cxgo_sint32 (*TimedLock)(const struct timespec *restrict abstime); } pthread_mutex_t; #define pthread_mutex_destroy(mutex) ((pthread_mutex_t*)mutex)->Destroy() #define pthread_mutex_init(mutex, attr) ((pthread_mutex_t*)mutex)->Init(attr) -#define pthread_mutex_lock(mutex) ((pthread_mutex_t*)mutex)->Lock() +#define pthread_mutex_lock(mutex) ((pthread_mutex_t*)mutex)->CLock() #define pthread_mutex_trylock(mutex) ((pthread_mutex_t*)mutex)->TryLock() -#define pthread_mutex_unlock(mutex) ((pthread_mutex_t*)mutex)->Unlock() +#define pthread_mutex_unlock(mutex) ((pthread_mutex_t*)mutex)->CUnlock() #define pthread_mutex_timedlock(mutex, abstime) ((pthread_mutex_t*)mutex)->TimedLock(abstime) #define pthread_join(thread, retval) ((pthread_t_*)thread)->Join(retval) diff --git a/libs/types.go b/libs/types.go index 5f5223c..7e43776 100644 --- a/libs/types.go +++ b/libs/types.go @@ -128,6 +128,12 @@ func (c *Env) checkType(t1 reflect.Type, t2 types.Type) error { } } return nil + case reflect.Struct: + _, ok := types.Unwrap(t2).(*types.StructType) + if !ok { + return fmt.Errorf("expected struct, got: %T", t2) + } + return nil default: return fmt.Errorf("unsupported type: %v (%v)", t1, t1.Kind()) } diff --git a/runtime/pthread/pthread.go b/runtime/pthread/pthread.go index 397ba58..7998044 100644 --- a/runtime/pthread/pthread.go +++ b/runtime/pthread/pthread.go @@ -49,24 +49,24 @@ func (m *MutexAttr) Destroy() int32 { } type Mutex struct { - mu *sync.Mutex + *sync.Mutex } func (m *Mutex) Init(attr *MutexAttr) int32 { if attr.typ == MUTEX_RECURSIVE { // FIXME } - m.mu = new(sync.Mutex) + m.Mutex = new(sync.Mutex) return 0 } func (m *Mutex) Destroy() int32 { - m.mu = nil + m.Mutex = nil return 0 } -func (m *Mutex) Lock() int32 { - m.mu.Lock() +func (m *Mutex) CLock() int32 { + m.Mutex.Lock() return 0 } @@ -78,7 +78,23 @@ func (m *Mutex) TimedLock(t *libc.TimeSpec) int32 { panic("TODO") } -func (m *Mutex) Unlock() int32 { - m.mu.Unlock() +func (m *Mutex) CUnlock() int32 { + m.Mutex.Unlock() + return 0 +} + +type CondAttr struct { + // FIXME +} + +func CondInit(c *sync.Cond, attr *CondAttr) int32 { + if attr != nil { + panic("TODO") + } + *c = *sync.NewCond(nil) + return 0 +} + +func CondFree(c *sync.Cond) int32 { return 0 }