Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PyThreadPool.cpp « python « bindings « audaspace « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 75811f082737a7211b0b123d64cba050006c2847 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*******************************************************************************
* Copyright 2009-2015 Juan Francisco Crespo Galán
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/

#include "PyThreadPool.h"

#include "Exception.h"
#include "util/ThreadPool.h"

extern PyObject* AUDError;

static PyObject *
ThreadPool_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
{
	ThreadPoolP* self = (ThreadPoolP*)type->tp_alloc(type, 0);

	if(self != nullptr)
	{
		unsigned int nThreads;
		if(!PyArg_ParseTuple(args, "I:nThreads", &nThreads))
			return nullptr;

		try
		{
			self->threadPool = new std::shared_ptr<aud::ThreadPool>(new aud::ThreadPool(nThreads));
		}
		catch(aud::Exception& e)
		{
			Py_DECREF(self);
			PyErr_SetString(AUDError, e.what());
			return nullptr;
		}
	}

	return (PyObject *)self;
}

static void
ThreadPool_dealloc(ThreadPoolP* self)
{
	if(self->threadPool)
		delete reinterpret_cast<std::shared_ptr<aud::ThreadPool>*>(self->threadPool);
	Py_TYPE(self)->tp_free((PyObject *)self);
}

static PyMethodDef ThreadPool_methods[] = {
	{ nullptr }  /* Sentinel */
};

PyDoc_STRVAR(M_aud_ThreadPool_doc,
	"A ThreadPool is used to parallelize convolution efficiently.");

PyTypeObject ThreadPoolType = {
	PyVarObject_HEAD_INIT(nullptr, 0)
	"aud.ThreadPool",						/* tp_name */
	sizeof(ThreadPoolP),					/* tp_basicsize */
	0,										/* tp_itemsize */
	(destructor)ThreadPool_dealloc,			/* tp_dealloc */
	0,										/* tp_print */
	0,										/* tp_getattr */
	0,										/* tp_setattr */
	0,										/* tp_reserved */
	0,										/* tp_repr */
	0,										/* tp_as_number */
	0,										/* tp_as_sequence */
	0,										/* tp_as_mapping */
	0,										/* tp_hash  */
	0,										/* tp_call */
	0,										/* tp_str */
	0,										/* tp_getattro */
	0,										/* tp_setattro */
	0,										/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT,						/* tp_flags */
	M_aud_ThreadPool_doc,					/* tp_doc */
	0,										/* tp_traverse */
	0,										/* tp_clear */
	0,										/* tp_richcompare */
	0,										/* tp_weaklistoffset */
	0,										/* tp_iter */
	0,										/* tp_iternext */
	ThreadPool_methods,						/* tp_methods */
	0,										/* tp_members */
	0,										/* tp_getset */
	0,										/* tp_base */
	0,										/* tp_dict */
	0,										/* tp_descr_get */
	0,										/* tp_descr_set */
	0,										/* tp_dictoffset */
	0,										/* tp_init */
	0,										/* tp_alloc */
	ThreadPool_new,							/* tp_new */
};

AUD_API PyObject* ThreadPool_empty()
{
	return ThreadPoolType.tp_alloc(&ThreadPoolType, 0);
}


AUD_API ThreadPoolP* checkThreadPool(PyObject* threadPool)
{
	if(!PyObject_TypeCheck(threadPool, &ThreadPoolType))
	{
		PyErr_SetString(PyExc_TypeError, "Object is not of type ThreadPool!");
		return nullptr;
	}

	return (ThreadPoolP*)threadPool;
}


bool initializeThreadPool()
{
	return PyType_Ready(&ThreadPoolType) >= 0;
}


void addThreadPoolToModule(PyObject* module)
{
	Py_INCREF(&ThreadPoolType);
	PyModule_AddObject(module, "ThreadPool", (PyObject *)&ThreadPoolType);
}