X-Git-Url: http://git.indexdata.com/?p=yazpp-moved-to-github.git;a=blobdiff_plain;f=src%2Fyaz-pdu-assoc-thread.cpp;h=e96a14e8f6eaa5e4e561a6c2ac2ad4cb33f3297d;hp=4a445fd48439472fe7c0452a238fefd05637ed6c;hb=9fef1c618f2f936041b3b58d61e406182033ab30;hpb=966d1a0443071c2b75426d0214bfb9960c5c3fba diff --git a/src/yaz-pdu-assoc-thread.cpp b/src/yaz-pdu-assoc-thread.cpp index 4a445fd..e96a14e 100644 --- a/src/yaz-pdu-assoc-thread.cpp +++ b/src/yaz-pdu-assoc-thread.cpp @@ -1,15 +1,26 @@ -/* - * Copyright (c) 1998-2001, Index Data. +/* This file is part of the yazpp toolkit. + * Copyright (C) 1998-2009 Index Data and Mike Taylor * See the file LICENSE for details. - * - * $Id: yaz-pdu-assoc-thread.cpp,v 1.4 2001-11-04 22:36:21 adam Exp $ */ #ifdef WIN32 +#define USE_THREADS 1 +#endif + +#if YAZ_POSIX_THREADS +#define USE_THREADS 1 +#endif + +#if USE_THREADS + +#if HAVE_UNISTD_H +#include +#endif + +#ifdef WIN32 #include #else #include -#include #endif @@ -17,18 +28,35 @@ #include #include -#include -#include +#include +#include +using namespace yazpp_1; +class worker { +public: + SocketManager *m_mgr; + PDU_Assoc *m_assoc; + void run(); +}; -Yaz_PDU_AssocThread::Yaz_PDU_AssocThread( - IYazSocketObservable *socketObservable) - : Yaz_PDU_Assoc(socketObservable) +PDU_AssocThread::PDU_AssocThread( + ISocketObservable *socketObservable) + : PDU_Assoc(socketObservable) { } +void worker::run() +{ + yaz_log (YLOG_LOG, "thread started"); + while (this->m_mgr->processEvent() > 0) + ; + yaz_log (YLOG_LOG, "thread finished"); + delete this->m_mgr; + delete this; +} + #ifdef WIN32 void __cdecl #else @@ -36,45 +64,59 @@ void * #endif events(void *p) { - Yaz_SocketManager *s = (Yaz_SocketManager *) p; - - yaz_log (LOG_LOG, "thread started"); - while (s->processEvent() > 0) - ; - yaz_log (LOG_LOG, "thread finished"); + worker *w = (worker *) p; + w->run(); #ifdef WIN32 #else return 0; #endif } -void Yaz_PDU_AssocThread::childNotify(COMSTACK cs) +void PDU_AssocThread::childNotify(COMSTACK cs) { - Yaz_SocketManager *socket_observable = new Yaz_SocketManager; - Yaz_PDU_Assoc *new_observable = new Yaz_PDU_Assoc (socket_observable, cs); - - new_observable->m_next = m_children; - m_children = new_observable; - new_observable->m_parent = this; + SocketManager *socket_observable = new SocketManager; + PDU_Assoc *new_observable = new PDU_Assoc (socket_observable, cs); /// Clone PDU Observer new_observable->m_PDU_Observer = - m_PDU_Observer->sessionNotify(new_observable, cs_fileno(cs)); + m_PDU_Observer->sessionNotify(new_observable, cs_fileno(cs)); + + if (!new_observable->m_PDU_Observer) + { + new_observable->shutdown(); + delete new_observable; + delete socket_observable; + return; + } + + worker *w = new worker; + w->m_assoc = new_observable; + w->m_mgr = socket_observable; + #ifdef WIN32 long t_id; - t_id = _beginthread (events, 0, socket_observable); + t_id = _beginthread (events, 0, w); if (t_id == -1) { - yaz_log (LOG_FATAL|LOG_ERRNO, "_beginthread failed"); + yaz_log (YLOG_FATAL|YLOG_ERRNO, "_beginthread failed"); exit (1); } #else pthread_t tid; - int id = pthread_create (&tid, 0, events, socket_observable); + int id = pthread_create (&tid, 0, events, w); if (id) - yaz_log (LOG_ERRNO|LOG_FATAL, "pthread_create returned id=%d", id); + yaz_log (YLOG_ERRNO|YLOG_FATAL, "pthread_create returned id=%d", id); else - pthread_detach (tid); + pthread_detach (tid); #endif } +#endif +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +