00001 /*-------------------------------------------------------------------- 00002 * 00003 * (C) Copyright Koninklijke Philips Electronics NV 2006. 00004 * All rights reserved. This software is licensed under the terms of 00005 * version 2.1 of the GNU Lesser General Public License as published 00006 * by the Free Software Foundation. For licensing and warranty 00007 * information, see the file COPYING in the main directory. 00008 * 00009 *------------------------------------------------------------------*/ 00010 00011 #include <sys/types.h> 00012 #include "cosyprocess.h" 00013 #include "processimpl.h" 00014 #include "taskapi.h" 00015 #include <assert.h> 00016 #include "rte.h" 00017 00018 00019 static void run_cosy_process(void* arg) 00020 { 00021 cosyProcess* p = (cosyProcess*)arg; 00022 p->api()->main(); 00023 p->exit(); 00024 } 00025 00026 cosyProcess::cosyProcess(ProcessImpl& p, cosyRte& r) : 00027 rteProcess(p), 00028 run(false) 00029 { 00030 rte = &r; 00031 task = new Task(run_cosy_process, this); 00032 } 00033 00034 cosyProcess::~cosyProcess() 00035 { 00036 assert(!run); 00037 Counters::iterator i; 00038 for (i=cntr.begin(); i!=cntr.end(); ++i) 00039 { 00040 free((*i).name); 00041 } 00042 00043 delete task; 00044 } 00045 00046 void cosyProcess::start() 00047 { 00048 #ifdef PREEMPTIVE 00049 lock(); // enter critical section 00050 #endif 00051 run = true; 00052 task->start(); 00053 #ifdef PREEMPTIVE 00054 unlock(); // leave critical section 00055 #endif 00056 } 00057 00058 void cosyProcess::exit() 00059 { 00060 #ifdef PREEMPTIVE 00061 lock(); // enter critical section 00062 #endif 00063 run = false; 00064 #ifdef PREEMPTIVE 00065 unlock(); // leave critical section 00066 #endif 00067 task->exit(); 00068 } 00069 00070 void cosyProcess::join() 00071 { 00072 if (run) 00073 { 00074 task->join(); 00075 } 00076 } 00077 00078 void cosyProcess::kill() 00079 { 00080 #ifdef PREEMPTIVE 00081 lock(); // enter critical section 00082 #endif 00083 if (run) 00084 { 00085 run = false; 00086 #ifdef PREEMPTIVE 00087 unlock(); // leave critical section 00088 #endif 00089 task->kill(); 00090 } 00091 #ifdef PREEMPTIVE 00092 else { 00093 unlock(); // leave critical section 00094 } 00095 #endif 00096 } 00097 00098 bool cosyProcess::finished() const 00099 { 00100 return !run; 00101 } 00102 00103 void cosyProcess::execute(const char* instr) 00104 { 00105 count(cntr, instr); 00106 } 00107 00108 #ifdef PREEMPTIVE 00109 void cosyProcess::lock() 00110 { 00111 mutex.lock(); 00112 } 00113 00114 void cosyProcess::unlock() 00115 { 00116 mutex.unlock(); 00117 } 00118 #endif 00119 00120