Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "P_EventSystem.h"
00032
00033
00034
00035
00036
00037 static ink_thread_key init_thread_key();
00038
00039 ProxyMutex *global_mutex = NULL;
00040 ink_hrtime
00041 Thread::cur_time = 0;
00042 inkcoreapi ink_thread_key
00043 Thread::thread_data_key = init_thread_key();
00044
00045 Thread::Thread()
00046 {
00047 mutex = new_ProxyMutex();
00048 mutex_ptr = mutex;
00049 MUTEX_TAKE_LOCK(mutex, (EThread *) this);
00050 mutex->nthread_holding = THREAD_MUTEX_THREAD_HOLDING;
00051 }
00052
00053 static void
00054 key_destructor(void *value)
00055 {
00056 (void) value;
00057 }
00058
00059 ink_thread_key
00060 init_thread_key()
00061 {
00062 ink_thread_key_create(&Thread::thread_data_key, key_destructor);
00063 return Thread::thread_data_key;
00064 }
00065
00066
00067
00068
00069
00070 struct thread_data_internal
00071 {
00072 ThreadFunction f;
00073 void *a;
00074 Thread *me;
00075 char name[MAX_THREAD_NAME_LENGTH];
00076 };
00077
00078 static void *
00079 spawn_thread_internal(void *a)
00080 {
00081 thread_data_internal *p = (thread_data_internal *) a;
00082
00083 p->me->set_specific();
00084 ink_set_thread_name(p->name);
00085 if (p->f)
00086 p->f(p->a);
00087 else
00088 p->me->execute();
00089 ats_free(a);
00090 return NULL;
00091 }
00092
00093 ink_thread
00094 Thread::start(const char* name, size_t stacksize, ThreadFunction f, void *a)
00095 {
00096 thread_data_internal *p = (thread_data_internal *)ats_malloc(sizeof(thread_data_internal));
00097
00098 p->f = f;
00099 p->a = a;
00100 p->me = this;
00101 memset(p->name, 0, MAX_THREAD_NAME_LENGTH);
00102 ink_strlcpy(p->name, name, MAX_THREAD_NAME_LENGTH);
00103 tid = ink_thread_create(spawn_thread_internal, (void *) p, 0, stacksize);
00104
00105 return tid;
00106 }