@@ -4083,12 +4083,19 @@ temporary_c_thread(void *data)
40834083 PyThread_exit_thread ();
40844084}
40854085
4086+ static test_c_thread_t test_c_thread ;
4087+
40864088static PyObject *
4087- call_in_temporary_c_thread (PyObject * self , PyObject * callback )
4089+ call_in_temporary_c_thread (PyObject * self , PyObject * args )
40884090{
40894091 PyObject * res = NULL ;
4090- test_c_thread_t test_c_thread ;
4092+ PyObject * callback = NULL ;
40914093 long thread ;
4094+ int wait = 1 ;
4095+ if (!PyArg_ParseTuple (args , "O|i" , & callback , & wait ))
4096+ {
4097+ return NULL ;
4098+ }
40924099
40934100 PyEval_InitThreads ();
40944101
@@ -4117,6 +4124,10 @@ call_in_temporary_c_thread(PyObject *self, PyObject *callback)
41174124 PyThread_acquire_lock (test_c_thread .start_event , 1 );
41184125 PyThread_release_lock (test_c_thread .start_event );
41194126
4127+ if (!wait ) {
4128+ Py_RETURN_NONE ;
4129+ }
4130+
41204131 Py_BEGIN_ALLOW_THREADS
41214132 PyThread_acquire_lock (test_c_thread .exit_event , 1 );
41224133 PyThread_release_lock (test_c_thread .exit_event );
@@ -4127,13 +4138,32 @@ call_in_temporary_c_thread(PyObject *self, PyObject *callback)
41274138
41284139exit :
41294140 Py_CLEAR (test_c_thread .callback );
4130- if (test_c_thread .start_event )
4141+ if (test_c_thread .start_event ) {
41314142 PyThread_free_lock (test_c_thread .start_event );
4132- if (test_c_thread .exit_event )
4143+ test_c_thread .start_event = NULL ;
4144+ }
4145+ if (test_c_thread .exit_event ) {
41334146 PyThread_free_lock (test_c_thread .exit_event );
4147+ test_c_thread .exit_event = NULL ;
4148+ }
41344149 return res ;
41354150}
41364151
4152+ static PyObject *
4153+ join_temporary_c_thread (PyObject * self , PyObject * Py_UNUSED (ignored ))
4154+ {
4155+ Py_BEGIN_ALLOW_THREADS
4156+ PyThread_acquire_lock (test_c_thread .exit_event , 1 );
4157+ PyThread_release_lock (test_c_thread .exit_event );
4158+ Py_END_ALLOW_THREADS
4159+ Py_CLEAR (test_c_thread .callback );
4160+ PyThread_free_lock (test_c_thread .start_event );
4161+ test_c_thread .start_event = NULL ;
4162+ PyThread_free_lock (test_c_thread .exit_event );
4163+ test_c_thread .exit_event = NULL ;
4164+ Py_RETURN_NONE ;
4165+ }
4166+
41374167/* marshal */
41384168
41394169static PyObject *
@@ -5281,8 +5311,9 @@ static PyMethodDef TestMethods[] = {
52815311 {"docstring_with_signature_with_defaults" ,
52825312 (PyCFunction )test_with_docstring , METH_NOARGS ,
52835313 docstring_with_signature_with_defaults },
5284- {"call_in_temporary_c_thread" , call_in_temporary_c_thread , METH_O ,
5314+ {"call_in_temporary_c_thread" , call_in_temporary_c_thread , METH_VARARGS ,
52855315 PyDoc_STR ("set_error_class(error_class) -> None" )},
5316+ {"join_temporary_c_thread" , join_temporary_c_thread , METH_NOARGS },
52865317 {"pymarshal_write_long_to_file" ,
52875318 pymarshal_write_long_to_file , METH_VARARGS },
52885319 {"pymarshal_write_object_to_file" ,
0 commit comments