New threaded PDU association.
[yazpp-moved-to-github.git] / src / yaz-pdu-assoc-thread.cpp
1 /*
2  * Copyright (c) 1998-2001, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Log: yaz-pdu-assoc-thread.cpp,v $
6  * Revision 1.1  2001-03-26 14:43:49  adam
7  * New threaded PDU association.
8  *
9  */
10
11 #include <yaz/log.h>
12 #include <yaz/tcpip.h>
13
14 #include <yaz++/yaz-pdu-assoc.h>
15 #include <yaz++/yaz-socket-manager.h>
16
17 #ifdef WIN32
18 #include <process.h>
19 #else
20 #include <pthread.h>
21 #endif
22
23
24 Yaz_PDU_AssocThread::Yaz_PDU_AssocThread(
25     IYazSocketObservable *socketObservable)
26     : Yaz_PDU_Assoc(socketObservable)
27 {
28     
29 }
30
31 #ifdef WIN32
32 void __cdecl
33 #else
34 void *
35 #endif 
36 events(void *p)
37 {
38     Yaz_SocketManager *s = (Yaz_SocketManager *) p;
39     
40     logf (LOG_LOG, "thread started");
41     while (s->processEvent() > 0)
42         ;
43     logf (LOG_LOG, "thread finished");
44 #ifdef WIN32
45 #else
46     return 0;
47 #endif
48 }
49
50 void Yaz_PDU_AssocThread::childNotify(COMSTACK cs)
51 {
52     Yaz_SocketManager *socket_observable = new Yaz_SocketManager;
53     Yaz_PDU_Assoc *new_observable = new Yaz_PDU_Assoc (socket_observable, cs);
54     
55     /// Clone PDU Observer
56     new_observable->m_PDU_Observer =
57         m_PDU_Observer->sessionNotify(new_observable, cs_fileno(cs));
58 #ifdef WIN32
59     long t_id;
60     t_id = _beginthread (events, 0, socket_observable);
61     if (t_id == -1)
62     {
63         yaz_log (LOG_FATAL|LOG_ERRNO, "_beginthread failed");
64         exit (1);
65     }
66 #else
67     pthread_t type;
68
69     int id = pthread_create (&type, 0, events, socket_observable);
70     yaz_log (LOG_LOG, "pthread_create returned id=%d", id);
71 #endif
72 }