1 /*metadoc Thread copyright
5 /*metadoc Thread license
9 /*metadoc Thread category
13 /*metadoc Thread description
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
);
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},
53 IoObject_addMethodTable_(self
, methodTable
);
61 IoThread
*IoThread_rawClone(IoThread
*proto
)
63 IoObject
*self
= IoObject_rawClonePrimitive(proto
);
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 // --------------------------------------------
86 IoThreadInfo
*IoThreadInfo_new(void)
88 IoThreadInfo
*self
= (IoThreadInfo
*)calloc(1, sizeof(IoThreadInfo
));
92 void IoThreadInfo_free(IoThreadInfo
*self
)
94 if(self
->evalString
) free(self
->evalString
);
98 void IoThreadInfo_setState_(IoThreadInfo
*self
, IoState
*state
)
103 IoState
*IoThreadInfo_state(IoThreadInfo
*self
)
108 void IoThreadInfo_setThread_(IoThreadInfo
*self
, Thread
*thread
)
110 self
->thread
= thread
;
113 Thread
*IoThreadInfo_thread(IoThreadInfo
*self
)
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
);
144 IoObject
*IoThread_createThread(IoObject
*self
, IoObject
*locals
, IoMessage
*m
)
146 IoSeq
*s
= IoMessage_locals_seqArgAt_(m
, locals
, 0);
147 IoState
*newState
= IoState_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
);
166 IoObject
*IoThread_threadCount(IoObject
*self
, IoObject
*locals
, IoMessage
*m
)
172 threads
= Thread_Threads();
173 count
= List_size(threads
);
176 return IONUMBER(count
);
179 IoObject
*IoThread_endCurrentThread(IoObject
*self
, IoObject
*locals
, IoMessage
*m
)
182 Thread_destroy(Thread_CurrentThread());