-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPyConnectPyModule.cpp
More file actions
98 lines (88 loc) · 2.3 KB
/
PyConnectPyModule.cpp
File metadata and controls
98 lines (88 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* PyConnectPyModule.cpp
*
* Copyright 2006, 2007 Xun Wang.
* This file is part of PyConnect.
*
* PyConnect 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.
*
* PyConnect 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef WIN32
#include <windows.h>
#include <process.h>
#else
#include <pthread.h>
#endif
#include "PyConnectNetComm.h"
#include "PyConnectStub.h"
// critical section/mutex
#ifdef WIN32
CRITICAL_SECTION g_criticalSection;
#else
pthread_mutex_t g_mutex;
pthread_mutexattr_t mta;
pthread_t g_iothread;
#endif
using namespace pyconnect;
PYCONNECT_LOGGING_DECLARE( "pyconnect.log" );
#ifdef WIN32
void ioprocess_thread( void * arg )
#else
void * ioprocess_thread( void * arg )
#endif
{
PyConnectNetComm::instance()->continuousProcessing();
#ifndef WIN32
return NULL;
#endif
}
static void finiPyConnect()
{
// stop the thread first
PyConnectNetComm::instance()->fini();
PyConnectStub::instance()->fini();
}
PyMODINIT_FUNC
#if PY_MAJOR_VERSION >= 3
PyInit_PyConnect( void )
#else
initPyConnect( void )
#endif
{
#ifdef WIN32
InitializeCriticalSection( &g_criticalSection );
#else
pthread_mutexattr_init( &mta );
pthread_mutexattr_settype( &mta, PTHREAD_MUTEX_RECURSIVE );
pthread_mutex_init( &g_mutex, &mta );
#endif
PYCONNECT_LOGGING_INIT;
PyEval_InitThreads();
PyConnectStub * pPyConnectStub = PyConnectStub::init();
PyObject * pMod = pPyConnectStub->getPyConnect();
Py_INCREF( pMod );
PyConnectNetComm::instance()->init( pPyConnectStub );
#ifdef WIN32
_beginthread( ioprocess_thread, 0, NULL );
#else
if (pthread_create( &g_iothread, NULL, ioprocess_thread, NULL)) {
ERROR_MSG( "Unable to create thread to handle PyConnect network data\n" );
abort();
}
#endif
#if PY_MAJOR_VERSION >= 3
return pMod;
#else
atexit( finiPyConnect );
#endif
}