Coder Social home page Coder Social logo

hagb / thread_reuse Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 25 KB

Threads reuse via LD_PRELOAD hook, and also a workaround for the memory leak of qemu-user when threads are created and destroyed

License: GNU Lesser General Public License v3.0

CMake 7.18% C 92.82%
qemu-user multithreading

thread_reuse's Introduction

Threads reuse via LD_PRELOAD hook

Save the "exited" threads in a "thread pool" and reuse them when new threads is needed, instead of creating and destroying threads again and again.

At the beginning, it was wriiten as a workaround for the memory leak of qemu-user when threads are created and destroyed.

Archived, because the bug has been fixed and merged to qemu 8.0.

DO NOT USE IN PRODUCTION ENVIRNOMENT

Build

mkdir build
cd build
cmake ..
make

And then there should be a ./libthread_reuse.so.

Use

LD_PRELOAD=path-to-libthread_reuse.so command

or if in qemu-user

qemu-ARCH -E LD_PRELOAD=path-to-libthread_reuse.so program

How can it be implemented

It hooks some functions of pthread:

  • pthread_create: (origin) create a new thread; (hooked) if there isn't enough threads in the thread pool, then create a new one, else get a thread from pool and submit the task
  • pthread_join: (origin) join the thread, after that the thread will be destroyed; (hooked) wait until the thread finish its task, and get the return value of the task function, and tell the real thread that it can come back to the pool
  • pthread_detach: (origin) ...; (hooked) tell the thread that it can come back to the pool
  • pthread_exit: (origin) exit the thread calling this function; (hooked) jump out of the task function via a long jump (exactly siglongjmp)
  • pthread_kill: (origin) send a signal to the thread if the signal is not 0, or return whether the thread is terminated if the signal is 0; (hooked) send a signal if the signal is not 0, or return whether the task function is terminated if the signal is 0

TODO

  • thread cancellation support
  • handle more signals
  • more comment and document
  • code clean-up and better style
  • more configurable
  • more tests
  • test suite
  • better POSIX compatibility

thread_reuse's People

Contributors

hagb avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.