11 static void lockDestroy(void* lock) { naFreeLock(lock); }
12 static naGhostType LockType = { lockDestroy };
14 static void semDestroy(void* sem) { naFreeSem(sem); }
15 static naGhostType SemType = { semDestroy };
23 static DWORD WINAPI threadtop(LPVOID param)
25 static void* threadtop(void* param)
28 ThreadData* td = param;
29 naCall(td->ctx, td->func, 0, 0, naNil(), naNil());
30 naFreeContext(td->ctx);
35 static naRef f_newthread(naContext c, naRef me, int argc, naRef* args)
38 if(argc < 1 || !naIsFunc(args[0]))
39 naRuntimeError(c, "bad/missing argument to newthread");
40 td = naAlloc(sizeof(*td));
41 td->ctx = naNewContext();
43 naTempSave(td->ctx, td->func);
45 CreateThread(0, 0, threadtop, td, 0, 0);
49 if((err = pthread_create(&t, 0, threadtop, td)))
50 naRuntimeError(c, "newthread failed: %s", strerror(err));
57 static naRef f_newlock(naContext c, naRef me, int argc, naRef* args)
59 return naNewGhost(c, &LockType, naNewLock());
62 static naRef f_lock(naContext c, naRef me, int argc, naRef* args)
64 if(argc > 0 && naGhost_type(args[0]) == &LockType) {
66 naLock(naGhost_ptr(args[0]));
72 static naRef f_unlock(naContext c, naRef me, int argc, naRef* args)
74 if(argc > 0 && naGhost_type(args[0]) == &LockType)
75 naUnlock(naGhost_ptr(args[0]));
79 static naRef f_newsem(naContext c, naRef me, int argc, naRef* args)
81 return naNewGhost(c, &SemType, naNewSem());
84 static naRef f_semdown(naContext c, naRef me, int argc, naRef* args)
86 if(argc > 0 && naGhost_type(args[0]) == &SemType) {
88 naSemDown(naGhost_ptr(args[0]));
94 static naRef f_semup(naContext c, naRef me, int argc, naRef* args)
96 if(argc > 0 && naGhost_type(args[0]) == &SemType)
97 naSemUp(naGhost_ptr(args[0]), 1);
101 static naCFuncItem funcs[] = {
102 { "newthread", f_newthread },
103 { "newlock", f_newlock },
105 { "unlock", f_unlock },
106 { "newsem", f_newsem },
107 { "semdown", f_semdown },
108 { "semup", f_semup },
112 naRef naInit_thread(naContext c)
114 return naGenLib(c, funcs);