| 1 | #define PY_SSIZE_T_CLEAN |
| 2 | #include <Python.h> |
| 3 | #include <heaplayers.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | #include <mutex> |
| 7 | |
| 8 | // This uses Python's buffer interface to view a mmap buffer passed in, |
| 9 | // which we assume has a layout of [ uint64_t | HL::SpinLock ]. |
| 10 | // |
| 11 | // We assume that the lock region has been fully initialized at this point, |
| 12 | // since initialization occurs at the bootstrapping of the per-thread heap |
| 13 | // |
| 14 | // This is derived in part from |
| 15 | // https://docs.python.org/3/extending/extending.html |
| 16 | // |
| 17 | // FIXME: Encapsulate under scalene namespace |
| 18 | // TODO: Wrap in Python library with ContextManager |
| 19 | |
| 20 | static PyObject* get_line_atomic(PyObject* self, PyObject* args) { |
| 21 | // Casts the pointer at the expected location to a SpinLock and then locks it |
| 22 | Py_buffer lock_mmap; |
| 23 | Py_buffer signal_mmap; |
| 24 | Py_buffer result_bytearray; |
| 25 | Py_buffer lastpos_buf; |
| 26 | if (!PyArg_ParseTuple( |
| 27 | args, "s*s*s*s*", &lock_mmap, &signal_mmap, &result_bytearray, |
| 28 | &lastpos_buf)) // "s*" means readable/writeable buffer as per |
| 29 | // https://docs.python.org/3/c-api/arg.html Buffer |
| 30 | // protocol is found here |
| 31 | // https://docs.python.org/3/c-api/buffer.html |
| 32 | return NULL; |
| 33 | |
| 34 | auto buf = reinterpret_cast<char*>(lock_mmap.buf) + sizeof(uint64_t); |
| 35 | using LockType = HL::SpinLock; |
| 36 | auto lock = reinterpret_cast<LockType*>(buf); |
| 37 | |
| 38 | std::lock_guard<LockType> theLock(*lock); |
| 39 | |
| 40 | auto lastpos = reinterpret_cast<uint64_t*>(lastpos_buf.buf); |
| 41 | auto current_iter = reinterpret_cast<char*>(signal_mmap.buf) + *lastpos; |
| 42 | auto start = current_iter; |
| 43 | auto result_iter = reinterpret_cast<char*>(result_bytearray.buf); |
| 44 | |
| 45 | char* nl = |
| 46 | reinterpret_cast<char*>(memchr(current_iter, '\n', result_bytearray.len)); |
| 47 | int len = (nl == nullptr) ? 0 : nl - start; |
| 48 | |
| 49 | if (len == 0) { |
| 50 | Py_RETURN_FALSE; |
| 51 | } |
| 52 | |
| 53 | #if 0 |
| 54 | char tmp[result_bytearray.len+1]; |
| 55 | memcpy(tmp, current_iter, len); |
| 56 | tmp[len] = '\0'; |
| 57 | tprintf::tprintf("read @ from @\n", tmp, *lastpos); |
| 58 | #endif |
| 59 | |
| 60 | // avoid 'memcpy', as Scalene interposes on it to measure memory copying |
| 61 | for (int i = 0; i <= len; i++) { |
| 62 | *(result_iter++) = *(current_iter++); |
| 63 | } |
| 64 | |
| 65 | *lastpos += len + 1; |
| 66 | |
| 67 | Py_RETURN_TRUE; |
| 68 | } |
| 69 | |
| 70 | static PyMethodDef MmapHlSpinlockMethods[] = { |
| 71 | {"get_line_atomicscip-python python scalene $ scalene/get_line_atomic/get_line_atomic().", get_line_atomic, METH_VARARGS, |
| 72 | "locks a mutex located in buffer"}, |
| 73 | {NULL, NULL, 0, NULL}}; |
| 74 | |
| 75 | static struct PyModuleDef mmaphlspinlockmodule = { |
| 76 | PyModuleDef_HEAD_INIT, "get_line_atomicscip-python python scalene $ scalene/get_line_atomic/__init__:", NULL, -1, MmapHlSpinlockMethods}; |
| 77 | |
| 78 | PyMODINIT_FUNC PyInit_get_line_atomic(void) { |
| 79 | return PyModule_Create(&mmaphlspinlockmodule); |
| 80 | } |