changeset 179:f49864df6f1f java-code-model tip

trying Py_BEGIN_ALLOW_THREADS
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 27 Jun 2024 21:53:03 +0200
parents 194ba7d6e0f4
children
files CodeAnalysis/FunctionBody.mustache CodeAnalysis/GenerateOrthancSDK.py
diffstat 2 files changed, 26 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/CodeAnalysis/FunctionBody.mustache	Thu Jun 27 18:51:36 2024 +0200
+++ b/CodeAnalysis/FunctionBody.mustache	Thu Jun 27 21:53:03 2024 +0200
@@ -17,12 +17,16 @@
   }
 {{/check_object_type}}{{/args}}
 {{#return_long}}
+  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
   long value = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
   {{#args}}{{release}}{{/args}}
   return PyLong_FromLong(value);
 {{/return_long}}
 {{#return_static_string}}
+  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
   const char* s = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
   {{#args}}{{release}}{{/args}}
   if (s == NULL)
   {
@@ -35,8 +39,10 @@
   }
 {{/return_static_string}}
 {{#return_dynamic_string}}
+  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
   OrthancPlugins::OrthancString s;
   s.Assign({{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}}));
+  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
   {{#args}}{{release}}{{/args}}
   if (s.GetContent() == NULL)
   {
@@ -49,14 +55,18 @@
   }
 {{/return_dynamic_string}}
 {{#return_void}}
+  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
   {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
   {{#args}}{{release}}{{/args}}
 
   Py_INCREF(Py_None);
   return Py_None;
 {{/return_void}}
 {{#return_error}}
+  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
   OrthancPluginErrorCode code = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
   {{#args}}{{release}}{{/args}}
 
   if (code == OrthancPluginErrorCode_Success)
@@ -72,7 +82,9 @@
 {{/return_error}}
 {{#return_object}}
   // This is the case of a constructor
+  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
   {{return_object}}* obj = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
   {{#args}}{{release}}{{/args}}
   if (obj == NULL)
   {
@@ -88,8 +100,10 @@
   }
 {{/return_object}}
 {{#return_bytes}}
+  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
   OrthancPlugins::MemoryBuffer buffer;
   OrthancPluginErrorCode code = {{c_function}}(OrthancPlugins::GetGlobalContext(), *buffer{{self}}{{call_args}});
+  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
   {{#args}}{{release}}{{/args}}
   if (code == OrthancPluginErrorCode_Success)
   {
@@ -102,7 +116,9 @@
   }
 {{/return_bytes}}
 {{#return_enumeration}}
+  {{#allow_threads}}PyThreadState *_save = PyEval_SaveThread();{{/allow_threads}}
   {{return_enumeration}} value = {{c_function}}(OrthancPlugins::GetGlobalContext(){{self}}{{call_args}});
+  {{#allow_threads}}PyEval_RestoreThread(_save);{{/allow_threads}}
   {{#args}}{{release}}{{/args}}
   return PyLong_FromLong(value);
 {{/return_enumeration}}
--- a/CodeAnalysis/GenerateOrthancSDK.py	Thu Jun 27 18:51:36 2024 +0200
+++ b/CodeAnalysis/GenerateOrthancSDK.py	Thu Jun 27 21:53:03 2024 +0200
@@ -184,6 +184,7 @@
         'count_args' : len(f['args']),
     }
 
+    allow_threads = True
     tuple_format = ''
     tuple_target = []
     call_args = []
@@ -198,6 +199,7 @@
                 'release' : 'PyBuffer_Release(&%s);' % arg['name'],
             })
             tuple_format += 's*'
+            allow_threads = False
         elif arg['sdk_type'] == 'const char *':
             args.append({
                 'name' : arg['name'],
@@ -205,6 +207,7 @@
                 'initialization' : ' = NULL',
             })
             tuple_format += 's'
+            allow_threads = False
         elif arg['sdk_type'] == 'enumeration':
             args.append({
                 'name' : arg['name'],
@@ -220,6 +223,7 @@
                 'check_object_type' : arg['sdk_class'],
             })
             tuple_format += 'O'
+            allow_threads = False
         elif arg['sdk_type'] in ORTHANC_TO_PYTHON_NUMERIC_TYPES:
             t = ORTHANC_TO_PYTHON_NUMERIC_TYPES[arg['sdk_type']]
             args.append({
@@ -270,11 +274,17 @@
         print('Ignoring function with unsupported return type: %s(), type = %s' % (f['c_function'], f['return_sdk_type']))
         return None
 
+    allow_threads = False   # TODO
+
     answer['tuple_format'] = ', '.join([ '"' + tuple_format + '"' ] + tuple_target)
+    answer['allow_threads'] = allow_threads
 
     if len(call_args) > 0:
         answer['call_args'] = ', ' + ', '.join(call_args)
 
+    if not allow_threads:
+        print('Threads are not allowed in function: %s()' % f['c_function'])
+
     return answer