Threading in tux modules

Jason Borden <[email protected]>
Newsgroups gmane.network.tux
Organization Sorenson Media
Message-ID <[email protected]>
I have a project I'm working on that caches data received from an ldap 
directory and runs a background thread to refresh the cache when the data 
changes. I've run into some issues with running threads inside of a tux 
module, but have found a way to make it work. For some reason, I am unable to 
make the thread work if it is created at the TUXAPI_init stage, but can make 
it work upon loading the first page through TUXAPI_handle_events. I've 
included some simple code to show what does and doesn't work for me.

//threadtest.c
//This doesn't work
#define __USE_GNU
#include <pthread.h>
#include <tuxmodule.h>

#define REPLY_HEADER "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"

pthread_mutex_t mymutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
int i=0;

int mythread(void *blah)
{
    while(1)
    {
        pthread_mutex_lock(&mymutex);
        i++;
        pthread_mutex_unlock(&mymutex);
        sleep(1);
    }
    return 0;
}

int TUXAPI_handle_events(user_req_t *req)
{
    int ret;
    char reply[128];

    switch (req->event)
    {
    case 0:
        pthread_mutex_lock(&mymutex);
        snprintf(reply, 128, "%s%d", REPLY_HEADER, i);
        pthread_mutex_unlock(&mymutex);
        req->event = 1;
        req->http_status = 200;
        req->object_addr = reply;
        req->objectlen = strlen(reply);
        ret = tux(TUX_ACTION_SEND_BUFFER, req);
        break;

    case 1:
        ret = tux(TUX_ACTION_FINISH_CLOSE_REQ, req);
        break;
    }
    return ret;
}

void TUXAPI_init(void)
{    
    char *stack;
    stack = malloc(8192);
    clone(mythread, stack+8192, CLONE_VM | CLONE_SIGHAND | CLONE_FILES | 
CLONE_FS, NULL);
}


//threadtest2.c
//This does work
#define __USE_GNU
#include <pthread.h>
#include <tuxmodule.h>

#define REPLY_HEADER "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n"

pthread_mutex_t mymutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
int i=0;

int mythread(void *blah)
{
    while(1)
    {
        pthread_mutex_lock(&mymutex);
        i++;
        pthread_mutex_unlock(&mymutex);
        sleep(1);
    }
    return 0;
}

int TUXAPI_handle_events(user_req_t *req)
{
    int ret;
    char reply[128];

    if (i == 0)
    {
        char *stack;
        stack = malloc(8192);
        clone(mythread, stack+8192, CLONE_VM | CLONE_SIGHAND | CLONE_FILES | 
CLONE_FS, NULL);
    }


    switch (req->event)
    {
    case 0:
        pthread_mutex_lock(&mymutex);
        snprintf(reply, 128, "%s%d", REPLY_HEADER, i);
        pthread_mutex_unlock(&mymutex);
        req->event = 1;
        req->http_status = 200;
        req->object_addr = reply;
        req->objectlen = strlen(reply);
        ret = tux(TUX_ACTION_SEND_BUFFER, req);
        break;

    case 1:
        ret = tux(TUX_ACTION_FINISH_CLOSE_REQ, req);
        break;
    }
    return ret;
}


Has anyone else done threads in a tux module and know how to start a thread in 
the TUX_init? Also is there a way to signal the module that tux is shutting 
down so I can "gracefully" shutdown my threads? Any input would be 
appriciated.

Thanks,
Jason
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.