Removed prefix Yaz_ from several class - and interface names now
[yazpp-moved-to-github.git] / src / yaz-pdu-assoc-thread.cpp
1 /*
2  * Copyright (c) 1998-2004, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-pdu-assoc-thread.cpp,v 1.11 2005-06-08 13:28:06 adam Exp $
6  */
7
8 #ifdef WIN32
9 #define USE_THREADS 1
10 #endif
11
12 #if YAZ_POSIX_THREADS
13 #define USE_THREADS 1
14 #endif
15
16 #if USE_THREADS
17
18 #if HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21
22 #ifdef WIN32
23 #include <process.h>
24 #else
25 #include <pthread.h>
26 #endif
27
28
29 #include <errno.h>
30 #include <yaz/log.h>
31 #include <yaz/tcpip.h>
32
33 #include <yaz++/pdu-assoc.h>
34 #include <yaz++/socket-manager.h>
35
36 using namespace yazpp_1;
37
38 PDU_AssocThread::PDU_AssocThread(
39     ISocketObservable *socketObservable)
40     : PDU_Assoc(socketObservable)
41 {
42     
43 }
44
45 #ifdef WIN32
46 void __cdecl
47 #else
48 void *
49 #endif 
50 events(void *p)
51 {
52     SocketManager *s = (SocketManager *) p;
53     
54     yaz_log (YLOG_LOG, "thread started");
55     while (s->processEvent() > 0)
56         ;
57     yaz_log (YLOG_LOG, "thread finished");
58 #ifdef WIN32
59 #else
60     return 0;
61 #endif
62 }
63
64 void PDU_AssocThread::childNotify(COMSTACK cs)
65 {
66     SocketManager *socket_observable = new SocketManager;
67     PDU_Assoc *new_observable = new PDU_Assoc (socket_observable, cs);
68
69     new_observable->m_next = m_children;
70     m_children = new_observable;
71     new_observable->m_parent = this;
72
73     /// Clone PDU Observer
74     new_observable->m_PDU_Observer =
75         m_PDU_Observer->sessionNotify(new_observable, cs_fileno(cs));
76 #ifdef WIN32
77     long t_id;
78     t_id = _beginthread (events, 0, socket_observable);
79     if (t_id == -1)
80     {
81         yaz_log (YLOG_FATAL|YLOG_ERRNO, "_beginthread failed");
82         exit (1);
83     }
84 #else
85     pthread_t tid;
86
87     int id = pthread_create (&tid, 0, events, socket_observable);
88     if (id)
89         yaz_log (YLOG_ERRNO|YLOG_FATAL, "pthread_create returned id=%d", id);
90     else
91         pthread_detach (tid);
92 #endif
93 }
94 #endif