Giter Club home page Giter Club logo

state-threads's Introduction

This is my fork of the state-threads library from http://state-threads.sourceforge.net/

Changes include:
- CMake build system
- Valgrind support for stack swaps
- Minor updates for modern C compilers (gcc/clang)
- pthread support (using __thread gcc extension)

See README.orig for more information.

state-threads's People

Contributors

toffaletti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

state-threads's Issues

static variables in key.c don't have __thread attribute

Probably forgotten: results in multiple worker pthreads sharing key counters. Cured with this simple patch:

diff --git a/key.c b/key.c
index 9708c35..9133e65 100644
--- a/key.c
+++ b/key.c
@@ -47,8 +47,8 @@
/*

  • Destructor table for per-thread private data
    */
    -static _st_destructor_t _st_destructors[ST_KEYS_MAX];
    -static int key_max = 0;
    +static __thread _st_destructor_t _st_destructors[ST_KEYS_MAX];
    +static __thread int key_max = 0;

st_usleep timeout granularity is in milliseconds

As a result, st_usleep(999) is equivalent to st_usleep(0).

Proposed solution:

diff --git a/event.c b/event.c
index 6ee32f7..f5ea4d9 100644
--- a/event.c
+++ b/event.c
@@ -1288,7 +1288,7 @@ ST_HIDDEN void _st_epoll_dispatch(void)

     /* Check for I/O operations */
     nfd = epoll_wait(_st_epoll_data->epfd, _st_epoll_data->evtlist,
-                     _st_epoll_data->evtlist_size, timeout);
+                     _st_epoll_data->evtlist_size, timeout == -1 ? -1 : 0);

     if (nfd > 0) {
         for (i = 0; i < nfd; i++) {
@@ -1359,6 +1359,14 @@ ST_HIDDEN void _st_epoll_dispatch(void)
             }
         }
     }
+    else if (nfd == 0) {
+       if (timeout >= 0) {
+           struct timespec t;
+           t.tv_sec = min_timeout / 1000000;
+           t.tv_nsec = min_timeout * 1000;
+           nanosleep(&t, NULL);
+       }
+    }
 }

a performance issue for epoll

I came across a performance issue in epoll mode, when there were thousands concurrent connections. Profiling shows that _st_epoll_dispatch() consumed a lot of CPU.

After reviewing the function, I think I've found the reason: there's a loop that enumerates ALL threads in the I/O queue.

    for (q = _ST_IOQ.next; q != &_ST_IOQ; q = q->next) {

As I'm using one thread per connection model, I believe this loop make epoll mode degraded effectively to select mode.

Epoll event engine is missing EPOLLRDHUP

This causes CLOSE_WAIT-state sockets to linger. This fix cures it:

@@ -142,7 +142,7 @@ static __thread struct _st_epolldata {
#define _ST_EPOLL_EXCEP_CNT(fd) (_st_epoll_data->fd_data[fd].ex_ref_cnt)
#define _ST_EPOLL_REVENTS(fd) (_st_epoll_data->fd_data[fd].revents)

-#define _ST_EPOLL_READ_BIT(fd) (_ST_EPOLL_READ_CNT(fd) ? EPOLLIN : 0)
+#define _ST_EPOLL_READ_BIT(fd) (_ST_EPOLL_READ_CNT(fd) ? (EPOLLIN|EPOLLRDHUP) : 0)
#define _ST_EPOLL_WRITE_BIT(fd) (_ST_EPOLL_WRITE_CNT(fd) ? EPOLLOUT : 0)
#define _ST_EPOLL_EXCEP_BIT(fd) (_ST_EPOLL_EXCEP_CNT(fd) ? EPOLLPRI : 0)
#define _ST_EPOLL_EVENTS(fd) \

commit accd04f in my fork.

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.