github.com/gotranspile/cxgo@v0.3.8-0.20240118201721-29871598a6a2/libs/pthread.go (about)

     1  package libs
     2  
     3  import (
     4  	"github.com/gotranspile/cxgo/runtime/pthread"
     5  	"github.com/gotranspile/cxgo/types"
     6  )
     7  
     8  const (
     9  	pthreadH = "pthread.h"
    10  )
    11  
    12  func init() {
    13  	RegisterLibrary(pthreadH, func(c *Env) *Library {
    14  		gintT := c.Go().Int()
    15  		intT := types.IntT(4)
    16  		argT := c.PtrT(nil)
    17  		retT := c.PtrT(nil)
    18  		timespecT := c.GetLibraryType(timeH, "timespec")
    19  		onceT := types.NamedTGo("pthread_once_t", "sync.Once", c.MethStructT(map[string]*types.FuncType{
    20  			"Do": c.FuncTT(nil, c.FuncTT(nil)),
    21  		}))
    22  		mutexAttrT := types.NamedTGo("pthread_mutexattr_t", "pthread.MutexAttr", c.MethStructT(map[string]*types.FuncType{
    23  			"Init":    c.FuncTT(intT),
    24  			"SetType": c.FuncTT(intT, intT),
    25  			"Destroy": c.FuncTT(intT),
    26  		}))
    27  		mutexT := types.NamedTGo("pthread_mutex_t", "pthread.Mutex", c.MethStructT(map[string]*types.FuncType{
    28  			"Init":      c.FuncTT(intT, c.PtrT(mutexAttrT)),
    29  			"Destroy":   c.FuncTT(intT),
    30  			"CLock":     c.FuncTT(intT),
    31  			"TryLock":   c.FuncTT(intT),
    32  			"TimedLock": c.FuncTT(intT, c.PtrT(timespecT)),
    33  			"CUnlock":   c.FuncTT(intT),
    34  		}))
    35  		condAttrT := types.NamedTGo("pthread_condattr_t", "pthread.CondAttr", types.StructT(nil))
    36  		condT := types.NamedTGo("pthread_cond_t", "sync.Cond", types.StructT([]*types.Field{
    37  			{Name: types.NewIdent("L", c.PtrT(mutexT))},
    38  			{Name: types.NewIdent("Wait", c.FuncTT(nil))},
    39  			{Name: types.NewIdent("Signal", c.FuncTT(nil))},
    40  			{Name: types.NewIdent("Broadcast", c.FuncTT(nil))},
    41  		}))
    42  		threadT := types.NamedTGo("pthread_t", "pthread.Thread", c.MethStructT(map[string]*types.FuncType{
    43  			"Join":        c.FuncTT(intT, c.PtrT(retT)),
    44  			"TimedJoinNP": c.FuncTT(intT, c.PtrT(retT), c.PtrT(timespecT)),
    45  		}))
    46  		threadAttrT := types.NamedTGo("pthread_attr_t", "pthread.Attr", c.MethStructT(map[string]*types.FuncType{}))
    47  		return &Library{
    48  			Imports: map[string]string{
    49  				"sync":    "sync",
    50  				"pthread": RuntimePrefix + "pthread",
    51  			},
    52  			Types: map[string]types.Type{
    53  				"pthread_t_":          threadT,
    54  				"pthread_t":           c.PtrT(threadT),
    55  				"pthread_once_t":      onceT,
    56  				"pthread_cond_t":      condT,
    57  				"pthread_condattr_t":  condAttrT,
    58  				"pthread_attr_t":      threadAttrT,
    59  				"pthread_mutex_t":     mutexT,
    60  				"pthread_mutexattr_t": mutexAttrT,
    61  			},
    62  			Idents: map[string]*types.Ident{
    63  				"PTHREAD_MUTEX_RECURSIVE": c.NewIdent("PTHREAD_MUTEX_RECURSIVE", "pthread.MUTEX_RECURSIVE", pthread.MUTEX_RECURSIVE, gintT),
    64  				"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)),
    65  				"pthread_cond_init":       c.NewIdent("pthread_cond_init", "pthread.CondInit", pthread.CondInit, c.FuncTT(intT, c.PtrT(condT), c.PtrT(condAttrT))),
    66  				"pthread_cond_destroy":    c.NewIdent("pthread_cond_destroy", "pthread.CondFree", pthread.CondFree, c.FuncTT(intT, c.PtrT(condT))),
    67  			},
    68  		}
    69  	})
    70  }