github.com/gotranspile/cxgo@v0.3.7/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  			// TODO
    69  			Header: `
    70  #include <` + BuiltinH + `>
    71  #include <` + timeH + `>
    72  
    73  const _cxgo_go_int PTHREAD_MUTEX_RECURSIVE = 1;
    74  
    75  typedef struct pthread_mutex_t pthread_mutex_t;
    76  
    77  typedef struct pthread_attr_t {} pthread_attr_t;
    78  
    79  typedef struct {
    80  	void (*Do)(void (*fnc)(void));
    81  } pthread_once_t;
    82  #define PTHREAD_ONCE_INIT {0}
    83  #define pthread_once(o,f) (o)->Do(f)
    84  
    85  typedef struct {
    86  	pthread_mutex_t* L;
    87  	void (*Wait)(void);
    88  	void (*Signal)(void);
    89  	void (*Broadcast)(void);
    90  } pthread_cond_t;
    91  typedef struct {} pthread_condattr_t;
    92  int pthread_cond_destroy(pthread_cond_t *cond);
    93  int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t * attr);
    94  #define pthread_cond_broadcast(c) (c)->Broadcast()
    95  #define pthread_cond_signal(c) (c)->Signal()
    96  #define PTHREAD_COND_INITIALIZER {0}
    97  
    98  typedef struct {} pthread_mutex_t;
    99  int pthread_cond_timedwait(pthread_cond_t * cond, pthread_mutex_t * mutex, const struct timespec * abstime);
   100  //int pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex);
   101  #define pthread_cond_wait(c,m) {(c)->L = m; (c)->Wait();}
   102  
   103  typedef struct{
   104  	_cxgo_sint32 (*Join)(void **retval);
   105  	_cxgo_sint32 (*TimedJoinNP)(void **retval, const struct timespec *abstime);
   106  } pthread_t_;
   107  #define pthread_t pthread_t_*
   108  
   109  _cxgo_sint32 pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
   110  
   111  typedef struct pthread_mutexattr_t {
   112  	_cxgo_sint32 (*Init)(void);
   113  	_cxgo_sint32 (*SetType)(_cxgo_sint32 type);
   114  	_cxgo_sint32 (*Destroy)(void);
   115  } pthread_mutexattr_t;
   116  #define pthread_mutexattr_init(attr) ((pthread_mutexattr_t*)attr)->Init()
   117  #define pthread_mutexattr_settype(attr, type) ((pthread_mutexattr_t*)attr)->SetType(type)
   118  #define pthread_mutexattr_destroy(attr) ((pthread_mutexattr_t*)attr)->Destroy()
   119  
   120  typedef struct pthread_mutex_t {
   121  	_cxgo_sint32 (*Destroy)(void);
   122  	_cxgo_sint32 (*Init)(const pthread_mutexattr_t *restrict attr);
   123  	_cxgo_sint32 (*CLock)(void);
   124  	_cxgo_sint32 (*TryLock)(void);
   125  	_cxgo_sint32 (*CUnlock)(void);
   126  	_cxgo_sint32 (*TimedLock)(const struct timespec *restrict abstime);
   127  } pthread_mutex_t;
   128  #define pthread_mutex_destroy(mutex) ((pthread_mutex_t*)mutex)->Destroy()
   129  #define pthread_mutex_init(mutex, attr) ((pthread_mutex_t*)mutex)->Init(attr)
   130  #define pthread_mutex_lock(mutex) ((pthread_mutex_t*)mutex)->CLock()
   131  #define pthread_mutex_trylock(mutex) ((pthread_mutex_t*)mutex)->TryLock()
   132  #define pthread_mutex_unlock(mutex) ((pthread_mutex_t*)mutex)->CUnlock()
   133  #define pthread_mutex_timedlock(mutex, abstime) ((pthread_mutex_t*)mutex)->TimedLock(abstime)
   134  
   135  #define pthread_join(thread, retval) ((pthread_t_*)thread)->Join(retval)
   136  #define pthread_timedjoin_np(thread, retval, abstime) ((pthread_t_*)thread)->TimedJoinNP(retval, abstime)
   137  `,
   138  		}
   139  	})
   140  }