Addons updated to new doc format
[io.git] / addons / Thread / source / IoThread.c
blob544bb7b9be293bbc7043e6c69dd8af82d8b9f48e
1 /*metadoc Thread copyright
2 Steve Dekorte, 2006
3 */
5 /*metadoc Thread license
6 BSD revised
7 */
9 /*metadoc Thread category
10 Compression
13 /*metadoc Thread description
14 For native threads.
15 Example use;
16 <code>
17 Thread createThread()
18 </code>
19 """)
22 #include "IoThread.h"
23 #include "IoState.h"
24 #include "IoNumber.h"
25 #include "IoSeq.h"
26 #include <errno.h>
27 #include "Thread.h"
28 #include "List.h"
30 IoTag *IoThread_newTag(void *state)
32 IoTag *tag = IoTag_newWithName_("Thread");
33 IoTag_state_(tag, state);
34 IoTag_freeFunc_(tag, (IoTagFreeFunc *)IoThread_free);
35 IoTag_cloneFunc_(tag, (IoTagCloneFunc *)IoThread_rawClone);
36 return tag;
39 IoThread *IoThread_proto(void *state)
41 IoThread *self = IoObject_new(state);
42 IoObject_tag_(self, IoThread_newTag(state));
44 IoState_registerProtoWithFunc_(state, self, IoThread_proto);
47 IoMethodTable methodTable[] = {
48 {"threadCount", IoThread_threadCount},
49 {"createThread", IoThread_createThread},
50 //{"endCurrentThread", IoThread_endCurrentThread},
51 {NULL, NULL},
53 IoObject_addMethodTable_(self, methodTable);
56 Thread_Init();
58 return self;
61 IoThread *IoThread_rawClone(IoThread *proto)
63 IoObject *self = IoObject_rawClonePrimitive(proto);
64 return self;
67 IoThread *IoThread_new(void *state)
69 IoObject *proto = IoState_protoWithInitFunction_(state, IoThread_proto);
70 return IOCLONE(proto);
73 void IoThread_free(IoThread *self)
77 // --------------------------------------------
79 typedef struct
81 IoState *state;
82 Thread *thread;
83 char *evalString;
84 } IoThreadInfo;
86 IoThreadInfo *IoThreadInfo_new(void)
88 IoThreadInfo *self = (IoThreadInfo *)calloc(1, sizeof(IoThreadInfo));
89 return self;
92 void IoThreadInfo_free(IoThreadInfo *self)
94 if(self->evalString) free(self->evalString);
95 free(self);
98 void IoThreadInfo_setState_(IoThreadInfo *self, IoState *state)
100 self->state = state;
103 IoState *IoThreadInfo_state(IoThreadInfo *self)
105 return self->state;
108 void IoThreadInfo_setThread_(IoThreadInfo *self, Thread *thread)
110 self->thread = thread;
113 Thread *IoThreadInfo_thread(IoThreadInfo *self)
115 return self->thread;
118 void IoThreadInfo_setEvalString_(IoThreadInfo *self, char *s)
120 self->evalString = strcpy(malloc(strlen(s) + 1), s);
123 char *IoThreadInfo_evalString(IoThreadInfo *self)
125 return self->evalString;
128 // ----------------------------------------------------
130 void *IoThread_BeginThread(void *vti)
132 IoThreadInfo *ti = (IoThreadInfo *)vti;
133 Thread *t = IoThreadInfo_thread(ti);
134 IoState *state = IoThreadInfo_state(ti);
136 Thread_setUserData_(t, state);
137 IoState_doCString_(state, IoThreadInfo_evalString(ti));
138 IoThreadInfo_free(ti);
139 IoState_free(state);
140 Thread_destroy(t);
141 return NULL;
144 IoObject *IoThread_createThread(IoObject *self, IoObject *locals, IoMessage *m)
146 IoSeq *s = IoMessage_locals_seqArgAt_(m, locals, 0);
147 IoState *newState = IoState_new();
148 Thread *t;
150 Thread_Init();
152 t = Thread_new();
154 IoThreadInfo *ti = IoThreadInfo_new();
155 IoThreadInfo_setState_(ti, newState);
156 IoThreadInfo_setThread_(ti, t);
157 IoThreadInfo_setEvalString_(ti, CSTRING(s));
159 Thread_setFunc_(t, IoThread_BeginThread);
160 Thread_setFuncArg_(t, (void *)ti);
161 Thread_start(t);
163 return self;
166 IoObject *IoThread_threadCount(IoObject *self, IoObject *locals, IoMessage *m)
168 Thread_Init();
169 List *threads;
170 size_t count;
172 threads = Thread_Threads();
173 count = List_size(threads);
174 List_free(threads);
176 return IONUMBER(count);
179 IoObject *IoThread_endCurrentThread(IoObject *self, IoObject *locals, IoMessage *m)
181 // shutdown vm?
182 Thread_destroy(Thread_CurrentThread());
183 return self;