Ticket #185: python-gil-pruning.patch

File python-gil-pruning.patch, 2.8 KB (added by Olly Betts, 16 years ago)

Patch to remove redundant locking in SWIG generated wrappers (with checks that the GIL is/isn't locked as we expect)

  • Source/Modules/python.cxx

     
    188188      if (bb) {
    189189        Append(f, bb);
    190190      } else {
     191        Append(f, "{ PyEval_AcquireLock(); PyEval_ReleaseLock(); } // Will deadlock if GIL already held by this thread.\n");
    191192        Append(f, "SWIG_PYTHON_THREAD_BEGIN_BLOCK;\n");
    192193      }
    193194    }
     
    200201        Append(f, eb);
    201202      } else {
    202203        Append(f, "SWIG_PYTHON_THREAD_END_BLOCK;\n");
     204        Append(f, "{ PyEval_AcquireLock(); PyEval_ReleaseLock(); } // Will deadlock if GIL already held by this thread.\n");
    203205      }
    204206    }
    205207  }
     
    14971499    Wrapper_add_local(f, "argc", "int argc");
    14981500    Printf(tmp, "PyObject *argv[%d]", maxargs + 1);
    14991501    Wrapper_add_local(f, "argv", tmp);
    1500     if (allow_thread)
    1501       thread_begin_block(n, f->code);
     1502    if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
     1503      Append(f->code, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
    15021504
    15031505    if (!fastunpack) {
    15041506      Wrapper_add_local(f, "ii", "int ii");
     
    15171519
    15181520    if (allow_thread) {
    15191521      String *ret = NewStringEmpty();
    1520       thread_end_block(n, ret);
     1522      if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
     1523        Append(ret, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
    15211524      Append(ret, "return ");
    15221525      Replaceall(dispatch, "return ", ret);
    15231526      Delete(ret);
     
    15251528
    15261529    Printv(f->code, dispatch, "\n", NIL);
    15271530
    1528     if (allow_thread)
    1529       thread_end_block(n, f->code);
     1531    if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
     1532      Append(f->code, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
    15301533
    15311534    if (GetFlag(n, "feature:python:maybecall")) {
    15321535      Append(f->code, "fail:\n");
     
    16341637    kwargs = NewString("");
    16351638
    16361639    int allow_thread = threads_enable(n);
    1637     if (allow_thread)
    1638       thread_begin_block(n, f->code);
     1640    if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
     1641      Append(f->code, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
    16391642
    16401643    Wrapper_add_local(f, "resultobj", "PyObject *resultobj = 0");
    16411644
     
    20662069      }
    20672070    }
    20682071
    2069     if (allow_thread)
    2070       thread_end_block(n, f->code);
     2072    if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
     2073      Append(f->code, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
    20712074    Append(f->code, "    return resultobj;\n");
    20722075
    20732076    /* Error handling code */
     
    20762079    if (need_cleanup) {
    20772080      Printv(f->code, cleanup, NIL);
    20782081    }
    2079     if (allow_thread)
    2080       thread_end_block(n, f->code);
     2082    if (allow_thread && !GetFlag(n, "feature:nothreadblock"))
     2083      Append(f->code, "(void)PyThreadState_Get(); // Aborts if GIL not held\n");
    20812084    Printv(f->code, ctab4, "return NULL;\n", NIL);
    20822085
    20832086