/* The producer thread waits until the mailbox is empty, and then writes * the next value to the mailbox and pokes the consumer. */ static pt_t producer_thr(env_t const env) { pc_thread_context_t * const c = env ; pt_resume(c) ; for (c->i = 1; c->i <= N; c->i++) { while (*c->mailbox) { /* mailbox is full */ pt_wait(c, c->mailbox) ; } *c->mailbox = c->i ; pt_signal(pt_get_pt(c), c->mailbox) ; } return PT_DONE ; }
/* The consumer thread waits until something (non-zero) appears in the * mailbox, verifies that it's the expected value, writes a zero to * signify that the mailbox is empty, and wakes up the producer. */ static pt_t consumer_thr(env_t const env) { pc_thread_context_t * const c = env ; pt_resume(c) ; for (c->i = 1; c->i <= N; c->i++) { while (*c->mailbox == 0) { /* mailbox is empty */ pt_wait(c, c->mailbox) ; } assert(*c->mailbox == c->i) ; *c->mailbox = 0 ; /* remove the item */ pt_signal(pt_get_pt(c), c->mailbox) ; } return PT_DONE ; }
/** * Description: * This is the signal handler, When called this sets the * global gShutdownFlag allowing the main processing * loop to exit cleanly. */ void sigHandler( int sig_num ) { // set a global shutdown flag gShutdownFlag = TRUE; // Unregister interest in the signal to prevent recursive callbacks pt_signal( sig_num, SIG_DFL ); // Minimize the chance that we loose log data OsSysLog::flush(); if (SIGTERM == sig_num) { OsSysLog::add( LOG_FACILITY, PRI_INFO, "sigHandler: terminate signal received."); } else { OsSysLog::add( LOG_FACILITY, PRI_CRIT, "sigHandler: caught signal: %d", sig_num ); } OsSysLog::flush(); }
static void test_ready(void) { protothread_t const pt = protothread_create() ; protothread_set_ready_function(pt, set_ready, &ready) ; bool_t more ; /* more work to do */ ready_context_t * const c = malloc(sizeof(*c)) ; /* nothing to run */ more = protothread_run(pt) ; assert(!more) ; assert(!ready) ; pt_create(pt, &c->pt_thread, ready_thr, c) ; assert(ready) ; ready = FALSE ; /* advance thread to the wait */ more = protothread_run(pt) ; assert(!more) ; assert(!ready) ; /* make the thread runnable */ pt_signal(pt, c) ; assert(ready) ; ready = FALSE ; /* should advance the thread to the pt_yield() */ more = protothread_run(pt) ; assert(more) ; assert(!ready) ; /* advance the thread to its exit, nothing ready to run */ more = protothread_run(pt) ; assert(!more) ; assert(!ready) ; free(c) ; protothread_free(pt) ; }
// // The main entry point to the sipXpark // int main(int argc, char* argv[]) { // Configuration Database (used for OsSysLog) OsConfigDb configDb; // Register Signal handlers so we can perform graceful shutdown pt_signal(SIGINT, sigHandler); // Trap Ctrl-C on NT pt_signal(SIGILL, sigHandler); pt_signal(SIGABRT, sigHandler); // Abort signal 6 pt_signal(SIGFPE, sigHandler); // Floading Point Exception pt_signal(SIGSEGV, sigHandler); // Address access violations signal 11 pt_signal(SIGTERM, sigHandler); // Trap kill -15 on UNIX #if defined(__pingtel_on_posix__) pt_signal(SIGHUP, sigHandler); // Hangup pt_signal(SIGQUIT, sigHandler); pt_signal(SIGPIPE, SIG_IGN); // Handle TCP Failure pt_signal(SIGBUS, sigHandler); pt_signal(SIGSYS, sigHandler); pt_signal(SIGXCPU, sigHandler); pt_signal(SIGXFSZ, sigHandler); pt_signal(SIGUSR1, sigHandler); pt_signal(SIGUSR2, sigHandler); #endif UtlString argString; for (int argIndex = 1; argIndex < argc; argIndex++) { osPrintf("arg[%d]: %s\n", argIndex, argv[argIndex]); argString = argv[argIndex]; NameValueTokenizer::frontBackTrim(&argString, "\t "); if(argString.compareTo("-v") == 0) { osPrintf("Version: %s (%s)\n", SIPXCHANGE_VERSION, SIPXCHANGE_VERSION_COMMENT); return(1); } else { osPrintf("usage: %s [-v]\nwhere:\n -v provides the software version\n", argv[0]); return(1); } } // Load configuration file file OsPath workingDirectory; if (OsFileSystem::exists(CONFIG_ETC_DIR)) { workingDirectory = CONFIG_ETC_DIR; OsPath path(workingDirectory); path.getNativePath(workingDirectory); } else { OsPath path; OsFileSystem::getWorkingDirectory(path); path.getNativePath(workingDirectory); } UtlString fileName = workingDirectory + OsPathBase::separator + CONFIG_SETTINGS_FILE; if (configDb.loadFromFile(fileName) != OS_SUCCESS) { exit(1); } // Initialize log file initSysLog(&configDb); // Read the user agent parameters from the config file. int UdpPort; if (configDb.get(CONFIG_SETTING_UDP_PORT, UdpPort) != OS_SUCCESS) { UdpPort = DEFAULT_UDP_PORT; } int TcpPort; if (configDb.get(CONFIG_SETTING_TCP_PORT, TcpPort) != OS_SUCCESS) { TcpPort = DEFAULT_TCP_PORT; } int HttpPort; if (configDb.get(CONFIG_SETTING_HTTP_PORT, HttpPort) != OS_SUCCESS) { HttpPort = DEFAULT_HTTP_PORT; } // Bind the SIP user agent to a port and start it up SipUserAgent* userAgent = new SipUserAgent(TcpPort, UdpPort); userAgent->start(); UtlString domainName; configDb.get(CONFIG_SETTING_DOMAIN_NAME, domainName); int refreshTimeout; if (configDb.get(CONFIG_SETTING_REFRESH_INTERVAL, refreshTimeout) != OS_SUCCESS) { refreshTimeout = DEFAULT_REFRESH_INTERVAL; } // Create the Sip Dialog Monitor SipDialogMonitor dialogMonitor(userAgent, domainName, TcpPort, refreshTimeout, true); // Create a XmlRpcDispatch XmlRpcDispatch XmlRpcServer(HttpPort, false, "/RPC2"); // Register all the XML-RPC methods DialogMonitorConfig xmlRpcMethods(&XmlRpcServer, &dialogMonitor); // Loop forever until signaled to shut down while (!gShutdownFlag) { OsTask::delay(2000); } // Shut down the sipUserAgent userAgent->shutdown(FALSE); while(!userAgent->isShutdownDone()) { ; } delete userAgent; // Flush the log file OsSysLog::flush(); // Say goodnight Gracie... return 0; }
// // The main entry point to the sipXpark // int main(int argc, char* argv[]) { // Configuration Database (used for OsSysLog) OsConfigDb configDb; // Register Signal handlers so we can perform graceful shutdown pt_signal(SIGINT, sigHandler); // Trap Ctrl-C on NT pt_signal(SIGILL, sigHandler); pt_signal(SIGABRT, sigHandler); // Abort signal 6 pt_signal(SIGFPE, sigHandler); // Floading Point Exception pt_signal(SIGSEGV, sigHandler); // Address access violations signal 11 pt_signal(SIGTERM, sigHandler); // Trap kill -15 on UNIX #if defined(__pingtel_on_posix__) pt_signal(SIGHUP, sigHandler); // Hangup pt_signal(SIGQUIT, sigHandler); pt_signal(SIGPIPE, SIG_IGN); // Handle TCP Failure pt_signal(SIGBUS, sigHandler); pt_signal(SIGSYS, sigHandler); pt_signal(SIGXCPU, sigHandler); pt_signal(SIGXFSZ, sigHandler); pt_signal(SIGUSR1, sigHandler); pt_signal(SIGUSR2, sigHandler); #endif UtlString argString; for(int argIndex = 1; argIndex < argc; argIndex++) { osPrintf("arg[%d]: %s\n", argIndex, argv[argIndex]); argString = argv[argIndex]; NameValueTokenizer::frontBackTrim(&argString, "\t "); if(argString.compareTo("-v") == 0) { osPrintf("Version: %s (%s)\n", SIPXCHANGE_VERSION, SIPXCHANGE_VERSION_COMMENT); return(1); } else { osPrintf("usage: %s [-v]\nwhere:\n -v provides the software version\n", argv[0]); return(1); } } // Load configuration file file OsPath workingDirectory; if (OsFileSystem::exists(CONFIG_ETC_DIR)) { workingDirectory = CONFIG_ETC_DIR; OsPath path(workingDirectory); path.getNativePath(workingDirectory); } else { OsPath path; OsFileSystem::getWorkingDirectory(path); path.getNativePath(workingDirectory); } UtlString fileName = workingDirectory + OsPathBase::separator + CONFIG_SETTINGS_FILE; if (configDb.loadFromFile(fileName) != OS_SUCCESS) { exit(1); } // Initialize log file initSysLog(&configDb); // Read the user agent parameters from the config file. int UdpPort; if (configDb.get(CONFIG_SETTING_UDP_PORT, UdpPort) != OS_SUCCESS) { UdpPort = PRESENCE_DEFAULT_UDP_PORT; } int TcpPort; if (configDb.get(CONFIG_SETTING_TCP_PORT, TcpPort) != OS_SUCCESS) { TcpPort = PRESENCE_DEFAULT_TCP_PORT; } UtlString bindIp; if (configDb.get(CONFIG_SETTING_BIND_IP, bindIp) != OS_SUCCESS || !OsSocket::isIp4Address(bindIp)) { bindIp = PRESENCE_DEFAULT_BIND_IP; } // Bind the SIP user agent to a port and start it up SipUserAgent* userAgent = new SipUserAgent(TcpPort, UdpPort, PORT_NONE, NULL, NULL, bindIp ); userAgent->start(); if (!userAgent->isOk()) { OsSysLog::add(LOG_FACILITY, PRI_EMERG, "SipUserAgent failed to initialize; requesting shutdown"); gShutdownFlag = TRUE; } UtlString domainName; configDb.get(CONFIG_SETTING_DOMAIN_NAME, domainName); // Normalize SIP domain name to all lower case. domainName.toLower(); // Create the SipPersistentSubscriptionMgr. SipPersistentSubscriptionMgr subscriptionMgr(SUBSCRIPTION_COMPONENT_PRESENCE, domainName); // Determine the name of the persistent file. UtlString pathName = SipXecsService::Path(SipXecsService::VarDirType, sPersistentFileName.data()); // Create the Sip Presence Monitor, which handles all of the processing. SipPresenceMonitor presenceMonitor(userAgent, &subscriptionMgr, domainName, TcpPort, &configDb, true, pathName.data()); // Loop forever until signaled to shut down while (!gShutdownFlag) { OsTask::delay(2000); } // Shut down the sipUserAgent userAgent->shutdown(FALSE); while(!userAgent->isShutdownDone()) { ; } delete userAgent; // Flush the log file OsSysLog::flush(); // Say goodnight Gracie... return 0; }