• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Thread.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 /****************************************************************************
00025 
00026   Basic Threads
00027 
00028 
00029 
00030 **************************************************************************/
00031 #include "P_EventSystem.h"
00032 
00033   ///////////////////////////////////////////////
00034   // Common Interface impl                     //
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   // Unix & non-NT Interface impl              //
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 }

Generated by  doxygen 1.7.1