halayli / lthread Goto Github PK
View Code? Open in Web Editor NEWlthread, a multicore enabled coroutine library written in C
License: Other
lthread, a multicore enabled coroutine library written in C
License: Other
I would like to point out that identifiers like "_LTHREAD_H_
" and "_LTHREAD_POLLER_H_
" do not fit to the expected naming convention of the C language standard.
Would you like to adjust your selection for unique names?
lthread.c:538:63: error: format specifies type 'long' but the argument has type '__darwin_suseconds_t' (aka 'int') [-Werror,-Wformat]
Is windows support planned?
Thanks a lot for this awesome gem!
https://github.com/halayli/lthread/blob/master/src/lthread_socket.c#L174
should close ret, not fd
When calling lthread_join, you need check return result before freeing the lthread struct.
It is too easy to cause memory leak or corruption.
A better solution is to follow pthread_join behavior: No timeout parameter. The caller need to free 'lthread' struct.
There is a bug in integer addition in _lthread_usec_now(): signed long time_t overflows. The return statement must look like this:
/* _lthread_usec_now(): */
return (uint64_t) t1.tv_sec * 1000000 + t1.tv_usec;
Bug shows itself as follows - after some point in time lthread functions with timeouts start trashing the cpu, in my case it was lthread_read(). I've found this after I was unable to recover seconds from sched->birth on my 32bit box.
Disabling the asserts with -DNDEBUG in CFLAGS causes a segmentation fault in _lthread_poller_ev_register_trigger () on Linux x86_64.
To run an lthread scheduler in each pthread, launch a pthread and create lthreads using lthread_create() followed by lthread_run() in each pthread.
when create more then one pthread and lthread scheduler,and use "ab -c 100 -n 10000" to test program,then "Assertion failed: (lt_write != NULL || lt_read != NULL), function lthread_run, file lthread_sched.c, line 252." occure.
But only one pthread will run ok.
Is Lthread incompatible with Windows systems? If so, would it be hard to port (just an estimate)?
I'm getting this assertion error: lthread/src/lthread_sched.c:252: lthread_run: Assertion `lt_write != ((void *)0) || lt_read != ((void *)0)' failed.
Trying this with some basic code:
pollfd p;
memset(&p, 0, sizeof(p));
p.fd = fd;
p.events = POLLIN;
lthread_poll(&p, 1, 1000000);
If I substitute these lines of code for:
lthread_wait_read(fd, 0);
Then it waits just fine.
A way to poll on multiple fds at once would be nice.
https://gist.github.com/bf95f7773e2548688e59
This is pretty hilarious.
Any use cases?
May be exists bug when call function lthread_sleep(0), because current CPU context was lose.
test [question] #question
new_next and sleep_next are initialized as follows:
lt->new_next.le_next = NULL;
lt->new_next.le_next = NULL;
lt->sleep_next.le_prev = NULL;
lt->sleep_next.le_prev = NULL;
should it not be as follows?
lt->new_next.le_next = NULL;
lt->new_next.le_prev = NULL;
lt->sleep_next.le_next = NULL;
lt->sleep_next.le_prev = NULL;
If I attempt to link liblthread.a with other code to create a shared library (.so), then I get the following error:
/usr/bin/ld: /usr/local/lib/liblthread.a(lthread.c.o): relocation R_X86_64_32 against `.text' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/liblthread.a: could not read symbols: Bad value
I tried recompiling lthread with -fPIC and this was enough to get the shared lib to successfully link with liblthread.a and produce a .so file. Unfortunately, the shared lib misbehaves at runtime. Specifically, I get this assertion:
lthread_sched.c:231: lthread_run: Assertion `lt_write != ((void *)0) || lt_read != ((void *)0)' failed.
It seems getting this to work as a shared lib requires a bit more thought than blindly adding -fPIC.
I think using aio better than multi-threading.
This is needed to integrate other event-driven-capable libraries like libzmq or libcurl. They expect to be woken up whenever certain fds become readable/writable, but they expect to be the ones doing the actual reading/writing.
I was able to get this to work by making _lthread_sched_event() public, and for example calling _lthread_sched_event(lthread_current(), fd, LT_EV_READ, 0) to wait until an fd becomes readable.
Would be nice to see some kind of solution in the public API. Libtask has fdwait() for this purpose.
Hi, good afternoon.
I read the source code, and I find the lthread looks good.
BUT Is there still something needs to be done before being applied?
For example. In the webserver demo. Just one thread is working for polling, computing, and doing functions in lthreads.
If I want to make lthread high-performance, Moving those computing tasks and functions to other pthreads would be better?
Maybe I can send a msg to other pthreads to reduce the burdens of the main pthread(epoll thread)?
When I tried to run example program (webserver.c), it crashes with core dump.
With gdb program halts at following:
Program received signal SIGSEGV, Segmentation fault.
0x001352ea in lthread_compute_end () at lthread_compute.c:126
126 asm("movl %0, 0(%%ebp)\n" ::"a"(lt->ebp));
Library and program compiled on Linux 3.0.0-16-generic #29-Ubuntu SMP i686 i686 i386 GNU/Linux
It would be useful to not choose the pthread an lthread is bound to ahead of time, but rather have the lthread resume running on whichever pthread is least contended.
In function b() you sleep for 1 second and in a() you try to join the corresponding lthread with a timeout of 10 milliseconds.
At https://github.com/halayli/lthread/blob/master/src/lthread_sched.c#L106 if errno is EINTR, then instead of aborting the poll should be retried!
The result of gettimeofday is the wall clock which can change backwards in some occassions (time drifted and ntp re-enabled). In that case timeouts may not expire for a while until the wall clock catches up.
It is better to use CLOCK_MONOTONIC with clock_gettime() to get a stable clock that never moves backwards.
I have looked at a few source files for your current software. I have noticed that some checks for return codes are missing.
Would you like to add more error handling for return values from functions like the following?
By removing "inline" from void _lthread_renice(struct lthread *lt) fixes compiling (see build error below) on MacOSX. I edited src/lthread_int.h and removed the inline keyword.
Performed a cmake .; make and it worked great.
Might be worth removing inline or adding preprocessor condition for MacOSX.
--Build Error--------------------
[ 11%] Building C object CMakeFiles/lthread.dir/src/lthread_socket.c.o
In file included from /Users/user/Projects/coroutines/lthread/lthread-master/src/lthread_socket.c:42:
/Users/user/Projects/coroutines/lthread/lthread-master/src/lthread_int.h:184:13: error: inline function '_lthread_renice' is not defined [-Werror,-Wundefined-inline]
inline void _lthread_renice(struct lthread *lt);
^
/Users/user/Projects/coroutines/lthread/lthread-master/src/lthread_socket.c:147:9: note: used here
_lthread_renice(lt);
^
1 error generated.
Would you like to add the configuration script "AX_PTHREAD" to your build specification?
Hi would lthread be able to provide a ucontext compat layer? This is needed for libcs that don't provide ucontext
how to listen Multi-port?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.