Re: Threading in tux modules
Jason Borden <[email protected]>
| Newsgroups | gmane.network.tux |
|---|---|
| Organization | Sorenson Media |
| Message-ID | <[email protected]> |
Here's a patch for tux-2.2.5 that implements the optional TUXAPI_init_postfork call. I'm not sure if I've implemented it the most efficient way or not, but it's a start. Jason On Monday 08 April 2002 05:41 am, Ingo Molnar wrote: > On Fri, 5 Apr 2002, Jason Borden wrote: > > 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, [...] > > TUXAPI_init is executed before the main TUX processes are forked - those > processes have a separate VM so the threads created in TUXAPI_init wont > work as expected. > > perhaps there should be an optional TUXAPI_init_postfork call as well? > > Ingo > > > > _______________________________________________ > tux-list mailing list > [email protected] > https://listman.redhat.com/mailman/listinfo/tux-list
postfork.patch
(text/x-diff, 3.1 KB)
*** tux-2.2.5/tux.c Thu Feb 7 10:27:43 2002
--- tux-2.2.5-jb/tux.c Thu Apr 11 12:49:42 2002
***************
*** 63,68 ****
--- 63,69 ----
static int max_module_idx = 1;
static int (**handle_events_array) (user_req_t *req);
+ static void (**postfork_array) (void);
char **TUXAPI_modulename_array;
void die(int exitcode, char *format, ...) {
***************
*** 442,449 ****
const char *modulename;
handle_events_array = malloc(sizeof(*handle_events_array) * (argc-3));
TUXAPI_modulename_array = malloc(sizeof(*TUXAPI_modulename_array) * (argc-3));
! if (!handle_events_array || !TUXAPI_modulename_array) {
tux_stop_date_timer(timer);
die(1, "%s: no RAM", strerror(errno));
}
--- 443,451 ----
const char *modulename;
handle_events_array = malloc(sizeof(*handle_events_array) * (argc-3));
+ postfork_array = calloc(sizeof(*postfork_array) * (argc-3), 1);
TUXAPI_modulename_array = malloc(sizeof(*TUXAPI_modulename_array) * (argc-3));
! if (!handle_events_array || !TUXAPI_modulename_array || !postfork_array) {
tux_stop_date_timer(timer);
die(1, "%s: no RAM", strerror(errno));
}
***************
*** 452,457 ****
--- 454,460 ----
user_req_t req;
int (*mod_handle_events) (user_req_t *req);
void (*mod_init) (void);
+ void (*mod_init_postfork) (void);
char *libname;
unsigned int modulename_len = strlen(modulename);
***************
*** 476,485 ****
die(1, "%s: TUXAPI_handle_events() function missing", strerror(errno));
}
mod_init = dlsym(handle, "TUXAPI_init");
printf ("TUXAPI_init: %p\n", mod_init);
printf ("TUXAPI_handle_events: %p\n", mod_handle_events);
!
memset(&req, 0, sizeof(req));
req.module_index = mod_idx;
strcpy(req.objectname, modulename);
--- 479,490 ----
die(1, "%s: TUXAPI_handle_events() function missing", strerror(errno));
}
mod_init = dlsym(handle, "TUXAPI_init");
+ mod_init_postfork = dlsym(handle, "TUXAPI_init_postfork");
printf ("TUXAPI_init: %p\n", mod_init);
+ printf ("TUXAPI_init_postfork: %p\n", mod_init_postfork);
printf ("TUXAPI_handle_events: %p\n", mod_handle_events);
!
memset(&req, 0, sizeof(req));
req.module_index = mod_idx;
strcpy(req.objectname, modulename);
***************
*** 491,496 ****
--- 496,502 ----
printf("register {%s} - TUX returned %d...\n", req.objectname, ret);
handle_events_array[mod_idx] = mod_handle_events;
+ postfork_array[mod_idx] = mod_init_postfork;
TUXAPI_modulename_array[mod_idx] = malloc(strlen(modulename)+1);
if (!TUXAPI_modulename_array[mod_idx]) {
tux_stop_date_timer(timer);
***************
*** 529,534 ****
--- 534,547 ----
if (pid[i])
continue;
+ if (i == 0) {
+ for (mod_idx = 0; mod_idx < max_module_idx; mod_idx++) {
+ if (postfork_array[mod_idx])
+ postfork_array[mod_idx]();
+ }
+ free(postfork_array);
+ }
+
sprintf(commandline, "[TUX worker %d]", i);
replace_commandline(argc, argv, commandline);