10 static void lockDestroy(void* lock) { naFreeLock(lock); }
11 static naGhostType LockType = { lockDestroy };
13 static void semDestroy(void* sem) { naFreeSem(sem); }
14 static naGhostType SemType = { semDestroy };
22 static DWORD WINAPI threadtop(LPVOID param)
24 static void* threadtop(void* param)
27 ThreadData* td = param;
28 naCall(td->ctx, td->func, 0, 0, naNil(), naNil());
29 naFreeContext(td->ctx);
34 static naRef f_newthread(naContext c, naRef me, int argc, naRef* args)
37 if(argc < 1 || !naIsFunc(args[0]))
38 naRuntimeError(c, "bad/missing argument to newthread");
39 td = naAlloc(sizeof(*td));
40 td->ctx = naNewContext();
42 naTempSave(td->ctx, td->func);
44 CreateThread(0, 0, threadtop, td, 0, 0);
46 { pthread_t t; pthread_create(&t, 0, threadtop, td); }
51 static naRef f_newlock(naContext c, naRef me, int argc, naRef* args)
53 return naNewGhost(c, &LockType, naNewLock());
56 static naRef f_lock(naContext c, naRef me, int argc, naRef* args)
58 if(argc > 0 && naGhost_type(args[0]) == &LockType)
59 naLock(naGhost_ptr(args[0]));
63 static naRef f_unlock(naContext c, naRef me, int argc, naRef* args)
65 if(argc > 0 && naGhost_type(args[0]) == &LockType)
66 naUnlock(naGhost_ptr(args[0]));
70 static naRef f_newsem(naContext c, naRef me, int argc, naRef* args)
72 return naNewGhost(c, &SemType, naNewSem());
75 static naRef f_semdown(naContext c, naRef me, int argc, naRef* args)
77 if(argc > 0 && naGhost_type(args[0]) == &SemType)
78 naSemDown(naGhost_ptr(args[0]));
82 static naRef f_semup(naContext c, naRef me, int argc, naRef* args)
84 if(argc > 0 && naGhost_type(args[0]) == &SemType)
85 naSemUp(naGhost_ptr(args[0]), 1);
89 static naCFuncItem funcs[] = {
90 { "newthread", f_newthread },
91 { "newlock", f_newlock },
93 { "unlock", f_unlock },
94 { "newsem", f_newsem },
95 { "semdown", f_semdown },
100 naRef naInit_thread(naContext c)
102 return naGenLib(c, funcs);