/* * * ProLinga-Run * * Copyright (C) 2002-2008 Xobas Software. * All rights reserved. * * This file is part of ProLinga-Run. * * ProLinga-Run is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * ProLinga-Run is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with ProLinga-Run. If not, see . * * More information is available at the following addresses: * * Website : http://www.prolinga.org * * Email : prolinga-list@prolinga.org * * */ #include "RunCommon.h" #include #include #include #include #include #include "BuiltIn.hpp" #include "DataRef.hpp" #include "Thread.hpp" #include "RepositoryCommand.hpp" #include "LogicCommands.hpp" #include extern GtkWidget *activeScreen; extern pthread_t mainThreadId; extern ThreadPtr threadPtr; extern BiAppnPtr biAppnPtr; extern BiThreadStatusPtr biThreadStatusPtr; GtkWidget *curScreen; //GtkWidget *curScreen = activeScreen; //void PBWidget2(GtkWidget *wdg, const char *wdgName) //{ // GtkWidget *curWdg; // GList *wdgList; // int i, lengthWdgList; // wdgList = gtk_container_get_children(GTK_CONTAINER(wdg)); // lengthWdgList = g_list_length(wdgList); // for (i = 0; i < lengthWdgList; i++) // { // curWdg = GTK_WIDGET(g_list_nth_data(wdgList, i)); //printf("WIDGET : %s NAME : %s\n", G_OBJECT_TYPE_NAME(G_OBJECT(curWdg)), gtk_widget_get_name(curWdg)); // if (wdgName != NULL) // { // if (strcmp(wdgName, (char *)gtk_widget_get_name(curWdg)) == 0) // { // //printf("OK, found 1\n"); // gtk_progress_bar_pulse(GTK_PROGRESS_BAR(curWdg)); // gtk_progress_bar_set_text(GTK_PROGRESS_BAR(curWdg), "update1"); // } // } // else // { // //printf("OK, found 2\n"); // gtk_progress_bar_pulse(GTK_PROGRESS_BAR(curWdg)); // gtk_progress_bar_set_text(GTK_PROGRESS_BAR(curWdg), "update2"); // } // // if (GTK_IS_CONTAINER(curWdg)) // PBWidget2(curWdg, wdgName); // } //} //void *xRunThread(void *args) //{ // char *tname = (char *)args; // int count = 0; // //printf("begin\n"); // printf("Child thread %ld\n", pthread_self()); // curScreen = activeScreen; //printf("active screen in thread %ld\n", activeScreen); // //// for (count=0; count<100;count++) // for (;;) // { // //if (threadPtr->getExitFlag(threadPtr, tname) == true) // if (threadPtr->getExitFlagByTid(threadPtr, pthread_self()) == true) // break; // // // printf("hello from the thread\n"); // // threadPtr->printList(threadPtr); // /* get GTK thread lock */ // gdk_threads_enter(); // ////printf("cur screen in thread %ld\n", curScreen); // PBWidget2(curScreen, "pb"); // // /* Sync with X server */ // gdk_flush(); // // /* release GTK thread lock */ // gdk_threads_leave(); // } // // /* Clean up */ // threadPtr->deleteOne(&threadPtr, tname); // // //pthread_exit(0); // //printf("do I get here?\n"); // // /* Return */ // return NULL; //} void *RunThread(void *args) { char *tname = (char *)args; char *argList[255]; char runThreadName[32]; char runLogicName[32]; bool isMainThread = false; /* Get Thread Name */ strcpy(runThreadName, tname); /* Kill Messenger */ delete tname; /* Run Logic */ strcpy(runLogicName, threadPtr->getLogicName(threadPtr, runThreadName)); argList[1] = runLogicName; CmdCall(argList, 1, isMainThread, false); /* Return */ return NULL; } /* Command : THREAD */ /* Syntax : THREAD START "thread_name" */ /* Syntax : THREAD STOP "thread_name" */ void CmdThread(char **argList, int argHits, bool isMainThread) { int retStatus = 0; char logicName[32], threadNameLocal[32]; char *threadName; xmlDocPtr docRes = NULL; xmlXPathContextPtr ctxRes; xmlXPathObjectPtr resRes; pthread_t tid; /* Init */ biThreadStatusPtr->setThreadStatus(0); /* Get thread name (gets deleted in thread */ threadName = new char[32]; getDataRef(argList[2], isMainThread, threadName, false); strcpy(threadNameLocal, threadName); /* Get option */ if (strcmp(argList[1], "START") == 0) { /* Not allowed to access from thread other than main */ if (pthread_equal(mainThreadId, pthread_self()) == 0) { biThreadStatusPtr->setThreadStatus(10301); return; } /* Get logic name */ retStatus = repGet(&docRes, biAppnPtr->getAppn(), "Thread", threadNameLocal); if (retStatus != 0) { biThreadStatusPtr->setThreadStatus(10302); return; } /* Create XPath environment */ ctxRes = xmlXPathNewContext(docRes); /* Get value and store */ resRes = xmlXPathEval((const xmlChar *)"/ProLinga/Repository/Command/Object/Thread/LogicName", ctxRes); strcpy(logicName, (char *)xmlXPathCastToString(resRes)); threadPtr->putLogicName(&threadPtr, threadNameLocal, logicName); /* Store output screen */ threadPtr->putOutputScreen(&threadPtr, threadNameLocal, activeScreen); /* Activate thread */ //pthread_create(&tid, NULL, RunThread, threadName); pthread_create(&tid, NULL, RunThread, (void *)threadName); /* Store tid */ threadPtr->putThreadId(&threadPtr, threadNameLocal, tid); // DEBUG // printf("Thread id %ld\n", tid); // threadPtr->printList(threadPtr); // tid = pthread_self(); // printf("Main thread via np %ld\n", tid); xmlXPathFreeObject(resRes); xmlXPathFreeContext(ctxRes); xmlFreeDoc(docRes); } else if (strcmp(argList[1], "STOP") == 0) { /* Get Thread Id */ tid = threadPtr->getThreadId(threadPtr, threadNameLocal); if (tid == 0) { biThreadStatusPtr->setThreadStatus(10303); return; } /* Set stop flag */ threadPtr->putExitFlag(&threadPtr, threadNameLocal, true); /* Extra leave and enter to work around possible deadlocks */ gdk_threads_leave(); pthread_join(tid, NULL); gdk_threads_enter(); /* Clean up */ threadPtr->deleteOne(&threadPtr, threadNameLocal); //threadPtr->printList(threadPtr); } /* Return */ return; }